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