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
use rust_crypto::symmetriccipher::SymmetricCipherError;
use std::error;
use std::fmt;
use std::io;
use xml::reader as xmlreader;
use xml::writer as xmlwriter;
#[derive(Debug)]
pub enum Error {
CryptoError(SymmetricCipherError),
InvalidBlockHash,
InvalidBlockId(u32),
InvalidDbSignature([u8; 4]),
InvalidFinalBlockHash([u8; 32]),
InvalidHeaderHash,
InvalidHeaderSize {
id: u8,
expected: u16,
actual: u16,
},
InvalidKey,
InvalidKeyFile,
Io(io::Error),
MissingHeader(u8),
UnhandledCompression(u32),
UnhandledDbType([u8; 4]),
UnhandledHeader(u8),
UnhandledMasterCipher([u8; 16]),
UnhandledStreamCipher(u32),
Unimplemented(String),
XmlError(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::CryptoError(err) => {
match err {
SymmetricCipherError::InvalidLength => {
write!(f, "Crypto error: invalid length.")
}
SymmetricCipherError::InvalidPadding => {
write!(f, "Crypto error: invalid padding.")
}
}
}
Error::InvalidBlockHash => write!(f, "Invalid block hash"),
Error::InvalidBlockId(val) => write!(f, "Invalid block id: {}", val),
Error::InvalidDbSignature(val) => write!(f, "Invalid database signature: {:?}", val),
Error::InvalidFinalBlockHash(val) => write!(f, "Invalid final block hash: {:?}", val),
Error::InvalidHeaderSize {
id,
expected,
actual,
} => {
write!(
f,
"Invalid header size: id: {}, expected: {}, actual: {}",
id,
expected,
actual
)
}
Error::InvalidHeaderHash => write!(f, "Invalid header hash"),
Error::InvalidKey => write!(f, "Invalid key"),
Error::InvalidKeyFile => write!(f, "Invalid key file"),
Error::Io(ref err) => write!(f, "IO error: {}", err),
Error::MissingHeader(val) => write!(f, "Missing header: {}", val),
Error::UnhandledCompression(val) => write!(f, "Unhandled compression: {}", val),
Error::UnhandledDbType(val) => write!(f, "Unhandled database type: {:?}", val),
Error::UnhandledHeader(val) => write!(f, "Unhandled header: {}", val),
Error::UnhandledMasterCipher(val) => write!(f, "Unhandled master cipher: {:?}", val),
Error::UnhandledStreamCipher(val) => write!(f, "Unhandled stream cipher: {}", val),
Error::Unimplemented(ref val) => write!(f, "Unimplemented: {}", val),
Error::XmlError(ref val) => write!(f, "XML error: {}", val),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::CryptoError(_) => "crypto error",
Error::InvalidBlockHash => "invalid block hash",
Error::InvalidBlockId(_) => "invalid block id",
Error::InvalidDbSignature(_) => "invalid database signature",
Error::InvalidFinalBlockHash(_) => "invalid final block hash",
Error::InvalidHeaderSize { .. } => "invalid header size",
Error::InvalidHeaderHash => "invalid header hash",
Error::InvalidKey => "invalid key",
Error::InvalidKeyFile => "invalid key file",
Error::Io(ref err) => err.description(),
Error::MissingHeader(_) => "missing header",
Error::UnhandledCompression(_) => "unhandled compression",
Error::UnhandledDbType(_) => "unhandled database type",
Error::UnhandledHeader(_) => "unhandled header",
Error::UnhandledMasterCipher(_) => "unhandled master cipher",
Error::UnhandledStreamCipher(_) => "unhandled stream cipher",
Error::Unimplemented(_) => "unimplemented",
Error::XmlError(_) => "xml error",
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::Io(ref err) => Some(err),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::Io(err)
}
}
impl From<xmlreader::Error> for Error {
fn from(err: xmlreader::Error) -> Error {
Error::XmlError(format!("{}", err))
}
}
impl From<xmlwriter::Error> for Error {
fn from(err: xmlwriter::Error) -> Error {
Error::XmlError(format!("{}", err))
}
}
impl From<SymmetricCipherError> for Error {
fn from(err: SymmetricCipherError) -> Error {
Error::CryptoError(err)
}
}