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 integer used a longer representation than necessary.
80    #[error("varint is not canonically encoded at offset {offset}")]
81    NonCanonicalVarInt {
82        /// Offset where the overlong integer was read.
83        offset: usize,
84    },
85
86    /// A variable-length string contained invalid UTF-8.
87    #[error("varstring contains invalid utf-8 at offset {offset}")]
88    InvalidUtf8String {
89        /// Offset where the invalid string was read.
90        offset: usize,
91    },
92
93    /// Script section cannot be empty.
94    #[error("script section cannot be empty")]
95    EmptyScript,
96
97    /// Source string exceeded the maximum supported length.
98    #[error("source string exceeds maximum length ({length} > {max})")]
99    SourceTooLong {
100        /// Actual string length.
101        length: usize,
102        /// Maximum allowed length.
103        max: usize,
104    },
105
106    /// Method token count exceeded the maximum supported value.
107    #[error("method token count exceeds maximum ({count} > {max})")]
108    TooManyMethodTokens {
109        /// Actual method token count.
110        count: usize,
111        /// Maximum allowed method token count.
112        max: usize,
113    },
114
115    /// Script section exceeded the maximum supported size.
116    #[error("script exceeds maximum size ({length} > {max})")]
117    ScriptTooLarge {
118        /// Actual script length in bytes.
119        length: usize,
120        /// Maximum allowed script length.
121        max: usize,
122    },
123
124    /// Method token name contained disallowed characters.
125    #[error("method token name {name:?} is not permitted")]
126    MethodNameInvalid {
127        /// The method name that was rejected.
128        name: String,
129    },
130
131    /// Call flags contained bits outside the allowed set.
132    #[error("method token call flags 0x{flags:02X} contain unsupported bits (allowed mask 0x{allowed:02X})")]
133    CallFlagsInvalid {
134        /// The unsupported flags value.
135        flags: u8,
136        /// Mask of allowed flag bits.
137        allowed: u8,
138    },
139
140    /// Input file exceeded the maximum supported size.
141    #[error("file size {size} exceeds maximum ({max} bytes)")]
142    FileTooLarge {
143        /// Actual file size in bytes.
144        size: u64,
145        /// Maximum allowed file size.
146        max: u64,
147    },
148}