2014-01-30 19:29:35 +01:00
|
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-08-04 14:42:39 -04:00
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
|
//! Hex binary-to-text encoding
|
2014-11-06 00:05:53 -08:00
|
|
|
|
|
|
|
|
|
pub use self::FromHexError::*;
|
|
|
|
|
|
2014-02-19 18:56:33 -08:00
|
|
|
|
use std::fmt;
|
2014-10-03 14:24:49 -07:00
|
|
|
|
use std::error;
|
2013-08-04 14:42:39 -04:00
|
|
|
|
|
|
|
|
|
/// A trait for converting a value to hexadecimal encoding
|
2015-01-04 21:39:02 -05:00
|
|
|
|
pub trait ToHex {
|
2013-08-04 14:42:39 -04:00
|
|
|
|
/// Converts the value of `self` to a hex value, returning the owned
|
|
|
|
|
/// string.
|
2014-05-22 16:57:53 -07:00
|
|
|
|
fn to_hex(&self) -> String;
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 15:36:53 +01:00
|
|
|
|
const CHARS: &'static [u8] = b"0123456789abcdef";
|
2013-08-04 14:42:39 -04:00
|
|
|
|
|
2014-11-06 11:25:09 -05:00
|
|
|
|
impl ToHex for [u8] {
|
2014-11-24 20:06:06 -05:00
|
|
|
|
/// Turn a vector of `u8` bytes into a hexadecimal string.
|
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
|
/// # Examples
|
2014-11-24 20:06:06 -05:00
|
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
|
/// ```
|
2015-07-27 10:50:19 -04:00
|
|
|
|
/// #![feature(rustc_private)]
|
|
|
|
|
///
|
2014-11-24 20:06:06 -05:00
|
|
|
|
/// extern crate serialize;
|
|
|
|
|
/// use serialize::hex::ToHex;
|
|
|
|
|
///
|
|
|
|
|
/// fn main () {
|
|
|
|
|
/// let str = [52,32].to_hex();
|
|
|
|
|
/// println!("{}", str);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2014-05-22 16:57:53 -07:00
|
|
|
|
fn to_hex(&self) -> String {
|
2014-04-17 15:59:07 -07:00
|
|
|
|
let mut v = Vec::with_capacity(self.len() * 2);
|
2015-01-31 12:20:46 -05:00
|
|
|
|
for &byte in self {
|
2015-03-25 17:06:52 -07:00
|
|
|
|
v.push(CHARS[(byte >> 4) as usize]);
|
|
|
|
|
v.push(CHARS[(byte & 0xf) as usize]);
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-04 23:51:26 -04:00
|
|
|
|
unsafe {
|
2014-11-20 10:11:15 -08:00
|
|
|
|
String::from_utf8_unchecked(v)
|
2013-08-04 23:51:26 -04:00
|
|
|
|
}
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A trait for converting hexadecimal encoded values
|
2015-01-04 21:39:02 -05:00
|
|
|
|
pub trait FromHex {
|
2013-08-04 16:09:04 -04:00
|
|
|
|
/// Converts the value of `self`, interpreted as hexadecimal encoded data,
|
|
|
|
|
/// into an owned vector of bytes, returning the vector.
|
2014-05-03 23:34:26 -07:00
|
|
|
|
fn from_hex(&self) -> Result<Vec<u8>, FromHexError>;
|
2014-01-15 23:15:04 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Errors that can occur when decoding a hex encoded string
|
2015-03-30 09:40:52 -04:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2014-01-15 23:15:04 -08:00
|
|
|
|
pub enum FromHexError {
|
|
|
|
|
/// The input contained a character not part of the hex format
|
2015-03-25 17:06:52 -07:00
|
|
|
|
InvalidHexCharacter(char, usize),
|
2014-01-30 19:29:35 +01:00
|
|
|
|
/// The input had an invalid length
|
2014-01-15 23:15:04 -08:00
|
|
|
|
InvalidHexLength,
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
|
impl fmt::Display for FromHexError {
|
2014-02-19 18:56:33 -08:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-01-15 23:15:04 -08:00
|
|
|
|
match *self {
|
|
|
|
|
InvalidHexCharacter(ch, idx) =>
|
2014-05-10 14:05:06 -07:00
|
|
|
|
write!(f, "Invalid character '{}' at position {}", ch, idx),
|
|
|
|
|
InvalidHexLength => write!(f, "Invalid input length"),
|
2014-01-15 23:15:04 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 14:24:49 -07:00
|
|
|
|
impl error::Error for FromHexError {
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
|
match *self {
|
2016-08-26 19:23:42 +03:00
|
|
|
|
InvalidHexCharacter(..) => "invalid character",
|
2014-10-03 14:24:49 -07:00
|
|
|
|
InvalidHexLength => "invalid length",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-11-06 11:25:09 -05:00
|
|
|
|
impl FromHex for str {
|
2014-11-24 20:06:06 -05:00
|
|
|
|
/// Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
|
|
|
|
|
/// to the byte values it encodes.
|
|
|
|
|
///
|
|
|
|
|
/// You can use the `String::from_utf8` function to turn a
|
|
|
|
|
/// `Vec<u8>` into a string with characters corresponding to those values.
|
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
|
/// # Examples
|
2014-11-24 20:06:06 -05:00
|
|
|
|
///
|
|
|
|
|
/// This converts a string literal to hexadecimal and back.
|
|
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
|
/// ```
|
2015-07-27 10:50:19 -04:00
|
|
|
|
/// #![feature(rustc_private)]
|
|
|
|
|
///
|
2014-11-24 20:06:06 -05:00
|
|
|
|
/// extern crate serialize;
|
|
|
|
|
/// use serialize::hex::{FromHex, ToHex};
|
|
|
|
|
///
|
|
|
|
|
/// fn main () {
|
|
|
|
|
/// let hello_str = "Hello, World".as_bytes().to_hex();
|
|
|
|
|
/// println!("{}", hello_str);
|
2015-01-26 21:21:15 -05:00
|
|
|
|
/// let bytes = hello_str.from_hex().unwrap();
|
2014-12-20 00:09:35 -08:00
|
|
|
|
/// println!("{:?}", bytes);
|
2014-11-24 20:06:06 -05:00
|
|
|
|
/// let result_str = String::from_utf8(bytes).unwrap();
|
|
|
|
|
/// println!("{}", result_str);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2014-05-03 23:34:26 -07:00
|
|
|
|
fn from_hex(&self) -> Result<Vec<u8>, FromHexError> {
|
2013-08-04 14:42:39 -04:00
|
|
|
|
// This may be an overestimate if there is any whitespace
|
2014-04-17 15:59:07 -07:00
|
|
|
|
let mut b = Vec::with_capacity(self.len() / 2);
|
2015-01-25 22:05:03 +01:00
|
|
|
|
let mut modulus = 0;
|
2015-03-03 10:42:26 +02:00
|
|
|
|
let mut buf = 0;
|
2013-08-04 14:42:39 -04:00
|
|
|
|
|
2013-11-23 11:18:51 +01:00
|
|
|
|
for (idx, byte) in self.bytes().enumerate() {
|
2013-08-04 14:42:39 -04:00
|
|
|
|
buf <<= 4;
|
|
|
|
|
|
2014-08-06 12:23:21 -04:00
|
|
|
|
match byte {
|
2014-09-26 21:13:20 -07:00
|
|
|
|
b'A'...b'F' => buf |= byte - b'A' + 10,
|
|
|
|
|
b'a'...b'f' => buf |= byte - b'a' + 10,
|
|
|
|
|
b'0'...b'9' => buf |= byte - b'0',
|
2014-08-06 12:23:21 -04:00
|
|
|
|
b' '|b'\r'|b'\n'|b'\t' => {
|
2013-08-04 14:42:39 -04:00
|
|
|
|
buf >>= 4;
|
2013-10-01 14:31:03 -07:00
|
|
|
|
continue
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
2016-04-07 10:42:53 -07:00
|
|
|
|
_ => {
|
|
|
|
|
let ch = self[idx..].chars().next().unwrap();
|
|
|
|
|
return Err(InvalidHexCharacter(ch, idx))
|
|
|
|
|
}
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
modulus += 1;
|
|
|
|
|
if modulus == 2 {
|
|
|
|
|
modulus = 0;
|
|
|
|
|
b.push(buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match modulus {
|
2014-09-14 20:27:36 -07:00
|
|
|
|
0 => Ok(b.into_iter().collect()),
|
2014-01-15 23:15:04 -08:00
|
|
|
|
_ => Err(InvalidHexLength),
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2014-02-14 09:49:11 +08:00
|
|
|
|
extern crate test;
|
2014-04-01 09:16:35 +08:00
|
|
|
|
use self::test::Bencher;
|
2014-02-12 08:40:52 +08:00
|
|
|
|
use hex::{FromHex, ToHex};
|
2013-08-04 14:42:39 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
pub fn test_to_hex() {
|
2014-11-27 19:36:41 -05:00
|
|
|
|
assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172");
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
pub fn test_from_hex_okay() {
|
2014-11-27 14:28:51 -05:00
|
|
|
|
assert_eq!("666f6f626172".from_hex().unwrap(),
|
|
|
|
|
b"foobar");
|
|
|
|
|
assert_eq!("666F6F626172".from_hex().unwrap(),
|
|
|
|
|
b"foobar");
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
pub fn test_from_hex_odd_len() {
|
|
|
|
|
assert!("666".from_hex().is_err());
|
|
|
|
|
assert!("66 6".from_hex().is_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
pub fn test_from_hex_invalid_char() {
|
|
|
|
|
assert!("66y6".from_hex().is_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
pub fn test_from_hex_ignores_whitespace() {
|
2014-11-27 14:28:51 -05:00
|
|
|
|
assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap(),
|
|
|
|
|
b"foobar");
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
pub fn test_to_hex_all_bytes() {
|
2015-01-24 14:39:32 +00:00
|
|
|
|
for i in 0..256 {
|
2015-03-25 17:06:52 -07:00
|
|
|
|
assert_eq!([i as u8].to_hex(), format!("{:02x}", i as usize));
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
pub fn test_from_hex_all_bytes() {
|
2015-01-24 14:39:32 +00:00
|
|
|
|
for i in 0..256 {
|
2014-08-04 14:19:02 +02:00
|
|
|
|
let ii: &[u8] = &[i as u8];
|
2015-03-25 17:06:52 -07:00
|
|
|
|
assert_eq!(format!("{:02x}", i as usize).from_hex()
|
2014-11-27 14:28:51 -05:00
|
|
|
|
.unwrap(),
|
2014-08-04 14:19:02 +02:00
|
|
|
|
ii);
|
2015-03-25 17:06:52 -07:00
|
|
|
|
assert_eq!(format!("{:02X}", i as usize).from_hex()
|
2014-11-27 14:28:51 -05:00
|
|
|
|
.unwrap(),
|
2014-08-04 14:19:02 +02:00
|
|
|
|
ii);
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
|
pub fn bench_to_hex(b: &mut Bencher) {
|
2013-08-04 14:42:39 -04:00
|
|
|
|
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
|
|
|
|
|
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
|
2014-04-01 09:16:35 +08:00
|
|
|
|
b.iter(|| {
|
2013-08-05 01:25:15 -04:00
|
|
|
|
s.as_bytes().to_hex();
|
2013-11-21 19:20:48 -08:00
|
|
|
|
});
|
2014-04-01 09:16:35 +08:00
|
|
|
|
b.bytes = s.len() as u64;
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
|
pub fn bench_from_hex(b: &mut Bencher) {
|
2013-08-04 14:42:39 -04:00
|
|
|
|
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
|
|
|
|
|
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
|
2014-04-01 09:16:35 +08:00
|
|
|
|
let sb = s.as_bytes().to_hex();
|
|
|
|
|
b.iter(|| {
|
2014-11-27 14:28:51 -05:00
|
|
|
|
sb.from_hex().unwrap();
|
2013-11-21 19:20:48 -08:00
|
|
|
|
});
|
2014-04-01 09:16:35 +08:00
|
|
|
|
b.bytes = sb.len() as u64;
|
2013-08-04 14:42:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|