1use crate::blocks::block::BlockType;
4use crate::blocks::literals_section::LiteralsSectionType;
5use crate::io::Error;
6use alloc::vec::Vec;
7use core::fmt;
8#[cfg(feature = "std")]
9use std::error::Error as StdError;
10
11#[derive(Debug)]
12#[non_exhaustive]
13pub enum GetBitsError {
14 TooManyBits {
15 num_requested_bits: usize,
16 limit: u8,
17 },
18 NotEnoughRemainingBits {
19 requested: usize,
20 remaining: usize,
21 },
22}
23
24#[derive(Debug)]
25#[non_exhaustive]
26pub enum FrameDescriptorError {
27 InvalidFrameContentSizeFlag { got: u8 },
28}
29
30impl fmt::Display for FrameDescriptorError {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Self::InvalidFrameContentSizeFlag { got } => write!(
34 f,
35 "Invalid Frame_Content_Size_Flag; Is: {}, Should be one of: 0, 1, 2, 3",
36 got
37 ),
38 }
39 }
40}
41
42#[cfg(feature = "std")]
43impl StdError for FrameDescriptorError {}
44
45#[derive(Debug)]
46#[non_exhaustive]
47pub enum FrameHeaderError {
48 WindowTooBig { got: u64 },
49 WindowTooSmall { got: u64 },
50 FrameDescriptorError(FrameDescriptorError),
51 DictIdTooSmall { got: usize, expected: usize },
52 MismatchedFrameSize { got: usize, expected: u8 },
53 FrameSizeIsZero,
54 InvalidFrameSize { got: u8 },
55}
56
57impl fmt::Display for FrameHeaderError {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 match self {
60 Self::WindowTooBig { got } => write!(
61 f,
62 "window_size bigger than allowed maximum. Is: {}, Should be lower than: {}",
63 got,
64 crate::decoding::frame::MAX_WINDOW_SIZE
65 ),
66 Self::WindowTooSmall { got } => write!(
67 f,
68 "window_size smaller than allowed minimum. Is: {}, Should be greater than: {}",
69 got,
70 crate::decoding::frame::MIN_WINDOW_SIZE
71 ),
72 Self::FrameDescriptorError(e) => write!(f, "{:?}", e),
73 Self::DictIdTooSmall { got, expected } => write!(
74 f,
75 "Not enough bytes in dict_id. Is: {}, Should be: {}",
76 got, expected
77 ),
78 Self::MismatchedFrameSize { got, expected } => write!(
79 f,
80 "frame_content_size does not have the right length. Is: {}, Should be: {}",
81 got, expected
82 ),
83 Self::FrameSizeIsZero => write!(f, "frame_content_size was zero"),
84 Self::InvalidFrameSize { got } => write!(
85 f,
86 "Invalid frame_content_size. Is: {}, Should be one of 1, 2, 4, 8 bytes",
87 got
88 ),
89 }
90 }
91}
92
93#[cfg(feature = "std")]
94impl StdError for FrameHeaderError {
95 fn source(&self) -> Option<&(dyn StdError + 'static)> {
96 match self {
97 FrameHeaderError::FrameDescriptorError(source) => Some(source),
98 _ => None,
99 }
100 }
101}
102
103impl From<FrameDescriptorError> for FrameHeaderError {
104 fn from(error: FrameDescriptorError) -> Self {
105 Self::FrameDescriptorError(error)
106 }
107}
108
109#[derive(Debug)]
110#[non_exhaustive]
111pub enum ReadFrameHeaderError {
112 MagicNumberReadError(Error),
113 BadMagicNumber(u32),
114 FrameDescriptorReadError(Error),
115 InvalidFrameDescriptor(FrameDescriptorError),
116 WindowDescriptorReadError(Error),
117 DictionaryIdReadError(Error),
118 FrameContentSizeReadError(Error),
119 SkipFrame { magic_number: u32, length: u32 },
120}
121
122impl fmt::Display for ReadFrameHeaderError {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 match self {
125 Self::MagicNumberReadError(e) => write!(f, "Error while reading magic number: {}", e),
126 Self::BadMagicNumber(e) => write!(f, "Read wrong magic number: 0x{:X}", e),
127 Self::FrameDescriptorReadError(e) => {
128 write!(f, "Error while reading frame descriptor: {}", e)
129 }
130 Self::InvalidFrameDescriptor(e) => write!(f, "{:?}", e),
131 Self::WindowDescriptorReadError(e) => {
132 write!(f, "Error while reading window descriptor: {}", e)
133 }
134 Self::DictionaryIdReadError(e) => write!(f, "Error while reading dictionary id: {}", e),
135 Self::FrameContentSizeReadError(e) => {
136 write!(f, "Error while reading frame content size: {}", e)
137 }
138 Self::SkipFrame {
139 magic_number,
140 length,
141 } => write!(
142 f,
143 "SkippableFrame encountered with MagicNumber 0x{:X} and length {} bytes",
144 magic_number, length
145 ),
146 }
147 }
148}
149
150#[cfg(feature = "std")]
151impl StdError for ReadFrameHeaderError {
152 fn source(&self) -> Option<&(dyn StdError + 'static)> {
153 match self {
154 ReadFrameHeaderError::MagicNumberReadError(source) => Some(source),
155 ReadFrameHeaderError::FrameDescriptorReadError(source) => Some(source),
156 ReadFrameHeaderError::InvalidFrameDescriptor(source) => Some(source),
157 ReadFrameHeaderError::WindowDescriptorReadError(source) => Some(source),
158 ReadFrameHeaderError::DictionaryIdReadError(source) => Some(source),
159 ReadFrameHeaderError::FrameContentSizeReadError(source) => Some(source),
160 _ => None,
161 }
162 }
163}
164
165impl From<FrameDescriptorError> for ReadFrameHeaderError {
166 fn from(error: FrameDescriptorError) -> Self {
167 Self::InvalidFrameDescriptor(error)
168 }
169}
170
171#[derive(Debug)]
172#[non_exhaustive]
173pub enum BlockHeaderReadError {
174 ReadError(Error),
175 FoundReservedBlock,
176 BlockTypeError(BlockTypeError),
177 BlockSizeError(BlockSizeError),
178}
179
180#[cfg(feature = "std")]
181impl std::error::Error for BlockHeaderReadError {
182 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
183 match self {
184 BlockHeaderReadError::ReadError(source) => Some(source),
185 BlockHeaderReadError::BlockTypeError(source) => Some(source),
186 BlockHeaderReadError::BlockSizeError(source) => Some(source),
187 BlockHeaderReadError::FoundReservedBlock => None,
188 }
189 }
190}
191
192impl ::core::fmt::Display for BlockHeaderReadError {
193 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> ::core::fmt::Result {
194 match self {
195 BlockHeaderReadError::ReadError(_) => write!(f, "Error while reading the block header"),
196 BlockHeaderReadError::FoundReservedBlock => write!(
197 f,
198 "Reserved block occured. This is considered corruption by the documentation"
199 ),
200 BlockHeaderReadError::BlockTypeError(e) => write!(f, "Error getting block type: {}", e),
201 BlockHeaderReadError::BlockSizeError(e) => {
202 write!(f, "Error getting block content size: {}", e)
203 }
204 }
205 }
206}
207
208impl From<Error> for BlockHeaderReadError {
209 fn from(val: Error) -> Self {
210 Self::ReadError(val)
211 }
212}
213
214impl From<BlockTypeError> for BlockHeaderReadError {
215 fn from(val: BlockTypeError) -> Self {
216 Self::BlockTypeError(val)
217 }
218}
219
220impl From<BlockSizeError> for BlockHeaderReadError {
221 fn from(val: BlockSizeError) -> Self {
222 Self::BlockSizeError(val)
223 }
224}
225
226#[derive(Debug)]
227#[non_exhaustive]
228pub enum BlockTypeError {
229 InvalidBlocktypeNumber { num: u8 },
230}
231
232#[cfg(feature = "std")]
233impl std::error::Error for BlockTypeError {}
234
235impl core::fmt::Display for BlockTypeError {
236 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
237 match self {
238 BlockTypeError::InvalidBlocktypeNumber { num } => {
239 write!(f,
240 "Invalid Blocktype number. Is: {} Should be one of: 0, 1, 2, 3 (3 is reserved though",
241 num,
242 )
243 }
244 }
245 }
246}
247
248#[derive(Debug)]
249#[non_exhaustive]
250pub enum BlockSizeError {
251 BlockSizeTooLarge { size: u32 },
252}
253
254#[cfg(feature = "std")]
255impl std::error::Error for BlockSizeError {}
256
257impl core::fmt::Display for BlockSizeError {
258 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
259 match self {
260 BlockSizeError::BlockSizeTooLarge { size } => {
261 write!(
262 f,
263 "Blocksize was bigger than the absolute maximum {} (128kb). Is: {}",
264 crate::decoding::block_decoder::ABSOLUTE_MAXIMUM_BLOCK_SIZE,
265 size,
266 )
267 }
268 }
269 }
270}
271
272#[derive(Debug)]
273#[non_exhaustive]
274pub enum DecompressBlockError {
275 BlockContentReadError(Error),
276 MalformedSectionHeader {
277 expected_len: usize,
278 remaining_bytes: usize,
279 },
280 DecompressLiteralsError(DecompressLiteralsError),
281 LiteralsSectionParseError(LiteralsSectionParseError),
282 SequencesHeaderParseError(SequencesHeaderParseError),
283 DecodeSequenceError(DecodeSequenceError),
284 ExecuteSequencesError(ExecuteSequencesError),
285}
286
287#[cfg(feature = "std")]
288impl std::error::Error for DecompressBlockError {
289 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
290 match self {
291 DecompressBlockError::BlockContentReadError(source) => Some(source),
292 DecompressBlockError::DecompressLiteralsError(source) => Some(source),
293 DecompressBlockError::LiteralsSectionParseError(source) => Some(source),
294 DecompressBlockError::SequencesHeaderParseError(source) => Some(source),
295 DecompressBlockError::DecodeSequenceError(source) => Some(source),
296 DecompressBlockError::ExecuteSequencesError(source) => Some(source),
297 _ => None,
298 }
299 }
300}
301
302impl core::fmt::Display for DecompressBlockError {
303 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
304 match self {
305 DecompressBlockError::BlockContentReadError(e) => {
306 write!(f, "Error while reading the block content: {}", e)
307 }
308 DecompressBlockError::MalformedSectionHeader {
309 expected_len,
310 remaining_bytes,
311 } => {
312 write!(f,
313 "Malformed section header. Says literals would be this long: {} but there are only {} bytes left",
314 expected_len,
315 remaining_bytes,
316 )
317 }
318 DecompressBlockError::DecompressLiteralsError(e) => write!(f, "{:?}", e),
319 DecompressBlockError::LiteralsSectionParseError(e) => write!(f, "{:?}", e),
320 DecompressBlockError::SequencesHeaderParseError(e) => write!(f, "{:?}", e),
321 DecompressBlockError::DecodeSequenceError(e) => write!(f, "{:?}", e),
322 DecompressBlockError::ExecuteSequencesError(e) => write!(f, "{:?}", e),
323 }
324 }
325}
326
327impl From<Error> for DecompressBlockError {
328 fn from(val: Error) -> Self {
329 Self::BlockContentReadError(val)
330 }
331}
332
333impl From<DecompressLiteralsError> for DecompressBlockError {
334 fn from(val: DecompressLiteralsError) -> Self {
335 Self::DecompressLiteralsError(val)
336 }
337}
338
339impl From<LiteralsSectionParseError> for DecompressBlockError {
340 fn from(val: LiteralsSectionParseError) -> Self {
341 Self::LiteralsSectionParseError(val)
342 }
343}
344
345impl From<SequencesHeaderParseError> for DecompressBlockError {
346 fn from(val: SequencesHeaderParseError) -> Self {
347 Self::SequencesHeaderParseError(val)
348 }
349}
350
351impl From<DecodeSequenceError> for DecompressBlockError {
352 fn from(val: DecodeSequenceError) -> Self {
353 Self::DecodeSequenceError(val)
354 }
355}
356
357impl From<ExecuteSequencesError> for DecompressBlockError {
358 fn from(val: ExecuteSequencesError) -> Self {
359 Self::ExecuteSequencesError(val)
360 }
361}
362
363#[derive(Debug)]
364#[non_exhaustive]
365pub enum DecodeBlockContentError {
366 DecoderStateIsFailed,
367 ExpectedHeaderOfPreviousBlock,
368 ReadError { step: BlockType, source: Error },
369 DecompressBlockError(DecompressBlockError),
370}
371
372#[cfg(feature = "std")]
373impl std::error::Error for DecodeBlockContentError {
374 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
375 match self {
376 DecodeBlockContentError::ReadError { step: _, source } => Some(source),
377 DecodeBlockContentError::DecompressBlockError(source) => Some(source),
378 _ => None,
379 }
380 }
381}
382
383impl core::fmt::Display for DecodeBlockContentError {
384 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
385 match self {
386 DecodeBlockContentError::DecoderStateIsFailed => {
387 write!(
388 f,
389 "Can't decode next block if failed along the way. Results will be nonsense",
390 )
391 }
392 DecodeBlockContentError::ExpectedHeaderOfPreviousBlock => {
393 write!(f,
394 "Can't decode next block body, while expecting to decode the header of the previous block. Results will be nonsense",
395 )
396 }
397 DecodeBlockContentError::ReadError { step, source } => {
398 write!(f, "Error while reading bytes for {}: {}", step, source,)
399 }
400 DecodeBlockContentError::DecompressBlockError(e) => write!(f, "{:?}", e),
401 }
402 }
403}
404
405impl From<DecompressBlockError> for DecodeBlockContentError {
406 fn from(val: DecompressBlockError) -> Self {
407 Self::DecompressBlockError(val)
408 }
409}
410
411#[derive(Debug)]
412#[non_exhaustive]
413pub enum DecodeBufferError {
414 NotEnoughBytesInDictionary { got: usize, need: usize },
415 OffsetTooBig { offset: usize, buf_len: usize },
416}
417
418#[cfg(feature = "std")]
419impl std::error::Error for DecodeBufferError {}
420
421impl core::fmt::Display for DecodeBufferError {
422 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
423 match self {
424 DecodeBufferError::NotEnoughBytesInDictionary { got, need } => {
425 write!(
426 f,
427 "Need {} bytes from the dictionary but it is only {} bytes long",
428 need, got,
429 )
430 }
431 DecodeBufferError::OffsetTooBig { offset, buf_len } => {
432 write!(f, "offset: {} bigger than buffer: {}", offset, buf_len,)
433 }
434 }
435 }
436}
437
438#[derive(Debug)]
439#[non_exhaustive]
440pub enum DictionaryDecodeError {
441 BadMagicNum { got: [u8; 4] },
442 FSETableError(FSETableError),
443 HuffmanTableError(HuffmanTableError),
444}
445
446#[cfg(feature = "std")]
447impl std::error::Error for DictionaryDecodeError {
448 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
449 match self {
450 DictionaryDecodeError::FSETableError(source) => Some(source),
451 DictionaryDecodeError::HuffmanTableError(source) => Some(source),
452 _ => None,
453 }
454 }
455}
456
457impl core::fmt::Display for DictionaryDecodeError {
458 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
459 match self {
460 DictionaryDecodeError::BadMagicNum { got } => {
461 write!(
462 f,
463 "Bad magic_num at start of the dictionary; Got: {:#04X?}, Expected: {:#04x?}",
464 got,
465 crate::decoding::dictionary::MAGIC_NUM,
466 )
467 }
468 DictionaryDecodeError::FSETableError(e) => write!(f, "{:?}", e),
469 DictionaryDecodeError::HuffmanTableError(e) => write!(f, "{:?}", e),
470 }
471 }
472}
473
474impl From<FSETableError> for DictionaryDecodeError {
475 fn from(val: FSETableError) -> Self {
476 Self::FSETableError(val)
477 }
478}
479
480impl From<HuffmanTableError> for DictionaryDecodeError {
481 fn from(val: HuffmanTableError) -> Self {
482 Self::HuffmanTableError(val)
483 }
484}
485
486#[derive(Debug)]
487#[non_exhaustive]
488pub enum FrameDecoderError {
489 ReadFrameHeaderError(ReadFrameHeaderError),
490 FrameHeaderError(FrameHeaderError),
491 WindowSizeTooBig { requested: u64 },
492 DictionaryDecodeError(DictionaryDecodeError),
493 FailedToReadBlockHeader(BlockHeaderReadError),
494 FailedToReadBlockBody(DecodeBlockContentError),
495 FailedToReadChecksum(Error),
496 NotYetInitialized,
497 FailedToInitialize(FrameHeaderError),
498 FailedToDrainDecodebuffer(Error),
499 FailedToSkipFrame,
500 TargetTooSmall,
501 DictNotProvided { dict_id: u32 },
502}
503
504#[cfg(feature = "std")]
505impl StdError for FrameDecoderError {
506 fn source(&self) -> Option<&(dyn StdError + 'static)> {
507 match self {
508 FrameDecoderError::ReadFrameHeaderError(source) => Some(source),
509 FrameDecoderError::FrameHeaderError(source) => Some(source),
510 FrameDecoderError::DictionaryDecodeError(source) => Some(source),
511 FrameDecoderError::FailedToReadBlockHeader(source) => Some(source),
512 FrameDecoderError::FailedToReadBlockBody(source) => Some(source),
513 FrameDecoderError::FailedToReadChecksum(source) => Some(source),
514 FrameDecoderError::FailedToInitialize(source) => Some(source),
515 FrameDecoderError::FailedToDrainDecodebuffer(source) => Some(source),
516 _ => None,
517 }
518 }
519}
520
521impl core::fmt::Display for FrameDecoderError {
522 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> ::core::fmt::Result {
523 match self {
524 FrameDecoderError::ReadFrameHeaderError(e) => {
525 write!(f, "{:?}", e)
526 }
527 FrameDecoderError::FrameHeaderError(e) => {
528 write!(f, "{:?}", e)
529 }
530 FrameDecoderError::WindowSizeTooBig { requested } => {
531 write!(
532 f,
533 "Specified window_size is too big; Requested: {}, Max: {}",
534 requested,
535 crate::decoding::frame::MAX_WINDOW_SIZE,
536 )
537 }
538 FrameDecoderError::DictionaryDecodeError(e) => {
539 write!(f, "{:?}", e)
540 }
541 FrameDecoderError::FailedToReadBlockHeader(e) => {
542 write!(f, "Failed to parse/decode block body: {}", e)
543 }
544 FrameDecoderError::FailedToReadBlockBody(e) => {
545 write!(f, "Failed to parse block header: {}", e)
546 }
547 FrameDecoderError::FailedToReadChecksum(e) => {
548 write!(f, "Failed to read checksum: {}", e)
549 }
550 FrameDecoderError::NotYetInitialized => {
551 write!(f, "Decoder must initialized or reset before using it",)
552 }
553 FrameDecoderError::FailedToInitialize(e) => {
554 write!(f, "Decoder encountered error while initializing: {}", e)
555 }
556 FrameDecoderError::FailedToDrainDecodebuffer(e) => {
557 write!(
558 f,
559 "Decoder encountered error while draining the decodebuffer: {}",
560 e,
561 )
562 }
563 FrameDecoderError::FailedToSkipFrame => {
564 write!(
565 f,
566 "Failed to skip bytes for the length given in the frame header"
567 )
568 }
569 FrameDecoderError::TargetTooSmall => {
570 write!(f, "Target must have at least as many bytes as the contentsize of the frame reports")
571 }
572 FrameDecoderError::DictNotProvided { dict_id } => {
573 write!(f, "Frame header specified dictionary id 0x{:X} that wasnt provided by add_dict() or reset_with_dict()", dict_id)
574 }
575 }
576 }
577}
578
579impl From<DictionaryDecodeError> for FrameDecoderError {
580 fn from(val: DictionaryDecodeError) -> Self {
581 Self::DictionaryDecodeError(val)
582 }
583}
584
585impl From<BlockHeaderReadError> for FrameDecoderError {
586 fn from(val: BlockHeaderReadError) -> Self {
587 Self::FailedToReadBlockHeader(val)
588 }
589}
590
591impl From<FrameHeaderError> for FrameDecoderError {
592 fn from(val: FrameHeaderError) -> Self {
593 Self::FrameHeaderError(val)
594 }
595}
596
597impl From<ReadFrameHeaderError> for FrameDecoderError {
598 fn from(val: ReadFrameHeaderError) -> Self {
599 Self::ReadFrameHeaderError(val)
600 }
601}
602
603#[derive(Debug)]
604#[non_exhaustive]
605pub enum DecompressLiteralsError {
606 MissingCompressedSize,
607 MissingNumStreams,
608 GetBitsError(GetBitsError),
609 HuffmanTableError(HuffmanTableError),
610 HuffmanDecoderError(HuffmanDecoderError),
611 UninitializedHuffmanTable,
612 MissingBytesForJumpHeader { got: usize },
613 MissingBytesForLiterals { got: usize, needed: usize },
614 ExtraPadding { skipped_bits: i32 },
615 BitstreamReadMismatch { read_til: isize, expected: isize },
616 DecodedLiteralCountMismatch { decoded: usize, expected: usize },
617}
618
619#[cfg(feature = "std")]
620impl std::error::Error for DecompressLiteralsError {
621 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
622 match self {
623 DecompressLiteralsError::GetBitsError(source) => Some(source),
624 DecompressLiteralsError::HuffmanTableError(source) => Some(source),
625 DecompressLiteralsError::HuffmanDecoderError(source) => Some(source),
626 _ => None,
627 }
628 }
629}
630impl core::fmt::Display for DecompressLiteralsError {
631 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
632 match self {
633 DecompressLiteralsError::MissingCompressedSize => {
634 write!(f,
635 "compressed size was none even though it must be set to something for compressed literals",
636 )
637 }
638 DecompressLiteralsError::MissingNumStreams => {
639 write!(f,
640 "num_streams was none even though it must be set to something (1 or 4) for compressed literals",
641 )
642 }
643 DecompressLiteralsError::GetBitsError(e) => write!(f, "{:?}", e),
644 DecompressLiteralsError::HuffmanTableError(e) => write!(f, "{:?}", e),
645 DecompressLiteralsError::HuffmanDecoderError(e) => write!(f, "{:?}", e),
646 DecompressLiteralsError::UninitializedHuffmanTable => {
647 write!(
648 f,
649 "Tried to reuse huffman table but it was never initialized",
650 )
651 }
652 DecompressLiteralsError::MissingBytesForJumpHeader { got } => {
653 write!(f, "Need 6 bytes to decode jump header, got {} bytes", got,)
654 }
655 DecompressLiteralsError::MissingBytesForLiterals { got, needed } => {
656 write!(
657 f,
658 "Need at least {} bytes to decode literals. Have: {} bytes",
659 needed, got,
660 )
661 }
662 DecompressLiteralsError::ExtraPadding { skipped_bits } => {
663 write!(f,
664 "Padding at the end of the sequence_section was more than a byte long: {} bits. Probably caused by data corruption",
665 skipped_bits,
666 )
667 }
668 DecompressLiteralsError::BitstreamReadMismatch { read_til, expected } => {
669 write!(
670 f,
671 "Bitstream was read till: {}, should have been: {}",
672 read_til, expected,
673 )
674 }
675 DecompressLiteralsError::DecodedLiteralCountMismatch { decoded, expected } => {
676 write!(
677 f,
678 "Did not decode enough literals: {}, Should have been: {}",
679 decoded, expected,
680 )
681 }
682 }
683 }
684}
685
686impl From<HuffmanDecoderError> for DecompressLiteralsError {
687 fn from(val: HuffmanDecoderError) -> Self {
688 Self::HuffmanDecoderError(val)
689 }
690}
691
692impl From<GetBitsError> for DecompressLiteralsError {
693 fn from(val: GetBitsError) -> Self {
694 Self::GetBitsError(val)
695 }
696}
697
698impl From<HuffmanTableError> for DecompressLiteralsError {
699 fn from(val: HuffmanTableError) -> Self {
700 Self::HuffmanTableError(val)
701 }
702}
703
704#[derive(Debug)]
705#[non_exhaustive]
706pub enum ExecuteSequencesError {
707 DecodebufferError(DecodeBufferError),
708 NotEnoughBytesForSequence { wanted: usize, have: usize },
709 ZeroOffset,
710}
711
712impl core::fmt::Display for ExecuteSequencesError {
713 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
714 match self {
715 ExecuteSequencesError::DecodebufferError(e) => {
716 write!(f, "{:?}", e)
717 }
718 ExecuteSequencesError::NotEnoughBytesForSequence { wanted, have } => {
719 write!(
720 f,
721 "Sequence wants to copy up to byte {}. Bytes in literalsbuffer: {}",
722 wanted, have
723 )
724 }
725 ExecuteSequencesError::ZeroOffset => {
726 write!(f, "Illegal offset: 0 found")
727 }
728 }
729 }
730}
731
732#[cfg(feature = "std")]
733impl std::error::Error for ExecuteSequencesError {
734 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
735 match self {
736 ExecuteSequencesError::DecodebufferError(source) => Some(source),
737 _ => None,
738 }
739 }
740}
741
742impl From<DecodeBufferError> for ExecuteSequencesError {
743 fn from(val: DecodeBufferError) -> Self {
744 Self::DecodebufferError(val)
745 }
746}
747
748#[derive(Debug)]
749#[non_exhaustive]
750pub enum DecodeSequenceError {
751 GetBitsError(GetBitsError),
752 FSEDecoderError(FSEDecoderError),
753 FSETableError(FSETableError),
754 ExtraPadding { skipped_bits: i32 },
755 UnsupportedOffset { offset_code: u8 },
756 ZeroOffset,
757 NotEnoughBytesForNumSequences,
758 ExtraBits { bits_remaining: isize },
759 MissingCompressionMode,
760 MissingByteForRleLlTable,
761 MissingByteForRleOfTable,
762 MissingByteForRleMlTable,
763}
764
765#[cfg(feature = "std")]
766impl std::error::Error for DecodeSequenceError {
767 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
768 match self {
769 DecodeSequenceError::GetBitsError(source) => Some(source),
770 DecodeSequenceError::FSEDecoderError(source) => Some(source),
771 DecodeSequenceError::FSETableError(source) => Some(source),
772 _ => None,
773 }
774 }
775}
776
777impl core::fmt::Display for DecodeSequenceError {
778 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
779 match self {
780 DecodeSequenceError::GetBitsError(e) => write!(f, "{:?}", e),
781 DecodeSequenceError::FSEDecoderError(e) => write!(f, "{:?}", e),
782 DecodeSequenceError::FSETableError(e) => write!(f, "{:?}", e),
783 DecodeSequenceError::ExtraPadding { skipped_bits } => {
784 write!(f,
785 "Padding at the end of the sequence_section was more than a byte long: {} bits. Probably caused by data corruption",
786 skipped_bits,
787 )
788 }
789 DecodeSequenceError::UnsupportedOffset { offset_code } => {
790 write!(
791 f,
792 "Do not support offsets bigger than 1<<32; got: {}",
793 offset_code,
794 )
795 }
796 DecodeSequenceError::ZeroOffset => write!(
797 f,
798 "Read an offset == 0. That is an illegal value for offsets"
799 ),
800 DecodeSequenceError::NotEnoughBytesForNumSequences => write!(
801 f,
802 "Bytestream did not contain enough bytes to decode num_sequences"
803 ),
804 DecodeSequenceError::ExtraBits { bits_remaining } => write!(f, "{}", bits_remaining),
805 DecodeSequenceError::MissingCompressionMode => write!(
806 f,
807 "compression modes are none but they must be set to something"
808 ),
809 DecodeSequenceError::MissingByteForRleLlTable => {
810 write!(f, "Need a byte to read for RLE ll table")
811 }
812 DecodeSequenceError::MissingByteForRleOfTable => {
813 write!(f, "Need a byte to read for RLE of table")
814 }
815 DecodeSequenceError::MissingByteForRleMlTable => {
816 write!(f, "Need a byte to read for RLE ml table")
817 }
818 }
819 }
820}
821
822impl From<GetBitsError> for DecodeSequenceError {
823 fn from(val: GetBitsError) -> Self {
824 Self::GetBitsError(val)
825 }
826}
827
828impl From<FSETableError> for DecodeSequenceError {
829 fn from(val: FSETableError) -> Self {
830 Self::FSETableError(val)
831 }
832}
833
834impl From<FSEDecoderError> for DecodeSequenceError {
835 fn from(val: FSEDecoderError) -> Self {
836 Self::FSEDecoderError(val)
837 }
838}
839
840#[derive(Debug)]
841#[non_exhaustive]
842pub enum LiteralsSectionParseError {
843 IllegalLiteralSectionType { got: u8 },
844 GetBitsError(GetBitsError),
845 NotEnoughBytes { have: usize, need: u8 },
846}
847
848#[cfg(feature = "std")]
849impl std::error::Error for LiteralsSectionParseError {
850 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
851 match self {
852 LiteralsSectionParseError::GetBitsError(source) => Some(source),
853 _ => None,
854 }
855 }
856}
857impl core::fmt::Display for LiteralsSectionParseError {
858 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
859 match self {
860 LiteralsSectionParseError::IllegalLiteralSectionType { got } => {
861 write!(
862 f,
863 "Illegal literalssectiontype. Is: {}, must be in: 0, 1, 2, 3",
864 got
865 )
866 }
867 LiteralsSectionParseError::GetBitsError(e) => write!(f, "{:?}", e),
868 LiteralsSectionParseError::NotEnoughBytes { have, need } => {
869 write!(
870 f,
871 "Not enough byte to parse the literals section header. Have: {}, Need: {}",
872 have, need,
873 )
874 }
875 }
876 }
877}
878
879impl From<GetBitsError> for LiteralsSectionParseError {
880 fn from(val: GetBitsError) -> Self {
881 Self::GetBitsError(val)
882 }
883}
884
885impl core::fmt::Display for LiteralsSectionType {
886 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
887 match self {
888 LiteralsSectionType::Compressed => write!(f, "Compressed"),
889 LiteralsSectionType::Raw => write!(f, "Raw"),
890 LiteralsSectionType::RLE => write!(f, "RLE"),
891 LiteralsSectionType::Treeless => write!(f, "Treeless"),
892 }
893 }
894}
895
896#[derive(Debug)]
897#[non_exhaustive]
898pub enum SequencesHeaderParseError {
899 NotEnoughBytes { need_at_least: u8, got: usize },
900}
901
902#[cfg(feature = "std")]
903impl std::error::Error for SequencesHeaderParseError {}
904
905impl core::fmt::Display for SequencesHeaderParseError {
906 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
907 match self {
908 SequencesHeaderParseError::NotEnoughBytes { need_at_least, got } => {
909 write!(
910 f,
911 "source must have at least {} bytes to parse header; got {} bytes",
912 need_at_least, got,
913 )
914 }
915 }
916 }
917}
918
919#[derive(Debug)]
920#[non_exhaustive]
921pub enum FSETableError {
922 AccLogIsZero,
923 AccLogTooBig {
924 got: u8,
925 max: u8,
926 },
927 GetBitsError(GetBitsError),
928 ProbabilityCounterMismatch {
929 got: u32,
930 expected_sum: u32,
931 symbol_probabilities: Vec<i32>,
932 },
933 TooManySymbols {
934 got: usize,
935 },
936}
937
938#[cfg(feature = "std")]
939impl std::error::Error for FSETableError {
940 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
941 match self {
942 FSETableError::GetBitsError(source) => Some(source),
943 _ => None,
944 }
945 }
946}
947
948impl core::fmt::Display for FSETableError {
949 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
950 match self {
951 FSETableError::AccLogIsZero => write!(f, "Acclog must be at least 1"),
952 FSETableError::AccLogTooBig { got, max } => {
953 write!(
954 f,
955 "Found FSE acc_log: {0} bigger than allowed maximum in this case: {1}",
956 got, max
957 )
958 }
959 FSETableError::GetBitsError(e) => write!(f, "{:?}", e),
960 FSETableError::ProbabilityCounterMismatch {
961 got,
962 expected_sum,
963 symbol_probabilities,
964 } => {
965 write!(f,
966 "The counter ({}) exceeded the expected sum: {}. This means an error or corrupted data \n {:?}",
967 got,
968 expected_sum,
969 symbol_probabilities,
970 )
971 }
972 FSETableError::TooManySymbols { got } => {
973 write!(
974 f,
975 "There are too many symbols in this distribution: {}. Max: 256",
976 got,
977 )
978 }
979 }
980 }
981}
982
983impl From<GetBitsError> for FSETableError {
984 fn from(val: GetBitsError) -> Self {
985 Self::GetBitsError(val)
986 }
987}
988
989#[derive(Debug)]
990#[non_exhaustive]
991pub enum FSEDecoderError {
992 GetBitsError(GetBitsError),
993 TableIsUninitialized,
994}
995
996#[cfg(feature = "std")]
997impl std::error::Error for FSEDecoderError {
998 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
999 match self {
1000 FSEDecoderError::GetBitsError(source) => Some(source),
1001 _ => None,
1002 }
1003 }
1004}
1005
1006impl core::fmt::Display for FSEDecoderError {
1007 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1008 match self {
1009 FSEDecoderError::GetBitsError(e) => write!(f, "{:?}", e),
1010 FSEDecoderError::TableIsUninitialized => {
1011 write!(f, "Tried to use an uninitialized table!")
1012 }
1013 }
1014 }
1015}
1016
1017impl From<GetBitsError> for FSEDecoderError {
1018 fn from(val: GetBitsError) -> Self {
1019 Self::GetBitsError(val)
1020 }
1021}
1022
1023#[derive(Debug)]
1024#[non_exhaustive]
1025pub enum HuffmanTableError {
1026 GetBitsError(GetBitsError),
1027 FSEDecoderError(FSEDecoderError),
1028 FSETableError(FSETableError),
1029 SourceIsEmpty,
1030 NotEnoughBytesForWeights {
1031 got_bytes: usize,
1032 expected_bytes: u8,
1033 },
1034 ExtraPadding {
1035 skipped_bits: i32,
1036 },
1037 TooManyWeights {
1038 got: usize,
1039 },
1040 MissingWeights,
1041 LeftoverIsNotAPowerOf2 {
1042 got: u32,
1043 },
1044 NotEnoughBytesToDecompressWeights {
1045 have: usize,
1046 need: usize,
1047 },
1048 FSETableUsedTooManyBytes {
1049 used: usize,
1050 available_bytes: u8,
1051 },
1052 NotEnoughBytesInSource {
1053 got: usize,
1054 need: usize,
1055 },
1056 WeightBiggerThanMaxNumBits {
1057 got: u8,
1058 },
1059 MaxBitsTooHigh {
1060 got: u8,
1061 },
1062}
1063
1064#[cfg(feature = "std")]
1065impl StdError for HuffmanTableError {
1066 fn source(&self) -> Option<&(dyn StdError + 'static)> {
1067 match self {
1068 HuffmanTableError::GetBitsError(source) => Some(source),
1069 HuffmanTableError::FSEDecoderError(source) => Some(source),
1070 HuffmanTableError::FSETableError(source) => Some(source),
1071 _ => None,
1072 }
1073 }
1074}
1075
1076impl core::fmt::Display for HuffmanTableError {
1077 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> ::core::fmt::Result {
1078 match self {
1079 HuffmanTableError::GetBitsError(e) => write!(f, "{:?}", e),
1080 HuffmanTableError::FSEDecoderError(e) => write!(f, "{:?}", e),
1081 HuffmanTableError::FSETableError(e) => write!(f, "{:?}", e),
1082 HuffmanTableError::SourceIsEmpty => write!(f, "Source needs to have at least one byte"),
1083 HuffmanTableError::NotEnoughBytesForWeights {
1084 got_bytes,
1085 expected_bytes,
1086 } => {
1087 write!(f, "Header says there should be {} bytes for the weights but there are only {} bytes in the stream",
1088 expected_bytes,
1089 got_bytes)
1090 }
1091 HuffmanTableError::ExtraPadding { skipped_bits } => {
1092 write!(f,
1093 "Padding at the end of the sequence_section was more than a byte long: {} bits. Probably caused by data corruption",
1094 skipped_bits,
1095 )
1096 }
1097 HuffmanTableError::TooManyWeights { got } => {
1098 write!(
1099 f,
1100 "More than 255 weights decoded (got {} weights). Stream is probably corrupted",
1101 got,
1102 )
1103 }
1104 HuffmanTableError::MissingWeights => {
1105 write!(f, "Can\'t build huffman table without any weights")
1106 }
1107 HuffmanTableError::LeftoverIsNotAPowerOf2 { got } => {
1108 write!(f, "Leftover must be power of two but is: {}", got)
1109 }
1110 HuffmanTableError::NotEnoughBytesToDecompressWeights { have, need } => {
1111 write!(
1112 f,
1113 "Not enough bytes in stream to decompress weights. Is: {}, Should be: {}",
1114 have, need,
1115 )
1116 }
1117 HuffmanTableError::FSETableUsedTooManyBytes {
1118 used,
1119 available_bytes,
1120 } => {
1121 write!(f,
1122 "FSE table used more bytes: {} than were meant to be used for the whole stream of huffman weights ({})",
1123 used,
1124 available_bytes,
1125 )
1126 }
1127 HuffmanTableError::NotEnoughBytesInSource { got, need } => {
1128 write!(
1129 f,
1130 "Source needs to have at least {} bytes, got: {}",
1131 need, got,
1132 )
1133 }
1134 HuffmanTableError::WeightBiggerThanMaxNumBits { got } => {
1135 write!(
1136 f,
1137 "Cant have weight: {} bigger than max_num_bits: {}",
1138 got,
1139 crate::huff0::MAX_MAX_NUM_BITS,
1140 )
1141 }
1142 HuffmanTableError::MaxBitsTooHigh { got } => {
1143 write!(
1144 f,
1145 "max_bits derived from weights is: {} should be lower than: {}",
1146 got,
1147 crate::huff0::MAX_MAX_NUM_BITS,
1148 )
1149 }
1150 }
1151 }
1152}
1153
1154impl From<GetBitsError> for HuffmanTableError {
1155 fn from(val: GetBitsError) -> Self {
1156 Self::GetBitsError(val)
1157 }
1158}
1159
1160impl From<FSEDecoderError> for HuffmanTableError {
1161 fn from(val: FSEDecoderError) -> Self {
1162 Self::FSEDecoderError(val)
1163 }
1164}
1165
1166impl From<FSETableError> for HuffmanTableError {
1167 fn from(val: FSETableError) -> Self {
1168 Self::FSETableError(val)
1169 }
1170}
1171
1172#[derive(Debug)]
1173#[non_exhaustive]
1174pub enum HuffmanDecoderError {
1175 GetBitsError(GetBitsError),
1176}
1177
1178impl core::fmt::Display for HuffmanDecoderError {
1179 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1180 match self {
1181 HuffmanDecoderError::GetBitsError(e) => write!(f, "{:?}", e),
1182 }
1183 }
1184}
1185
1186#[cfg(feature = "std")]
1187impl StdError for HuffmanDecoderError {
1188 fn source(&self) -> Option<&(dyn StdError + 'static)> {
1189 match self {
1190 HuffmanDecoderError::GetBitsError(source) => Some(source),
1191 }
1192 }
1193}
1194
1195impl From<GetBitsError> for HuffmanDecoderError {
1196 fn from(val: GetBitsError) -> Self {
1197 Self::GetBitsError(val)
1198 }
1199}