forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinascii.rs
More file actions
144 lines (127 loc) · 4.53 KB
/
binascii.rs
File metadata and controls
144 lines (127 loc) · 4.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
pub(crate) use decl::make_module;
#[pymodule(name = "binascii")]
mod decl {
use crate::function::OptionalArg;
use crate::obj::objbytearray::{PyByteArray, PyByteArrayRef};
use crate::obj::objbyteinner::PyBytesLike;
use crate::obj::objbytes::{PyBytes, PyBytesRef};
use crate::obj::objstr::{PyString, PyStringRef};
use crate::pyobject::{PyObjectRef, PyResult, TryFromObject, TypeProtocol};
use crate::vm::VirtualMachine;
use crc::{crc32, Hasher32};
use itertools::Itertools;
enum SerializedData {
Bytes(PyBytesRef),
Buffer(PyByteArrayRef),
Ascii(PyStringRef),
}
impl TryFromObject for SerializedData {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
match_class!(match obj {
b @ PyBytes => Ok(SerializedData::Bytes(b)),
b @ PyByteArray => Ok(SerializedData::Buffer(b)),
a @ PyString => {
if a.as_str().is_ascii() {
Ok(SerializedData::Ascii(a))
} else {
Err(vm.new_value_error(
"string argument should contain only ASCII characters".to_owned(),
))
}
}
obj => Err(vm.new_type_error(format!(
"argument should be bytes, buffer or ASCII string, not '{}'",
obj.class().name,
))),
})
}
}
impl SerializedData {
#[inline]
pub fn with_ref<R>(&self, f: impl FnOnce(&[u8]) -> R) -> R {
match self {
SerializedData::Bytes(b) => f(b.get_value()),
SerializedData::Buffer(b) => f(&b.borrow_value().elements),
SerializedData::Ascii(a) => f(a.as_str().as_bytes()),
}
}
}
fn hex_nibble(n: u8) -> u8 {
match n {
0..=9 => b'0' + n,
10..=15 => b'a' + n,
_ => unreachable!(),
}
}
#[pyfunction(name = "b2a_hex")]
#[pyfunction]
fn hexlify(data: PyBytesLike) -> Vec<u8> {
data.with_ref(|bytes| {
let mut hex = Vec::<u8>::with_capacity(bytes.len() * 2);
for b in bytes.iter() {
hex.push(hex_nibble(b >> 4));
hex.push(hex_nibble(b & 0xf));
}
hex
})
}
fn unhex_nibble(c: u8) -> Option<u8> {
match c {
b'0'..=b'9' => Some(c - b'0'),
b'a'..=b'f' => Some(c - b'a' + 10),
b'A'..=b'F' => Some(c - b'A' + 10),
_ => None,
}
}
#[pyfunction(name = "a2b_hex")]
#[pyfunction]
fn unhexlify(data: SerializedData, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
data.with_ref(|hex_bytes| {
if hex_bytes.len() % 2 != 0 {
return Err(vm.new_value_error("Odd-length string".to_owned()));
}
let mut unhex = Vec::<u8>::with_capacity(hex_bytes.len() / 2);
for (n1, n2) in hex_bytes.iter().tuples() {
if let (Some(n1), Some(n2)) = (unhex_nibble(*n1), unhex_nibble(*n2)) {
unhex.push(n1 << 4 | n2);
} else {
return Err(vm.new_value_error("Non-hexadecimal digit found".to_owned()));
}
}
Ok(unhex)
})
}
#[pyfunction]
fn crc32(data: SerializedData, value: OptionalArg<u32>, vm: &VirtualMachine) -> PyResult {
let crc = value.unwrap_or(0);
let mut digest = crc32::Digest::new_with_initial(crc32::IEEE, crc);
data.with_ref(|bytes| digest.write(&bytes));
Ok(vm.ctx.new_int(digest.sum32()))
}
#[derive(FromArgs)]
struct NewlineArg {
#[pyarg(keyword_only, default = "true")]
newline: bool,
}
/// trim a newline from the end of the bytestring, if it exists
fn trim_newline(b: &[u8]) -> &[u8] {
if b.ends_with(b"\n") {
&b[..b.len() - 1]
} else {
b
}
}
#[pyfunction]
fn a2b_base64(s: SerializedData, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
s.with_ref(|b| base64::decode(trim_newline(b)))
.map_err(|err| vm.new_value_error(format!("error decoding base64: {}", err)))
}
#[pyfunction]
fn b2a_base64(data: PyBytesLike, NewlineArg { newline }: NewlineArg) -> Vec<u8> {
let mut encoded = data.with_ref(base64::encode).into_bytes();
if newline {
encoded.push(b'\n');
}
encoded
}
}