1use super::{Deserialize, Error, Serialize};
2use crate::{elements, io};
3use alloc::{string::String, vec::Vec};
4
5#[cfg(feature = "reduced-stack-buffer")]
6const PRIMITIVES_BUFFER_LENGTH: usize = 256;
7
8#[cfg(not(feature = "reduced-stack-buffer"))]
9const PRIMITIVES_BUFFER_LENGTH: usize = 1024;
10
11#[derive(Debug, Copy, Clone, PartialEq)]
14pub struct VarUint32(u32);
15
16impl From<VarUint32> for usize {
17 fn from(var: VarUint32) -> usize {
18 var.0 as usize
19 }
20}
21
22impl From<VarUint32> for u32 {
23 fn from(var: VarUint32) -> u32 {
24 var.0
25 }
26}
27
28impl From<u32> for VarUint32 {
29 fn from(i: u32) -> VarUint32 {
30 VarUint32(i)
31 }
32}
33
34pub struct TryFromUsizeForVarUint32(());
35
36impl TryFrom<usize> for VarUint32 {
37 type Error = TryFromUsizeForVarUint32;
38 fn try_from(i: usize) -> Result<VarUint32, Self::Error> {
39 let value: u32 = i.try_into().map_err(|_| TryFromUsizeForVarUint32(()))?;
40 Ok(VarUint32(value))
41 }
42}
43
44impl VarUint32 {
45 pub fn deserialize_with_first<R: io::Read>(reader: &mut R, byte: u8) -> Result<Self, Error> {
47 let mut result = (byte & 0x7F) as u32;
48 let mut shift = 7;
49 loop {
50 let byte: u8 = Uint8::deserialize(reader)?.into();
51 result |= ((byte & 0x7F) as u32) << shift;
52 if shift >= 25 && (byte >> (32 - shift)) != 0 {
53 return Err(Error::InvalidVarUint32);
54 }
55 shift += 7;
56 if (byte & 0x80) == 0 {
57 break;
58 }
59 }
60 Ok(VarUint32(result))
61 }
62}
63
64impl Deserialize for VarUint32 {
65 type Error = Error;
66
67 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
68 let mut res = 0;
69 let mut shift = 0;
70 let mut u8buf = [0u8; 1];
71 loop {
72 if shift > 31 {
73 return Err(Error::InvalidVarUint32);
74 }
75
76 reader.read(&mut u8buf)?;
77 let b = u8buf[0] as u32;
78 res |= (b & 0x7f).checked_shl(shift).ok_or(Error::InvalidVarUint32)?;
79 shift += 7;
80 if (b >> 7) == 0 {
81 if shift >= 32 && (b as u8).leading_zeros() < 4 {
82 return Err(Error::InvalidVarInt32);
83 }
84 break;
85 }
86 }
87 Ok(VarUint32(res))
88 }
89}
90
91impl Serialize for VarUint32 {
92 type Error = Error;
93
94 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
95 let mut buf = [0u8; 1];
96 let mut v = self.0;
97 loop {
98 buf[0] = (v & 0b0111_1111) as u8;
99 v >>= 7;
100 if v > 0 {
101 buf[0] |= 0b1000_0000;
102 }
103 writer.write(&buf[..])?;
104 if v == 0 {
105 break;
106 }
107 }
108
109 Ok(())
110 }
111}
112
113#[derive(Debug, Copy, Clone, PartialEq)]
116pub struct VarUint64(u64);
117
118impl From<VarUint64> for u64 {
119 fn from(var: VarUint64) -> u64 {
120 var.0
121 }
122}
123
124impl Deserialize for VarUint64 {
125 type Error = Error;
126
127 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
128 let mut res = 0;
129 let mut shift = 0;
130 let mut u8buf = [0u8; 1];
131 loop {
132 if shift > 63 {
133 return Err(Error::InvalidVarUint64);
134 }
135
136 reader.read(&mut u8buf)?;
137 let b = u8buf[0] as u64;
138 res |= (b & 0x7f).checked_shl(shift).ok_or(Error::InvalidVarUint64)?;
139 shift += 7;
140 if (b >> 7) == 0 {
141 if shift >= 64 && (b as u8).leading_zeros() < 7 {
142 return Err(Error::InvalidVarInt64);
143 }
144 break;
145 }
146 }
147 Ok(VarUint64(res))
148 }
149}
150
151impl Serialize for VarUint64 {
152 type Error = Error;
153
154 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
155 let mut buf = [0u8; 1];
156 let mut v = self.0;
157 loop {
158 buf[0] = (v & 0b0111_1111) as u8;
159 v >>= 7;
160 if v > 0 {
161 buf[0] |= 0b1000_0000;
162 }
163 writer.write(&buf[..])?;
164 if v == 0 {
165 break;
166 }
167 }
168
169 Ok(())
170 }
171}
172
173impl From<u64> for VarUint64 {
174 fn from(u: u64) -> VarUint64 {
175 VarUint64(u)
176 }
177}
178
179#[derive(Debug, Copy, Clone, PartialEq)]
181pub struct VarUint7(u8);
182
183impl From<VarUint7> for u8 {
184 fn from(v: VarUint7) -> u8 {
185 v.0
186 }
187}
188
189impl From<u8> for VarUint7 {
190 fn from(v: u8) -> Self {
191 VarUint7(v)
192 }
193}
194
195impl Deserialize for VarUint7 {
196 type Error = Error;
197
198 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
199 let mut u8buf = [0u8; 1];
200 reader.read(&mut u8buf)?;
201 Ok(VarUint7(u8buf[0]))
202 }
203}
204
205impl Serialize for VarUint7 {
206 type Error = Error;
207
208 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
209 writer.write(&[self.0])?;
211 Ok(())
212 }
213}
214
215#[derive(Debug, Copy, Clone, PartialEq)]
217pub struct VarInt7(i8);
218
219impl From<VarInt7> for i8 {
220 fn from(v: VarInt7) -> i8 {
221 v.0
222 }
223}
224
225impl From<i8> for VarInt7 {
226 fn from(v: i8) -> VarInt7 {
227 VarInt7(v)
228 }
229}
230
231impl Deserialize for VarInt7 {
232 type Error = Error;
233
234 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
235 let mut u8buf = [0u8; 1];
236 reader.read(&mut u8buf)?;
237
238 if u8buf[0] & 0b1000_0000 != 0 {
240 return Err(Error::InvalidVarInt7(u8buf[0]));
241 }
242
243 if u8buf[0] & 0b0100_0000 == 0b0100_0000 {
245 u8buf[0] |= 0b1000_0000
246 }
247
248 Ok(VarInt7(u8buf[0] as i8))
249 }
250}
251
252impl Serialize for VarInt7 {
253 type Error = Error;
254
255 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
256 let mut b: u8 = self.0 as u8;
258 if self.0 < 0 {
259 b |= 0b0100_0000;
260 b &= 0b0111_1111;
261 }
262 writer.write(&[b])?;
263 Ok(())
264 }
265}
266
267#[derive(Debug, Copy, Clone, PartialEq)]
270pub struct Uint8(u8);
271
272impl From<Uint8> for u8 {
273 fn from(v: Uint8) -> u8 {
274 v.0
275 }
276}
277
278impl From<u8> for Uint8 {
279 fn from(v: u8) -> Self {
280 Uint8(v)
281 }
282}
283
284impl Deserialize for Uint8 {
285 type Error = Error;
286
287 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
288 let mut u8buf = [0u8; 1];
289 reader.read(&mut u8buf)?;
290 Ok(Uint8(u8buf[0]))
291 }
292}
293
294impl Serialize for Uint8 {
295 type Error = Error;
296
297 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
298 writer.write(&[self.0])?;
299 Ok(())
300 }
301}
302
303#[derive(Debug, Copy, Clone, PartialEq)]
305pub struct VarInt32(i32);
306
307impl From<VarInt32> for i32 {
308 fn from(v: VarInt32) -> i32 {
309 v.0
310 }
311}
312
313impl From<i32> for VarInt32 {
314 fn from(v: i32) -> VarInt32 {
315 VarInt32(v)
316 }
317}
318
319impl Deserialize for VarInt32 {
320 type Error = Error;
321
322 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
323 let mut res = 0;
324 let mut shift = 0;
325 let mut u8buf = [0u8; 1];
326 loop {
327 if shift > 31 {
328 return Err(Error::InvalidVarInt32);
329 }
330 reader.read(&mut u8buf)?;
331 let b = u8buf[0];
332
333 res |= ((b & 0x7f) as i32).checked_shl(shift).ok_or(Error::InvalidVarInt32)?;
334
335 shift += 7;
336 if (b >> 7) == 0 {
337 if shift < 32 && b & 0b0100_0000 == 0b0100_0000 {
338 res |= (1i32 << shift).wrapping_neg();
339 } else if shift >= 32 && b & 0b0100_0000 == 0b0100_0000 {
340 if (!(b | 0b1000_0000)).leading_zeros() < 5 {
341 return Err(Error::InvalidVarInt32);
342 }
343 } else if shift >= 32 && b & 0b0100_0000 == 0 && b.leading_zeros() < 5 {
344 return Err(Error::InvalidVarInt32);
345 }
346 break;
347 }
348 }
349 Ok(VarInt32(res))
350 }
351}
352
353impl Serialize for VarInt32 {
354 type Error = Error;
355
356 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
357 let mut buf = [0u8; 1];
358 let mut v = self.0;
359 let mut more = true;
360 while more {
361 buf[0] = (v & 0b0111_1111) as u8;
362 v >>= 7;
363 if (v == 0 && buf[0] & 0b0100_0000 == 0) ||
364 (v == -1 && buf[0] & 0b0100_0000 == 0b0100_0000)
365 {
366 more = false
367 } else {
368 buf[0] |= 0b1000_0000
369 }
370
371 writer.write(&buf[..])?;
372 }
373
374 Ok(())
375 }
376}
377
378#[derive(Debug, Copy, Clone, PartialEq)]
380pub struct VarInt64(i64);
381
382impl From<VarInt64> for i64 {
383 fn from(v: VarInt64) -> i64 {
384 v.0
385 }
386}
387
388impl From<i64> for VarInt64 {
389 fn from(v: i64) -> VarInt64 {
390 VarInt64(v)
391 }
392}
393
394impl Deserialize for VarInt64 {
395 type Error = Error;
396
397 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
398 let mut res = 0i64;
399 let mut shift = 0;
400 let mut u8buf = [0u8; 1];
401
402 loop {
403 if shift > 63 {
404 return Err(Error::InvalidVarInt64);
405 }
406 reader.read(&mut u8buf)?;
407 let b = u8buf[0];
408
409 res |= ((b & 0x7f) as i64).checked_shl(shift).ok_or(Error::InvalidVarInt64)?;
410
411 shift += 7;
412 if (b >> 7) == 0 {
413 if shift < 64 && b & 0b0100_0000 == 0b0100_0000 {
414 res |= (1i64 << shift).wrapping_neg();
415 } else if shift >= 64 && b & 0b0100_0000 == 0b0100_0000 {
416 if (b | 0b1000_0000) as i8 != -1 {
417 return Err(Error::InvalidVarInt64);
418 }
419 } else if shift >= 64 && b != 0 {
420 return Err(Error::InvalidVarInt64);
421 }
422 break;
423 }
424 }
425 Ok(VarInt64(res))
426 }
427}
428
429impl Serialize for VarInt64 {
430 type Error = Error;
431
432 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
433 let mut buf = [0u8; 1];
434 let mut v = self.0;
435 let mut more = true;
436 while more {
437 buf[0] = (v & 0b0111_1111) as u8;
438 v >>= 7;
439 if (v == 0 && buf[0] & 0b0100_0000 == 0) ||
440 (v == -1 && buf[0] & 0b0100_0000 == 0b0100_0000)
441 {
442 more = false
443 } else {
444 buf[0] |= 0b1000_0000
445 }
446
447 writer.write(&buf[..])?;
448 }
449
450 Ok(())
451 }
452}
453
454#[derive(Debug, Copy, Clone, PartialEq)]
456pub struct Uint32(u32);
457
458impl Deserialize for Uint32 {
459 type Error = Error;
460
461 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
462 let mut buf = [0u8; 4];
463 reader.read(&mut buf)?;
464 Ok(u32::from_le_bytes(buf).into())
466 }
467}
468
469impl From<Uint32> for u32 {
470 fn from(var: Uint32) -> u32 {
471 var.0
472 }
473}
474
475impl Serialize for Uint32 {
476 type Error = Error;
477
478 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
479 writer.write(&self.0.to_le_bytes())?;
480 Ok(())
481 }
482}
483
484impl From<u32> for Uint32 {
485 fn from(u: u32) -> Self {
486 Uint32(u)
487 }
488}
489
490#[derive(Debug, Copy, Clone, PartialEq)]
492pub struct Uint64(u64);
493
494impl Deserialize for Uint64 {
495 type Error = Error;
496
497 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
498 let mut buf = [0u8; 8];
499 reader.read(&mut buf)?;
500 Ok(u64::from_le_bytes(buf).into())
502 }
503}
504
505impl Serialize for Uint64 {
506 type Error = Error;
507
508 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
509 writer.write(&self.0.to_le_bytes())?;
510 Ok(())
511 }
512}
513
514impl From<u64> for Uint64 {
515 fn from(u: u64) -> Self {
516 Uint64(u)
517 }
518}
519
520impl From<Uint64> for u64 {
521 fn from(var: Uint64) -> u64 {
522 var.0
523 }
524}
525
526#[derive(Debug, Copy, Clone, PartialEq)]
528pub struct VarUint1(bool);
529
530impl From<VarUint1> for bool {
531 fn from(v: VarUint1) -> bool {
532 v.0
533 }
534}
535
536impl From<bool> for VarUint1 {
537 fn from(b: bool) -> Self {
538 VarUint1(b)
539 }
540}
541
542impl Deserialize for VarUint1 {
543 type Error = Error;
544
545 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
546 let mut u8buf = [0u8; 1];
547 reader.read(&mut u8buf)?;
548 match u8buf[0] {
549 0 => Ok(VarUint1(false)),
550 1 => Ok(VarUint1(true)),
551 v => Err(Error::InvalidVarUint1(v)),
552 }
553 }
554}
555
556impl Serialize for VarUint1 {
557 type Error = Error;
558
559 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
560 writer.write(&[if self.0 { 1u8 } else { 0u8 }])?;
561 Ok(())
562 }
563}
564
565impl Deserialize for String {
566 type Error = Error;
567
568 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
569 let length = u32::from(VarUint32::deserialize(reader)?) as usize;
570 if length > 0 {
571 String::from_utf8(buffered_read!(PRIMITIVES_BUFFER_LENGTH, length, reader))
572 .map_err(|_| Error::NonUtf8String)
573 } else {
574 Ok(String::new())
575 }
576 }
577}
578
579impl Serialize for String {
580 type Error = Error;
581
582 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Error> {
583 VarUint32::try_from(self.len())
584 .map_err(|_| Self::Error::InvalidVarInt32)?
585 .serialize(writer)?;
586 writer.write(&self.into_bytes()[..])?;
587 Ok(())
588 }
589}
590
591#[derive(Debug, Clone)]
594pub struct CountedList<T: Deserialize>(Vec<T>);
595
596impl<T: Deserialize> CountedList<T> {
597 pub fn into_inner(self) -> Vec<T> {
599 self.0
600 }
601}
602
603impl<T: Deserialize> Deserialize for CountedList<T>
604where
605 T::Error: From<Error>,
606{
607 type Error = T::Error;
608
609 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
610 let count: usize = VarUint32::deserialize(reader)?.into();
611 let mut result = Vec::new();
612 for _ in 0..count {
613 result.push(T::deserialize(reader)?);
614 }
615 Ok(CountedList(result))
616 }
617}
618
619#[derive(Debug)]
622pub struct CountedWriter<'a, W: 'a + io::Write> {
623 writer: &'a mut W,
624 data: Vec<u8>,
625}
626
627impl<'a, W: 'a + io::Write> CountedWriter<'a, W> {
628 pub fn new(writer: &'a mut W) -> Self {
630 CountedWriter { writer, data: Vec::new() }
631 }
632
633 pub fn done(self) -> io::Result<()> {
636 let writer = self.writer;
637 let data = self.data;
638 VarUint32::try_from(data.len())
639 .map_err(|_| io::Error::InvalidInput)?
640 .serialize(writer)
641 .map_err(|_| io::Error::InvalidData)?;
642 writer.write(&data[..])?;
643 Ok(())
644 }
645}
646
647impl<'a, W: 'a + io::Write> io::Write for CountedWriter<'a, W> {
648 fn write(&mut self, buf: &[u8]) -> io::Result<()> {
649 self.data.extend_from_slice(buf);
650 Ok(())
651 }
652}
653
654#[derive(Debug, Clone)]
657pub struct CountedListWriter<I: Serialize<Error = elements::Error>, T: IntoIterator<Item = I>>(
658 pub usize,
659 pub T,
660);
661
662impl<I: Serialize<Error = elements::Error>, T: IntoIterator<Item = I>> Serialize
663 for CountedListWriter<I, T>
664{
665 type Error = Error;
666
667 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
668 let len_us = self.0;
669 let data = self.1;
670 let len: VarUint32 = len_us.try_into().map_err(|_| Error::InvalidVarUint32)?;
671 len.serialize(writer)?;
672 for data_element in data {
673 data_element.serialize(writer)?
674 }
675
676 Ok(())
677 }
678}
679
680#[cfg(test)]
681mod tests {
682
683 use super::{
684 super::{deserialize_buffer, Serialize},
685 CountedList, VarInt32, VarInt64, VarInt7, VarUint32, VarUint64,
686 };
687 use crate::{alloc::vec::Vec, elements::Error};
688
689 fn varuint32_ser_test(val: u32, expected: Vec<u8>) {
690 let mut buf = Vec::new();
691 let v1: VarUint32 = val.into();
692 v1.serialize(&mut buf).expect("to be serialized ok");
693 assert_eq!(expected, buf);
694 }
695
696 fn varuint32_de_test(dt: Vec<u8>, expected: u32) {
697 let val: VarUint32 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
698 assert_eq!(expected, val.into());
699 }
700
701 fn varuint32_serde_test(dt: Vec<u8>, val: u32) {
702 varuint32_de_test(dt.clone(), val);
703 varuint32_ser_test(val, dt);
704 }
705
706 fn varint32_ser_test(val: i32, expected: Vec<u8>) {
707 let mut buf = Vec::new();
708 let v1: VarInt32 = val.into();
709 v1.serialize(&mut buf).expect("to be serialized ok");
710 assert_eq!(expected, buf);
711 }
712
713 fn varint32_de_test(dt: Vec<u8>, expected: i32) {
714 let val: VarInt32 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
715 assert_eq!(expected, val.into());
716 }
717
718 fn varint32_serde_test(dt: Vec<u8>, val: i32) {
719 varint32_de_test(dt.clone(), val);
720 varint32_ser_test(val, dt);
721 }
722
723 fn varuint64_ser_test(val: u64, expected: Vec<u8>) {
724 let mut buf = Vec::new();
725 let v1: VarUint64 = val.into();
726 v1.serialize(&mut buf).expect("to be serialized ok");
727 assert_eq!(expected, buf);
728 }
729
730 fn varuint64_de_test(dt: Vec<u8>, expected: u64) {
731 let val: VarUint64 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
732 assert_eq!(expected, val.into());
733 }
734
735 fn varuint64_serde_test(dt: Vec<u8>, val: u64) {
736 varuint64_de_test(dt.clone(), val);
737 varuint64_ser_test(val, dt);
738 }
739
740 fn varint64_ser_test(val: i64, expected: Vec<u8>) {
741 let mut buf = Vec::new();
742 let v1: VarInt64 = val.into();
743 v1.serialize(&mut buf).expect("to be serialized ok");
744 assert_eq!(expected, buf);
745 }
746
747 fn varint64_de_test(dt: Vec<u8>, expected: i64) {
748 let val: VarInt64 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
749 assert_eq!(expected, val.into());
750 }
751
752 fn varint64_serde_test(dt: Vec<u8>, val: i64) {
753 varint64_de_test(dt.clone(), val);
754 varint64_ser_test(val, dt);
755 }
756
757 #[test]
758 fn varuint32_0() {
759 varuint32_serde_test(vec![0u8; 1], 0);
760 }
761
762 #[test]
763 fn varuint32_1() {
764 varuint32_serde_test(vec![1u8; 1], 1);
765 }
766
767 #[test]
768 fn varuint32_135() {
769 varuint32_serde_test(vec![135u8, 0x01], 135);
770 }
771
772 #[test]
773 fn varuint32_8192() {
774 varuint32_serde_test(vec![0x80, 0x40], 8192);
775 }
776
777 #[test]
778 fn varint32_8192() {
779 varint32_serde_test(vec![0x80, 0xc0, 0x00], 8192);
780 }
781
782 #[test]
783 fn varint32_neg_8192() {
784 varint32_serde_test(vec![0x80, 0x40], -8192);
785 }
786
787 #[test]
788 fn varuint64_0() {
789 varuint64_serde_test(vec![0u8; 1], 0);
790 }
791
792 #[test]
793 fn varuint64_1() {
794 varuint64_serde_test(vec![1u8; 1], 1);
795 }
796
797 #[test]
798 fn varuint64_135() {
799 varuint64_serde_test(vec![135u8, 0x01], 135);
800 }
801
802 #[test]
803 fn varuint64_8192() {
804 varuint64_serde_test(vec![0x80, 0x40], 8192);
805 }
806
807 #[test]
808 fn varint64_8192() {
809 varint64_serde_test(vec![0x80, 0xc0, 0x00], 8192);
810 }
811
812 #[test]
813 fn varint64_neg_8192() {
814 varint64_serde_test(vec![0x80, 0x40], -8192);
815 }
816
817 #[test]
818 fn varint64_min() {
819 varint64_serde_test(
820 vec![0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f],
821 -9223372036854775808,
822 );
823 }
824
825 #[test]
826 fn varint64_bad_extended() {
827 let res = deserialize_buffer::<VarInt64>(
828 &[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x6f][..],
829 );
830 assert!(res.is_err());
831 }
832
833 #[test]
834 fn varint32_bad_extended() {
835 let res = deserialize_buffer::<VarInt32>(&[0x80, 0x80, 0x80, 0x80, 0x6f][..]);
836 assert!(res.is_err());
837 }
838
839 #[test]
840 fn varint32_bad_extended2() {
841 let res = deserialize_buffer::<VarInt32>(&[0x80, 0x80, 0x80, 0x80, 0x41][..]);
842 assert!(res.is_err());
843 }
844
845 #[test]
846 fn varint64_max() {
847 varint64_serde_test(
848 vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00],
849 9223372036854775807,
850 );
851 }
852
853 #[test]
854 fn varint64_too_long() {
855 assert!(deserialize_buffer::<VarInt64>(
856 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00][..],
857 )
858 .is_err());
859 }
860
861 #[test]
862 fn varint32_too_long() {
863 assert!(deserialize_buffer::<VarInt32>(&[0xff, 0xff, 0xff, 0xff, 0xff, 0x00][..],).is_err());
864 }
865
866 #[test]
867 fn varuint64_too_long() {
868 assert!(deserialize_buffer::<VarUint64>(
869 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00][..],
870 )
871 .is_err());
872 }
873
874 #[test]
875 fn varuint32_too_long() {
876 assert!(
877 deserialize_buffer::<VarUint32>(&[0xff, 0xff, 0xff, 0xff, 0xff, 0x00][..],).is_err()
878 );
879 }
880
881 #[test]
882 fn varuint32_too_long_trailing() {
883 assert!(deserialize_buffer::<VarUint32>(&[0xff, 0xff, 0xff, 0xff, 0x7f][..],).is_err());
884 }
885
886 #[test]
887 fn varuint64_too_long_trailing() {
888 assert!(deserialize_buffer::<VarUint64>(
889 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x04][..],
890 )
891 .is_err());
892 }
893
894 #[test]
895 fn varint32_min() {
896 varint32_serde_test(vec![0x80, 0x80, 0x80, 0x80, 0x78], -2147483648);
897 }
898
899 #[test]
900 fn varint7_invalid() {
901 match deserialize_buffer::<VarInt7>(&[240]) {
902 Err(Error::InvalidVarInt7(_)) => {},
903 _ => panic!("Should be invalid varint7 error!"),
904 }
905 }
906
907 #[test]
908 fn varint7_neg() {
909 assert_eq!(-0x10i8, deserialize_buffer::<VarInt7>(&[0x70]).expect("fail").into());
910 }
911
912 #[test]
913 fn varuint32_too_long_nulled() {
914 match deserialize_buffer::<VarUint32>(&[
915 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x78,
916 ]) {
917 Err(Error::InvalidVarUint32) => {},
918 _ => panic!("Should be invalid varuint32"),
919 }
920 }
921
922 #[test]
923 fn varint32_max() {
924 varint32_serde_test(vec![0xff, 0xff, 0xff, 0xff, 0x07], 2147483647);
925 }
926
927 #[test]
928 fn counted_list() {
929 let payload = [
930 133u8, 0x80, 0x80, 0x80, 0x0, 0x01, 0x7d, 0x05, 0x07, 0x09,
933 ];
934
935 let list: CountedList<VarInt7> =
936 deserialize_buffer(&payload).expect("type_section be deserialized");
937
938 let vars = list.into_inner();
939 assert_eq!(5, vars.len());
940 let v3: i8 = (*vars.get(1).unwrap()).into();
941 assert_eq!(-0x03i8, v3);
942 }
943}