1#![deny(rustdoc::broken_intra_doc_links)]
6#![deny(missing_docs)]
7#![allow(clippy::redundant_field_names)]
8#![forbid(unsafe_code)]
9
10pub mod error;
11
12use std::io;
13
14use num::NumCast;
15
16use crate::error::Error;
17
18trait Bitsize {
20 fn bitsize() -> usize;
21}
22
23impl<T: NumCast> Bitsize for T {
24 fn bitsize() -> usize {
25 std::mem::size_of::<T>() * 8
26 }
27}
28
29#[derive(Debug)]
33pub struct BitCursor<T: AsRef<[u8]>> {
34 byte_len: usize,
38
39 inner: T,
41
42 byte_pos: usize,
45
46 current_block: u64,
48
49 bit_index: usize,
52}
53
54impl<T: AsRef<[u8]>> BitCursor<T> {
55 const BLOCK_SIZE: usize = std::mem::size_of::<u64>();
56 const BLOCK_SIZE_BITS: usize = u64::BITS as usize;
57 const MAX_VBR_BITS: usize = 32;
58
59 pub fn new(inner: T) -> Self {
61 Self {
62 byte_len: inner.as_ref().len(),
63 inner: inner,
64 byte_pos: 0,
65 current_block: 0,
66 bit_index: 0,
67 }
68 }
69
70 pub fn new_with_len(inner: T, byte_len: usize) -> Result<Self, Error> {
74 if byte_len > inner.as_ref().len() {
75 return Err(Error::InvalidLength);
76 }
77
78 Ok(Self {
79 byte_len: byte_len,
80 inner: inner,
81 byte_pos: 0,
82 current_block: 0,
83 bit_index: 0,
84 })
85 }
86
87 pub fn byte_len(&self) -> usize {
89 self.byte_len
90 }
91
92 pub fn bit_len(&self) -> usize {
94 self.byte_len() * 8
95 }
96
97 pub fn tell_bit(&self) -> usize {
99 (self.byte_pos * 8) - self.bit_index
100 }
101
102 pub fn tell_byte(&self) -> usize {
104 self.tell_bit() / 8
105 }
106
107 pub fn exhausted(&self) -> bool {
110 self.bit_index == 0 && self.byte_len() <= self.byte_pos
111 }
112
113 pub fn seek_bit(&mut self, pos: usize) -> Result<(), Error> {
119 log::debug!("seek_bit: seeking to {}", pos);
120
121 let byte_pos = (pos / 8) & !(Self::BLOCK_SIZE - 1);
123
124 if byte_pos > self.byte_len() {
125 return Err(Error::Eof);
126 }
127
128 self.byte_pos = byte_pos;
130 self.clear_block_state();
131
132 let bits_to_consume = pos % Self::BLOCK_SIZE_BITS;
141 log::debug!("bits_to_consume={}", bits_to_consume);
142 if bits_to_consume > 0 {
143 self.read(bits_to_consume)?;
144 }
145
146 Ok(())
147 }
148
149 fn clear_block_state(&mut self) {
155 self.current_block = 0;
156 self.bit_index = 0;
157 }
158
159 fn load_current_block(&mut self) -> Result<(), Error> {
164 if self.tell_byte() >= self.byte_len() {
165 return Err(Error::Eof);
166 }
167
168 self.clear_block_state();
172
173 let block_bytes = if self.tell_byte() + Self::BLOCK_SIZE < self.byte_len() {
176 &self.inner.as_ref()[self.tell_byte()..(self.tell_byte() + Self::BLOCK_SIZE)]
177 } else {
178 &self.inner.as_ref()[self.tell_byte()..self.byte_len()]
179 };
180
181 self.current_block = 0;
182 for (idx, byte) in block_bytes.iter().enumerate() {
183 self.current_block |= (*byte as u64) << (idx * 8);
184 }
185
186 self.byte_pos += block_bytes.len();
188
189 self.bit_index = block_bytes.len() * 8;
191
192 log::debug!(
193 "load_current_block finished: current_block={}, bit_index={}",
194 self.current_block,
195 self.bit_index
196 );
197
198 Ok(())
199 }
200
201 pub fn read(&mut self, nbits: usize) -> Result<u64, Error> {
207 log::debug!(
208 "read: nbits={}, current_block={}, bit_index={}",
209 nbits,
210 self.current_block,
211 self.bit_index
212 );
213
214 if nbits == 0 || nbits >= Self::BLOCK_SIZE_BITS {
215 return Err(Error::InvalidReadSize);
216 }
217
218 if self.bit_index >= nbits {
221 log::debug!("we have enough bits!");
222
223 let read = self.current_block & (!0 >> (Self::BLOCK_SIZE_BITS - nbits));
224
225 self.current_block >>= nbits;
226 self.bit_index -= nbits;
227
228 return Ok(read);
229 }
230
231 let bits_left = nbits - self.bit_index;
234 let part_1 = if self.bit_index > 0 {
235 self.current_block
236 } else {
237 0
238 };
239
240 self.load_current_block()?;
241
242 if bits_left > self.bit_index {
245 return Err(Error::Short);
246 }
247
248 let part_2 = self.current_block & (!0 >> (Self::BLOCK_SIZE_BITS - bits_left));
249
250 self.current_block >>= bits_left;
251 self.bit_index -= bits_left;
252
253 log::debug!(
254 "part_2 done: current_block={}, bit_index={}",
255 self.current_block,
256 self.bit_index
257 );
258
259 Ok(part_1 | (part_2 << (nbits - bits_left)))
261 }
262
263 pub fn read_as<Int: NumCast>(&mut self, nbits: usize) -> Result<Int, Error> {
268 let res: Int = num::cast(self.read(nbits)?).ok_or(Error::BadCast)?;
269 Ok(res)
270 }
271
272 pub fn read_exact<Int: NumCast>(&mut self) -> Result<Int, Error> {
276 self.read_as::<Int>(Int::bitsize())
277 }
278
279 #[cfg(any(feature = "vbr", doc))]
284 pub fn read_vbr(&mut self, width: usize) -> Result<u64, Error> {
285 if !(2..=Self::MAX_VBR_BITS).contains(&width) {
288 return Err(Error::InvalidVbrWidth);
289 }
290
291 let block_mask = 1 << (width - 1);
292
293 let mut result: u64 = 0;
296 let mut shift = 0;
297 loop {
298 let block = self.read(width)?;
300 log::debug!("block: {:#b}, masked: {:#b}", block, block & !block_mask);
301 result |= (block & !block_mask) << shift;
302
303 let continuation = (block & block_mask) != 0;
305 if !continuation {
306 break;
307 };
308
309 shift += width - 1;
311 }
312
313 Ok(result)
314 }
315
316 #[cfg(any(feature = "vbr", doc))]
321 pub fn read_svbr(&mut self, width: usize) -> Result<isize, Error> {
322 let mut result = self.read_vbr(width)?;
323
324 let sgn = (result & 1) != 0;
326 result >>= 1;
327
328 if sgn {
329 Ok(-(result as isize))
330 } else {
331 Ok(result as isize)
332 }
333 }
334
335 pub fn align32(&mut self) {
339 log::debug!("aligning the cursor");
340
341 if self.bit_index >= 32 {
342 self.current_block >>= self.bit_index - 32;
343 self.bit_index = 32;
344 } else {
345 self.clear_block_state();
346 }
347 }
348}
349
350impl<T: AsRef<[u8]>> io::Seek for BitCursor<T> {
358 fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
359 let off = match pos {
364 io::SeekFrom::Start(pos) => pos,
365 io::SeekFrom::End(pos) => {
366 if pos >= 0 {
367 return Err(io::Error::new(
368 io::ErrorKind::Unsupported,
369 "cannot seek past end",
370 ));
371 }
372
373 ((self.byte_len() as i64) + pos) as u64
375 }
376 io::SeekFrom::Current(pos) => ((self.tell_byte() as i64) + pos) as u64,
377 } as usize;
378
379 if off > self.byte_len() {
385 return Err(io::Error::new(
386 io::ErrorKind::InvalidInput,
387 "impossible seek requested",
388 ));
389 }
390
391 self.byte_pos = off;
393
394 self.clear_block_state();
397
398 Ok(off as u64)
399 }
400
401 fn stream_position(&mut self) -> io::Result<u64> {
402 Ok(self.tell_byte() as u64)
403 }
404
405 }
410
411#[cfg(test)]
412mod tests {
413 use std::io::Seek;
414
415 use super::*;
416
417 fn cursor(buf: &[u8]) -> BitCursor<&[u8]> {
418 BitCursor::new(&buf)
419 }
420
421 #[test]
422 fn test_new_with_len_invalid_length() {
423 assert!(BitCursor::new_with_len(&[0xff, 0xee], 3).is_err());
424 }
425
426 #[test]
427 fn test_read_basic() {
428 let mut cur = cursor(&[0b00011011]);
429
430 assert_eq!(cur.bit_len(), 8);
432 assert_eq!(cur.byte_len(), 1);
433 assert_eq!(cur.tell_bit(), 0);
434 assert_eq!(cur.tell_byte(), 0);
435
436 assert_eq!(cur.read(2).unwrap(), 0b11);
438 assert_eq!(cur.tell_bit(), 2);
439 assert_eq!(cur.tell_byte(), 0);
440 assert_eq!(cur.read(2).unwrap(), 0b10);
441 assert_eq!(cur.tell_bit(), 4);
442 assert_eq!(cur.tell_byte(), 0);
443 assert_eq!(cur.read(2).unwrap(), 0b01);
444 assert_eq!(cur.tell_bit(), 6);
445 assert_eq!(cur.tell_byte(), 0);
446 assert_eq!(cur.read(2).unwrap(), 0b00);
447 assert_eq!(cur.tell_bit(), 8);
448 assert_eq!(cur.tell_byte(), 1);
449
450 assert!(cur.read(1).is_err());
452 }
453
454 #[cfg(feature = "vbr")]
455 #[test]
456 fn test_invalid_reads() {
457 let mut cur = cursor(&[0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22]);
458
459 assert!(cur.read_vbr(33).is_err());
461
462 assert!(cur.read(64).is_err());
464 }
465
466 #[test]
467 fn test_read_multiple_sizes() {
468 let mut cur = cursor(&[0xAA, 0xBB, 0xCC, 0b10010101]);
469
470 assert_eq!(cur.read(24).unwrap(), 0xCCBBAA);
471 assert_eq!(cur.read(5).unwrap(), 0b10101);
472 assert_eq!(cur.read(3).unwrap(), 0b100);
473
474 assert!(cur.read(1).is_err());
476 }
477
478 #[test]
479 fn test_read_bounds() {
480 let mut cur = cursor(&[0xAA]);
481
482 assert!(cur.read(0).is_err());
484 assert!(cur.read(usize::BITS as usize + 1).is_err());
485 }
486
487 #[test]
488 fn test_read_llvm_wrapper_magic() {
489 let mut cur = cursor(&[0xde, 0xc0, 0x17, 0x0b]);
490
491 assert_eq!(cur.read(32).unwrap(), 0x0B17C0DE);
492 }
493
494 #[test]
495 fn test_read_llvm_raw_magic() {
496 let mut cur = cursor(&[b'B', b'C', 0xc0, 0xde]);
497
498 assert_eq!(cur.read(32).unwrap(), 0xdec04342);
499 }
500
501 #[test]
502 fn test_read_across_blocks() {
503 #[rustfmt::skip]
504 let mut cur = cursor(&[
505 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
506 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
507 ]);
508
509 assert_eq!(cur.read(56).unwrap(), 0x77665544332211);
510 assert_eq!(cur.read(24).unwrap(), 0x221188);
511 }
512
513 #[test]
514 fn test_read_across_blocks_unaligned() {
515 #[rustfmt::skip]
516 let mut cur = cursor(&[
517 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0b11111111,
518 0b00011001, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
519 ]);
520
521 assert_eq!(cur.read(56).unwrap(), 0x77665544332211);
522 assert_eq!(cur.read(5).unwrap(), 0b11111);
523 assert_eq!(cur.read(5).unwrap(), 0b01111);
524 assert_eq!(cur.read(6).unwrap(), 0b000110);
525 }
526
527 #[test]
528 fn test_read_and_align() {
529 #[rustfmt::skip]
530 let mut cur = cursor(&[
531 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0b11111111,
532 0b00011001, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
533 ]);
534
535 assert_eq!(cur.read(56).unwrap(), 0x77665544332211);
536 assert_eq!(cur.read(5).unwrap(), 0b11111);
537 assert_eq!(cur.read(5).unwrap(), 0b01111);
538 cur.align32();
539 assert_eq!(cur.read(8).unwrap(), 0x55);
540 assert_eq!(cur.read(16).unwrap(), 0x7766);
541 assert_eq!(cur.read(5).unwrap(), 0b01000);
542 assert_eq!(cur.read(3).unwrap(), 0b100);
543 assert!(cur.read(1).is_err());
544 }
545
546 #[test]
547 fn test_read_as() {
548 let mut cur = cursor(&[0xAA, 0xBB, 0xCC, 0xDD]);
549
550 assert_eq!(cur.read_as::<u16>(16).unwrap(), 0xBBAA);
551 assert_eq!(cur.read_as::<u32>(16).unwrap(), 0xDDCC);
552 }
553
554 #[test]
555 fn test_read_as_bounds() {
556 let mut cur = cursor(&[0xFF, 0xFF, 0xFF, 0xFF]);
557
558 assert!(cur.read_as::<u16>(17).is_err());
561 }
562
563 #[test]
564 fn test_read_exact() {
565 let mut cur = cursor(&[0xAA, 0xBB, 0xCC, 0xDD, 0xEE]);
566
567 assert_eq!(cur.read_exact::<u32>().unwrap(), 0xDDCCBBAA);
569 assert_eq!(cur.tell_bit(), u32::BITS as usize);
570 assert_eq!(cur.tell_byte(), (u32::BITS / 8) as usize);
571
572 assert_eq!(cur.read_exact::<u8>().unwrap(), 0xEE);
573 assert_eq!(cur.tell_bit(), (u32::BITS + u8::BITS) as usize);
574 assert_eq!(cur.tell_byte(), ((u32::BITS + u8::BITS) / 8) as usize);
575 }
576
577 #[test]
578 fn test_seek_bit() {
579 let mut cur = cursor(&[0b1111_1110, 0b1010_0111]);
580
581 assert_eq!(cur.read(4).unwrap(), 0b1110);
582
583 cur.seek_bit(4).unwrap();
585 assert_eq!(cur.tell_bit(), 4);
586 assert_eq!(cur.tell_byte(), 0);
587 assert_eq!(cur.bit_index, 12);
588
589 assert_eq!(cur.read(8).unwrap(), 0b0111_1111);
595 assert_eq!(cur.tell_bit(), 12);
596 assert_eq!(cur.tell_byte(), 1);
597 assert_eq!(cur.bit_index, 4);
598
599 assert_eq!(cur.read(4).unwrap(), 0b1010);
601
602 assert!(cur.read(1).is_err());
604 }
605
606 #[test]
607 fn test_seek() {
608 let mut cur = cursor(&[0xAA, 0xBB, 0xCC, 0xDD]);
609
610 assert_eq!(cur.read(32).unwrap(), 0xDDCCBBAA);
612 assert_eq!(cur.tell_bit(), 32);
613 assert_eq!(cur.tell_byte(), 4);
614
615 cur.seek(io::SeekFrom::Current(-2)).unwrap();
617 assert_eq!(cur.tell_bit(), 16);
618 assert_eq!(cur.tell_byte(), 2);
619 assert_eq!(cur.read(16).unwrap(), 0xDDCC);
620 assert_eq!(cur.tell_bit(), 32);
621 assert_eq!(cur.tell_byte(), 4);
622
623 cur.seek(io::SeekFrom::Start(0)).unwrap();
625 assert_eq!(cur.tell_bit(), 0);
626 assert_eq!(cur.tell_byte(), 0);
627 assert_eq!(cur.read(32).unwrap(), 0xDDCCBBAA);
628 assert_eq!(cur.tell_bit(), 32);
629 assert_eq!(cur.tell_byte(), 4);
630
631 cur.seek(io::SeekFrom::Start(1)).unwrap();
633 assert_eq!(cur.tell_bit(), 8);
634 assert_eq!(cur.tell_byte(), 1);
635 assert_eq!(cur.read(8).unwrap(), 0xBB);
636
637 cur.seek(io::SeekFrom::End(-1)).unwrap();
639 assert_eq!(cur.tell_bit(), 24);
640 assert_eq!(cur.tell_byte(), 3);
641 assert_eq!(cur.read(8).unwrap(), 0xDD);
642
643 assert!(cur.seek(io::SeekFrom::End(1)).is_err())
645 }
646
647 #[cfg(feature = "vbr")]
648 #[test]
649 fn test_vbr2_continuation() {
650 let mut cur = cursor(&[0b01101011]);
651
652 assert_eq!(cur.read_vbr(2).unwrap(), 9);
653 }
654
655 #[cfg(feature = "vbr")]
656 #[test]
657 fn test_vbr4_basic() {
658 let mut cur = cursor(&[0b00000111]);
659
660 assert_eq!(cur.read_vbr(4).unwrap(), 7);
661 }
662
663 #[cfg(feature = "vbr")]
664 #[test]
665 fn test_vbr4_continuation() {
666 let mut cur = cursor(&[0b0011_1011]);
667
668 assert_eq!(cur.read_vbr(4).unwrap(), 27);
669 }
670
671 #[cfg(feature = "vbr")]
672 #[test]
673 fn test_vbr6_basic() {
674 let mut cur = cursor(&[0b00_010000]);
675 assert_eq!(cur.read_vbr(6).unwrap(), 16);
676 assert_eq!(cur.tell_bit(), 6);
677 }
678
679 #[cfg(feature = "vbr")]
680 #[test]
681 fn test_vbr6_continuation() {
682 let mut cur = cursor(&[0b01_100001, 0b0011_1001, 0b100111_00]);
683 assert_eq!(cur.read_vbr(6).unwrap(), 3233);
684 assert_eq!(cur.read(3).unwrap(), 0b111);
685 assert_eq!(cur.read(3).unwrap(), 0b100);
686 }
687
688 #[cfg(feature = "vbr")]
689 #[test]
690 fn test_svbr4() {
691 let mut cur = cursor(&[0b0000_0111]);
693
694 assert_eq!(cur.read_svbr(4).unwrap(), -3);
695 }
696
697 #[test]
698 fn test_align32() {
699 {
700 let mut cur = cursor(&[0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22]);
701
702 assert_eq!(cur.read(8).unwrap(), 0xAA);
703 cur.align32();
704 assert_eq!(cur.tell_bit(), 32);
705 assert_eq!(cur.read(8).unwrap(), 0xEE);
706 assert_eq!(cur.read(24).unwrap(), 0x2211FF);
707 assert_eq!(cur.tell_bit(), 64);
708 }
709
710 {
711 let mut cur = cursor(&[0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22]);
712
713 cur.align32();
714 assert_eq!(cur.tell_bit(), 0);
715
716 cur.read(32).unwrap();
717 cur.align32();
718 assert_eq!(cur.tell_bit(), 32);
719 }
720
721 {
722 #[rustfmt::skip]
723 let mut cur = cursor(&[
724 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
725 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
726 ]);
727
728 cur.read(63).unwrap();
729 cur.read(1).unwrap();
730 assert_eq!(cur.tell_bit(), 64);
731 cur.align32();
732 assert_eq!(cur.tell_bit(), 64);
733
734 cur.seek_bit(0).unwrap();
735 cur.read(63).unwrap();
736 cur.read(1).unwrap();
737 cur.read(32).unwrap();
738 cur.align32();
739 assert_eq!(cur.tell_bit(), 96);
740 cur.read(1).unwrap();
741 cur.align32();
742 assert_eq!(cur.tell_bit(), 128);
743 }
744 }
745
746 #[test]
747 fn test_align32_unaligned() {
748 let mut cur = cursor(&[0b00011100, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22]);
749
750 assert_eq!(cur.read(5).unwrap(), 0b11100);
751 cur.align32();
752 assert_eq!(cur.tell_bit(), 32);
753 assert_eq!(cur.read(32).unwrap(), 0x2211FFEE);
754 assert_eq!(cur.tell_bit(), 64);
755 }
756
757 #[test]
758 fn test_align32_next_block() {
759 {
760 #[rustfmt::skip]
761 let mut cur = cursor(&[
762 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
763 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
764 ]);
765
766 cur.read(56).unwrap();
767 cur.align32();
768 assert_eq!(cur.read(32).unwrap(), 0xCCBBAA99);
769 assert_eq!(cur.tell_bit(), 96);
770 }
771
772 {
773 #[rustfmt::skip]
774 let mut cur = cursor(&[
775 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
776 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00,
777 ]);
778
779 cur.read(56).unwrap();
780 cur.read(17).unwrap();
781 cur.align32();
782 assert_eq!(cur.read(32).unwrap(), 0x00FFEEDD);
783 assert_eq!(cur.tell_bit(), 128);
784 }
785 }
786
787 #[cfg(feature = "vbr")]
788 #[test]
789 fn test_parse_unabbrev() {
790 #[rustfmt::skip]
792 let mut cur = cursor(&[
793 0b0001_01_11, 0b000001_00, 0b00_000110, 0xFF,
794 0b0001_01_11, 0b000001_00, 0b00_000110, 0b11111111,
795 0b1_01_11_101, 0b001_00000, 0b00000_000, 0b00000011,
796 ]);
797
798 assert_eq!(cur.read_vbr(2).unwrap(), 3); assert_eq!(cur.read_vbr(6).unwrap(), 1); assert_eq!(cur.read_vbr(6).unwrap(), 1); assert_eq!(cur.read_vbr(6).unwrap(), 6); cur.align32();
804
805 assert_eq!(cur.read_vbr(2).unwrap(), 3); assert_eq!(cur.read_vbr(6).unwrap(), 1); assert_eq!(cur.read_vbr(6).unwrap(), 1); assert_eq!(cur.read_vbr(6).unwrap(), 6); assert_eq!(cur.tell_bit(), 54);
811 assert_eq!(cur.read(13).unwrap(), 0b101_11111111_00);
812 assert_eq!(cur.read_vbr(2).unwrap(), 3); assert_eq!(cur.read_vbr(6).unwrap(), 1); assert_eq!(cur.read_vbr(6).unwrap(), 1); assert_eq!(cur.read_vbr(6).unwrap(), 32); }
817
818 #[cfg(feature = "vbr")]
819 #[test]
820 fn test_pseudo_bitstream1() {
821 let bytes = b"\xAA\xAA\x42\x43\xC0\xDE\x35\x14\x00\x00\x05\x00\x00\x00\x62\x0C";
822 let mut cur = cursor(bytes);
823
824 assert_eq!(cur.read(16).unwrap(), 0xAAAA);
825 assert_eq!(cur.read(32).unwrap(), 0xDEC04342);
826 assert_eq!(cur.read(2).unwrap(), 0b01); assert_eq!(cur.read_vbr(8).unwrap(), 13); assert_eq!(cur.read_vbr(5).unwrap(), 5); assert_eq!(cur.bit_index, 1);
830 assert_eq!(cur.tell_bit(), 63);
831 cur.align32();
832 assert_eq!(cur.bit_index, 0);
833 assert_eq!(cur.current_block, 0);
834 assert_eq!(cur.tell_bit(), 64);
835 cur.read(16).unwrap();
836 assert_eq!(cur.read(32).unwrap(), 5);
837 }
838}