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
use std::time::{SystemTime, UNIX_EPOCH};
use crate::impl_display_from_debug;
use crate::python_util::PythonVersion;
use crate::Str;
pub const fn get_magic_num_bytes(python_ver: u32) -> [u8; 4] {
pub const PREFIX: u32 = 0xA0D0000;
(PREFIX | python_ver).to_le_bytes()
}
pub const fn get_magic_num_from_bytes(bytes: &[u8; 4]) -> u32 {
u32::from_le_bytes([bytes[0], bytes[1], 0, 0])
}
pub const fn get_ver_from_magic_num(magic_num: u32) -> PythonVersion {
match magic_num {
3360..=3379 => PythonVersion::new(3, Some(6), Some(0)),
3390..=3394 => PythonVersion::new(3, Some(7), Some(0)),
3400..=3413 => PythonVersion::new(3, Some(8), Some(0)),
3420..=3425 => PythonVersion::new(3, Some(9), Some(0)),
3430..=3439 => PythonVersion::new(3, Some(10), Some(0)), 3495 => PythonVersion::new(3, Some(11), Some(0)),
_ => panic!("unknown magic number"),
}
}
pub fn get_timestamp_bytes() -> [u8; 4] {
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|dur| dur.as_secs() as u32)
.unwrap_or(0);
secs.to_le_bytes()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum DataTypePrefix {
Illegal = 0,
Int32 = b'i', Int64 = b'I', Float = b'f', BinFloat = b'g', Complex = b'x', BinComplex = b'y', True = b'T', False = b'F', None = b'N', StopIter = b'S', Ref = b'r',
Long = b'l', Str = b's', ShortAscii = b'z' + 0x80, ShortAsciiInterned = b'Z' + 0x80, Unicode = b'u', Interned = b't', SmallTuple = b')', Tuple = b'(', Code = b'c' + 0x80, Builtin = b'b', Nat = b'n',
}
impl_display_from_debug!(DataTypePrefix);
impl From<u8> for DataTypePrefix {
fn from(item: u8) -> Self {
match item as char {
'i' | '\u{00E9}' => Self::Int32,
'I' => Self::Int64,
'l' => Self::Long,
'f' => Self::Float,
'g' => Self::BinFloat,
'x' => Self::Complex,
'y' => Self::BinComplex,
'T' => Self::True,
'F' => Self::False,
'N' => Self::None,
'S' => Self::StopIter,
's' | '\u{00F3}' => Self::Str,
'Z' | '\u{00DA}' => Self::ShortAsciiInterned,
'z' | '\u{00FA}' => Self::ShortAscii,
'u' => Self::Unicode,
't' => Self::Interned,
'(' | '\u{00A8}' => Self::Tuple,
')' | '\u{00A9}' => Self::SmallTuple,
'c' | '\u{00E3}' => Self::Code,
'b' => Self::Builtin,
'n' => Self::Nat,
_ => Self::Illegal,
}
}
}
impl DataTypePrefix {
pub const fn is_sized(&self) -> bool {
matches!(
self,
Self::Long
| Self::Str
| Self::ShortAscii
| Self::ShortAsciiInterned
| Self::Unicode
| Self::Interned
| Self::SmallTuple
| Self::Tuple
| Self::Code
| Self::Builtin
)
}
}
pub fn strs_into_bytes(names: Vec<Str>) -> Vec<u8> {
let mut tuple = vec![];
if names.len() > u8::MAX as usize {
tuple.push(DataTypePrefix::Tuple as u8);
tuple.append(&mut (names.len() as u32).to_le_bytes().to_vec());
} else {
tuple.push(DataTypePrefix::SmallTuple as u8);
tuple.push(names.len() as u8);
}
for name in names.into_iter() {
tuple.append(&mut str_into_bytes(name, true));
}
tuple
}
pub fn str_into_bytes(cont: Str, is_interned: bool) -> Vec<u8> {
let mut bytes = vec![];
if cont.is_ascii() {
if is_interned {
bytes.push(DataTypePrefix::ShortAsciiInterned as u8);
} else {
bytes.push(DataTypePrefix::ShortAscii as u8);
}
bytes.push(cont.len() as u8);
} else {
bytes.push(DataTypePrefix::Unicode as u8);
bytes.append(&mut (cont.len() as u32).to_le_bytes().to_vec());
};
bytes.append(&mut cont.as_bytes().to_vec());
bytes
}
pub fn raw_string_into_bytes(mut cont: Vec<u8>) -> Vec<u8> {
let mut tuple = vec![DataTypePrefix::Str as u8];
tuple.append(&mut (cont.len() as u32).to_le_bytes().to_vec());
tuple.append(&mut cont);
tuple
}