1use std::io;
2use byteorder::{LittleEndian, ByteOrder};
3use super::{Error, Deserialize, Serialize};
4
5#[derive(Copy, Clone)]
8pub struct VarUint32(u32);
9
10impl From<VarUint32> for usize {
11 fn from(var: VarUint32) -> usize {
12 var.0 as usize
13 }
14}
15
16impl From<VarUint32> for u32 {
17 fn from(var: VarUint32) -> u32 {
18 var.0
19 }
20}
21
22impl From<u32> for VarUint32 {
23 fn from(i: u32) -> VarUint32 {
24 VarUint32(i)
25 }
26}
27
28impl From<usize> for VarUint32 {
29 fn from(i: usize) -> VarUint32 {
30 assert!(i <= ::std::u32::MAX as usize);
31 VarUint32(i as u32)
32 }
33}
34
35impl Deserialize for VarUint32 {
36 type Error = Error;
37
38 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
39 let mut res = 0;
40 let mut shift = 0;
41 let mut u8buf = [0u8; 1];
42 loop {
43 reader.read_exact(&mut u8buf)?;
44 let b = u8buf[0] as u32;
45 res |= (b & 0x7f) << shift;
46 shift += 7;
47 if (b >> 7) == 0 {
48 break;
49 }
50 }
51 Ok(VarUint32(res))
52 }
53}
54
55impl Serialize for VarUint32 {
56 type Error = Error;
57
58 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
59 let mut buf = [0u8; 1];
60 let mut v = self.0;
61 loop {
62 buf[0] = (v & 0b0111_1111) as u8;
63 v >>= 7;
64 if v > 0 {
65 buf[0] |= 0b1000_0000;
66 }
67 writer.write_all(&buf[..])?;
68 if v == 0 { break; }
69 }
70
71 Ok(())
72 }
73}
74
75#[derive(Copy, Clone)]
78pub struct VarUint64(u64);
79
80impl From<VarUint64> for u64 {
81 fn from(var: VarUint64) -> u64 {
82 var.0
83 }
84}
85
86impl Deserialize for VarUint64 {
87 type Error = Error;
88
89 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
90 let mut res = 0;
91 let mut shift = 0;
92 let mut u8buf = [0u8; 1];
93 loop {
94 reader.read_exact(&mut u8buf)?;
95 let b = u8buf[0] as u64;
96 res |= (b & 0x7f) << shift;
97 shift += 7;
98 if (b >> 7) == 0 {
99 break;
100 }
101 }
102 Ok(VarUint64(res))
103 }
104}
105
106impl Serialize for VarUint64 {
107 type Error = Error;
108
109 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
110 let mut buf = [0u8; 1];
111 let mut v = self.0;
112 loop {
113 buf[0] = (v & 0b0111_1111) as u8;
114 v >>= 7;
115 if v > 0 {
116 buf[0] |= 0b1000_0000;
117 }
118 writer.write_all(&buf[..])?;
119 if v == 0 { break; }
120 }
121
122 Ok(())
123 }
124}
125
126impl From<u64> for VarUint64 {
127 fn from(u: u64) -> VarUint64 {
128 VarUint64(u)
129 }
130}
131
132#[derive(Copy, Clone)]
134pub struct VarUint7(u8);
135
136impl From<VarUint7> for u8 {
137 fn from(v: VarUint7) -> u8 {
138 v.0
139 }
140}
141
142impl From<u8> for VarUint7 {
143 fn from(v: u8) -> Self {
144 VarUint7(v)
145 }
146}
147
148impl Deserialize for VarUint7 {
149 type Error = Error;
150
151 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
152 let mut u8buf = [0u8; 1];
153 reader.read_exact(&mut u8buf)?;
154 Ok(VarUint7(u8buf[0]))
155 }
156}
157
158impl Serialize for VarUint7 {
159 type Error = Error;
160
161 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
162 writer.write_all(&[self.0])?;
164 Ok(())
165 }
166}
167
168#[derive(Copy, Clone)]
170pub struct VarInt7(i8);
171
172impl From<VarInt7> for i8 {
173 fn from(v: VarInt7) -> i8 {
174 v.0
175 }
176}
177
178impl From<i8> for VarInt7 {
179 fn from(v: i8) -> VarInt7 {
180 VarInt7(v)
181 }
182}
183
184impl Deserialize for VarInt7 {
185 type Error = Error;
186
187 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
188 let mut u8buf = [0u8; 1];
189 reader.read_exact(&mut u8buf)?;
190 if u8buf[0] & 0b0100_0000 == 0b0100_0000 { u8buf[0] |= 0b1000_0000 }
192 Ok(VarInt7(u8buf[0] as i8))
194 }
195}
196
197impl Serialize for VarInt7 {
198 type Error = Error;
199
200 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
201 let mut b: u8 = self.0 as u8;
203 if self.0 < 0 { b |= 0b0100_0000; b &= 0b0111_1111; }
204 writer.write_all(&[b])?;
205 Ok(())
206 }
207}
208
209#[derive(Copy, Clone)]
211pub struct VarInt32(i32);
212
213impl From<VarInt32> for i32 {
214 fn from(v: VarInt32) -> i32 {
215 v.0
216 }
217}
218
219impl From<i32> for VarInt32 {
220 fn from(v: i32) -> VarInt32 {
221 VarInt32(v)
222 }
223}
224
225impl Deserialize for VarInt32 {
226 type Error = Error;
227
228 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
229 let mut res = 0;
230 let mut shift = 0;
231 let mut u8buf = [0u8; 1];
232 loop {
233 if shift > 31 { return Err(Error::InvalidVarInt32); }
234 reader.read_exact(&mut u8buf)?;
235 let b = u8buf[0];
236
237 res |= ((b & 0x7f) as i32) << shift;
238 shift += 7;
239 if (b >> 7) == 0 {
240 if shift < 32 && b & 0b0100_0000 == 0b0100_0000 {
241 res |= (1i32 << shift).wrapping_neg();
242 }
243 break;
244 }
245 }
246 Ok(VarInt32(res))
247 }
248}
249
250impl Serialize for VarInt32 {
251 type Error = Error;
252
253 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
254 let mut buf = [0u8; 1];
255 let mut v = self.0;
256 let mut more = true;
257 while more {
258 buf[0] = (v & 0b0111_1111) as u8;
259 v >>= 7;
260 if (v == 0 && buf[0] & 0b0100_0000 == 0) || (v == -1 && buf[0] & 0b0100_0000 == 0b0100_0000) {
261 more = false
262 } else {
263 buf[0] |= 0b1000_0000
264 }
265
266 writer.write_all(&buf[..])?;
267 }
268
269 Ok(())
270 }
271}
272
273#[derive(Copy, Clone)]
275pub struct VarInt64(i64);
276
277impl From<VarInt64> for i64 {
278 fn from(v: VarInt64) -> i64 {
279 v.0
280 }
281}
282
283impl From<i64> for VarInt64 {
284 fn from(v: i64) -> VarInt64 {
285 VarInt64(v)
286 }
287}
288
289impl Deserialize for VarInt64 {
290 type Error = Error;
291
292 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
293 let mut res = 0i64;
294 let mut shift = 0;
295 let mut u8buf = [0u8; 1];
296 loop {
297 if shift > 63 { return Err(Error::InvalidVarInt64); }
298 reader.read_exact(&mut u8buf)?;
299 let b = u8buf[0];
300
301 res |= ((b & 0x7f) as i64) << shift;
302 shift += 7;
303 if (b >> 7) == 0 {
304 if shift < 64 && b & 0b0100_0000 == 0b0100_0000 {
305 res |= (1i64 << shift).wrapping_neg();
306 }
307 break;
308 }
309 }
310 Ok(VarInt64(res))
311 }
312}
313
314impl Serialize for VarInt64 {
315 type Error = Error;
316
317 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
318 let mut buf = [0u8; 1];
319 let mut v = self.0;
320 let mut more = true;
321 while more {
322 buf[0] = (v & 0b0111_1111) as u8;
323 v >>= 7;
324 if (v == 0 && buf[0] & 0b0100_0000 == 0) || (v == -1 && buf[0] & 0b0100_0000 == 0b0100_0000) {
325 more = false
326 } else {
327 buf[0] |= 0b1000_0000
328 }
329
330 writer.write_all(&buf[..])?;
331 }
332
333 Ok(())
334 }
335}
336
337#[derive(Copy, Clone)]
339pub struct Uint32(u32);
340
341impl Deserialize for Uint32 {
342 type Error = Error;
343
344 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
345 let mut buf = [0u8; 4];
346 reader.read_exact(&mut buf)?;
347 Ok(Uint32(LittleEndian::read_u32(&buf)))
349 }
350}
351
352impl From<Uint32> for u32 {
353 fn from(var: Uint32) -> u32 {
354 var.0
355 }
356}
357
358impl Serialize for Uint32 {
359 type Error = Error;
360
361 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
362 let mut buf = [0u8; 4];
363 LittleEndian::write_u32(&mut buf, self.0);
364 writer.write_all(&buf)?;
365 Ok(())
366 }
367}
368
369impl From<u32> for Uint32 {
370 fn from(u: u32) -> Self { Uint32(u) }
371}
372
373#[derive(Copy, Clone)]
375pub struct Uint64(u64);
376
377impl Deserialize for Uint64 {
378 type Error = Error;
379
380 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
381 let mut buf = [0u8; 8];
382 reader.read_exact(&mut buf)?;
383 Ok(Uint64(LittleEndian::read_u64(&buf)))
385 }
386}
387
388impl Serialize for Uint64 {
389 type Error = Error;
390
391 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
392 let mut buf = [0u8; 8];
393 LittleEndian::write_u64(&mut buf, self.0);
394 writer.write_all(&buf)?;
395 Ok(())
396 }
397}
398
399impl From<u64> for Uint64 {
400 fn from(u: u64) -> Self { Uint64(u) }
401}
402
403impl From<Uint64> for u64 {
404 fn from(var: Uint64) -> u64 {
405 var.0
406 }
407}
408
409
410#[derive(Copy, Clone)]
412pub struct VarUint1(bool);
413
414impl From<VarUint1> for bool {
415 fn from(v: VarUint1) -> bool {
416 v.0
417 }
418}
419
420impl From<bool> for VarUint1 {
421 fn from(b: bool) -> Self {
422 VarUint1(b)
423 }
424}
425
426impl Deserialize for VarUint1 {
427 type Error = Error;
428
429 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
430 let mut u8buf = [0u8; 1];
431 reader.read_exact(&mut u8buf)?;
432 match u8buf[0] {
433 0 => Ok(VarUint1(false)),
434 1 => Ok(VarUint1(true)),
435 v @ _ => Err(Error::InvalidVarUint1(v)),
436 }
437 }
438}
439
440impl Serialize for VarUint1 {
441 type Error = Error;
442
443 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
444 writer.write_all(&[
445 if self.0 { 1u8 } else { 0u8 }
446 ])?;
447 Ok(())
448 }
449}
450
451impl Deserialize for String {
452 type Error = Error;
453
454 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
455 let length = VarUint32::deserialize(reader)?.into();
456 if length > 0 {
457 let mut buf = vec![0u8; length];
458 reader.read_exact(&mut buf)?;
459 String::from_utf8(buf).map_err(|_| Error::NonUtf8String)
460 }
461 else {
462 Ok(String::new())
463 }
464 }
465}
466
467impl Serialize for String {
468 type Error = Error;
469
470 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Error> {
471 VarUint32::from(self.len()).serialize(writer)?;
472 writer.write_all(&self.into_bytes()[..])?;
473 Ok(())
474 }
475}
476
477pub struct CountedList<T: Deserialize>(Vec<T>);
480
481impl<T: Deserialize> CountedList<T> {
482 pub fn into_inner(self) -> Vec<T> { self.0 }
484}
485
486impl<T: Deserialize> Deserialize for CountedList<T> where T::Error: From<Error> {
487 type Error = T::Error;
488
489 fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
490 let count: usize = VarUint32::deserialize(reader)?.into();
491 let mut result = Vec::new();
492 for _ in 0..count { result.push(T::deserialize(reader)?); }
493 Ok(CountedList(result))
494 }
495}
496
497pub struct CountedWriter<'a, W: 'a + io::Write> {
500 writer: &'a mut W,
501 data: Vec<u8>,
502}
503
504impl<'a, W: 'a + io::Write> CountedWriter<'a, W> {
505 pub fn new(writer: &'a mut W) -> Self {
507 CountedWriter {
508 writer: writer,
509 data: Vec::new(),
510 }
511 }
512
513 pub fn done(self) -> io::Result<()> {
516 let writer = self.writer;
517 let data = self.data;
518 VarUint32::from(data.len())
519 .serialize(writer)
520 .map_err(
521 |_| io::Error::new(
522 io::ErrorKind::Other,
523 "Length serialization error",
524 )
525 )?;
526 writer.write_all(&data[..])?;
527 Ok(())
528 }
529}
530
531impl<'a, W: 'a + io::Write> io::Write for CountedWriter<'a, W> {
532 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
533 self.data.extend(buf.to_vec());
534 Ok(buf.len())
535 }
536
537 fn flush(&mut self) -> io::Result<()> {
538 Ok(())
539 }
540}
541
542pub struct CountedListWriter<I: Serialize<Error=::elements::Error>, T: IntoIterator<Item=I>>(pub usize, pub T);
545
546impl<I: Serialize<Error=::elements::Error>, T: IntoIterator<Item=I>> Serialize for CountedListWriter<I, T> {
547 type Error = Error;
548
549 fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
550 let len_us = self.0;
551 let data = self.1;
552 let len: VarUint32 = len_us.into();
553 len.serialize(writer)?;
554 for data_element in data { data_element.serialize(writer)? }
555
556 Ok(())
557 }
558}
559
560#[cfg(test)]
561mod tests {
562
563 use super::super::{deserialize_buffer, Serialize};
564 use super::{CountedList, VarInt7, VarUint32, VarInt32, VarInt64, VarUint64};
565
566 fn varuint32_ser_test(val: u32, expected: Vec<u8>) {
567 let mut buf = Vec::new();
568 let v1: VarUint32 = val.into();
569 v1.serialize(&mut buf).expect("to be serialized ok");
570 assert_eq!(expected, buf);
571 }
572
573 fn varuint32_de_test(dt: Vec<u8>, expected: u32) {
574 let val: VarUint32 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
575 assert_eq!(expected, val.into());
576 }
577
578 fn varuint32_serde_test(dt: Vec<u8>, val: u32) {
579 varuint32_de_test(dt.clone(), val);
580 varuint32_ser_test(val, dt);
581 }
582
583 fn varint32_ser_test(val: i32, expected: Vec<u8>) {
584 let mut buf = Vec::new();
585 let v1: VarInt32 = val.into();
586 v1.serialize(&mut buf).expect("to be serialized ok");
587 assert_eq!(expected, buf);
588 }
589
590 fn varint32_de_test(dt: Vec<u8>, expected: i32) {
591 let val: VarInt32 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
592 assert_eq!(expected, val.into());
593 }
594
595 fn varint32_serde_test(dt: Vec<u8>, val: i32) {
596 varint32_de_test(dt.clone(), val);
597 varint32_ser_test(val, dt);
598 }
599
600 fn varuint64_ser_test(val: u64, expected: Vec<u8>) {
601 let mut buf = Vec::new();
602 let v1: VarUint64 = val.into();
603 v1.serialize(&mut buf).expect("to be serialized ok");
604 assert_eq!(expected, buf);
605 }
606
607 fn varuint64_de_test(dt: Vec<u8>, expected: u64) {
608 let val: VarUint64 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
609 assert_eq!(expected, val.into());
610 }
611
612 fn varuint64_serde_test(dt: Vec<u8>, val: u64) {
613 varuint64_de_test(dt.clone(), val);
614 varuint64_ser_test(val, dt);
615 }
616
617 fn varint64_ser_test(val: i64, expected: Vec<u8>) {
618 let mut buf = Vec::new();
619 let v1: VarInt64 = val.into();
620 v1.serialize(&mut buf).expect("to be serialized ok");
621 assert_eq!(expected, buf);
622 }
623
624 fn varint64_de_test(dt: Vec<u8>, expected: i64) {
625 let val: VarInt64 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
626 assert_eq!(expected, val.into());
627 }
628
629 fn varint64_serde_test(dt: Vec<u8>, val: i64) {
630 varint64_de_test(dt.clone(), val);
631 varint64_ser_test(val, dt);
632 }
633
634 #[test]
635 fn varuint32_0() {
636 varuint32_serde_test(vec![0u8; 1], 0);
637 }
638
639 #[test]
640 fn varuint32_1() {
641 varuint32_serde_test(vec![1u8; 1], 1);
642 }
643
644 #[test]
645 fn varuint32_135() {
646 varuint32_serde_test(vec![135u8, 0x01], 135);
647 }
648
649 #[test]
650 fn varuint32_8192() {
651 varuint32_serde_test(vec![0x80, 0x40], 8192);
652 }
653
654 #[test]
655 fn varint32_8192() {
656 varint32_serde_test(vec![0x80, 0xc0, 0x00], 8192);
657 }
658
659 #[test]
660 fn varint32_neg_8192() {
661 varint32_serde_test(vec![0x80, 0x40], -8192);
662 }
663
664 #[test]
665 fn varuint64_0() {
666 varuint64_serde_test(vec![0u8; 1], 0);
667 }
668
669 #[test]
670 fn varuint64_1() {
671 varuint64_serde_test(vec![1u8; 1], 1);
672 }
673
674 #[test]
675 fn varuint64_135() {
676 varuint64_serde_test(vec![135u8, 0x01], 135);
677 }
678
679 #[test]
680 fn varuint64_8192() {
681 varuint64_serde_test(vec![0x80, 0x40], 8192);
682 }
683
684 #[test]
685 fn varint64_8192() {
686 varint64_serde_test(vec![0x80, 0xc0, 0x00], 8192);
687 }
688
689 #[test]
690 fn varint64_neg_8192() {
691 varint64_serde_test(vec![0x80, 0x40], -8192);
692 }
693
694 #[test]
695 fn varint64_min() {
696 varint64_serde_test(
697 vec![0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f],
698 -9223372036854775808,
699 );
700 }
701
702 #[test]
703 fn varint64_max() {
704 varint64_serde_test(
705 vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00],
706 9223372036854775807,
707 );
708 }
709
710 #[test]
711 fn varint32_min() {
712 varint32_serde_test(
713 vec![0x80, 0x80, 0x80, 0x80, 0x78],
714 -2147483648,
715 );
716 }
717
718 #[test]
719 fn varint32_max() {
720 varint32_serde_test(
721 vec![0xff, 0xff, 0xff, 0xff, 0x07],
722 2147483647,
723 );
724 }
725
726
727 #[test]
728 fn counted_list() {
729 let payload = [
730 133u8, 0x80, 0x80, 0x80, 0x0, 0x01,
733 0x7d,
734 0x05,
735 0x07,
736 0x09,
737 ];
738
739 let list: CountedList<VarInt7> =
740 deserialize_buffer(&payload).expect("type_section be deserialized");
741
742 let vars = list.into_inner();
743 assert_eq!(5, vars.len());
744 let v3: i8 = (*vars.get(1).unwrap()).into();
745 assert_eq!(-0x03i8, v3);
746 }
747}