-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathhex.rs
More file actions
245 lines (215 loc) · 6.53 KB
/
hex.rs
File metadata and controls
245 lines (215 loc) · 6.53 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
pub use hex::FromHexError;
use std::ops::Deref;
use tw_memory::Data;
pub type FromHexResult<T> = Result<T, FromHexError>;
pub trait ToHex {
fn to_hex(&self) -> String;
fn to_hex_prefixed(&self) -> String;
}
impl<T> ToHex for T
where
T: AsRef<[u8]>,
{
fn to_hex(&self) -> String {
encode(self, false)
}
fn to_hex_prefixed(&self) -> String {
encode(self, true)
}
}
pub trait DecodeHex {
fn decode_hex(&self) -> FromHexResult<Data>;
}
impl<T> DecodeHex for T
where
T: Deref<Target = str>,
{
fn decode_hex(&self) -> FromHexResult<Data> {
decode(self.deref())
}
}
/// Decodes the given hexadecimal string.
pub fn decode(data: &str) -> FromHexResult<Data> {
let hex_string = data.trim_start_matches("0x");
hex::decode(hex_string)
}
/// Decodes the given hexadecimal string leniently allowing to pass odd number of chars.
/// For example, `0x0` is extended to `0x00`, `0x123` is extended to `0x0123`.
pub fn decode_lenient(data: &str) -> FromHexResult<Data> {
let hex_string = data.trim_start_matches("0x");
if hex_string.len() % 2 == 0 {
hex::decode(hex_string)
} else {
// Insert a leading 0.
let standard_hex = format!("0{hex_string}");
hex::decode(standard_hex)
}
}
pub fn encode<T: AsRef<[u8]>>(data: T, prefixed: bool) -> String {
let encoded = hex::encode(data.as_ref());
if prefixed {
return format!("0x{encoded}");
}
encoded
}
pub fn hex_to_bits(str: &str) -> FromHexResult<String> {
let hex_str = str.trim_start_matches("0x");
let mut bits = String::new();
for (index, c) in hex_str.chars().enumerate() {
let val = c
.to_digit(16)
.ok_or(FromHexError::InvalidHexCharacter { c, index })?;
bits.push_str(&format!("{:04b}", val));
}
Ok(bits)
}
pub fn data_to_bits(data: &[u8]) -> FromHexResult<String> {
let hex_str = encode(data, false);
hex_to_bits(&hex_str)
}
pub fn bits_to_u32(bits: &str, from: usize, to: usize) -> FromHexResult<u32> {
u32::from_str_radix(&bits[from..to], 2)
.map_err(|_| FromHexError::InvalidHexCharacter { c: '0', index: 0 })
}
pub mod as_hex {
use super::*;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
/// Helps to serialize certain types as hex.
/// For example,
/// ```rust
/// # use serde::Serialize;
/// # use tw_encoding::hex::as_hex::AsHex;
///
/// #[derive(Serialize)]
/// struct Foo {
/// // Compile error (as_hex doesn't work for optional):
/// // #[serde(with = "tw_encoding::hex::as_hex")]
/// // data: Option<Vec<u8>>,
/// data: Option<AsHex<Vec<u8>>>,
/// }
/// ```
///
/// Consider using `#[serde(with = "as_hex")]` when possible.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AsHex<T>(pub T);
impl<T: ToHex> Serialize for AsHex<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serialize(&self.0, serializer)
}
}
impl<'de, T, E> Deserialize<'de> for AsHex<T>
where
T: for<'a> TryFrom<&'a [u8], Error = E>,
E: fmt::Debug,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize(deserializer).map(AsHex)
}
}
/// Serializes the `value` as a hex.
pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: ToHex,
S: Serializer,
{
value.to_hex().serialize(serializer)
}
pub fn deserialize<'de, D, T, E>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: for<'a> TryFrom<&'a [u8], Error = E>,
E: fmt::Debug,
{
let s = String::deserialize(deserializer)?;
let data = decode(&s).map_err(|e| Error::custom(format!("{e:?}")))?;
T::try_from(&data).map_err(|e| Error::custom(format!("Error parsing from bytes: {e:?}")))
}
}
pub mod as_hex_prefixed {
use crate::hex::encode;
use serde::{Deserializer, Serialize, Serializer};
use std::fmt;
/// Serializes the `value` as a `0x` prefixed hex.
pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: AsRef<[u8]>,
S: Serializer,
{
encode(value, true).serialize(serializer)
}
pub fn deserialize<'de, D, T, E>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: for<'a> TryFrom<&'a [u8], Error = E>,
E: fmt::Debug,
{
// `as_hex::deserialize` handles the prefix already.
super::as_hex::deserialize(deserializer)
}
}
pub mod u8_as_hex {
use super::*;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(value: &u8, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let hex_str = encode([*value], true);
serializer.serialize_str(&hex_str)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<u8, D::Error>
where
D: Deserializer<'de>,
{
let hex_str = String::deserialize(deserializer)?;
let bytes = decode(&hex_str).map_err(serde::de::Error::custom)?;
bytes
.first()
.copied()
.ok_or_else(|| serde::de::Error::custom("Expected single byte but got empty bytes"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_vec_encode_prefixed() {
let v: &[u8] = &[
45, 181, 0, 172, 145, 156, 221, 227, 81, 172, 54, 227, 113, 29, 131, 44, 109, 185, 118,
105,
];
assert_eq!(
encode(v, true),
"0x2db500ac919cdde351ac36e3711d832c6db97669"
);
}
#[test]
fn test_vec_decode_prefixed() {
let expected = vec![
45, 181, 0, 172, 145, 156, 221, 227, 81, 172, 54, 227, 113, 29, 131, 44, 109, 185, 118,
105,
];
assert_eq!(
decode("0x2db500ac919cdde351ac36e3711d832c6db97669"),
Ok(expected)
);
}
#[test]
fn test_encode_bits() {
assert_eq!(
hex_to_bits("0x2db500ac919cdde351ac36e3711d832c6db97669").unwrap(),
"0010110110110101000000001010110010010001100111001101110111100011010100011010110000110110111000110111000100011101100000110010110001101101101110010111011001101001"
);
}
}