Skip to main content

neo_decompiler/error/
nef.rs

1use thiserror::Error;
2
3/// Errors returned while parsing NEF containers.
4#[derive(Debug, Error)]
5#[non_exhaustive]
6pub enum NefError {
7    /// File too short to contain a NEF header and checksum.
8    #[error("file too short to contain a NEF header")]
9    TooShort,
10
11    /// Magic bytes at the start of the file did not match the NEF format.
12    #[error("invalid magic bytes: expected {expected:?}, got {actual:?}")]
13    InvalidMagic {
14        /// Expected magic sequence.
15        expected: [u8; 4],
16        /// Actual bytes found in the input.
17        actual: [u8; 4],
18    },
19
20    /// Checksum mismatch between the stored and calculated values.
21    #[error("checksum mismatch: expected {expected:#010x}, calculated {calculated:#010x}")]
22    ChecksumMismatch {
23        /// Checksum stored in the NEF file.
24        expected: u32,
25        /// Checksum calculated from the payload.
26        calculated: u32,
27    },
28
29    /// Trailing bytes were present after the checksum.
30    #[error("unexpected trailing data after checksum (extra {extra} bytes)")]
31    TrailingData {
32        /// Number of extra bytes after the checksum.
33        extra: usize,
34    },
35
36    /// The compiler field contained invalid UTF-8.
37    #[error("compiler field is not valid UTF-8")]
38    InvalidCompiler,
39
40    /// Reserved byte fields must be zero according to the NEF spec.
41    #[error("reserved byte at offset {offset} must be zero (found {value:#04X})")]
42    ReservedByteNonZero {
43        /// Byte offset of the reserved field.
44        offset: usize,
45        /// Non-zero value that was found.
46        value: u8,
47    },
48
49    /// Reserved word fields must be zero according to the NEF spec.
50    #[error("reserved word at offset {offset} must be zero (found {value:#06X})")]
51    ReservedWordNonZero {
52        /// Byte offset of the reserved field.
53        offset: usize,
54        /// Non-zero value that was found.
55        value: u16,
56    },
57
58    /// Input ended unexpectedly while parsing.
59    #[error("unexpected end of data at offset {offset}")]
60    UnexpectedEof {
61        /// Byte offset where parsing expected more data.
62        offset: usize,
63    },
64
65    /// A method token entry was malformed.
66    #[error("invalid method token at index {index}")]
67    InvalidMethodToken {
68        /// Index of the method token entry.
69        index: usize,
70    },
71
72    /// A variable-length integer exceeded the supported range.
73    #[error("varint exceeds supported range at offset {offset}")]
74    IntegerOverflow {
75        /// Offset where the oversized integer was read.
76        offset: usize,
77    },
78
79    /// A variable-length string contained invalid UTF-8.
80    #[error("varstring contains invalid utf-8 at offset {offset}")]
81    InvalidUtf8String {
82        /// Offset where the invalid string was read.
83        offset: usize,
84    },
85
86    /// Script section cannot be empty.
87    #[error("script section cannot be empty")]
88    EmptyScript,
89
90    /// Source string exceeded the maximum supported length.
91    #[error("source string exceeds maximum length ({length} > {max})")]
92    SourceTooLong {
93        /// Actual string length.
94        length: usize,
95        /// Maximum allowed length.
96        max: usize,
97    },
98
99    /// Method token count exceeded the maximum supported value.
100    #[error("method token count exceeds maximum ({count} > {max})")]
101    TooManyMethodTokens {
102        /// Actual method token count.
103        count: usize,
104        /// Maximum allowed method token count.
105        max: usize,
106    },
107
108    /// Script section exceeded the maximum supported size.
109    #[error("script exceeds maximum size ({length} > {max})")]
110    ScriptTooLarge {
111        /// Actual script length in bytes.
112        length: usize,
113        /// Maximum allowed script length.
114        max: usize,
115    },
116
117    /// Method token name contained disallowed characters.
118    #[error("method token name {name:?} is not permitted")]
119    MethodNameInvalid {
120        /// The method name that was rejected.
121        name: String,
122    },
123
124    /// Call flags contained bits outside the allowed set.
125    #[error("method token call flags 0x{flags:02X} contain unsupported bits (allowed mask 0x{allowed:02X})")]
126    CallFlagsInvalid {
127        /// The unsupported flags value.
128        flags: u8,
129        /// Mask of allowed flag bits.
130        allowed: u8,
131    },
132
133    /// Input file exceeded the maximum supported size.
134    #[error("file size {size} exceeds maximum ({max} bytes)")]
135    FileTooLarge {
136        /// Actual file size in bytes.
137        size: u64,
138        /// Maximum allowed file size.
139        max: u64,
140    },
141}