1#![warn(missing_docs)]
77#![forbid(unsafe_code)]
78#![cfg_attr(feature = "alloc", no_std)]
79
80#[cfg(feature = "alloc")]
81extern crate alloc;
82use core::fmt::Debug;
83use core::marker::PhantomData;
84use core::mem;
85use core::ops::{BitOrAssign, BitXor, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub};
86#[cfg(feature = "alloc")]
87use core2::io;
88#[cfg(not(feature = "alloc"))]
89use std::io;
90
91pub mod huffman;
92pub mod read;
93pub mod write;
94pub use read::{
95 BitRead, BitReader, ByteRead, ByteReader, FromBitStream, FromBitStreamWith, FromByteStream,
96 FromByteStreamWith, HuffmanRead,
97};
98pub use write::{
99 BitCounter, BitRecorder, BitWrite, BitWriter, ByteWrite, ByteWriter, HuffmanWrite, ToBitStream,
100 ToBitStreamWith, ToByteStream, ToByteStreamWith,
101};
102
103pub trait Primitive {
107 type Bytes: AsRef<[u8]> + AsMut<[u8]>;
109
110 fn buffer() -> Self::Bytes;
112
113 fn to_be_bytes(self) -> Self::Bytes;
115
116 fn to_le_bytes(self) -> Self::Bytes;
118
119 fn from_be_bytes(bytes: Self::Bytes) -> Self;
121
122 fn from_le_bytes(bytes: Self::Bytes) -> Self;
124}
125
126macro_rules! define_primitive_numeric {
127 ($t:ty) => {
128 impl Primitive for $t {
129 type Bytes = [u8; mem::size_of::<$t>()];
130
131 #[inline(always)]
132 fn buffer() -> Self::Bytes {
133 [0; mem::size_of::<$t>()]
134 }
135 #[inline(always)]
136 fn to_be_bytes(self) -> Self::Bytes {
137 self.to_be_bytes()
138 }
139 #[inline(always)]
140 fn to_le_bytes(self) -> Self::Bytes {
141 self.to_le_bytes()
142 }
143 #[inline(always)]
144 fn from_be_bytes(bytes: Self::Bytes) -> Self {
145 <$t>::from_be_bytes(bytes)
146 }
147 #[inline(always)]
148 fn from_le_bytes(bytes: Self::Bytes) -> Self {
149 <$t>::from_le_bytes(bytes)
150 }
151 }
152 };
153}
154
155impl<const N: usize> Primitive for [u8; N] {
156 type Bytes = [u8; N];
157
158 #[inline(always)]
159 fn buffer() -> Self::Bytes {
160 [0; N]
161 }
162
163 #[inline(always)]
164 fn to_be_bytes(self) -> Self::Bytes {
165 self
166 }
167
168 #[inline(always)]
169 fn to_le_bytes(self) -> Self::Bytes {
170 self
171 }
172
173 #[inline(always)]
174 fn from_be_bytes(bytes: Self::Bytes) -> Self {
175 bytes
176 }
177
178 #[inline(always)]
179 fn from_le_bytes(bytes: Self::Bytes) -> Self {
180 bytes
181 }
182}
183
184pub trait Numeric:
188 Primitive
189 + Sized
190 + Copy
191 + Default
192 + Debug
193 + PartialOrd
194 + Shl<u32, Output = Self>
195 + ShlAssign<u32>
196 + Shr<u32, Output = Self>
197 + ShrAssign<u32>
198 + Rem<Self, Output = Self>
199 + RemAssign<Self>
200 + BitOrAssign<Self>
201 + BitXor<Self, Output = Self>
202 + Not<Output = Self>
203 + Sub<Self, Output = Self>
204{
205 const BITS_SIZE: u32;
207
208 const ONE: Self;
210
211 fn is_zero(self) -> bool;
213
214 fn from_u8(u: u8) -> Self;
216
217 fn to_u8(self) -> u8;
219
220 fn count_ones(self) -> u32;
222
223 fn leading_zeros(self) -> u32;
225
226 fn trailing_zeros(self) -> u32;
228
229 fn unsigned_value(self) -> write::UnsignedValue;
231}
232
233macro_rules! define_numeric {
234 ($t:ty) => {
235 define_primitive_numeric!($t);
236
237 impl Numeric for $t {
238 const BITS_SIZE: u32 = mem::size_of::<$t>() as u32 * 8;
239
240 const ONE: Self = 1;
241
242 #[inline(always)]
243 fn is_zero(self) -> bool {
244 self == 0
245 }
246 #[inline(always)]
247 fn from_u8(u: u8) -> Self {
248 u as $t
249 }
250 #[inline(always)]
251 fn to_u8(self) -> u8 {
252 self as u8
253 }
254 #[inline(always)]
255 fn count_ones(self) -> u32 {
256 self.count_ones()
257 }
258 #[inline(always)]
259 fn leading_zeros(self) -> u32 {
260 self.leading_zeros()
261 }
262 #[inline(always)]
263 fn trailing_zeros(self) -> u32 {
264 self.trailing_zeros()
265 }
266 #[inline(always)]
267 fn unsigned_value(self) -> write::UnsignedValue {
268 self.into()
269 }
270 }
271 };
272}
273
274pub trait SignedNumeric: Numeric {
277 fn is_negative(self) -> bool;
279
280 fn as_negative(self, bits: u32) -> Self;
283
284 fn as_negative_fixed<const BITS: u32>(self) -> Self;
287
288 fn as_unsigned(self, bits: u32) -> Self;
291
292 fn as_unsigned_fixed<const BITS: u32>(self) -> Self;
295
296 fn signed_value(self) -> write::SignedValue;
298}
299
300macro_rules! define_signed_numeric {
301 ($t:ty) => {
302 impl SignedNumeric for $t {
303 #[inline(always)]
304 fn is_negative(self) -> bool {
305 self < 0
306 }
307 #[inline(always)]
308 fn as_negative(self, bits: u32) -> Self {
309 self + (-1 << (bits - 1))
310 }
311 #[inline(always)]
312 fn as_negative_fixed<const BITS: u32>(self) -> Self {
313 self + (-1 << (BITS - 1))
314 }
315 #[inline(always)]
316 fn as_unsigned(self, bits: u32) -> Self {
317 self - (-1 << (bits - 1))
318 }
319 #[inline(always)]
320 fn as_unsigned_fixed<const BITS: u32>(self) -> Self {
321 self - (-1 << (BITS - 1))
322 }
323 #[inline(always)]
324 fn signed_value(self) -> write::SignedValue {
325 self.into()
326 }
327 }
328 };
329}
330
331define_numeric!(u8);
332define_numeric!(i8);
333define_numeric!(u16);
334define_numeric!(i16);
335define_numeric!(u32);
336define_numeric!(i32);
337define_numeric!(u64);
338define_numeric!(i64);
339define_numeric!(u128);
340define_numeric!(i128);
341
342define_signed_numeric!(i8);
343define_signed_numeric!(i16);
344define_signed_numeric!(i32);
345define_signed_numeric!(i64);
346define_signed_numeric!(i128);
347
348define_primitive_numeric!(f32);
349define_primitive_numeric!(f64);
350
351pub trait Endianness: Sized {
359 fn push<N>(queue: &mut BitQueue<Self, N>, bits: u32, value: N)
362 where
363 N: Numeric;
364
365 fn push_fixed<const B: u32, N>(queue: &mut BitQueue<Self, N>, value: N)
368 where
369 N: Numeric;
370
371 fn pop<N>(queue: &mut BitQueue<Self, N>, bits: u32) -> N
374 where
375 N: Numeric;
376
377 fn pop_fixed<const B: u32, N>(queue: &mut BitQueue<Self, N>) -> N
380 where
381 N: Numeric;
382
383 fn drop<N>(queue: &mut BitQueue<Self, N>, bits: u32)
386 where
387 N: Numeric;
388
389 fn next_zeros<N>(queue: &BitQueue<Self, N>) -> u32
392 where
393 N: Numeric;
394
395 fn next_ones<N>(queue: &BitQueue<Self, N>) -> u32
398 where
399 N: Numeric;
400
401 fn read_signed<R, S>(r: &mut R, bits: u32) -> io::Result<S>
403 where
404 R: BitRead,
405 S: SignedNumeric;
406
407 fn read_signed_fixed<R, const B: u32, S>(r: &mut R) -> io::Result<S>
409 where
410 R: BitRead,
411 S: SignedNumeric;
412
413 fn write_signed<W, S>(w: &mut W, bits: u32, value: S) -> io::Result<()>
415 where
416 W: BitWrite,
417 S: SignedNumeric;
418
419 fn write_signed_fixed<W, const B: u32, S>(w: &mut W, value: S) -> io::Result<()>
421 where
422 W: BitWrite,
423 S: SignedNumeric;
424
425 fn read_primitive<R, V>(r: &mut R) -> io::Result<V>
427 where
428 R: BitRead,
429 V: Primitive;
430
431 fn write_primitive<W, V>(w: &mut W, value: V) -> io::Result<()>
433 where
434 W: BitWrite,
435 V: Primitive;
436
437 fn read_numeric<R, V>(r: R) -> io::Result<V>
439 where
440 R: io::Read,
441 V: Primitive;
442
443 fn write_numeric<W, V>(w: W, value: V) -> io::Result<()>
445 where
446 W: io::Write,
447 V: Primitive;
448}
449
450#[derive(Copy, Clone, Debug)]
452pub struct BigEndian;
453
454pub type BE = BigEndian;
456
457impl Endianness for BigEndian {
458 #[inline]
459 fn push<N>(queue: &mut BitQueue<Self, N>, bits: u32, value: N)
460 where
461 N: Numeric,
462 {
463 if !queue.value.is_zero() {
464 queue.value <<= bits;
465 }
466 queue.value |= value;
467 queue.bits += bits;
468 }
469
470 #[inline]
471 fn push_fixed<const B: u32, N>(queue: &mut BitQueue<Self, N>, value: N)
472 where
473 N: Numeric,
474 {
475 if !queue.value.is_zero() {
476 queue.value <<= B;
477 }
478 queue.value |= value;
479 queue.bits += B;
480 }
481
482 #[inline]
483 fn pop<N>(queue: &mut BitQueue<Self, N>, bits: u32) -> N
484 where
485 N: Numeric,
486 {
487 if bits < queue.bits {
488 let offset = queue.bits - bits;
489 let to_return = queue.value >> offset;
490 queue.value %= N::ONE << offset;
491 queue.bits -= bits;
492 to_return
493 } else {
494 let to_return = queue.value;
495 queue.value = N::default();
496 queue.bits = 0;
497 to_return
498 }
499 }
500
501 #[inline]
502 fn pop_fixed<const B: u32, N>(queue: &mut BitQueue<Self, N>) -> N
503 where
504 N: Numeric,
505 {
506 if B < queue.bits {
507 let offset = queue.bits - B;
508 let to_return = queue.value >> offset;
509 queue.value %= N::ONE << offset;
510 queue.bits -= B;
511 to_return
512 } else {
513 let to_return = queue.value;
514 queue.value = N::default();
515 queue.bits = 0;
516 to_return
517 }
518 }
519
520 #[inline]
521 fn drop<N>(queue: &mut BitQueue<Self, N>, bits: u32)
522 where
523 N: Numeric,
524 {
525 if bits < queue.bits {
526 queue.value %= N::ONE << (queue.bits - bits);
527 queue.bits -= bits;
528 } else {
529 queue.value = N::default();
530 queue.bits = 0;
531 }
532 }
533
534 #[inline]
535 fn next_zeros<N>(queue: &BitQueue<Self, N>) -> u32
536 where
537 N: Numeric,
538 {
539 queue.value.leading_zeros() - (N::BITS_SIZE - queue.bits)
540 }
541
542 #[inline]
543 fn next_ones<N>(queue: &BitQueue<Self, N>) -> u32
544 where
545 N: Numeric,
546 {
547 if queue.bits < N::BITS_SIZE {
548 (queue.value ^ ((N::ONE << queue.bits) - N::ONE)).leading_zeros()
549 - (N::BITS_SIZE - queue.bits)
550 } else {
551 (!queue.value).leading_zeros()
552 }
553 }
554
555 fn read_signed<R, S>(r: &mut R, bits: u32) -> io::Result<S>
556 where
557 R: BitRead,
558 S: SignedNumeric,
559 {
560 let is_negative = r.read_bit()?;
561 let unsigned = r.read::<S>(bits - 1)?;
562 Ok(if is_negative {
563 unsigned.as_negative(bits)
564 } else {
565 unsigned
566 })
567 }
568
569 fn read_signed_fixed<R, const B: u32, S>(r: &mut R) -> io::Result<S>
570 where
571 R: BitRead,
572 S: SignedNumeric,
573 {
574 let is_negative = r.read_bit()?;
575 let unsigned = r.read::<S>(B - 1)?;
576 Ok(if is_negative {
577 unsigned.as_negative_fixed::<B>()
578 } else {
579 unsigned
580 })
581 }
582
583 fn write_signed<W, S>(w: &mut W, bits: u32, value: S) -> io::Result<()>
584 where
585 W: BitWrite,
586 S: SignedNumeric,
587 {
588 if bits == S::BITS_SIZE {
589 w.write_bytes(value.to_be_bytes().as_ref())
590 } else if value.is_negative() {
591 w.write_bit(true)
592 .and_then(|()| w.write(bits - 1, value.as_unsigned(bits)))
593 } else {
594 w.write_bit(false).and_then(|()| w.write(bits - 1, value))
595 }
596 }
597
598 fn write_signed_fixed<W, const B: u32, S>(w: &mut W, value: S) -> io::Result<()>
599 where
600 W: BitWrite,
601 S: SignedNumeric,
602 {
603 if B == S::BITS_SIZE {
604 w.write_bytes(value.to_be_bytes().as_ref())
605 } else if value.is_negative() {
606 w.write_bit(true)
607 .and_then(|()| w.write(B - 1, value.as_unsigned(B)))
608 } else {
609 w.write_bit(false).and_then(|()| w.write(B - 1, value))
610 }
611 }
612
613 #[inline]
614 fn read_primitive<R, V>(r: &mut R) -> io::Result<V>
615 where
616 R: BitRead,
617 V: Primitive,
618 {
619 let mut buffer = V::buffer();
620 r.read_bytes(buffer.as_mut())?;
621 Ok(V::from_be_bytes(buffer))
622 }
623
624 #[inline]
625 fn write_primitive<W, V>(w: &mut W, value: V) -> io::Result<()>
626 where
627 W: BitWrite,
628 V: Primitive,
629 {
630 w.write_bytes(value.to_be_bytes().as_ref())
631 }
632
633 #[inline]
634 fn read_numeric<R, V>(mut r: R) -> io::Result<V>
635 where
636 R: io::Read,
637 V: Primitive,
638 {
639 let mut buffer = V::buffer();
640 r.read_exact(buffer.as_mut())?;
641 Ok(V::from_be_bytes(buffer))
642 }
643
644 #[inline]
645 fn write_numeric<W, V>(mut w: W, value: V) -> io::Result<()>
646 where
647 W: io::Write,
648 V: Primitive,
649 {
650 w.write_all(value.to_be_bytes().as_ref())
651 }
652}
653
654#[derive(Copy, Clone, Debug)]
656pub struct LittleEndian;
657
658pub type LE = LittleEndian;
660
661impl Endianness for LittleEndian {
662 #[inline]
663 fn push<N>(queue: &mut BitQueue<Self, N>, bits: u32, mut value: N)
664 where
665 N: Numeric,
666 {
667 if !value.is_zero() {
668 value <<= queue.bits;
669 queue.value |= value;
670 }
671 queue.bits += bits;
672 }
673
674 #[inline]
675 fn push_fixed<const B: u32, N>(queue: &mut BitQueue<Self, N>, mut value: N)
676 where
677 N: Numeric,
678 {
679 if !value.is_zero() {
680 value <<= queue.bits;
681 queue.value |= value;
682 }
683 queue.bits += B;
684 }
685
686 #[inline]
687 fn pop<N>(queue: &mut BitQueue<Self, N>, bits: u32) -> N
688 where
689 N: Numeric,
690 {
691 if bits < queue.bits {
692 let to_return = queue.value % (N::ONE << bits);
693 queue.value >>= bits;
694 queue.bits -= bits;
695 to_return
696 } else {
697 let to_return = queue.value;
698 queue.value = N::default();
699 queue.bits = 0;
700 to_return
701 }
702 }
703
704 fn pop_fixed<const B: u32, N>(queue: &mut BitQueue<Self, N>) -> N
705 where
706 N: Numeric,
707 {
708 if B < queue.bits {
709 let to_return = queue.value % (N::ONE << B);
710 queue.value >>= B;
711 queue.bits -= B;
712 to_return
713 } else {
714 let to_return = queue.value;
715 queue.value = N::default();
716 queue.bits = 0;
717 to_return
718 }
719 }
720
721 #[inline]
722 fn drop<N>(queue: &mut BitQueue<Self, N>, bits: u32)
723 where
724 N: Numeric,
725 {
726 if bits < queue.bits {
727 queue.value >>= bits;
728 queue.bits -= bits;
729 } else {
730 queue.value = N::default();
731 queue.bits = 0;
732 }
733 }
734
735 #[inline(always)]
736 fn next_zeros<N>(queue: &BitQueue<Self, N>) -> u32
737 where
738 N: Numeric,
739 {
740 queue.value.trailing_zeros()
741 }
742
743 #[inline]
744 fn next_ones<N>(queue: &BitQueue<Self, N>) -> u32
745 where
746 N: Numeric,
747 {
748 (queue.value ^ !N::default()).trailing_zeros()
749 }
750
751 fn read_signed<R, S>(r: &mut R, bits: u32) -> io::Result<S>
752 where
753 R: BitRead,
754 S: SignedNumeric,
755 {
756 let unsigned = r.read::<S>(bits - 1)?;
757 let is_negative = r.read_bit()?;
758 Ok(if is_negative {
759 unsigned.as_negative(bits)
760 } else {
761 unsigned
762 })
763 }
764
765 fn read_signed_fixed<R, const B: u32, S>(r: &mut R) -> io::Result<S>
766 where
767 R: BitRead,
768 S: SignedNumeric,
769 {
770 let unsigned = r.read::<S>(B - 1)?;
771 let is_negative = r.read_bit()?;
772 Ok(if is_negative {
773 unsigned.as_negative_fixed::<B>()
774 } else {
775 unsigned
776 })
777 }
778
779 fn write_signed<W, S>(w: &mut W, bits: u32, value: S) -> io::Result<()>
780 where
781 W: BitWrite,
782 S: SignedNumeric,
783 {
784 if bits == S::BITS_SIZE {
785 w.write_bytes(value.to_le_bytes().as_ref())
786 } else if value.is_negative() {
787 w.write(bits - 1, value.as_unsigned(bits))
788 .and_then(|()| w.write_bit(true))
789 } else {
790 w.write(bits - 1, value).and_then(|()| w.write_bit(false))
791 }
792 }
793
794 fn write_signed_fixed<W, const B: u32, S>(w: &mut W, value: S) -> io::Result<()>
795 where
796 W: BitWrite,
797 S: SignedNumeric,
798 {
799 if B == S::BITS_SIZE {
800 w.write_bytes(value.to_le_bytes().as_ref())
801 } else if value.is_negative() {
802 w.write(B - 1, value.as_unsigned_fixed::<B>())
803 .and_then(|()| w.write_bit(true))
804 } else {
805 w.write(B - 1, value).and_then(|()| w.write_bit(false))
806 }
807 }
808
809 #[inline]
810 fn read_primitive<R, V>(r: &mut R) -> io::Result<V>
811 where
812 R: BitRead,
813 V: Primitive,
814 {
815 let mut buffer = V::buffer();
816 r.read_bytes(buffer.as_mut())?;
817 Ok(V::from_le_bytes(buffer))
818 }
819
820 #[inline]
821 fn write_primitive<W, V>(w: &mut W, value: V) -> io::Result<()>
822 where
823 W: BitWrite,
824 V: Primitive,
825 {
826 w.write_bytes(value.to_le_bytes().as_ref())
827 }
828
829 fn read_numeric<R, V>(mut r: R) -> io::Result<V>
830 where
831 R: io::Read,
832 V: Primitive,
833 {
834 let mut buffer = V::buffer();
835 r.read_exact(buffer.as_mut())?;
836 Ok(V::from_le_bytes(buffer))
837 }
838
839 #[inline]
840 fn write_numeric<W, V>(mut w: W, value: V) -> io::Result<()>
841 where
842 W: io::Write,
843 V: Primitive,
844 {
845 w.write_all(value.to_le_bytes().as_ref())
846 }
847}
848
849#[derive(Clone, Debug, Default)]
852pub struct BitQueue<E: Endianness, N: Numeric> {
853 phantom: PhantomData<E>,
854 value: N,
855 bits: u32,
856}
857
858impl<E: Endianness, N: Numeric> BitQueue<E, N> {
859 #[inline]
861 pub fn new() -> BitQueue<E, N> {
862 BitQueue {
863 phantom: PhantomData,
864 value: N::default(),
865 bits: 0,
866 }
867 }
868
869 #[inline]
872 pub fn from_value(value: N, bits: u32) -> BitQueue<E, N> {
873 assert!(if bits < N::BITS_SIZE {
874 value < (N::ONE << bits)
875 } else {
876 bits <= N::BITS_SIZE
877 });
878 BitQueue {
879 phantom: PhantomData,
880 value,
881 bits,
882 }
883 }
884
885 #[inline]
888 pub fn set(&mut self, value: N, bits: u32) {
889 assert!(if bits < N::BITS_SIZE {
890 value < (N::ONE << bits)
891 } else {
892 bits <= N::BITS_SIZE
893 });
894 self.value = value;
895 self.bits = bits;
896 }
897
898 #[inline(always)]
900 pub fn value(self) -> N {
901 self.value
902 }
903
904 #[inline(always)]
906 pub fn len(&self) -> u32 {
907 self.bits
908 }
909
910 #[inline(always)]
912 pub fn max_len(&self) -> u32 {
913 N::BITS_SIZE
914 }
915
916 #[inline(always)]
918 pub fn remaining_len(&self) -> u32 {
919 self.max_len() - self.len()
920 }
921
922 #[inline(always)]
924 pub fn is_empty(&self) -> bool {
925 self.bits == 0
926 }
927
928 #[inline(always)]
930 pub fn is_full(&self) -> bool {
931 self.bits == N::BITS_SIZE
932 }
933
934 #[inline(always)]
936 pub fn clear(&mut self) {
937 self.set(N::default(), 0)
938 }
939
940 #[inline(always)]
942 pub fn all_0(&self) -> bool {
943 self.value.count_ones() == 0
944 }
945
946 #[inline(always)]
948 pub fn all_1(&self) -> bool {
949 self.value.count_ones() == self.bits
950 }
951
952 #[inline(always)]
955 pub fn push(&mut self, bits: u32, value: N) {
956 assert!(bits <= self.remaining_len()); E::push(self, bits, value)
958 }
959
960 #[inline(always)]
963 pub fn push_fixed<const B: u32>(&mut self, value: N) {
964 assert!(B <= self.remaining_len()); E::push_fixed::<B, N>(self, value)
966 }
967
968 #[inline(always)]
972 pub fn pop(&mut self, bits: u32) -> N {
973 assert!(bits <= self.len()); E::pop(self, bits)
975 }
976
977 pub fn pop_fixed<const B: u32>(&mut self) -> N {
979 assert!(B <= self.len()); E::pop_fixed::<B, N>(self)
981 }
982
983 #[inline]
986 pub fn pop_all(&mut self) -> N {
987 let to_return = self.value;
988 self.value = N::default();
989 self.bits = 0;
990 to_return
991 }
992
993 #[inline(always)]
998 pub fn drop(&mut self, bits: u32) {
999 assert!(bits <= self.len()); E::drop(self, bits)
1001 }
1002
1003 #[inline]
1006 pub fn pop_0(&mut self) -> u32 {
1007 let zeros = E::next_zeros(self);
1008 self.drop(zeros + 1);
1009 zeros
1010 }
1011
1012 #[inline]
1015 pub fn pop_1(&mut self) -> u32 {
1016 let ones = E::next_ones(self);
1017 self.drop(ones + 1);
1018 ones
1019 }
1020}
1021
1022impl<E: Endianness> BitQueue<E, u8> {
1023 #[inline(always)]
1026 pub fn to_state(&self) -> usize {
1027 (1 << self.bits) | (self.value as usize)
1028 }
1029}