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
#![allow(clippy::unusual_byte_groupings)]
use crate::{ARRAY, BREAK, BYTES, MAP, SIMPLE, TAGGED, TEXT, SIGNED, UNSIGNED};
use crate::data::{Tag, Type};
use crate::decode::{Decode, Error};
use core::{char, f32, i8, i16, i32, i64};
use core::{convert::TryInto, marker, str};
macro_rules! u_to_i {
($val: expr, $typ: ty, $max: expr, $msg: expr) => {{
let val = $val;
if val > $max {
Err(Error::Overflow(u64::from(val), $msg))
} else {
Ok(-1 - val as $typ)
}
}}
}
macro_rules! u_as_i {
($val: expr, $typ: ty, $max: expr, $msg: expr) => {{
let val = $val;
if val > $max {
Err(Error::Overflow(u64::from(val), $msg))
} else {
Ok(val as $typ)
}
}}
}
#[derive(Debug, Clone)]
pub struct Decoder<'b> {
buf: &'b [u8],
pos: usize
}
impl<'b> Decoder<'b> {
pub fn new(bytes: &'b [u8]) -> Self {
Decoder { buf: bytes, pos: 0 }
}
pub fn decode<T: Decode<'b>>(&mut self) -> Result<T, Error> {
T::decode(self)
}
pub fn position(&self) -> usize {
self.pos
}
pub fn set_position(&mut self, pos: usize) {
self.pos = pos
}
pub fn probe<'a>(&'a mut self) -> Probe<'a, 'b> {
Probe {
decoder: self.clone(),
_marker: marker::PhantomData
}
}
pub fn bool(&mut self) -> Result<bool, Error> {
match self.read()? {
0xf4 => Ok(false),
0xf5 => Ok(true),
b => Err(Error::TypeMismatch(b, "expected bool"))
}
}
pub fn u8(&mut self) -> Result<u8, Error> {
match self.read()? {
n @ 0 ..= 0x17 => Ok(n),
0x18 => self.read(),
b => Err(Error::TypeMismatch(b, "expected u8"))
}
}
pub fn u16(&mut self) -> Result<u16, Error> {
match self.read()? {
n @ 0 ..= 0x17 => Ok(u16::from(n)),
0x18 => self.read().map(u16::from),
0x19 => self.read_slice(2).map(read_u16),
b => Err(Error::TypeMismatch(b, "expected u16"))
}
}
pub fn u32(&mut self) -> Result<u32, Error> {
match self.read()? {
n @ 0 ..= 0x17 => Ok(u32::from(n)),
0x18 => self.read().map(u32::from),
0x19 => self.read_slice(2).map(read_u16).map(u32::from),
0x1a => self.read_slice(4).map(read_u32),
b => Err(Error::TypeMismatch(b, "expected u32"))
}
}
pub fn u64(&mut self) -> Result<u64, Error> {
let n = self.read()?;
self.unsigned(n)
}
pub fn i8(&mut self) -> Result<i8, Error> {
match self.read()? {
n @ 0x00 ..= 0x17 => Ok(n as i8),
0x18 => u_as_i!(self.read()?, i8, i8::MAX as u8, "u8->i8"),
n @ 0x20 ..= 0x37 => Ok(-1 - (n - 0x20) as i8),
0x38 => u_to_i!(self.read()?, i8, i8::MAX as u8, "u8->i8"),
b => Err(Error::TypeMismatch(b, "expected i8"))
}
}
pub fn i16(&mut self) -> Result<i16, Error> {
match self.read()? {
n @ 0x00 ..= 0x17 => Ok(i16::from(n)),
0x18 => self.read().map(i16::from),
0x19 => u_as_i!(self.read_slice(2).map(read_u16)?, i16, i16::MAX as u16, "u16->i16"),
n @ 0x20 ..= 0x37 => Ok(-1 - i16::from(n - 0x20)),
0x38 => self.read().map(|n| -1 - i16::from(n)),
0x39 => u_to_i!(self.read_slice(2).map(read_u16)?, i16, i16::MAX as u16, "u16->i16"),
b => Err(Error::TypeMismatch(b, "expected i16"))
}
}
pub fn i32(&mut self) -> Result<i32, Error> {
match self.read()? {
n @ 0x00 ..= 0x17 => Ok(i32::from(n)),
0x18 => self.read().map(i32::from),
0x19 => self.read_slice(2).map(read_u16).map(i32::from),
0x1a => u_as_i!(self.read_slice(4).map(read_u32)?, i32, i32::MAX as u32, "u32->i32"),
n @ 0x20 ..= 0x37 => Ok(-1 - i32::from(n - 0x20)),
0x38 => self.read().map(|n| -1 - i32::from(n)),
0x39 => self.read_slice(2).map(read_u16).map(|n| -1 - i32::from(n)),
0x3a => u_to_i!(self.read_slice(4).map(read_u32)?, i32, i32::MAX as u32, "u32->i32"),
b => Err(Error::TypeMismatch(b, "expected i32"))
}
}
pub fn i64(&mut self) -> Result<i64, Error> {
match self.read()? {
n @ 0x00 ..= 0x17 => Ok(i64::from(n)),
0x18 => self.read().map(i64::from),
0x19 => self.read_slice(2).map(read_u16).map(i64::from),
0x1a => self.read_slice(4).map(read_u32).map(i64::from),
0x1b => u_as_i!(self.read_slice(8).map(read_u64)?, i64, i64::MAX as u64, "u64->i64"),
n @ 0x20 ..= 0x37 => Ok(-1 - i64::from(n - 0x20)),
0x38 => self.read().map(|n| -1 - i64::from(n)),
0x39 => self.read_slice(2).map(read_u16).map(|n| -1 - i64::from(n)),
0x3a => self.read_slice(4).map(read_u32).map(|n| -1 - i64::from(n)),
0x3b => u_to_i!(self.read_slice(8).map(read_u64)?, i64, i64::MAX as u64, "u64->i64"),
b => Err(Error::TypeMismatch(b, "expected i64"))
}
}
#[cfg(feature = "half")]
pub fn f16(&mut self) -> Result<f32, Error> {
let b = self.read()?;
if 0xf9 != b {
return Err(Error::TypeMismatch(b, "expected f16"))
}
let mut n = [0; 2];
n.copy_from_slice(self.read_slice(2)?);
Ok(half::f16::from_bits(u16::from_be_bytes(n)).to_f32())
}
pub fn f32(&mut self) -> Result<f32, Error> {
match self.current()? {
#[cfg(feature = "half")]
0xf9 => self.f16(),
0xfa => {
self.read()?;
let mut n = [0; 4];
n.copy_from_slice(self.read_slice(4)?);
Ok(f32::from_be_bytes(n))
}
b => Err(Error::TypeMismatch(b, "expected f32"))
}
}
pub fn f64(&mut self) -> Result<f64, Error> {
match self.current()? {
#[cfg(feature = "half")]
0xf9 => self.f16().map(f64::from),
0xfa => self.f32().map(f64::from),
0xfb => {
self.read()?;
let mut n = [0; 8];
n.copy_from_slice(self.read_slice(8)?);
Ok(f64::from_be_bytes(n))
}
b => Err(Error::TypeMismatch(b, "expected f64"))
}
}
pub fn char(&mut self) -> Result<char, Error> {
let n = self.u32()?;
char::from_u32(n).ok_or(Error::InvalidChar(n))
}
pub fn bytes(&mut self) -> Result<&'b [u8], Error> {
let b = self.read()?;
if BYTES != type_of(b) || info_of(b) == 31 {
return Err(Error::TypeMismatch(b, "expected bytes (definite length)"))
}
let n = u64_to_usize(self.unsigned(info_of(b))?)?;
self.read_slice(n)
}
pub fn bytes_iter(&mut self) -> Result<BytesIter<'_, 'b>, Error> {
let b = self.read()?;
if BYTES != type_of(b) {
return Err(Error::TypeMismatch(b, "expected bytes"))
}
match info_of(b) {
31 => Ok(BytesIter { decoder: self, len: None }),
n => {
let len = u64_to_usize(self.unsigned(n)?)?;
Ok(BytesIter { decoder: self, len: Some(len) })
}
}
}
pub fn str(&mut self) -> Result<&'b str, Error> {
let b = self.read()?;
if TEXT != type_of(b) || info_of(b) == 31 {
return Err(Error::TypeMismatch(b, "expected text (definite length)"))
}
let n = u64_to_usize(self.unsigned(info_of(b))?)?;
let d = self.read_slice(n)?;
str::from_utf8(d).map_err(Error::from)
}
pub fn str_iter(&mut self) -> Result<StrIter<'_, 'b>, Error> {
let b = self.read()?;
if TEXT != type_of(b) {
return Err(Error::TypeMismatch(b, "expected text"))
}
match info_of(b) {
31 => Ok(StrIter { decoder: self, len: None }),
n => {
let len = u64_to_usize(self.unsigned(n)?)?;
Ok(StrIter { decoder: self, len: Some(len) })
}
}
}
pub fn array(&mut self) -> Result<Option<u64>, Error> {
let b = self.read()?;
if ARRAY != type_of(b) {
return Err(Error::TypeMismatch(b, "expected array"))
}
match info_of(b) {
31 => Ok(None),
n => Ok(Some(self.unsigned(n)?))
}
}
pub fn array_iter<T>(&mut self) -> Result<ArrayIter<'_, 'b, T>, Error>
where
T: Decode<'b>
{
let len = self.array()?;
Ok(ArrayIter { decoder: self, len, _mark: marker::PhantomData })
}
pub fn map(&mut self) -> Result<Option<u64>, Error> {
let b = self.read()?;
if MAP != type_of(b) {
return Err(Error::TypeMismatch(b, "expected map"))
}
match info_of(b) {
31 => Ok(None),
n => Ok(Some(self.unsigned(n)?))
}
}
pub fn map_iter<K, V>(&mut self) -> Result<MapIter<'_, 'b, K, V>, Error>
where
K: Decode<'b>,
V: Decode<'b>
{
let len = self.map()?;
Ok(MapIter { decoder: self, len, _mark: marker::PhantomData })
}
pub fn tag(&mut self) -> Result<Tag, Error> {
let b = self.read()?;
if TAGGED != type_of(b) {
return Err(Error::TypeMismatch(b, "expected tag"))
}
self.unsigned(info_of(b)).map(Tag::from)
}
pub fn simple(&mut self) -> Result<u8, Error> {
match self.read()? {
n @ SIMPLE ..= 0xf3 => Ok(n - SIMPLE),
0xf8 => self.read(),
n => Err(Error::TypeMismatch(n, "expected simple value"))
}
}
pub fn datatype(&self) -> Result<Type, Error> {
self.current().map(Type::read)
}
pub fn skip(&mut self) -> Result<(), Error> {
let mut nrounds = 1u64;
let mut irounds = 0u64;
while nrounds > 0 || irounds > 0 {
match self.current()? {
UNSIGNED ..= 0x1b => { self.u64()?; }
SIGNED ..= 0x3b => { self.i64()?; }
BYTES ..= 0x5f => { for _ in self.bytes_iter()? {} }
TEXT ..= 0x7f => { for _ in self.str_iter()? {} }
ARRAY ..= 0x9f =>
if let Some(n) = self.array()? {
nrounds = nrounds.saturating_add(n)
} else {
irounds = irounds.saturating_add(1)
}
MAP ..= 0xbf =>
if let Some(n) = self.map()? {
nrounds = nrounds.saturating_add(n.saturating_mul(2))
} else {
irounds = irounds.saturating_add(1)
}
TAGGED ..= 0xdb => {
self.read().and_then(|n| self.unsigned(info_of(n)))?;
nrounds = nrounds.saturating_add(1)
}
SIMPLE ..= 0xfb => {
self.read().and_then(|n| self.unsigned(info_of(n)))?;
}
BREAK => {
self.read()?;
irounds = irounds.saturating_sub(1)
}
other => return Err(Error::TypeMismatch(other, "unknown type"))
}
nrounds = nrounds.saturating_sub(1)
}
Ok(())
}
fn unsigned(&mut self, b: u8) -> Result<u64, Error> {
match b {
n @ 0 ..= 0x17 => Ok(u64::from(n)),
0x18 => self.read().map(u64::from),
0x19 => self.read_slice(2).map(read_u16).map(u64::from),
0x1a => self.read_slice(4).map(read_u32).map(u64::from),
0x1b => self.read_slice(8).map(read_u64),
_ => Err(Error::TypeMismatch(b, "expected u64"))
}
}
fn current(&self) -> Result<u8, Error> {
if let Some(b) = self.buf.get(self.pos) {
return Ok(*b)
}
Err(Error::EndOfInput)
}
fn read(&mut self) -> Result<u8, Error> {
if let Some(b) = self.buf.get(self.pos) {
self.pos += 1;
return Ok(*b)
}
Err(Error::EndOfInput)
}
fn read_slice(&mut self, n: usize) -> Result<&'b [u8], Error> {
if let Some(b) = self.buf.get(self.pos .. self.pos + n) {
self.pos += n;
return Ok(b)
}
Err(Error::EndOfInput)
}
}
#[derive(Debug)]
pub struct BytesIter<'a, 'b> {
decoder: &'a mut Decoder<'b>,
len: Option<usize>
}
impl<'a, 'b> Iterator for BytesIter<'a, 'b> {
type Item = Result<&'b [u8], Error>;
fn next(&mut self) -> Option<Self::Item> {
match self.len {
None => match self.decoder.current() {
Ok(BREAK) => self.decoder.read().map(|_| None).transpose(),
Ok(_) => Some(self.decoder.bytes()),
Err(e) => Some(Err(e))
}
Some(0) => None,
Some(n) => {
self.len = Some(0);
Some(self.decoder.read_slice(n))
}
}
}
}
#[derive(Debug)]
pub struct StrIter<'a, 'b> {
decoder: &'a mut Decoder<'b>,
len: Option<usize>
}
impl<'a, 'b> Iterator for StrIter<'a, 'b> {
type Item = Result<&'b str, Error>;
fn next(&mut self) -> Option<Self::Item> {
match self.len {
None => match self.decoder.current() {
Ok(BREAK) => self.decoder.read().map(|_| None).transpose(),
Ok(_) => Some(self.decoder.str()),
Err(e) => Some(Err(e))
}
Some(0) => None,
Some(n) => {
self.len = Some(0);
Some(self.decoder.read_slice(n).and_then(|d| str::from_utf8(d).map_err(Error::from)))
}
}
}
}
#[derive(Debug)]
pub struct ArrayIter<'a, 'b, T> {
decoder: &'a mut Decoder<'b>,
len: Option<u64>,
_mark: marker::PhantomData<&'a T>
}
impl<'a, 'b, T: Decode<'b>> Iterator for ArrayIter<'a, 'b, T> {
type Item = Result<T, Error>;
fn next(&mut self) -> Option<Self::Item> {
match self.len {
None => match self.decoder.current() {
Ok(BREAK) => self.decoder.read().map(|_| None).transpose(),
Ok(_) => Some(T::decode(&mut self.decoder)),
Err(e) => Some(Err(e))
}
Some(0) => None,
Some(n) => {
self.len = Some(n - 1);
Some(T::decode(&mut self.decoder))
}
}
}
}
#[derive(Debug)]
pub struct MapIter<'a, 'b, K, V> {
decoder: &'a mut Decoder<'b>,
len: Option<u64>,
_mark: marker::PhantomData<&'a (K, V)>
}
impl<'a, 'b, K, V> Iterator for MapIter<'a, 'b, K, V>
where
K: Decode<'b>,
V: Decode<'b>
{
type Item = Result<(K, V), Error>;
fn next(&mut self) -> Option<Self::Item> {
fn pair<'b, K, V>(d: &mut Decoder<'b>) -> Result<(K, V), Error>
where
K: Decode<'b>,
V: Decode<'b>
{
Ok((K::decode(d)?, V::decode(d)?))
}
match self.len {
None => match self.decoder.current() {
Ok(BREAK) => self.decoder.read().map(|_| None).transpose(),
Ok(_) => Some(pair(&mut self.decoder)),
Err(e) => Some(Err(e))
}
Some(0) => None,
Some(n) => {
self.len = Some(n - 1);
Some(pair(&mut self.decoder))
}
}
}
}
#[derive(Debug)]
pub struct Probe<'a, 'b> {
decoder: Decoder<'b>,
_marker: marker::PhantomData<&'a mut ()>
}
impl<'b> core::ops::Deref for Probe<'_, 'b> {
type Target = Decoder<'b>;
fn deref(&self) -> &Self::Target {
&self.decoder
}
}
impl<'b> core::ops::DerefMut for Probe<'_, 'b> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.decoder
}
}
fn read_u16(b: &[u8]) -> u16 {
let mut n = [0; 2];
n.copy_from_slice(b);
u16::from_be_bytes(n)
}
fn read_u32(b: &[u8]) -> u32 {
let mut n = [0; 4];
n.copy_from_slice(b);
u32::from_be_bytes(n)
}
fn read_u64(b: &[u8]) -> u64 {
let mut n = [0; 8];
n.copy_from_slice(b);
u64::from_be_bytes(n)
}
fn type_of(b: u8) -> u8 {
b & 0b111_00000
}
fn info_of(b: u8) -> u8 {
b & 0b000_11111
}
fn u64_to_usize(n: u64) -> Result<usize, Error> {
n.try_into().map_err(|_| Error::Overflow(n, "u64->usize"))
}