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