1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! Error types for AutoIt extraction.
use core::fmt;
/// Crate-wide error type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
kind: ErrorKind,
}
impl Error {
/// Creates a not-recognized error.
///
/// # Returns
///
/// An [`Error`] wrapping [`RecognitionFailure::NotRecognized`].
#[must_use]
pub const fn not_recognized() -> Self {
Self {
kind: ErrorKind::Recognition(RecognitionFailure::NotRecognized),
}
}
/// Creates an unsupported-encoding error.
///
/// # Returns
///
/// An [`Error`] wrapping [`RecognitionFailure::UnsupportedEncoding`].
#[must_use]
pub const fn unsupported_encoding() -> Self {
Self {
kind: ErrorKind::Recognition(RecognitionFailure::UnsupportedEncoding),
}
}
/// Creates a truncated-input error.
///
/// # Returns
///
/// An [`Error`] wrapping [`RecognitionFailure::Truncated`].
#[must_use]
pub const fn truncated() -> Self {
Self {
kind: ErrorKind::Recognition(RecognitionFailure::Truncated),
}
}
/// Creates a malformed-container error.
///
/// # Returns
///
/// An [`Error`] wrapping [`RecognitionFailure::MalformedContainer`].
#[must_use]
pub const fn malformed_container() -> Self {
Self {
kind: ErrorKind::Recognition(RecognitionFailure::MalformedContainer),
}
}
/// Creates a limit-exceeded error.
///
/// # Returns
///
/// An [`Error`] wrapping [`RecognitionFailure::LimitExceeded`].
#[must_use]
pub const fn limit_exceeded() -> Self {
Self {
kind: ErrorKind::Recognition(RecognitionFailure::LimitExceeded),
}
}
/// Creates a crypto-mismatch error.
///
/// # Returns
///
/// An [`Error`] wrapping [`RecognitionFailure::CryptoMismatch`].
#[must_use]
pub const fn crypto_mismatch() -> Self {
Self {
kind: ErrorKind::Recognition(RecognitionFailure::CryptoMismatch),
}
}
/// Creates a decompression error.
///
/// # Returns
///
/// An [`Error`] wrapping [`RecognitionFailure::CompressionError`].
#[must_use]
pub const fn compression_error() -> Self {
Self {
kind: ErrorKind::Recognition(RecognitionFailure::CompressionError),
}
}
/// Creates a token-decoding error.
///
/// # Returns
///
/// An [`Error`] wrapping [`RecognitionFailure::TokenError`].
#[must_use]
pub const fn token_error() -> Self {
Self {
kind: ErrorKind::Recognition(RecognitionFailure::TokenError),
}
}
/// Returns the recognition failure when this error represents one.
///
/// # Returns
///
/// `Some` with the wrapped [`RecognitionFailure`]; currently every [`Error`]
/// carries one, so this never returns `None`.
#[must_use]
pub const fn recognition_failure(&self) -> Option<RecognitionFailure> {
match self.kind {
ErrorKind::Recognition(failure) => Some(failure),
}
}
}
impl fmt::Display for Error {
/// Formats the error by delegating to its wrapped [`RecognitionFailure`].
///
/// # Arguments
///
/// * `f` - The formatter to write into.
///
/// # Returns
///
/// A [`fmt::Result`] propagating any formatter write error.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
ErrorKind::Recognition(failure) => write!(f, "{failure}"),
}
}
}
impl std::error::Error for Error {}
/// Reasons an input could not be recognized or accepted as AutoIt.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecognitionFailure {
/// No AutoIt container or payload markers were found.
NotRecognized,
/// AutoIt markers were found, but the encoding is not yet supported.
UnsupportedEncoding,
/// The outer container shape was recognized but malformed.
MalformedContainer,
/// The input ended before required AutoIt data could be read.
Truncated,
/// A configured extraction limit was exceeded.
LimitExceeded,
/// AutoIt data was recognized but a cryptographic validation step failed.
CryptoMismatch,
/// AutoIt data was recognized but decompression failed.
CompressionError,
/// AutoIt data was recognized but token decoding failed.
TokenError,
}
impl fmt::Display for RecognitionFailure {
/// Writes a human-readable description of the recognition failure.
///
/// # Arguments
///
/// * `f` - The formatter to write into.
///
/// # Returns
///
/// A [`fmt::Result`] propagating any formatter write error.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotRecognized => f.write_str("input is not recognized as AutoIt"),
Self::UnsupportedEncoding => f.write_str("AutoIt encoding is not supported"),
Self::MalformedContainer => f.write_str("AutoIt container is malformed"),
Self::Truncated => f.write_str("AutoIt input is truncated"),
Self::LimitExceeded => f.write_str("AutoIt extraction limit exceeded"),
Self::CryptoMismatch => f.write_str("AutoIt cryptographic validation failed"),
Self::CompressionError => f.write_str("AutoIt decompression failed"),
Self::TokenError => f.write_str("AutoIt token decoding failed"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ErrorKind {
Recognition(RecognitionFailure),
}