-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
104 lines (99 loc) · 3.15 KB
/
error.rs
File metadata and controls
104 lines (99 loc) · 3.15 KB
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
use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PatchError {
Truncated,
InvalidMagic,
InvalidEncoding,
InputSizeMismatch {
expected: u64,
actual: u64,
},
InputHashMismatch {
expected: u32,
actual: u32,
},
OutputHashMismatch {
expected: u32,
actual: u32,
},
PatchHashMismatch {
expected: u32,
actual: u32,
},
InputMd5Mismatch {
expected: [u8; 16],
actual: [u8; 16],
},
OutputMd5Mismatch {
expected: [u8; 16],
actual: [u8; 16],
},
OffsetOutOfRange {
offset: u64,
max: u64,
},
OutputTooLarge {
declared: u64,
max: u64,
},
NoMatchingFile,
UnsupportedFeature(&'static str),
}
fn fmt_md5(bytes: &[u8; 16]) -> String {
use core::fmt::Write;
let mut s = String::with_capacity(32);
for b in bytes {
let _ = write!(&mut s, "{b:02x}");
}
s
}
impl fmt::Display for PatchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Truncated => f.write_str("patch is truncated"),
Self::InvalidMagic => f.write_str("invalid patch magic bytes"),
Self::InvalidEncoding => f.write_str("invalid patch encoding"),
Self::InputSizeMismatch { expected, actual } => {
write!(
f,
"input ROM size mismatch: expected {expected}, got {actual}"
)
}
Self::InputHashMismatch { expected, actual } => write!(
f,
"input ROM hash mismatch: expected {expected:#010x}, got {actual:#010x}"
),
Self::OutputHashMismatch { expected, actual } => write!(
f,
"output ROM hash mismatch: expected {expected:#010x}, got {actual:#010x}"
),
Self::PatchHashMismatch { expected, actual } => write!(
f,
"patch file hash mismatch: expected {expected:#010x}, got {actual:#010x}"
),
Self::InputMd5Mismatch { expected, actual } => write!(
f,
"input ROM MD5 mismatch: expected {}, got {}",
fmt_md5(expected),
fmt_md5(actual)
),
Self::OutputMd5Mismatch { expected, actual } => write!(
f,
"output ROM MD5 mismatch: expected {}, got {}",
fmt_md5(expected),
fmt_md5(actual)
),
Self::OffsetOutOfRange { offset, max } => {
write!(f, "patch offset {offset} exceeds limit {max}")
}
Self::OutputTooLarge { declared, max } => write!(
f,
"patch declares output size {declared} which exceeds the {max}-byte safety cap"
),
Self::NoMatchingFile => f.write_str("no file in patch matches the input ROM"),
Self::UnsupportedFeature(name) => write!(f, "unsupported patch feature: {name}"),
}
}
}
impl std::error::Error for PatchError {}
pub type Result<T> = core::result::Result<T, PatchError>;