1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
// This Source Code Form is subject to the terms of
// the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You
// can obtain one at http://mozilla.org/MPL/2.0/.

//! CBOR ([RFC 7049](http://tools.ietf.org/html/rfc7049))
//! decoder implementation supporting the following features:
//!
//! - Generic decoding using an intermediate representation
//! - Tag validation
//! - Resource limits (e.g. maximum nesting level)
//! - Direct decoding into Rust types
//! - Indefinite sized bytes, strings, arrays and objects
//!
//! The module is structured as follows:
//!
//! 1. `Kernel` contains the basic decoding functionality, capable of
//! decoding simple unstructured types.
//! 2. `Decoder` directly decodes into native Rust types.
//! 3. `GenericDecoder` handles arbitrary CBOR items and decodes them
//! into an `Value` AST.
//!
//! # Example 1: Direct decoding
//!
//! ```
//! use cborian::{Config, Decoder};
//! use std::io::Cursor;
//!
//! let mut d = Decoder::new(Config::default(), Cursor::new(vec![0u8]));
//! assert_eq!(Some(0), d.u64().ok())
//! ```
//!
//! # Example 2: Direct decoding (nested array)
//!
//! ```
//!
//! use cborian::{Config, Decoder};
//! use rustc_serialize::hex::FromHex;
//! use std::io::Cursor;
//!
//!     let input   = Cursor::new("828301020383010203".from_hex().unwrap());
//!     let mut dec = Decoder::new(Config::default(), input);
//!     let mut res = Vec::new();
//!     for _ in 0 .. dec.array().unwrap() {
//!         let mut vec = Vec::new();
//!         for _ in 0 .. dec.array().unwrap() {
//!             vec.push(dec.u8().unwrap())
//!         }
//!         res.push(vec)
//!     }
//!     assert_eq!(vec![vec![1, 2, 3], vec![1, 2, 3]], res)
//! ```
//!
//! # Example 3: Generic decoding
//!
//! ```
//! use cborian::{Config, GenericDecoder};
//! use cborian::value::{self, Key};
//! use rustc_serialize::hex::FromHex;
//! use std::io::Cursor;
//!
//!     let input = Cursor::new("a2616101028103".from_hex().unwrap());
//!     let mut d = GenericDecoder::new(Config::default(), input);
//!     let value = d.value().unwrap();
//!     let     c = value::Cursor::new(&value);
//!     assert_eq!(Some(1), c.field("a").u8());
//!     assert_eq!(Some(3), c.get(Key::u64(2)).at(0).u8())
//! ```
//!
//! # Example 4: Direct decoding (optional value)
//!
//! ```
//! use cborian::{opt, Config, Decoder};
//! use std::io::Cursor;
//!
//! let mut d = Decoder::new(Config::default(), Cursor::new(vec![0xF6]));
//! assert_eq!(None, opt(d.u8()).unwrap())
//! ```

use crate::skip::Skip;
use crate::slice::{ReadSlice, ReadSliceError};
use crate::types::{Tag, Type};
use crate::value::{self, Bytes, Int, Key, Simple, Text, Value};
use byteorder::{BigEndian, ReadBytesExt};
use std::collections::{BTreeMap, LinkedList};
use std::error::Error;
use std::f32;
use std::fmt;
use std::io;
use std::str::{from_utf8, Utf8Error};
use std::string;
use std::{i16, i32, i64, i8};

// Decoder Configuration ////////////////////////////////////////////////////

/// `Config` contains various settings which limit resource consumption
/// or enable certain validation options. Please note that the various
/// maximum length/size values apply to an individual element only.
///
/// This is mainly to prevent attackers from providing CBOR values whose
/// length is larger than the available memory. In combination the memory
/// consumption can still become large and it is best to limit the incoming
/// bytes to a specific upper bound, e.g. by using `std::io::Take`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Config {
    /// Maximum number of array elements
    pub max_len_array: usize,
    /// Maximum length of a byte string
    pub max_len_bytes: usize,
    /// Maximum length of a string
    pub max_len_text: usize,
    /// Maximum number of object fields
    pub max_size_map: usize,
    /// Maximum recursion steps when decoder `Value`s
    pub max_nesting: usize,
    /// Ignore `Tag`s when decoding `Value`s
    pub skip_tags: bool,
    /// Validate `Value` type matches `Tag`.
    pub check_tags: bool,
}

const DEFAULT_CONFIG: Config = Config {
    max_len_array: 1000,
    max_len_bytes: 0x500000,
    max_len_text: 0x500000,
    max_size_map: 1000,
    max_nesting: 16,
    skip_tags: false,
    check_tags: true,
};

impl Config {
    /// Create default configuration with
    ///
    /// - `max_len_array` = 1000
    /// - `max_len_bytes` = 5 MB
    /// - `max_len_text` = 5 MB
    /// - `max_size_map` = 1000
    /// - `max_nesting` = 16
    /// - `skip_tags` = false
    /// - `check_tags` = true
    pub fn default() -> Config {
        DEFAULT_CONFIG
    }
}

// Decode Error Type ////////////////////////////////////////////////////////

pub type DecodeResult<A> = Result<A, DecodeError>;

#[derive(Debug)]
pub enum DecodeError {
    /// An object contains the same key multiple times
    DuplicateKey(Key),
    /// The signed integer is greater that its max value
    IntOverflow(u64),
    /// The decoded `Value` can not serve as a `Key` in an object
    InvalidKey(Value),
    /// The type of `Value` is not what is expected for a `Tag`
    InvalidTag(Value),
    /// The string is not encoded in UTF-8
    InvalidUtf8(Utf8Error),
    /// Some I/O error
    IoError(io::Error),
    /// The maximum configured length is exceeded
    TooLong { max: usize, actual: u64 },
    /// `Value`s are nested deeper than the configured maximum
    TooNested,
    /// The end of file has been encountered unexpectedly
    UnexpectedEOF,
    /// An unexpected type has been encountered
    UnexpectedType { datatype: Type, info: u8 },
    /// A break was encountered at some unexpected point while
    /// decoding an indefinite object.
    UnexpectedBreak,
    /// Some other error.
    Other(Box<dyn Error + Send + Sync>),
}

/// When decoding an optional item, i.e. a `Null` value has to be
/// considered, this function will map `Null` to `None` and any
/// other value to `Some(value)`.
pub fn opt<A>(r: DecodeResult<A>) -> DecodeResult<Option<A>> {
    match r {
        Ok(x) => Ok(Some(x)),
        Err(e) => {
            if is_null(&e) {
                Ok(None)
            } else {
                Err(e)
            }
        }
    }
}

/// When decoding an item which may be `Undefined` this function
/// will map `Undefined` to `None` and any other value to `Some(value)`.
pub fn maybe<A>(r: DecodeResult<A>) -> DecodeResult<Option<A>> {
    match r {
        Ok(x) => Ok(Some(x)),
        Err(e) => {
            if is_undefined(&e) {
                Ok(None)
            } else {
                Err(e)
            }
        }
    }
}

/// When decoding an indefinite item, every element item can be a `Break`
/// value. By wrapping it in `or_break`, this case can be handled more
/// conveniently.
pub fn or_break<A>(r: DecodeResult<A>) -> DecodeResult<Option<A>> {
    match r {
        Ok(x) => Ok(Some(x)),
        Err(e) => {
            if is_break(&e) {
                Ok(None)
            } else {
                Err(e)
            }
        }
    }
}

fn is_break(error: &DecodeError) -> bool {
    matches!(*error, DecodeError::UnexpectedType {
        datatype: Type::Break,
        ..
    })
}

fn is_null(error: &DecodeError) -> bool {
    matches!(*error, DecodeError::UnexpectedType {
        datatype: Type::Null,
        ..
    })
}

fn is_undefined(error: &DecodeError) -> bool {
    matches!(*error, DecodeError::UnexpectedType {
        datatype: Type::Undefined,
        ..
    })
}

impl fmt::Display for DecodeError {
    fn fmt(&self, format: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            DecodeError::DuplicateKey(ref k) => {
                write!(format, "DecodeError: duplicate key: {:?}", *k)
            }
            DecodeError::IntOverflow(n) => write!(format, "DecodeError: integer overflow: {}", n),
            DecodeError::InvalidKey(ref k) => {
                write!(format, "DecodeError: unsuitable map key: {:?}", *k)
            }
            DecodeError::InvalidTag(ref v) => {
                write!(format, "DecodeError: value does not match tag: {:?}", *v)
            }
            DecodeError::InvalidUtf8(ref e) => {
                write!(format, "DecodeError: Invalid UTF-8 encoding: {}", *e)
            }
            DecodeError::IoError(ref e) => write!(format, "DecodeError: I/O error: {}", *e),
            DecodeError::TooNested => write!(format, "DecodeError: value is too nested"),
            DecodeError::UnexpectedEOF => write!(format, "DecodeError: unexpected end-of-file"),
            DecodeError::UnexpectedBreak => write!(format, "DecodeError: unexpected break"),
            DecodeError::Other(ref e) => write!(format, "DecodeError: other: {:?}", e),
            DecodeError::TooLong { max: m, actual: a } => {
                write!(format, "DecodeError: value is too long {} (max={})", a, m)
            }
            DecodeError::UnexpectedType {
                datatype: t,
                info: i,
            } => write!(format, "DecodeError: unexpected type {:?} (info={})", t, i),
        }
    }
}

impl Error for DecodeError {
    fn description(&self) -> &str {
        match *self {
            DecodeError::DuplicateKey(_) => "duplicate key in objects",
            DecodeError::IntOverflow(_) => "integer overflow",
            DecodeError::InvalidKey(_) => "invalid object key",
            DecodeError::InvalidTag(_) => "invalid tag",
            DecodeError::InvalidUtf8(_) => "invalid utf-8",
            DecodeError::IoError(_) => "i/o error",
            DecodeError::TooNested => "too deeply nested objects/arrays",
            DecodeError::UnexpectedEOF => "unexpected eof",
            DecodeError::UnexpectedBreak => "unexpected break",
            DecodeError::Other(_) => "other error",
            DecodeError::TooLong { .. } => "value is too long",
            DecodeError::UnexpectedType { .. } => "unexpected type",
        }
    }

    fn cause(&self) -> Option<&dyn Error> {
        match *self {
            DecodeError::IoError(ref e) => Some(e),
            DecodeError::InvalidUtf8(ref e) => Some(e),
            DecodeError::Other(ref e) => Some(&**e),
            _ => None,
        }
    }
}

impl From<io::Error> for DecodeError {
    fn from(e: io::Error) -> DecodeError {
        DecodeError::IoError(e)
    }
}

impl From<string::FromUtf8Error> for DecodeError {
    fn from(e: string::FromUtf8Error) -> DecodeError {
        DecodeError::InvalidUtf8(e.utf8_error())
    }
}

impl From<Utf8Error> for DecodeError {
    fn from(e: Utf8Error) -> DecodeError {
        DecodeError::InvalidUtf8(e)
    }
}

impl From<ReadSliceError> for DecodeError {
    fn from(e: ReadSliceError) -> DecodeError {
        match e {
            ReadSliceError::InsufficientData => DecodeError::UnexpectedEOF,
            ReadSliceError::IoError(e) => DecodeError::IoError(e),
        }
    }
}

// Macros ///////////////////////////////////////////////////////////////////

// Like `read_signed` but always using `read_u8`
macro_rules! read_signed_byte {
    ($this: ident, $from_type: ty, $to: ident, $to_type: ty) => {{
        $this.reader.read_u8().map_err(From::from).and_then(|n| {
            if n > $to::MAX as $from_type {
                Err(DecodeError::IntOverflow(n as u64))
            } else {
                Ok(-1 - n as $to_type)
            }
        })
    }};
}

// Read from reader, check value range and map to negative integer.
macro_rules! read_signed {
    ($this: ident, $from: ident, $from_type: ty, $to: ident, $to_type: ty) => {{
        $this
            .reader
            .$from::<BigEndian>()
            .map_err(From::from)
            .and_then(|n| {
                if n > $to::MAX as $from_type {
                    Err(DecodeError::IntOverflow(n as u64))
                } else {
                    Ok(-1 - n as $to_type)
                }
            })
    }};
}

// Read unsigned integer, check value range and cast to target type.
macro_rules! cast_unsigned {
    ($this: ident, $from: ident, $info: ident, $from_type: ty, $to: ident, $to_type: ty) => {{
        $this.$from($info).and_then(|n| {
            if n > $to::MAX as $from_type {
                Err(DecodeError::IntOverflow(n as u64))
            } else {
                Ok(n as $to_type)
            }
        })
    }};
}

// Decoder Kernel ///////////////////////////////////////////////////////////

pub type TypeInfo = (Type, u8);

/// This decoding kernel reads from an underlying `std::io::Read` type
/// primitive CBOR values such as unsigned and signed integers as well
/// as raw bytes.
/// It forms the basis on which `Decoder` and `GenericDecoder` add logic
/// for handling `Tag`s, heterogenous data and generic value decoding.
pub struct Kernel<R> {
    reader: R,
}

impl<R: ReadBytesExt> Kernel<R> {
    pub fn new(r: R) -> Kernel<R> {
        Kernel { reader: r }
    }

    pub fn into_reader(self) -> R {
        self.reader
    }

    pub fn typeinfo(&mut self) -> DecodeResult<TypeInfo> {
        Type::read(&mut self.reader).map_err(From::from)
    }

    pub fn simple(&mut self, ti: &TypeInfo) -> DecodeResult<Simple> {
        match ti.0 {
            Type::Unassigned { major: 7, info: a } => Ok(Simple::Unassigned(a)),
            Type::Reserved { major: 7, info: a } => Ok(Simple::Reserved(a)),
            _ => unexpected_type(ti),
        }
    }

    pub fn bool(&mut self, ti: &TypeInfo) -> DecodeResult<bool> {
        match *ti {
            (Type::Bool, 20) => Ok(false),
            (Type::Bool, 21) => Ok(true),
            _ => unexpected_type(ti),
        }
    }

    pub fn u8(&mut self, ti: &TypeInfo) -> DecodeResult<u8> {
        match *ti {
            (Type::UInt8, n @ 0..=23) => Ok(n),
            (Type::UInt8, 24) => self.reader.read_u8().map_err(From::from),
            _ => unexpected_type(ti),
        }
    }

    pub fn u16(&mut self, ti: &TypeInfo) -> DecodeResult<u16> {
        match *ti {
            (Type::UInt8, n @ 0..=23) => Ok(n as u16),
            (Type::UInt8, 24) => self.reader.read_u8().map(|n| n as u16).map_err(From::from),
            (Type::UInt16, 25) => self.reader.read_u16::<BigEndian>().map_err(From::from),
            _ => unexpected_type(ti),
        }
    }

    pub fn u32(&mut self, ti: &TypeInfo) -> DecodeResult<u32> {
        match *ti {
            (Type::UInt8, n @ 0..=23) => Ok(n as u32),
            (Type::UInt8, 24) => self.reader.read_u8().map(|n| n as u32).map_err(From::from),
            (Type::UInt16, 25) => self
                .reader
                .read_u16::<BigEndian>()
                .map(|n| n as u32)
                .map_err(From::from),
            (Type::UInt32, 26) => self.reader.read_u32::<BigEndian>().map_err(From::from),
            _ => unexpected_type(ti),
        }
    }

    pub fn u64(&mut self, ti: &TypeInfo) -> DecodeResult<u64> {
        match *ti {
            (Type::UInt8, n @ 0..=23) => Ok(n as u64),
            (Type::UInt8, 24) => self.reader.read_u8().map(|n| n as u64).map_err(From::from),
            (Type::UInt16, 25) => self
                .reader
                .read_u16::<BigEndian>()
                .map(|n| n as u64)
                .map_err(From::from),
            (Type::UInt32, 26) => self
                .reader
                .read_u32::<BigEndian>()
                .map(|n| n as u64)
                .map_err(From::from),
            (Type::UInt64, 27) => self.reader.read_u64::<BigEndian>().map_err(From::from),
            _ => unexpected_type(ti),
        }
    }

    pub fn i8(&mut self, ti: &TypeInfo) -> DecodeResult<i8> {
        match *ti {
            (Type::Int8, n @ 0..=23) => Ok(-1 - n as i8),
            (Type::Int8, 24) => read_signed_byte!(self, u8, i8, i8),
            (Type::UInt8, _) => cast_unsigned!(self, u8, ti, u8, i8, i8),
            _ => unexpected_type(ti),
        }
    }

    pub fn i16(&mut self, ti: &TypeInfo) -> DecodeResult<i16> {
        match *ti {
            (Type::Int8, n @ 0..=23) => Ok(-1 - n as i16),
            (Type::Int8, 24) => read_signed_byte!(self, u8, i16, i16),
            (Type::Int16, 25) => read_signed!(self, read_u16, u16, i16, i16),
            (Type::UInt8, _) => cast_unsigned!(self, u8, ti, u8, i16, i16),
            (Type::UInt16, _) => cast_unsigned!(self, u16, ti, u16, i16, i16),
            _ => unexpected_type(ti),
        }
    }

    pub fn i32(&mut self, ti: &TypeInfo) -> DecodeResult<i32> {
        match *ti {
            (Type::Int8, n @ 0..=23) => Ok(-1 - n as i32),
            (Type::Int8, 24) => read_signed_byte!(self, u8, i32, i32),
            (Type::Int16, 25) => read_signed!(self, read_u16, u16, i32, i32),
            (Type::Int32, 26) => read_signed!(self, read_u32, u32, i32, i32),
            (Type::UInt8, _) => cast_unsigned!(self, u8, ti, u8, i32, i32),
            (Type::UInt16, _) => cast_unsigned!(self, u16, ti, u16, i32, i32),
            (Type::UInt32, _) => cast_unsigned!(self, u32, ti, u32, i32, i32),
            _ => unexpected_type(ti),
        }
    }

    pub fn i64(&mut self, ti: &TypeInfo) -> DecodeResult<i64> {
        match *ti {
            (Type::Int8, n @ 0..=23) => Ok(-1 - n as i64),
            (Type::Int8, 24) => read_signed_byte!(self, u8, i64, i64),
            (Type::Int16, 25) => read_signed!(self, read_u16, u16, i64, i64),
            (Type::Int32, 26) => read_signed!(self, read_u32, u32, i64, i64),
            (Type::Int64, 27) => read_signed!(self, read_u64, u64, i64, i64),
            (Type::UInt8, _) => cast_unsigned!(self, u8, ti, u8, i64, i64),
            (Type::UInt16, _) => cast_unsigned!(self, u16, ti, u16, i64, i64),
            (Type::UInt32, _) => cast_unsigned!(self, u32, ti, u32, i64, i64),
            (Type::UInt64, _) => cast_unsigned!(self, u64, ti, u64, i64, i64),
            _ => unexpected_type(ti),
        }
    }

    pub fn int(&mut self, ti: &TypeInfo) -> DecodeResult<Int> {
        match *ti {
            (Type::Int8, a) | (Type::Int16, a) | (Type::Int32, a) | (Type::Int64, a) => {
                self.unsigned(a).map(Int::Neg)
            }

            (Type::UInt8, a) | (Type::UInt16, a) | (Type::UInt32, a) | (Type::UInt64, a) => {
                self.unsigned(a).map(Int::Pos)
            }

            _ => unexpected_type(ti),
        }
    }

    pub fn f16(&mut self, ti: &TypeInfo) -> DecodeResult<f32> {
        match ti.0 {
            Type::Float16 => {
                // Copied from RFC 7049 Appendix D:
                let half = self.reader.read_u16::<BigEndian>()?;
                let exp = half >> 10 & 0x1F;
                let mantissa = half & 0x3FF;
                let value = match exp {
                    0 => ffi::c_ldexpf(mantissa as f32, -24),
                    31 => {
                        if mantissa == 0 {
                            f32::INFINITY
                        } else {
                            f32::NAN
                        }
                    }
                    _ => ffi::c_ldexpf(mantissa as f32 + 1024.0, exp as isize - 25),
                };
                Ok(if half & 0x8000 == 0 { value } else { -value })
            }
            _ => unexpected_type(ti),
        }
    }

    pub fn f32(&mut self, ti: &TypeInfo) -> DecodeResult<f32> {
        match ti.0 {
            Type::Float32 => self.reader.read_f32::<BigEndian>().map_err(From::from),
            _ => unexpected_type(ti),
        }
    }

    pub fn f64(&mut self, ti: &TypeInfo) -> DecodeResult<f64> {
        match ti.0 {
            Type::Float64 => self.reader.read_f64::<BigEndian>().map_err(From::from),
            _ => unexpected_type(ti),
        }
    }

    /// Decode `first` and potentially the following bytes as an
    /// unsigned value following the rules of major type 0.
    pub fn unsigned(&mut self, first: u8) -> DecodeResult<u64> {
        match first {
            n @ 0..=23 => Ok(n as u64),
            24 => self.reader.read_u8().map(|n| n as u64).map_err(From::from),
            25 => self
                .reader
                .read_u16::<BigEndian>()
                .map(|n| n as u64)
                .map_err(From::from),
            26 => self
                .reader
                .read_u32::<BigEndian>()
                .map(|n| n as u64)
                .map_err(From::from),
            27 => self.reader.read_u64::<BigEndian>().map_err(From::from),
            _ => unexpected_type(&(Type::UInt64, first)),
        }
    }

    /// Read `begin` as the length and return that many raw bytes.
    ///
    /// If length is greater than the given `max_len`, `DecodeError::TooLong`
    /// is returned instead.
    pub fn raw_data(&mut self, begin: u8, max_len: usize) -> DecodeResult<Vec<u8>> {
        let len = self.unsigned(begin)?;
        if len > max_len as u64 {
            return Err(DecodeError::TooLong {
                max: max_len,
                actual: len,
            });
        }
        let n = len as usize;
        let mut v = vec![0u8; n];
        let mut i = 0;
        while i < n {
            match self.reader.read(&mut v[i..]) {
                Ok(0) => return Err(DecodeError::UnexpectedEOF),
                Ok(j) => i += j,
                Err(e) => {
                    if e.kind() != io::ErrorKind::Interrupted {
                        return Err(DecodeError::IoError(e));
                    }
                }
            }
        }
        Ok(v)
    }

    /// Read `begin` as the length and read that many raw bytes into `buf`.
    ///
    /// If length is greater than the given buffer, `DecodeError::TooLong`
    /// is returned instead.
    pub fn read_raw_data(&mut self, begin: u8, buf: &mut [u8]) -> DecodeResult<usize> {
        let len = self.unsigned(begin)?;
        if len > buf.len() as u64 {
            return Err(DecodeError::TooLong {
                max: buf.len(),
                actual: len,
            });
        }
        let n = len as usize;
        let mut i = 0;
        while i < n {
            match self.reader.read(&mut buf[i..]) {
                Ok(0) => return Err(DecodeError::UnexpectedEOF),
                Ok(j) => i += j,
                Err(e) => {
                    if e.kind() != io::ErrorKind::Interrupted {
                        return Err(DecodeError::IoError(e));
                    }
                }
            }
        }
        Ok(n)
    }
}

impl<R: ReadBytesExt + ReadSlice> Kernel<R> {
    /// Read `begin` as the length and return that many raw bytes as a slice.
    ///
    /// If length is greater than the given `max_len`, `DecodeError::TooLong`
    /// is returned instead.
    pub fn raw_slice(&mut self, begin: u8, max_len: usize) -> DecodeResult<&[u8]> {
        let len = self.unsigned(begin)?;
        if len > max_len as u64 {
            return Err(DecodeError::TooLong {
                max: max_len,
                actual: len,
            });
        }
        self.reader.read_slice(len as usize).map_err(From::from)
    }
}

// Workaround to not require the currently unstable `f32::ldexp`:
mod ffi {
    use libc::c_int;

    extern "C" {
        pub fn ldexpf(x: f32, exp: c_int) -> f32;
    }

    #[inline]
    pub fn c_ldexpf(x: f32, exp: isize) -> f32 {
        unsafe { ldexpf(x, exp as c_int) }
    }
}

fn unexpected_type<A>(ti: &TypeInfo) -> DecodeResult<A> {
    Err(DecodeError::UnexpectedType {
        datatype: ti.0,
        info: ti.1,
    })
}

// Decoder //////////////////////////////////////////////////////////////////

/// The actual decoder type definition
pub struct Decoder<R> {
    kernel: Kernel<R>,
    config: Config,
}

impl<R: ReadBytesExt> Decoder<R> {
    pub fn new(c: Config, r: R) -> Decoder<R> {
        Decoder {
            kernel: Kernel::new(r),
            config: c,
        }
    }

    pub fn into_reader(self) -> R {
        self.kernel.into_reader()
    }

    pub fn kernel(&mut self) -> &mut Kernel<R> {
        &mut self.kernel
    }

    pub fn simple(&mut self) -> DecodeResult<Simple> {
        self.typeinfo().and_then(|ti| self.kernel.simple(&ti))
    }

    pub fn bool(&mut self) -> DecodeResult<bool> {
        self.typeinfo().and_then(|ti| self.kernel.bool(&ti))
    }

    pub fn u8(&mut self) -> DecodeResult<u8> {
        self.typeinfo().and_then(|ti| self.kernel.u8(&ti))
    }

    pub fn u16(&mut self) -> DecodeResult<u16> {
        self.typeinfo().and_then(|ti| self.kernel.u16(&ti))
    }

    pub fn u32(&mut self) -> DecodeResult<u32> {
        self.typeinfo().and_then(|ti| self.kernel.u32(&ti))
    }

    pub fn u64(&mut self) -> DecodeResult<u64> {
        self.typeinfo().and_then(|ti| self.kernel.u64(&ti))
    }

    pub fn i8(&mut self) -> DecodeResult<i8> {
        self.typeinfo().and_then(|ti| self.kernel.i8(&ti))
    }

    pub fn i16(&mut self) -> DecodeResult<i16> {
        self.typeinfo().and_then(|ti| self.kernel.i16(&ti))
    }

    pub fn i32(&mut self) -> DecodeResult<i32> {
        self.typeinfo().and_then(|ti| self.kernel.i32(&ti))
    }

    pub fn i64(&mut self) -> DecodeResult<i64> {
        self.typeinfo().and_then(|ti| self.kernel.i64(&ti))
    }

    pub fn int(&mut self) -> DecodeResult<Int> {
        self.typeinfo().and_then(|ti| self.kernel.int(&ti))
    }

    pub fn f16(&mut self) -> DecodeResult<f32> {
        self.typeinfo().and_then(|ti| self.kernel.f16(&ti))
    }

    pub fn f32(&mut self) -> DecodeResult<f32> {
        self.typeinfo().and_then(|ti| self.kernel.f32(&ti))
    }

    pub fn f64(&mut self) -> DecodeResult<f64> {
        self.typeinfo().and_then(|ti| self.kernel.f64(&ti))
    }

    /// Decode a single byte string into the given buffer.
    ///
    /// The provided buffer must be large enough to hold the entire
    /// byte string, otherwise an error is returned.
    ///
    /// Please note that indefinite byte strings are not supported by this
    /// method (Consider using `Decoder::bytes_iter()` for this use-case).
    pub fn read_bytes(&mut self, b: &mut [u8]) -> DecodeResult<usize> {
        match self.typeinfo()? {
            (Type::Bytes, 31) => unexpected_type(&(Type::Bytes, 31)),
            (Type::Bytes, i) => self.kernel.read_raw_data(i, b),
            ti => unexpected_type(&ti),
        }
    }

    /// Decode a single byte string.
    ///
    /// Please note that indefinite byte strings are not supported by this
    /// method (Consider using `Decoder::bytes_iter()` for this use-case).
    pub fn bytes(&mut self) -> DecodeResult<Vec<u8>> {
        match self.typeinfo()? {
            (Type::Bytes, 31) => unexpected_type(&(Type::Bytes, 31)),
            (Type::Bytes, i) => {
                let max = self.config.max_len_bytes;
                self.kernel.raw_data(i, max)
            }
            ti => unexpected_type(&ti),
        }
    }

    /// Decode an indefinite byte string.
    pub fn bytes_iter(&mut self) -> DecodeResult<BytesIter<R>> {
        match self.typeinfo()? {
            (Type::Bytes, 31) => Ok(BytesIter { decoder: self }),
            ti => unexpected_type(&ti),
        }
    }

    /// Decode a single UTF-8 encoded String.
    ///
    /// Please note that indefinite strings are not supported by this method
    /// (Consider using `Decoder::text_iter()` for this use-case).
    pub fn text(&mut self) -> DecodeResult<String> {
        match self.typeinfo()? {
            (Type::Text, 31) => unexpected_type(&(Type::Text, 31)),
            (Type::Text, i) => {
                let max = self.config.max_len_text;
                let data = self.kernel.raw_data(i, max)?;
                String::from_utf8(data).map_err(From::from)
            }
            ti => unexpected_type(&ti),
        }
    }

    /// Decode an indefinite string.
    pub fn text_iter(&mut self) -> DecodeResult<TextIter<R>> {
        match self.typeinfo()? {
            (Type::Text, 31) => Ok(TextIter { decoder: self }),
            ti => unexpected_type(&ti),
        }
    }

    /// Decode a `Tag`.
    /// If no tag is found an `UnexpectedType` error is returned.
    pub fn tag(&mut self) -> DecodeResult<Tag> {
        match self.kernel.typeinfo()? {
            (Type::Tagged, i) => self.kernel.unsigned(i).map(Tag::of),
            ti => unexpected_type(&ti),
        }
    }

    /// Decode the begin of an array. The length is returned unless it
    /// exceeds the configured maximum.
    ///
    /// Please note that indefinite arrays are not supported by this method
    /// (Consider using `Decoder::array_begin()` for this use-case).
    pub fn array(&mut self) -> DecodeResult<usize> {
        match self.typeinfo()? {
            (Type::Array, 31) => unexpected_type(&(Type::Array, 31)),
            (Type::Array, a) => {
                let len = self.kernel.unsigned(a)?;
                if len > self.config.max_len_array as u64 {
                    return Err(DecodeError::TooLong {
                        max: self.config.max_len_array,
                        actual: len,
                    });
                }
                Ok(len as usize)
            }
            ti => unexpected_type(&ti),
        }
    }

    /// Decode the begin of an indefinite array.
    /// After this one can continue decoding items, but a `Break` value
    /// will be encountered at some unknown point.
    ///
    /// (Consider using `or_break` around every decoding step within an
    /// indefinite array to handle this case).
    pub fn array_begin(&mut self) -> DecodeResult<()> {
        match self.typeinfo()? {
            (Type::Array, 31) => Ok(()),
            ti => unexpected_type(&ti),
        }
    }

    /// Decode the begin of an object. The size (number of key-value pairs)
    /// is returned unless it exceeds the configured maximum.
    ///
    /// Please note that indefinite objects are not supported by this method
    /// (Consider using `Decoder::object_begin` for this use-case).
    pub fn object(&mut self) -> DecodeResult<usize> {
        match self.typeinfo()? {
            (Type::Object, 31) => unexpected_type(&(Type::Object, 31)),
            (Type::Object, a) => {
                let len = self.kernel.unsigned(a)?;
                if len > self.config.max_size_map as u64 {
                    return Err(DecodeError::TooLong {
                        max: self.config.max_size_map,
                        actual: len,
                    });
                }
                Ok(len as usize)
            }
            ti => unexpected_type(&ti),
        }
    }

    /// Decode the begin of an indefinite object.
    /// After this one can continue decoding items, but a `Break` value
    /// will be encountered at some unknown point.
    ///
    /// (Consider using `or_break` around every decoding step within an
    /// indefinite object to handle this case).
    pub fn object_begin(&mut self) -> DecodeResult<()> {
        match self.typeinfo()? {
            (Type::Object, 31) => Ok(()),
            ti => unexpected_type(&ti),
        }
    }

    // Decode type information while skipping tags
    fn typeinfo(&mut self) -> DecodeResult<TypeInfo> {
        fn go<A: ReadBytesExt>(d: &mut Decoder<A>, level: usize) -> DecodeResult<TypeInfo> {
            if level == 0 {
                return Err(DecodeError::TooNested);
            }
            match d.kernel.typeinfo()? {
                (Type::Tagged, i) => d.kernel.unsigned(i).and(go(d, level - 1)),
                ti => Ok(ti),
            }
        }
        let start = self.config.max_nesting;
        go(self, start)
    }
}

impl<R: ReadBytesExt + Skip> Decoder<R> {
    /// Skip over a single CBOR value.
    ///
    /// Please note that this function does not validate the value hence
    /// it might not even be well-formed CBOR. Instead `skip` is an
    /// optimization over `GenericDecoder::value()` and generally only
    /// determines as much information as necessary to safely skip a value
    /// without keeping all of it in memory.
    pub fn skip(&mut self) -> DecodeResult<()> {
        let start = self.config.max_nesting;
        self.skip_value(start).and(Ok(()))
    }

    fn skip_value(&mut self, level: usize) -> DecodeResult<bool> {
        if level == 0 {
            return Err(DecodeError::TooNested);
        }
        match self.typeinfo()? {
            (Type::UInt8, n) => self.kernel.unsigned(n).and(Ok(true)),
            (Type::UInt16, n) => self.kernel.unsigned(n).and(Ok(true)),
            (Type::UInt32, n) => self.kernel.unsigned(n).and(Ok(true)),
            (Type::UInt64, n) => self.kernel.unsigned(n).and(Ok(true)),
            (Type::Int8, n) => self.kernel.unsigned(n).and(Ok(true)),
            (Type::Int16, n) => self.kernel.unsigned(n).and(Ok(true)),
            (Type::Int32, n) => self.kernel.unsigned(n).and(Ok(true)),
            (Type::Int64, n) => self.kernel.unsigned(n).and(Ok(true)),
            (Type::Bool, _) => Ok(true),
            (Type::Null, _) => Ok(true),
            (Type::Undefined, _) => Ok(true),
            (Type::Break, _) => Ok(false),
            (Type::Float16, _) => {
                self.kernel.reader.skip(2)?;
                Ok(true)
            }
            (Type::Float32, _) => {
                self.kernel.reader.skip(4)?;
                Ok(true)
            }
            (Type::Float64, _) => {
                self.kernel.reader.skip(8)?;
                Ok(true)
            }
            (Type::Bytes, 31) => self.skip_until_break(Type::Bytes).and(Ok(true)),
            (Type::Bytes, a) => {
                let n = self.kernel.unsigned(a)?;
                self.kernel.reader.skip(n)?;
                Ok(true)
            }
            (Type::Text, 31) => self.skip_until_break(Type::Text).and(Ok(true)),
            (Type::Text, a) => {
                let n = self.kernel.unsigned(a)?;
                self.kernel.reader.skip(n)?;
                Ok(true)
            }
            (Type::Array, 31) => {
                while self.skip_value(level - 1)? {}
                Ok(true)
            }
            (Type::Object, 31) => {
                while self.skip_value(level - 1)? {}
                Ok(true)
            }
            (Type::Array, a) => {
                let n = self.kernel.unsigned(a)?;
                for _ in 0..n {
                    self.skip_value(level - 1)?;
                }
                Ok(true)
            }
            (Type::Object, a) => {
                let n = self.kernel.unsigned(a)?;
                // n == number of fields => need to skip over keys and values.
                // Instead of doubling n we loop twice in order to avoid
                // overflowing n.
                for _ in 0..n {
                    self.skip_value(level - 1)?;
                }
                for _ in 0..n {
                    self.skip_value(level - 1)?;
                }
                Ok(true)
            }
            (Type::Unassigned { .. }, _) => Ok(true),
            (Type::Reserved { .. }, _) => Ok(true),
            ti @ (Type::Tagged, _) => unexpected_type(&ti),
            ti @ (Type::Unknown { .. }, _) => unexpected_type(&ti),
        }
    }

    // Skip over `Text` or `Bytes` until a `Break` is encountered.
    fn skip_until_break(&mut self, ty: Type) -> DecodeResult<()> {
        loop {
            let (t, a) = self.typeinfo()?;
            if t == Type::Break {
                break;
            }
            if t != ty || a == 31 {
                return unexpected_type(&(t, a));
            }
            let n = self.kernel.unsigned(a)?;
            self.kernel.reader.skip(n)?
        }
        Ok(())
    }
}

impl<R: ReadBytesExt + ReadSlice> Decoder<R> {
    /// Decode a single UTF-8 encoded String and borrow it from underlying
    /// buffer instead of allocating.
    ///
    /// Please note that indefinite strings are not supported by this method.
    pub fn text_borrow(&mut self) -> DecodeResult<&str> {
        match self.typeinfo()? {
            (Type::Text, 31) => unexpected_type(&(Type::Text, 31)),
            (Type::Text, i) => {
                let max = self.config.max_len_text;
                let data = self.kernel.raw_slice(i, max)?;
                from_utf8(data).map_err(From::from)
            }
            ti => unexpected_type(&ti),
        }
    }

    /// Decode a single byte string and borrow it from underlying
    /// buffer instead of allocating.
    ///
    /// Please note that indefinite byte strings are not supported by this
    /// method.
    pub fn bytes_borrow(&mut self) -> DecodeResult<&[u8]> {
        match self.typeinfo()? {
            (Type::Bytes, 31) => unexpected_type(&(Type::Bytes, 31)),
            (Type::Bytes, i) => {
                let max = self.config.max_len_bytes;
                self.kernel.raw_slice(i, max)
            }
            ti => unexpected_type(&ti),
        }
    }
}

// Iterators ////////////////////////////////////////////////////////////////

/// Iterator over the chunks of an indefinite bytes item.
pub struct BytesIter<'r, R: 'r> {
    decoder: &'r mut Decoder<R>,
}

impl<'r, R: 'r + ReadBytesExt> Iterator for BytesIter<'r, R> {
    type Item = DecodeResult<Vec<u8>>;

    fn next(&mut self) -> Option<DecodeResult<Vec<u8>>> {
        match or_break(self.decoder.bytes()) {
            Ok(None) => None,
            Ok(Some(b)) => Some(Ok(b)),
            Err(e) => Some(Err(e)),
        }
    }
}

/// Iterator over the chunks of an indefinite text item.
pub struct TextIter<'r, R: 'r> {
    decoder: &'r mut Decoder<R>,
}

impl<'r, R: 'r + ReadBytesExt> Iterator for TextIter<'r, R> {
    type Item = DecodeResult<String>;

    fn next(&mut self) -> Option<DecodeResult<String>> {
        match or_break(self.decoder.text()) {
            Ok(None) => None,
            Ok(Some(b)) => Some(Ok(b)),
            Err(e) => Some(Err(e)),
        }
    }
}

// Generic Decoder //////////////////////////////////////////////////////////

/// A generic decoder decodes arbitrary CBOR into a `Value` AST.
pub struct GenericDecoder<R> {
    decoder: Decoder<R>,
}

impl<R: ReadBytesExt> GenericDecoder<R> {
    pub fn new(c: Config, r: R) -> GenericDecoder<R> {
        GenericDecoder {
            decoder: Decoder::new(c, r),
        }
    }

    pub fn from_decoder(d: Decoder<R>) -> GenericDecoder<R> {
        GenericDecoder { decoder: d }
    }

    pub fn into_inner(self) -> Decoder<R> {
        self.decoder
    }

    // dead code
    // pub(crate) fn borrow_mut(&mut self) -> &mut Decoder<R> {
    //    &mut self.decoder
    // }

    /// Decode into a `Value`, i.e. an intermediate representation which
    /// can be further deconstructed using a `Cursor`.
    /// This supports indefinite decoding as well as tag validation
    /// (if not disabled).
    pub fn value(&mut self) -> DecodeResult<Value> {
        let start = self.decoder.config.max_nesting;
        self.decode_value(start)
    }

    fn decode_value(&mut self, level: usize) -> DecodeResult<Value> {
        if level == 0 {
            return Err(DecodeError::TooNested);
        }
        match self.decoder.kernel.typeinfo()? {
            ti @ (Type::UInt8, _) => self.decoder.kernel.u8(&ti).map(Value::U8),
            ti @ (Type::UInt16, _) => self.decoder.kernel.u16(&ti).map(Value::U16),
            ti @ (Type::UInt32, _) => self.decoder.kernel.u32(&ti).map(Value::U32),
            ti @ (Type::UInt64, _) => self.decoder.kernel.u64(&ti).map(Value::U64),
            ti @ (Type::Int8, _) => self.decoder.kernel.i16(&ti).map(|n| {
                if n > i8::MAX as i16 || n < i8::MIN as i16 {
                    Value::I16(n)
                } else {
                    Value::I8(n as i8)
                }
            }),
            ti @ (Type::Int16, _) => self.decoder.kernel.i32(&ti).map(|n| {
                if n > i16::MAX as i32 || n < i16::MIN as i32 {
                    Value::I32(n)
                } else {
                    Value::I16(n as i16)
                }
            }),
            ti @ (Type::Int32, _) => self.decoder.kernel.i64(&ti).map(|n| {
                if n > i32::MAX as i64 || n < i32::MIN as i64 {
                    Value::I64(n)
                } else {
                    Value::I32(n as i32)
                }
            }),
            (Type::Int64, a) => self.decoder.kernel.unsigned(a).map(|n| {
                if n > i64::MAX as u64 {
                    Value::Int(Int::Neg(n))
                } else {
                    Value::I64(-1 - n as i64)
                }
            }),
            ti @ (Type::Float16, _) => self.decoder.kernel.f16(&ti).map(Value::F32),
            ti @ (Type::Float32, _) => self.decoder.kernel.f32(&ti).map(Value::F32),
            ti @ (Type::Float64, _) => self.decoder.kernel.f64(&ti).map(Value::F64),
            ti @ (Type::Bool, _) => self.decoder.kernel.bool(&ti).map(Value::Bool),
            (Type::Null, _) => Ok(Value::Null),
            (Type::Undefined, _) => Ok(Value::Undefined),
            (Type::Break, _) => Ok(Value::Break),
            (Type::Bytes, 31) => {
                // indefinite byte string
                let mut i = 0u64;
                let mut v = LinkedList::new();
                loop {
                    match self.decoder.bytes() {
                        Ok(chunk) => {
                            i += chunk.len() as u64;
                            if i > self.decoder.config.max_len_bytes as u64 {
                                return Err(DecodeError::TooLong {
                                    max: self.decoder.config.max_len_bytes,
                                    actual: i,
                                });
                            }
                            v.push_back(chunk)
                        }
                        Err(ref e) if is_break(e) => break,
                        Err(e) => return Err(e),
                    }
                }
                Ok(Value::Bytes(Bytes::Chunks(v)))
            }
            (Type::Bytes, a) => {
                let max = self.decoder.config.max_len_bytes;
                self.decoder
                    .kernel
                    .raw_data(a, max)
                    .map(|x| Value::Bytes(Bytes::Bytes(x)))
            }
            (Type::Text, 31) => {
                // indefinite string
                let mut i = 0u64;
                let mut v = LinkedList::new();
                loop {
                    match self.decoder.text() {
                        Ok(chunk) => {
                            i += chunk.len() as u64;
                            if i > self.decoder.config.max_len_text as u64 {
                                return Err(DecodeError::TooLong {
                                    max: self.decoder.config.max_len_text,
                                    actual: i,
                                });
                            }
                            v.push_back(chunk)
                        }
                        Err(ref e) if is_break(e) => break,
                        Err(e) => return Err(e),
                    }
                }
                Ok(Value::Text(Text::Chunks(v)))
            }
            (Type::Text, a) => {
                let max = self.decoder.config.max_len_text;
                let data = self.decoder.kernel.raw_data(a, max)?;
                String::from_utf8(data)
                    .map(|x| Value::Text(Text::Text(x)))
                    .map_err(From::from)
            }
            (Type::Array, 31) => {
                // indefinite length array
                let mut i = 0u64;
                let mut v = Vec::new();
                loop {
                    i += 1;
                    if i > self.decoder.config.max_len_array as u64 {
                        return Err(DecodeError::TooLong {
                            max: self.decoder.config.max_len_array,
                            actual: i,
                        });
                    }
                    match self.decode_value(level - 1) {
                        Ok(Value::Break) => break,
                        Ok(x) => v.push(x),
                        e => return e,
                    }
                }
                Ok(Value::Array(v))
            }
            (Type::Array, a) => {
                let len = self.decoder.kernel.unsigned(a)?;
                if len > self.decoder.config.max_len_array as u64 {
                    return Err(DecodeError::TooLong {
                        max: self.decoder.config.max_len_array,
                        actual: len,
                    });
                }
                let n = len as usize;
                let mut v = Vec::with_capacity(n);
                for _ in 0..n {
                    v.push(self.decode_value(level - 1)?);
                }
                Ok(Value::Array(v))
            }
            (Type::Object, 31) => {
                // indefinite size object
                let mut i = 0u64;
                let mut m = BTreeMap::new();
                loop {
                    i += 1;
                    if i > self.decoder.config.max_size_map as u64 {
                        return Err(DecodeError::TooLong {
                            max: self.decoder.config.max_size_map,
                            actual: i,
                        });
                    }
                    match self.decode_key(level - 1) {
                        Ok(key) => {
                            if m.contains_key(&key) {
                                return Err(DecodeError::DuplicateKey(key));
                            }
                            match self.decode_value(level - 1)? {
                                Value::Break => return Err(DecodeError::UnexpectedBreak),
                                value => {
                                    m.insert(key, value);
                                }
                            }
                        }
                        Err(DecodeError::InvalidKey(Value::Break)) => break,
                        Err(e) => return Err(e),
                    }
                }
                Ok(Value::Map(m))
            }
            (Type::Object, a) => {
                let len = self.decoder.kernel.unsigned(a)?;
                if len > self.decoder.config.max_size_map as u64 {
                    return Err(DecodeError::TooLong {
                        max: self.decoder.config.max_size_map,
                        actual: len,
                    });
                }
                let n = len as usize;
                let mut m = BTreeMap::new();
                for _ in 0..n {
                    let key = self.decode_key(level - 1)?;
                    if m.contains_key(&key) {
                        return Err(DecodeError::DuplicateKey(key));
                    }
                    m.insert(key, self.decode_value(level - 1)?);
                }
                Ok(Value::Map(m))
            }
            (Type::Tagged, a) => {
                let tag = self.decoder.kernel.unsigned(a).map(Tag::of)?;
                if self.decoder.config.skip_tags {
                    return self.decode_value(level - 1);
                }
                let val = self
                    .decode_value(level - 1)
                    .map(|v| Value::Tagged(tag, Box::new(v)))?;
                if self.decoder.config.check_tags && !value::check(&val) {
                    return Err(DecodeError::InvalidTag(val));
                }
                Ok(val)
            }
            (Type::Unassigned { major: 7, info: a }, _) => Ok(Value::Simple(Simple::Unassigned(a))),
            (Type::Reserved { major: 7, info: a }, _) => Ok(Value::Simple(Simple::Reserved(a))),
            ti => unexpected_type(&ti),
        }
    }

    fn decode_key(&mut self, level: usize) -> DecodeResult<Key> {
        match self.decode_value(level)? {
            Value::Bool(x) => Ok(Key::Bool(x)),
            Value::Bytes(x) => Ok(Key::Bytes(x)),
            Value::Text(x) => Ok(Key::Text(x)),
            Value::I8(x) => Ok(Key::i64(x as i64)),
            Value::I16(x) => Ok(Key::i64(x as i64)),
            Value::I32(x) => Ok(Key::i64(x as i64)),
            Value::I64(x) => Ok(Key::i64(x)),
            Value::U8(x) => Ok(Key::u64(x as u64)),
            Value::U16(x) => Ok(Key::u64(x as u64)),
            Value::U32(x) => Ok(Key::u64(x as u64)),
            Value::U64(x) => Ok(Key::u64(x)),
            Value::Int(x) => Ok(Key::Int(x)),
            other => Err(DecodeError::InvalidKey(other)),
        }
    }
}

// Tests ////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::Tag;
    use crate::value::{self, Int, Key, Simple, Value};
    use rustc_serialize::hex::FromHex;
    use std::collections::BTreeMap;
    use std::io::Cursor;
    use std::{f32, f64, u64};

    #[test]
    fn unsigned() {
        assert_eq!(Some(0), decoder("00").u8().ok());
        assert_eq!(Some(1), decoder("01").u8().ok());
        assert_eq!(Some(10), decoder("0a").u8().ok());
        assert_eq!(Some(23), decoder("17").u8().ok());
        assert_eq!(Some(24), decoder("1818").u8().ok());
        assert_eq!(Some(25), decoder("1819").u8().ok());
        assert_eq!(Some(100), decoder("1864").u8().ok());
        assert_eq!(Some(1000), decoder("1903e8").u16().ok());
        assert_eq!(Some(1000000), decoder("1a000f4240").u32().ok());
        assert_eq!(
            Some(1000000000000),
            decoder("1b000000e8d4a51000").u64().ok()
        );
        assert_eq!(
            Some(18446744073709551615),
            decoder("1bffffffffffffffff").u64().ok()
        );
    }

    #[test]
    fn signed() {
        assert_eq!(Some(-1), decoder("20").i8().ok());
        assert_eq!(Some(-10), decoder("29").i8().ok());
        assert_eq!(Some(-100), decoder("3863").i8().ok());
        assert_eq!(Some(-500), decoder("3901f3").i16().ok());
        assert_eq!(Some(-1000), decoder("3903e7").i16().ok());
        assert_eq!(Some(-343434), decoder("3a00053d89").i32().ok());
        assert_eq!(Some(-23764523654), decoder("3b000000058879da85").i64().ok())
    }

    #[test]
    fn mixed() {
        assert_eq!(Some(0), decoder("00").i8().ok());
        assert_eq!(Some(1), decoder("01").i8().ok());
        assert_eq!(Some(10), decoder("0a").i8().ok());
        assert_eq!(Some(23), decoder("17").i8().ok());
        assert_eq!(Some(24), decoder("1818").i8().ok());
        assert_eq!(Some(25), decoder("1819").i8().ok());
        assert_eq!(Some(100), decoder("1864").i8().ok());
        assert_eq!(Some(1000), decoder("1903e8").i16().ok());
        assert_eq!(Some(1000000), decoder("1a000f4240").i32().ok());
    }

    #[test]
    fn int() {
        assert_eq!(Some(Some(0)), decoder("00").int().ok().map(|n| n.i64()));
        assert_eq!(Some(Some(1)), decoder("01").int().ok().map(|n| n.i64()));
        assert_eq!(Some(Some(10)), decoder("0a").int().ok().map(|n| n.i64()));
        assert_eq!(Some(Some(23)), decoder("17").int().ok().map(|n| n.i64()));
        assert_eq!(Some(Some(24)), decoder("1818").int().ok().map(|n| n.i64()));
        assert_eq!(Some(Some(25)), decoder("1819").int().ok().map(|n| n.i64()));
        assert_eq!(Some(Some(100)), decoder("1864").int().ok().map(|n| n.i64()));
        assert_eq!(
            Some(Some(1000)),
            decoder("1903e8").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Some(1000000)),
            decoder("1a000f4240").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Some(1000000000000)),
            decoder("1b000000e8d4a51000").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(None),
            decoder("1bffffffffffffffff").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Some(0xffffffffffffffff)),
            decoder("1bffffffffffffffff").int().ok().map(|n| n.u64())
        );
        assert_eq!(
            Some(Some(0x7fffffffffffffff)),
            decoder("1b7fffffffffffffff").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Some(-9223372036854775808)),
            decoder("3b7fffffffffffffff").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Int::Neg(u64::MAX)),
            decoder("3bffffffffffffffff").int().ok()
        );
        assert_eq!(Some(Some(-1)), decoder("20").int().ok().map(|n| n.i64()));
        assert_eq!(Some(Some(-10)), decoder("29").int().ok().map(|n| n.i64()));
        assert_eq!(
            Some(Some(-100)),
            decoder("3863").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Some(-500)),
            decoder("3901f3").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Some(-1000)),
            decoder("3903e7").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Some(-343434)),
            decoder("3a00053d89").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Some(-23764523654)),
            decoder("3b000000058879da85").int().ok().map(|n| n.i64())
        );
        assert_eq!(
            Some(Value::Int(Int::Neg(u64::MAX))),
            gen_decoder("3bffffffffffffffff").value().ok()
        )
    }

    #[test]
    fn float() {
        assert_eq!(Some(0.0), decoder("f90000").f16().ok());
        assert_eq!(Some(-0.0), decoder("f98000").f16().ok());
        assert_eq!(Some(1.0), decoder("f93c00").f16().ok());
        assert_eq!(Some(1.5), decoder("f93e00").f16().ok());
        assert_eq!(Some(65504.0), decoder("f97bff").f16().ok());
        assert_eq!(Some(f32::INFINITY), decoder("f97c00").f16().ok());
        assert_eq!(Some(-f32::INFINITY), decoder("f9fc00").f16().ok());
        assert!(decoder("f97e00").f16().ok().unwrap().is_nan());

        assert_eq!(Some(100000.0), decoder("fa47c35000").f32().ok());
        assert_eq!(
            Some(3.4028234663852886e+38),
            decoder("fa7f7fffff").f32().ok()
        );
        assert_eq!(Some(-4.1), decoder("fbc010666666666666").f64().ok());

        assert_eq!(Some(f32::INFINITY), decoder("fa7f800000").f32().ok());
        assert_eq!(Some(-f32::INFINITY), decoder("faff800000").f32().ok());
        assert!(decoder("fa7fc00000").f32().ok().unwrap().is_nan());

        assert_eq!(Some(1.0e+300), decoder("fb7e37e43c8800759c").f64().ok());
        assert_eq!(
            Some(f64::INFINITY),
            decoder("fb7ff0000000000000").f64().ok()
        );
        assert_eq!(
            Some(-f64::INFINITY),
            decoder("fbfff0000000000000").f64().ok()
        );
        assert!(decoder("fb7ff8000000000000").f64().ok().unwrap().is_nan())
    }

    #[test]
    fn bool() {
        assert_eq!(Some(false), decoder("f4").bool().ok());
        assert_eq!(Some(true), decoder("f5").bool().ok());
    }

    #[test]
    fn simple() {
        assert_eq!(Some(Simple::Unassigned(16)), decoder("f0").simple().ok());
        assert_eq!(Some(Simple::Reserved(24)), decoder("f818").simple().ok());
        assert_eq!(Some(Simple::Unassigned(255)), decoder("f8ff").simple().ok())
    }

    #[test]
    fn bytes() {
        assert_eq!(Some(vec![1, 2, 3, 4]), decoder("4401020304").bytes().ok())
    }

    #[test]
    fn read_bytes() {
        let mut buf = [0u8; 4];
        assert_eq!(Some(4), decoder("4401020304").read_bytes(&mut buf).ok());
        assert_eq!([1, 2, 3, 4], buf)
    }

    #[test]
    fn text() {
        let expected1 = String::from("dfsdfsdf\r\nsdf\r\nhello\r\nsdfsfsdfs");
        assert_eq!(
            Some(expected1),
            decoder("781f64667364667364660d0a7364660d0a68656c6c6f0d0a736466736673646673")
                .text()
                .ok()
        );

        let expected2 = String::from("\u{00fc}");
        assert_eq!(Some(expected2), decoder("62c3bc").text().ok());

        let mut r = Vec::new();
        let mut d = decoder("7f657374726561646d696e67ff");
        for t in d.text_iter().unwrap() {
            r.push(t.unwrap())
        }
        assert_eq!(vec![String::from("strea"), String::from("ming")], r);
    }

    #[test]
    fn text_borrow() {
        let expected1 = "dfsdfsdf\r\nsdf\r\nhello\r\nsdfsfsdfs";
        assert_eq!(
            Some(expected1),
            decoder("781f64667364667364660d0a7364660d0a68656c6c6f0d0a736466736673646673")
                .text_borrow()
                .ok()
        );
    }

    #[test]
    fn bytes_borrow() {
        let expected1 = &b"dfsdfsdf\r\nsdf\r\nhello\r\nsdfsfsdfs"[..];
        assert_eq!(
            Some(expected1),
            decoder("581f64667364667364660d0a7364660d0a68656c6c6f0d0a736466736673646673")
                .bytes_borrow()
                .ok()
        );
    }

    #[test]
    fn option() {
        let none: Option<u8> = None;
        let some: Option<u8> = Some(1);
        assert_eq!(Some(none), opt(decoder("f6").u8()).ok());
        assert_eq!(Some(some), opt(decoder("01").u8()).ok())
    }

    #[test]
    fn undefined() {
        let undef: Option<u8> = None;
        let def: Option<u8> = Some(1);
        assert_eq!(Some(undef), maybe(decoder("f7").u8()).ok());
        assert_eq!(Some(def), maybe(decoder("01").u8()).ok())
    }

    #[test]
    fn empty_arrays() {
        assert_eq!(Some(Value::Array(vec![])), gen_decoder("9fff").value().ok());

        let mut d = decoder("9fff");
        d.array_begin().unwrap();
        or_break(d.u8()).unwrap();
    }

    #[test]
    fn array() {
        let mut d = decoder("83010203");
        assert_eq!(3, d.array().unwrap());
        assert_eq!(Some(1u32), d.u32().ok());
        assert_eq!(Some(2u32), d.u32().ok());
        assert_eq!(Some(3u32), d.u32().ok());
    }

    #[test]
    fn object() {
        let mut map = BTreeMap::new();
        map.insert(String::from("a"), 1u8);
        map.insert(String::from("b"), 2u8);
        map.insert(String::from("c"), 3u8);
        let mut d = decoder("a3616101616202616303");
        let mut x = BTreeMap::new();
        for _ in 0..d.object().unwrap() {
            x.insert(d.text().unwrap(), d.u8().unwrap());
        }
        assert_eq!(map, x);
    }

    #[test]
    fn skip() {
        let mut d = decoder("a66161016162820203616382040561647f657374726561646d696e67ff61659f070405ff61666568656c6c6f");
        for _ in 0..d.object().unwrap() {
            match d.text().unwrap().as_ref() {
                "a" => {
                    d.u8().unwrap();
                }
                "b" => {
                    for _ in 0..d.array().unwrap() {
                        d.u8().unwrap();
                    }
                }
                "c" => d.skip().unwrap(),
                "d" => d.skip().unwrap(),
                "e" => d.skip().unwrap(),
                "f" => d.skip().unwrap(),
                key => panic!("unexpected key: {}", key),
            }
        }
    }

    #[test]
    fn array_of_array() {
        let mut d = decoder("828301020383010203");
        let outer = d.array().unwrap();
        let mut v = Vec::with_capacity(outer);
        for _ in 0..outer {
            let inner = d.array().unwrap();
            let mut w = Vec::with_capacity(inner);
            for _ in 0..inner {
                w.push(d.u8().unwrap())
            }
            v.push(w)
        }
        assert_eq!(vec![vec![1, 2, 3], vec![1, 2, 3]], v)
    }

    #[test]
    fn object_value() {
        let v = gen_decoder("a2616101028103").value().ok().unwrap();
        let d = value::Cursor::new(&v);
        assert_eq!(Some(1), d.field("a").u8());
        assert_eq!(Some(3), d.get(Key::u64(2)).at(0).u8())
    }

    #[test]
    fn tagged() {
        // by default, tags are ignored
        assert_eq!(Some(1363896240), decoder("c11a514b67b0").u32().ok());
        // but can be extracted if desired
        let mut d = decoder("c11a514b67b0");
        assert_eq!(Some(Tag::Timestamp), d.tag().ok());
        assert_eq!(Some(1363896240), d.u32().ok())
    }

    #[test]
    fn tagged_value() {
        match gen_decoder("c11a514b67b0").value().ok() {
            Some(Value::Tagged(Tag::Timestamp, ref v)) if **v == Value::U32(1363896240) => (),
            other => panic!("impossible tagged value: {:?}", other),
        }
        match gen_decoder("c1fb41d452d9ec200000").value().ok() {
            Some(Value::Tagged(Tag::Timestamp, ref v)) if **v == Value::F64(1363896240.5) => (),
            other => panic!("impossible tagged value: {:?}", other),
        }
    }

    fn decoder(s: &str) -> Decoder<Cursor<Vec<u8>>> {
        Decoder::new(Config::default(), Cursor::new(s.from_hex().unwrap()))
    }

    fn gen_decoder(s: &str) -> GenericDecoder<Cursor<Vec<u8>>> {
        GenericDecoder::from_decoder(decoder(s))
    }
}