1use crate::buf::Buffer;
6use crate::buf::FixedBuf;
7use crate::{cfg_feature_alloc, IntegerValue};
8use irox_bits::{Bits, BitsError, Error, MutBits};
9
10macro_rules! round {
11 ($val:expr) => {{
12 *$val >>= 7;
13 let a = ((*$val & 0x7F) | 0x80) as u8;
14 a
15 }};
16}
17macro_rules! one_byte_mask {
18 () => {
19 0x7F
20 };
21}
22macro_rules! two_byte_mask {
23 () => {
24 0x3FFF
25 };
26}
27macro_rules! three_byte_mask {
28 () => {
29 0x1F_FFFF
30 };
31}
32macro_rules! four_byte_mask {
33 () => {
34 0xFFF_FFFF
35 };
36}
37macro_rules! five_byte_mask {
38 () => {
39 0x7_FFFF_FFFF
40 };
41}
42macro_rules! six_byte_mask {
43 () => {
44 0x3FF_FFFF_FFFF
45 };
46}
47macro_rules! seven_byte_mask {
48 () => {
49 0x1_FFFF_FFFF_FFFF
50 };
51}
52macro_rules! eight_byte_mask {
53 () => {
54 0xFF_FFFF_FFFF_FFFF
55 };
56}
57macro_rules! nine_byte_mask {
58 () => {
59 0x7FFF_FFFF_FFFF_FFFF
60 };
61}
62
63pub fn encode_7bits(val: u8) -> [u8; 1] {
72 [val & 0x7F]
73}
74pub fn encode_8bits(val: u8) -> [u8; 2] {
87 let upper = (val & 0x80) >> 7;
88 [0x80 | upper, val & 0x7F]
89}
90
91pub fn encode_14bits(mut val: u16) -> [u8; 2] {
100 let b = (val & 0x7F) as u8;
101 let a = round!(&mut val);
102 [a, b]
103}
104
105pub fn encode_16bits(mut val: u16) -> [u8; 3] {
114 let c = (val & 0x7F) as u8;
115 let b = round!(&mut val);
116 let a = round!(&mut val);
117 [a, b, c]
118}
119pub fn encode_21bits(mut val: u32) -> [u8; 3] {
129 let c = (val & 0x7F) as u8;
130 let b = round!(&mut val);
131 let a = round!(&mut val);
132 [a, b, c]
133}
134pub fn encode_28bits(mut val: u32) -> [u8; 4] {
145 let d = (val & 0x7F) as u8;
146 let c = round!(&mut val);
147 let b = round!(&mut val);
148 let a = round!(&mut val);
149 [a, b, c, d]
150}
151
152pub fn encode_32bits(mut val: u32) -> [u8; 5] {
168 let e = (val & 0x7F) as u8;
169 let d = round!(&mut val);
170 let c = round!(&mut val);
171 let b = round!(&mut val);
172 let a = round!(&mut val);
173 [a, b, c, d, e]
174}
175
176pub fn encode_35bits(mut val: u64) -> [u8; 5] {
192 let e = (val & 0x7F) as u8;
193 let d = round!(&mut val);
194 let c = round!(&mut val);
195 let b = round!(&mut val);
196 let a = round!(&mut val);
197 [a, b, c, d, e]
198}
199
200pub fn encode_42bits(mut val: u64) -> [u8; 6] {
216 let f = (val & 0x7F) as u8;
217 let e = round!(&mut val);
218 let d = round!(&mut val);
219 let c = round!(&mut val);
220 let b = round!(&mut val);
221 let a = round!(&mut val);
222 [a, b, c, d, e, f]
223}
224
225pub fn encode_49bits(mut val: u64) -> [u8; 7] {
241 let g = (val & 0x7F) as u8;
242 let f = round!(&mut val);
243 let e = round!(&mut val);
244 let d = round!(&mut val);
245 let c = round!(&mut val);
246 let b = round!(&mut val);
247 let a = round!(&mut val);
248 [a, b, c, d, e, f, g]
249}
250pub fn encode_56bits(mut val: u64) -> [u8; 8] {
266 let h = (val & 0x7F) as u8;
267 let g = round!(&mut val);
268 let f = round!(&mut val);
269 let e = round!(&mut val);
270 let d = round!(&mut val);
271 let c = round!(&mut val);
272 let b = round!(&mut val);
273 let a = round!(&mut val);
274 [a, b, c, d, e, f, g, h]
275}
276pub fn encode_63bits(mut val: u64) -> [u8; 9] {
292 let i = (val & 0x7F) as u8;
293 let h = round!(&mut val);
294 let g = round!(&mut val);
295 let f = round!(&mut val);
296 let e = round!(&mut val);
297 let d = round!(&mut val);
298 let c = round!(&mut val);
299 let b = round!(&mut val);
300 let a = round!(&mut val);
301 [a, b, c, d, e, f, g, h, i]
302}
303
304pub fn encode_64bits(mut val: u64) -> [u8; 10] {
320 let j = (val & 0x7F) as u8;
321 let i = round!(&mut val);
322 let h = round!(&mut val);
323 let g = round!(&mut val);
324 let f = round!(&mut val);
325 let e = round!(&mut val);
326 let d = round!(&mut val);
327 let c = round!(&mut val);
328 let b = round!(&mut val);
329 let a = round!(&mut val);
330 [a, b, c, d, e, f, g, h, i, j]
331}
332
333pub fn encode_u128bits(mut val: u128) -> FixedBuf<19, u8> {
358 let mut out = FixedBuf::<19, u8>::new();
359 loop {
360 if val == 0 && !out.is_empty() {
361 break;
362 }
363 let mut i = (val & 0x7F) as u8;
364 val >>= 7;
365 if val != 0 {
366 i |= 0x80;
367 }
368 let _ = out.push(i);
369 }
370 out
371}
372
373pub fn encode_vbyte_to<T: MutBits + ?Sized>(val: u128, out: &mut T) -> Result<usize, BitsError> {
374 encode_u128bits(val).write_to(out)
375}
376
377macro_rules! zigzag_impl {
378 ($id:ident,$un:ident,$sig:ty,$usig:ty,$len:literal) => {
379 pub fn $id(n: $sig) -> $usig {
380 ((n << 1) ^ (n >> ($len - 1))) as $usig
381 }
382 pub fn $un(n: $usig) -> $sig {
383 let v = (n & 0x01) as $sig;
384 let v = (n >> 1) as $sig ^ -v;
385 v as $sig
386 }
387 impl ZigZag for $sig {
388 type Output = $usig;
389 fn zigzag(self) -> $usig {
390 $id(self)
391 }
392 }
393 impl ZagZig for $usig {
394 type Output = $sig;
395 fn zagzig(self) -> Self::Output {
396 $un(self)
397 }
398 }
399 };
400}
401zigzag_impl!(zigzag_i8, zagzig_u8, i8, u8, 8);
402zigzag_impl!(zigzag_i16, zagzig_u16, i16, u16, 16);
403zigzag_impl!(zigzag_i32, zagzig_u32, i32, u32, 32);
404zigzag_impl!(zigzag_i64, zagzig_u64, i64, u64, 64);
405zigzag_impl!(zigzag_i128, zagzig_u128, i128, u128, 128);
406
407pub trait ZigZag {
408 type Output;
409 fn zigzag(self) -> Self::Output;
410}
411pub trait ZagZig {
412 type Output;
413 fn zagzig(self) -> Self::Output;
414}
415pub fn encode_le_integer_to<T: MutBits + ?Sized>(
416 val: IntegerValue,
417 out: &mut T,
418) -> Result<usize, BitsError> {
419 encode_integer_to(val.to_le(), out)
420}
421pub fn encode_integer_to<T: MutBits + ?Sized>(
422 val: IntegerValue,
423 out: &mut T,
424) -> Result<usize, BitsError> {
425 match val {
426 IntegerValue::U8(v) => {
427 if v <= one_byte_mask!() {
428 out.write_all_bytes(&encode_7bits(v))?;
429 Ok(1)
430 } else {
431 out.write_all_bytes(&encode_8bits(v))?;
432 Ok(2)
433 }
434 }
435 IntegerValue::U16(v) => {
436 if v <= one_byte_mask!() {
437 out.write_all_bytes(&encode_7bits(v as u8))?;
438 Ok(1)
439 } else if v <= two_byte_mask!() {
440 out.write_all_bytes(&encode_14bits(v))?;
441 Ok(2)
442 } else {
443 out.write_all_bytes(&encode_16bits(v))?;
444 Ok(3)
445 }
446 }
447 IntegerValue::U32(v) => {
448 if v <= one_byte_mask!() {
449 out.write_all_bytes(&encode_7bits(v as u8))?;
450 Ok(1)
451 } else if v <= two_byte_mask!() {
452 out.write_all_bytes(&encode_14bits(v as u16))?;
453 Ok(2)
454 } else if v <= three_byte_mask!() {
455 out.write_all_bytes(&encode_21bits(v))?;
456 Ok(3)
457 } else if v <= four_byte_mask!() {
458 out.write_all_bytes(&encode_28bits(v))?;
459 Ok(4)
460 } else {
461 out.write_all_bytes(&encode_32bits(v))?;
462 Ok(5)
463 }
464 }
465 IntegerValue::U64(v) => {
466 if v <= one_byte_mask!() {
467 out.write_all_bytes(&encode_7bits(v as u8))?;
468 Ok(1)
469 } else if v <= two_byte_mask!() {
470 out.write_all_bytes(&encode_14bits(v as u16))?;
471 Ok(2)
472 } else if v <= three_byte_mask!() {
473 out.write_all_bytes(&encode_21bits(v as u32))?;
474 Ok(3)
475 } else if v <= four_byte_mask!() {
476 out.write_all_bytes(&encode_28bits(v as u32))?;
477 Ok(4)
478 } else if v <= five_byte_mask!() {
479 out.write_all_bytes(&encode_35bits(v))?;
480 Ok(5)
481 } else if v <= six_byte_mask!() {
482 out.write_all_bytes(&encode_42bits(v))?;
483 Ok(6)
484 } else if v <= seven_byte_mask!() {
485 out.write_all_bytes(&encode_49bits(v))?;
486 Ok(7)
487 } else if v <= eight_byte_mask!() {
488 out.write_all_bytes(&encode_56bits(v))?;
489 Ok(8)
490 } else if v <= nine_byte_mask!() {
491 out.write_all_bytes(&encode_63bits(v))?;
492 Ok(9)
493 } else {
494 out.write_all_bytes(&encode_64bits(v))?;
495 Ok(10)
496 }
497 }
498 IntegerValue::U128(v) => encode_u128bits(v).write_to(out),
499 IntegerValue::I8(v) => zigzag_i8(v).encode_vbyte_to(out),
500 IntegerValue::I16(v) => zigzag_i16(v).encode_vbyte_to(out),
501 IntegerValue::I32(v) => zigzag_i32(v).encode_vbyte_to(out),
502 IntegerValue::I64(v) => zigzag_i64(v).encode_vbyte_to(out),
503 IntegerValue::I128(v) => zigzag_i128(v).encode_vbyte_to(out),
504 }
505}
506pub trait EncodeVByteTo {
507 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError>;
508}
509impl EncodeVByteTo for u128 {
510 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
511 encode_integer_to(IntegerValue::U128(*self), out)
512 }
513}
514impl EncodeVByteTo for i128 {
515 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
516 encode_integer_to(IntegerValue::I128(*self), out)
517 }
518}
519impl EncodeVByteTo for u64 {
520 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
521 encode_integer_to(IntegerValue::U64(*self), out)
522 }
523}
524impl EncodeVByteTo for i64 {
525 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
526 encode_integer_to(IntegerValue::I64(*self), out)
527 }
528}
529impl EncodeVByteTo for u32 {
530 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
531 encode_integer_to(IntegerValue::U32(*self), out)
532 }
533}
534impl EncodeVByteTo for i32 {
535 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
536 encode_integer_to(IntegerValue::I32(*self), out)
537 }
538}
539impl EncodeVByteTo for u16 {
540 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
541 encode_integer_to(IntegerValue::U16(*self), out)
542 }
543}
544impl EncodeVByteTo for i16 {
545 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
546 encode_integer_to(IntegerValue::I16(*self), out)
547 }
548}
549impl EncodeVByteTo for u8 {
550 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
551 encode_integer_to(IntegerValue::U8(*self), out)
552 }
553}
554impl EncodeVByteTo for i8 {
555 fn encode_vbyte_to<T: MutBits + ?Sized>(&self, out: &mut T) -> Result<usize, BitsError> {
556 encode_integer_to(IntegerValue::I8(*self), out)
557 }
558}
559cfg_feature_alloc! {
560 pub fn encode_integer(val: IntegerValue) -> alloc::boxed::Box<[u8]> {
561 use alloc::boxed::Box;
562 match val {
563 IntegerValue::U8(v) => {
564 if v <= one_byte_mask!() {
565 Box::new(encode_7bits(v))
566 } else {
567 Box::new(encode_8bits(v))
568 }
569 }
570 IntegerValue::U16(v) => {
571 if v <= one_byte_mask!() {
572 Box::new(encode_7bits(v as u8))
573 } else if v <= two_byte_mask!() {
574 Box::new(encode_14bits(v))
575 } else {
576 Box::new(encode_16bits(v))
577 }
578 }
579 IntegerValue::U32(v) => {
580 if v <= one_byte_mask!() {
581 Box::new(encode_7bits(v as u8))
582 } else if v <= two_byte_mask!() {
583 Box::new(encode_14bits(v as u16))
584 } else if v <= three_byte_mask!() {
585 Box::new(encode_21bits(v))
586 } else if v <= four_byte_mask!() {
587 Box::new(encode_28bits(v))
588 } else {
589 Box::new(encode_32bits(v))
590 }
591 }
592 IntegerValue::U64(v) => {
593 if v <= one_byte_mask!() {
594 Box::new(encode_7bits(v as u8))
595 } else if v <= two_byte_mask!() {
596 Box::new(encode_14bits(v as u16))
597 } else if v <= three_byte_mask!() {
598 Box::new(encode_21bits(v as u32))
599 } else if v <= four_byte_mask!() {
600 Box::new(encode_28bits(v as u32))
601 } else if v <= five_byte_mask!() {
602 Box::new(encode_35bits(v))
603 } else if v <= six_byte_mask!() {
604 Box::new(encode_42bits(v))
605 } else if v <= seven_byte_mask!() {
606 Box::new(encode_49bits(v))
607 } else if v <= eight_byte_mask!() {
608 Box::new(encode_56bits(v))
609 } else if v <= nine_byte_mask!() {
610 Box::new(encode_63bits(v))
611 } else {
612 Box::new(encode_64bits(v))
613 }
614 }
615 _ => {
622 todo!()
623 }
624 }
625 }
626}
627
628pub const fn resultant_length(value: IntegerValue) -> u8 {
629 let v = value.to_be_u64();
630 match v {
631 0x0000_0000_0000_0000..=0x0000_0000_0000_007F => 1,
632 0x0000_0000_0000_0080..=0x0000_0000_0000_3FFF => 2,
633 0x0000_0000_0000_4000..=0x0000_0000_001F_FFFF => 3,
634 0x0000_0000_0020_0000..=0x0000_0000_0FFF_FFFF => 4,
635 0x0000_0000_1000_0000..=0x0000_0007_FFFF_FFFF => 5,
636 0x0000_0008_0000_0000..=0x0000_03FF_FFFF_FFFF => 6,
637 0x0000_0400_0000_0000..=0x0001_FFFF_FFFF_FFFF => 7,
638 0x0002_0000_0000_0000..=0x00FF_FFFF_FFFF_FFFF => 8,
639 0x0100_0000_0000_0000..=0x7FFF_FFFF_FFFF_FFFF => 9,
640 _ => 10,
641 }
642}
643cfg_feature_alloc! {
644 extern crate alloc;
645 pub trait EncodeVByte {
646 fn encode_vbyte(&self) -> alloc::boxed::Box<[u8]>;
647 }
648}
649pub trait EncodeVByteLength {
650 fn vbyte_length(&self) -> u8;
651}
652impl<T> EncodeVByteLength for T
653where
654 T: Into<IntegerValue> + Copy,
655{
656 fn vbyte_length(&self) -> u8 {
657 resultant_length(Into::<IntegerValue>::into(*self))
658 }
659}
660cfg_feature_alloc! {
661 macro_rules! impl_encode {
662 ($typ:ty) => {
663 impl crate::codec::vbyte::EncodeVByte for $typ {
664 fn encode_vbyte(&self) -> alloc::boxed::Box<[u8]> {
665 crate::codec::vbyte::encode_integer(self.into())
666 }
667 }
668 impl EncodeVByte for &$typ {
674 fn encode_vbyte(&self) -> alloc::boxed::Box<[u8]> {
675 let v: IntegerValue = (*self).into();
676 crate::codec::vbyte::encode_integer(v)
677 }
678 }
679 };
686 }
687
688 impl_encode!(u8);
689 impl_encode!(i8);
690 impl_encode!(u16);
691 impl_encode!(i16);
692 impl_encode!(u32);
693 impl_encode!(i32);
694 impl_encode!(u64);
695 impl_encode!(i64);
696}
697
698pub trait DecodeVByte {
699 fn decode_vbyte(&mut self) -> Result<u128, Error>;
700}
701
702pub fn decode_vbyte<T: Bits>(inp: &mut T) -> Result<u128, Error> {
703 let mut out: u128 = 0;
704 while let Some(val) = inp.next_u8()? {
705 let v = (val & 0x7F) as u128;
706 out = (out << 7) | v;
707 if val & 0x80 == 0 {
708 break;
709 }
710 }
711 Ok(out)
712}
713
714impl<T: Bits> DecodeVByte for T {
715 fn decode_vbyte(&mut self) -> Result<u128, Error> {
716 decode_vbyte(self)
717 }
718}
719
720#[cfg(all(test, feature = "alloc"))]
721mod tests {
722 use crate::codec::vbyte::{DecodeVByte, EncodeVByte};
723 use crate::codec::EncodeVByteLength;
724 use irox_bits::Error;
725
726 #[test]
727 pub fn test_encode() {
728 assert_eq!(0x00u8.encode_vbyte().as_ref(), &[0x00]);
729 assert_eq!(0x7Fu8.encode_vbyte().as_ref(), &[0x7F]);
730 assert_eq!(0x80u8.encode_vbyte().as_ref(), &[0x81, 0x00]);
731 assert_eq!(0x2000u16.encode_vbyte().as_ref(), &[0xC0, 0x00]);
732 assert_eq!(0x3FFFu16.encode_vbyte().as_ref(), &[0xFF, 0x7F]);
733 assert_eq!(0x4000u16.encode_vbyte().as_ref(), &[0x81, 0x80, 0x00]);
734 assert_eq!(0x1F_FFFFu32.encode_vbyte().as_ref(), &[0xFF, 0xFF, 0x7F]);
735 assert_eq!(
736 0x20_0000u32.encode_vbyte().as_ref(),
737 &[0x81, 0x80, 0x80, 0x00]
738 );
739 assert_eq!(
740 0x800_0000u32.encode_vbyte().as_ref(),
741 &[0xC0, 0x80, 0x80, 0x00]
742 );
743 assert_eq!(
744 0xFFF_FFFFu32.encode_vbyte().as_ref(),
745 &[0xFF, 0xFF, 0xFF, 0x7F]
746 );
747 }
748
749 #[test]
750 pub fn test_decode() -> Result<(), Error> {
751 assert_eq_hex!(0x0, [0x0].as_ref().decode_vbyte()?);
752 assert_eq_hex!(0x7F, [0x7F].as_ref().decode_vbyte()?);
753 assert_eq_hex!(0x80, [0x81, 0x00].as_ref().decode_vbyte()?);
754 assert_eq_hex!(0x2000, [0xC0, 0x00].as_ref().decode_vbyte()?);
755 assert_eq_hex!(0x3FFF, [0xFF, 0x7F].as_ref().decode_vbyte()?);
756 assert_eq_hex!(0x4000, [0x81, 0x80, 0x00].as_ref().decode_vbyte()?);
757 assert_eq_hex!(0x1F_FFFF, [0xFF, 0xFF, 0x7F].as_ref().decode_vbyte()?);
758 assert_eq_hex!(
759 0x800_0000,
760 [0xC0, 0x80, 0x80, 0x00].as_ref().decode_vbyte()?
761 );
762 assert_eq_hex!(
763 0xFFF_FFFF,
764 [0xFF, 0xFF, 0xFF, 0x7F].as_ref().decode_vbyte()?
765 );
766
767 Ok(())
768 }
769
770 #[test]
771 pub fn test_vbyte_length() -> Result<(), Error> {
772 assert_eq!(2, 0xCC.vbyte_length());
773 assert_eq!(3, 0xAAAA.vbyte_length());
774 Ok(())
775 }
776}