1pub use self::FromHexError::*;
16
17use std::fmt;
18use std::error;
19
20pub trait ToHex {
22 fn to_hex(&self) -> String;
25}
26
27static CHARS: &'static[u8] = b"0123456789abcdef";
28
29impl ToHex for [u8] {
30 fn to_hex(&self) -> String {
44 let mut v = Vec::with_capacity(self.len() * 2);
45 for &byte in self.iter() {
46 v.push(CHARS[(byte >> 4) as usize]);
47 v.push(CHARS[(byte & 0xf) as usize]);
48 }
49
50 unsafe {
51 String::from_utf8_unchecked(v)
52 }
53 }
54}
55
56impl<'a, T: ?Sized + ToHex> ToHex for &'a T {
57 fn to_hex(&self) -> String {
58 (**self).to_hex()
59 }
60}
61
62pub trait FromHex {
64 fn from_hex(&self) -> Result<Vec<u8>, FromHexError>;
67}
68
69#[derive(Clone, Copy)]
71pub enum FromHexError {
72 InvalidHexCharacter(char, usize),
74 InvalidHexLength,
76}
77
78impl fmt::Debug for FromHexError {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 match *self {
81 InvalidHexCharacter(ch, idx) =>
82 write!(f, "Invalid character '{}' at position {}", ch, idx),
83 InvalidHexLength => write!(f, "Invalid input length"),
84 }
85 }
86}
87
88impl error::Error for FromHexError {
89 fn description(&self) -> &str {
90 match *self {
91 InvalidHexCharacter(_, _) => "invalid character",
92 InvalidHexLength => "invalid length",
93 }
94 }
95}
96
97impl fmt::Display for FromHexError {
98 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99 fmt::Debug::fmt(&self, f)
100 }
101}
102
103impl FromHex for str {
104 fn from_hex(&self) -> Result<Vec<u8>, FromHexError> {
128 let mut b = Vec::with_capacity(self.len() / 2);
130 let mut modulus = 0;
131 let mut buf = 0;
132
133 for (idx, byte) in self.bytes().enumerate() {
134 buf <<= 4;
135
136 match byte {
137 b'A'...b'F' => buf |= byte - b'A' + 10,
138 b'a'...b'f' => buf |= byte - b'a' + 10,
139 b'0'...b'9' => buf |= byte - b'0',
140 b' '|b'\r'|b'\n'|b'\t' => {
141 buf >>= 4;
142 continue
143 }
144 _ => {
145 let ch = self[idx..].chars().next().unwrap();
146 return Err(InvalidHexCharacter(ch, idx))
147 }
148 }
149
150 modulus += 1;
151 if modulus == 2 {
152 modulus = 0;
153 b.push(buf);
154 }
155 }
156
157 match modulus {
158 0 => Ok(b.into_iter().collect()),
159 _ => Err(InvalidHexLength),
160 }
161 }
162}
163
164impl<'a, T: ?Sized + FromHex> FromHex for &'a T {
165 fn from_hex(&self) -> Result<Vec<u8>, FromHexError> {
166 (**self).from_hex()
167 }
168}
169
170#[cfg(test)]
171mod tests {
172 use hex::{FromHex, ToHex};
173
174 #[test]
175 pub fn test_to_hex() {
176 assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172");
177 }
178
179 #[test]
180 pub fn test_from_hex_okay() {
181 assert_eq!("666f6f626172".from_hex().unwrap(),
182 b"foobar");
183 assert_eq!("666F6F626172".from_hex().unwrap(),
184 b"foobar");
185 }
186
187 #[test]
188 pub fn test_from_hex_odd_len() {
189 assert!("666".from_hex().is_err());
190 assert!("66 6".from_hex().is_err());
191 }
192
193 #[test]
194 pub fn test_from_hex_invalid_char() {
195 assert!("66y6".from_hex().is_err());
196 }
197
198 #[test]
199 pub fn test_from_hex_ignores_whitespace() {
200 assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap(),
201 b"foobar");
202 }
203
204 #[test]
205 pub fn test_to_hex_all_bytes() {
206 for i in 0..256 {
207 assert_eq!([i as u8].to_hex(), format!("{:02x}", i));
208 }
209 }
210
211 #[test]
212 pub fn test_from_hex_all_bytes() {
213 for i in 0..256 {
214 let ii: &[u8] = &[i as u8];
215 assert_eq!(format!("{:02x}", i).from_hex().unwrap(),
216 ii);
217 assert_eq!(format!("{:02X}", i).from_hex().unwrap(),
218 ii);
219 }
220 }
221}