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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use std::hint::{assert_unchecked, unreachable_unchecked};
use zerocopy::byteorder;
use crate::{
ByteOrder, Error, Result, cold_path,
immutable::mark::{Cache, Mark},
};
pub unsafe fn read_unsafe<O: ByteOrder, R>(
mut current_pos: *const u8,
len: usize,
f: impl FnOnce(Vec<Mark>) -> R,
) -> Result<R> {
// Size in bytes of each primitive tag type's payload
const TAG_SIZE: [usize; 13] = [
0, // End
1, // Byte
2, // Short
4, // Int
8, // Long
4, // Float
8, // Double
1, // ByteArray (element size)
0, // String (variable)
0, // List (variable)
0, // Compound (variable)
4, // IntArray (element size)
8, // LongArray (element size)
];
#[inline(always)]
const unsafe fn tag_size(tag_id: u8) -> usize {
unsafe { assert_unchecked(tag_id < 13) };
TAG_SIZE[tag_id as usize]
}
// Special marker value for compound tags in the mark table
const COMPOUND_TAG_MARKER: u64 = 13;
// Number of bits to shift for tag type in packed representation
const TAG_TYPE_SHIFT: u64 = 60;
// Mask for extracting parent offset from packed value
const PARENT_OFFSET_MASK: u64 = (1 << 60) - 1;
macro_rules! check_bounds {
($bytes_read:expr, $len:expr) => {
if $bytes_read > $len {
cold_path();
return Err(Error::EOF);
}
};
}
let mut bytes_read: usize = 1;
let mut mark = Vec::with_capacity(len / 32);
let mut current: usize = 0;
let mut parent: usize;
// State machine labels for the parser
enum Label {
CompItemBegin, // Start parsing a compound item
ListItemBegin, // Start parsing a list item
}
let mut label: Label;
unsafe {
macro_rules! comp_begin {
() => {{
parent = current;
mark.push(Mark {
cache: Cache::default(),
});
current = mark.len() - 1;
mark.get_unchecked_mut(current).cache.general_parent_offset =
(current - parent) as u64 | (COMPOUND_TAG_MARKER << TAG_TYPE_SHIFT);
label = Label::CompItemBegin;
}};
}
macro_rules! list_begin {
() => {{
parent = current;
mark.push(Mark {
cache: Cache::default(),
});
current = mark.len() - 1;
bytes_read += 1 + 4;
check_bounds!(bytes_read, len);
let element_type = *current_pos;
let element_count =
byteorder::U32::<O>::from_bytes(*current_pos.add(1).cast()).get();
let cur = mark.get_unchecked_mut(current);
cur.cache.general_parent_offset =
((current - parent) as u64) | ((element_type as u64) << TAG_TYPE_SHIFT);
if element_type <= 6 {
let element_size = tag_size(element_type);
let total_size = element_count as usize * element_size;
bytes_read += total_size;
check_bounds!(bytes_read, len);
current_pos = current_pos.add(5 + total_size);
cur.cache.list_total_length = 0;
cur.cache.list_current_length = 0;
} else if element_type <= 12 {
current_pos = current_pos.add(5);
cur.cache.list_total_length = element_count;
cur.cache.list_current_length = 0;
} else {
cold_path();
return Err(Error::INVALID(element_type));
}
label = Label::ListItemBegin;
}};
}
check_bounds!(bytes_read, len);
let root_tag = *current_pos;
current_pos = current_pos.add(1);
if root_tag == 0 {
cold_path();
return Ok(f(mark));
}
bytes_read += 2;
check_bounds!(bytes_read, len);
let name_len = byteorder::U16::<O>::from_bytes(*current_pos.cast()).get() as usize;
bytes_read += name_len;
check_bounds!(bytes_read, len);
current_pos = current_pos.add(2 + name_len);
match root_tag {
0 => unreachable_unchecked(),
1..=6 => {
cold_path();
bytes_read += tag_size(root_tag);
check_bounds!(bytes_read, len);
if bytes_read < len {
cold_path();
return Err(Error::REMAIN(len - bytes_read));
}
return Ok(f(mark));
}
7 | 11 | 12 => {
cold_path();
bytes_read += 4;
check_bounds!(bytes_read, len);
let array_len = byteorder::U32::<O>::from_bytes(*current_pos.cast()).get() as usize;
let element_size = tag_size(root_tag);
bytes_read += array_len * element_size;
check_bounds!(bytes_read, len);
if bytes_read < len {
cold_path();
return Err(Error::REMAIN(len - bytes_read));
}
return Ok(f(mark));
}
8 => {
cold_path();
bytes_read += 2;
check_bounds!(bytes_read, len);
let str_len = byteorder::U16::<O>::from_bytes(*current_pos.cast()).get() as usize;
bytes_read += str_len;
check_bounds!(bytes_read, len);
if bytes_read < len {
cold_path();
return Err(Error::REMAIN(len - bytes_read));
}
return Ok(f(mark));
}
9 => list_begin!(),
10 => comp_begin!(),
_ => {
cold_path();
return Err(Error::INVALID(root_tag));
}
}
loop {
match label {
Label::CompItemBegin => loop {
bytes_read += 1;
check_bounds!(bytes_read, len);
let tag_id = *current_pos;
current_pos = current_pos.add(1);
if tag_id == 0 {
cold_path();
let mark_len = mark.len();
let cur = mark.get_unchecked_mut(current);
cur.store.end_pointer = current_pos;
cur.store.flat_next_mark = (mark_len - current) as u64;
if current == 0 {
cold_path();
if bytes_read < len {
cold_path();
return Err(Error::REMAIN(len - bytes_read));
}
return Ok(f(mark));
}
current = parent;
parent = parent
- ((mark.get_unchecked(parent).cache.general_parent_offset)
& PARENT_OFFSET_MASK) as usize;
if ((mark.get_unchecked(current).cache.general_parent_offset)
>> TAG_TYPE_SHIFT)
== COMPOUND_TAG_MARKER
{
label = Label::CompItemBegin;
} else {
label = Label::ListItemBegin;
};
break;
}
bytes_read += 2;
check_bounds!(bytes_read, len);
let name_len =
byteorder::U16::<O>::from_bytes(*current_pos.cast()).get() as usize;
bytes_read += name_len;
check_bounds!(bytes_read, len);
current_pos = current_pos.add(2 + name_len);
match tag_id {
1..=6 => {
let size = tag_size(tag_id);
bytes_read += size;
check_bounds!(bytes_read, len);
current_pos = current_pos.add(size);
}
7 | 11 | 12 => {
bytes_read += 4;
check_bounds!(bytes_read, len);
let array_len =
byteorder::U32::<O>::from_bytes(*current_pos.cast()).get() as usize;
let element_size = tag_size(tag_id);
let size = array_len * element_size;
bytes_read += size;
check_bounds!(bytes_read, len);
current_pos = current_pos.add(4 + size);
}
8 => {
bytes_read += 2;
check_bounds!(bytes_read, len);
let str_len =
byteorder::U16::<O>::from_bytes(*current_pos.cast()).get() as usize;
bytes_read += str_len;
check_bounds!(bytes_read, len);
current_pos = current_pos.add(2 + str_len);
}
9 => {
list_begin!();
break;
}
10 => {
comp_begin!();
break;
}
_ => {
cold_path();
return Err(Error::INVALID(tag_id));
}
}
},
Label::ListItemBegin => loop {
let cur = mark.get_unchecked_mut(current);
if cur.cache.list_current_length >= cur.cache.list_total_length {
cold_path();
let mark_len = mark.len();
let cur = mark.get_unchecked_mut(current);
cur.store.end_pointer = current_pos;
cur.store.flat_next_mark = (mark_len - current) as u64;
if current == 0 {
cold_path();
if bytes_read < len {
cold_path();
return Err(Error::REMAIN(len - bytes_read));
}
return Ok(f(mark));
}
current = parent;
parent = parent
- ((mark.get_unchecked(parent).cache.general_parent_offset)
& PARENT_OFFSET_MASK) as usize;
if ((mark.get_unchecked(current).cache.general_parent_offset)
>> TAG_TYPE_SHIFT)
== COMPOUND_TAG_MARKER
{
label = Label::CompItemBegin;
} else {
label = Label::ListItemBegin;
}
break;
}
cur.cache.list_current_length += 1;
let element_type = ((cur.cache.general_parent_offset) >> TAG_TYPE_SHIFT) as u8;
match element_type {
0..=6 => unreachable_unchecked(),
7 | 11 | 12 => {
bytes_read += 4;
check_bounds!(bytes_read, len);
let array_len =
byteorder::U32::<O>::from_bytes(*current_pos.cast()).get() as usize;
let element_size = tag_size(element_type);
let size = array_len * element_size;
bytes_read += size;
check_bounds!(bytes_read, len);
current_pos = current_pos.add(4 + size);
}
8 => {
bytes_read += 2;
check_bounds!(bytes_read, len);
let str_len =
byteorder::U16::<O>::from_bytes(*current_pos.cast()).get() as usize;
bytes_read += str_len;
check_bounds!(bytes_read, len);
current_pos = current_pos.add(2 + str_len);
}
9 => {
list_begin!();
break;
}
10 => {
comp_begin!();
break;
}
_ => {
cold_path();
return Err(Error::INVALID(element_type));
}
}
},
}
}
}
}