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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use core::ptr::copy_nonoverlapping;
use core::mem::transmute;
use core::mem::size_of;
use core::str;
use error;
use endian;
pub type DefaultCtx = endian::Endian;
pub const CTX: DefaultCtx = endian::NATIVE;
#[derive(Debug, Copy, Clone)]
pub struct StrCtx {
pub delimiter: u8
}
pub const NULL: StrCtx = StrCtx { delimiter: 0 };
pub const SPACE: StrCtx = StrCtx { delimiter: 0x20 };
pub const RET: StrCtx = StrCtx { delimiter: 0x0a };
pub const TAB: StrCtx = StrCtx { delimiter: 0x09 };
impl Default for StrCtx {
#[inline]
fn default() -> Self {
NULL
}
}
impl From<u8> for StrCtx {
fn from(delimiter: u8) -> Self {
StrCtx { delimiter: delimiter }
}
}
pub trait FromCtx<'a, Ctx: Copy = DefaultCtx, This: ?Sized = [u8]> where Self: 'a + Sized {
#[inline]
fn from_ctx(this: &'a This, ctx: Ctx) -> Self;
}
pub trait TryFromCtx<'a, Ctx: Copy = (usize, DefaultCtx), This: ?Sized = [u8]> where Self: 'a + Sized {
type Error;
#[inline]
fn try_from_ctx(from: &'a This, ctx: Ctx) -> Result<Self, Self::Error>;
}
pub trait IntoCtx<Ctx: Copy = DefaultCtx, This: ?Sized = [u8]>: Sized {
fn into_ctx(self, &mut This, ctx: Ctx);
}
pub trait TryIntoCtx<Ctx: Copy = (usize, DefaultCtx), This: ?Sized = [u8]>: Sized {
type Error;
fn try_into_ctx(self, &mut This, ctx: Ctx) -> Result<(), Self::Error>;
}
pub trait RefFrom<This: ?Sized = [u8], I = usize> {
type Error;
#[inline]
fn ref_from(from: &This, offset: I, count: I) -> Result<&Self, Self::Error>;
}
pub trait TryRefFromCtx<Ctx: Copy = (usize, usize, DefaultCtx), This: ?Sized = [u8]> {
type Error;
#[inline]
fn try_ref_from_ctx(from: &This, ctx: Ctx) -> Result<&Self, Self::Error>;
}
pub trait TryRefIntoCtx<Ctx: Copy = (usize, usize, DefaultCtx), This: ?Sized = [u8]>: Sized {
type Error;
fn try_ref_into_ctx(self, &mut This, ctx: Ctx) -> Result<(), Self::Error>;
}
impl<T> TryRefFromCtx<(usize, usize, super::Endian), T> for [u8] where T: AsRef<[u8]> {
type Error = error::Error;
#[inline]
fn try_ref_from_ctx(b: &T, (offset, count, _): (usize, usize, super::Endian)) -> error::Result<&[u8]> {
let b = b.as_ref();
if offset + count > b.len () {
Err(error::Error::BadRange((offset..offset+count), b.len()))
} else {
Ok(&b[offset..(offset+count)])
}
}
}
impl TryRefFromCtx for [u8] {
type Error = error::Error;
#[inline]
fn try_ref_from_ctx(b: &[u8], (offset, count, _): (usize, usize, super::Endian)) -> error::Result<&[u8]> {
if offset + count > b.len () {
Err(error::Error::BadRange((offset..offset+count), b.len()))
} else {
Ok(&b[offset..(offset+count)])
}
}
}
impl TryRefFromCtx for str {
type Error = error::Error;
#[inline]
fn try_ref_from_ctx(b: &[u8], (offset, count, _): (usize, usize, super::Endian)) -> error::Result<&str> {
if offset + count > b.len () {
Err(error::Error::BadRange((offset..offset+count), b.len()))
} else {
let bytes = &b[offset..(offset+count)];
str::from_utf8(bytes).map_err(| _err | {
error::Error::BadInput((offset..offset+count), bytes.len(), "invalid utf8")
})
}
}
}
impl<T> TryRefFromCtx<(usize, usize, super::Endian), T> for str where T: AsRef<[u8]> {
type Error = error::Error;
#[inline]
fn try_ref_from_ctx(b: &T, (offset, count, _): (usize, usize, super::Endian)) -> error::Result<&str> {
let b = b.as_ref();
if offset + count > b.len () {
Err(error::Error::BadRange((offset..offset+count), b.len()))
} else {
let bytes = &b[offset..(offset+count)];
str::from_utf8(bytes).map_err(| _err | {
error::Error::BadInput((offset..offset+count), bytes.len(), "invalid utf8")
})
}
}
}
macro_rules! signed_to_unsigned {
(i8) => {u8 };
(u8) => {u8 };
(i16) => {u16};
(u16) => {u16};
(i32) => {u32};
(u32) => {u32};
(i64) => {u64};
(u64) => {u64};
(f32) => {u32};
(f64) => {u64};
}
macro_rules! write_into {
($typ:ty, $size:expr, $n:expr, $dst:expr, $endian:expr) => ({
unsafe {
let bytes = transmute::<$typ, [u8; $size]>(if $endian.is_little() { $n.to_le() } else { $n.to_be() });
copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size);
}
});
}
macro_rules! into_ctx_impl {
($typ:tt, $size:expr, $ctx:ty) => {
impl IntoCtx for $typ {
#[inline]
fn into_ctx(self, dst: &mut [u8], le: super::Endian) {
write_into!($typ, $size, self, dst, le);
}
}
impl TryIntoCtx<(usize, $ctx)> for $typ where $typ: IntoCtx<$ctx> {
type Error = error::Error;
#[inline]
fn try_into_ctx(self, dst: &mut [u8], (offset, le): (usize, super::Endian)) -> error::Result<()> {
if offset + $size > dst.len () {
return Err(error::Error::BadRange((offset..offset+$size), dst.len()))
}
<$typ as IntoCtx<$ctx>>::into_ctx(self, &mut dst[offset..(offset+$size)], le);
Ok(())
}
}
}
}
macro_rules! from_ctx_impl {
($typ:tt, $size:expr, $ctx:ty) => {
impl<'a> FromCtx<'a, $ctx> for $typ {
#[inline]
fn from_ctx(src: &'a [u8], le: $ctx) -> Self {
let mut data: signed_to_unsigned!($typ) = 0;
unsafe {
copy_nonoverlapping(
src.as_ptr(),
&mut data as *mut signed_to_unsigned!($typ) as *mut u8,
$size);
}
(if le.is_little() { data.to_le() } else { data.to_be() }) as $typ
}
}
impl<'a> TryFromCtx<'a, (usize, $ctx)> for $typ where $typ: FromCtx<'a, $ctx> {
type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a [u8], (offset, le): (usize, $ctx)) -> error::Result<Self> {
if offset + $size > src.len () {
return Err(error::Error::BadRange((offset..offset+$size), src.len()))
}
Ok(FromCtx::from_ctx(&src[offset..(offset + $size)], le))
}
}
impl<'a, T> FromCtx<'a, $ctx, T> for $typ where T: AsRef<[u8]> {
#[inline]
fn from_ctx(src: &T, le: $ctx) -> Self {
let src = src.as_ref();
let mut data: signed_to_unsigned!($typ) = 0;
unsafe {
copy_nonoverlapping(
src.as_ptr(),
&mut data as *mut signed_to_unsigned!($typ) as *mut u8,
$size);
}
(if le.is_little() { data.to_le() } else { data.to_be() }) as $typ
}
}
impl<'a, T> TryFromCtx<'a, (usize, $ctx), T> for $typ where $typ: FromCtx<'a, $ctx, T>, T: AsRef<[u8]> {
type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a T, (offset, le): (usize, $ctx)) -> error::Result<Self> {
let src = src.as_ref();
if offset + $size > src.len () {
return Err(error::Error::BadRange((offset..offset+$size), src.len()))
}
Ok(FromCtx::from_ctx(&src[offset..(offset + $size)], le))
}
}
};
}
macro_rules! ctx_impl {
($typ:tt, $size:expr) => {
from_ctx_impl!($typ, $size, super::Endian);
};
}
ctx_impl!(u8, 1);
ctx_impl!(i8, 1);
ctx_impl!(u16, 2);
ctx_impl!(i16, 2);
ctx_impl!(u32, 4);
ctx_impl!(i32, 4);
ctx_impl!(u64, 8);
ctx_impl!(i64, 8);
macro_rules! from_ctx_float_impl {
($typ:tt, $size:expr, $ctx:ty) => {
impl<'a> FromCtx<'a, $ctx> for $typ {
#[inline]
fn from_ctx(src: &[u8], le: $ctx) -> Self {
let mut data: signed_to_unsigned!($typ) = 0;
unsafe {
copy_nonoverlapping(
src.as_ptr(),
&mut data as *mut signed_to_unsigned!($typ) as *mut u8,
$size);
transmute((if le.is_little() { data.to_le() } else { data.to_be() }))
}
}
}
impl<'a> TryFromCtx<'a, (usize, $ctx)> for $typ where $typ: FromCtx<'a, $ctx> {
type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a [u8], (offset, le): (usize, $ctx)) -> error::Result<Self> {
if offset + $size > src.len () {
return Err(error::Error::BadRange((offset..offset+$size), src.len()))
}
Ok(FromCtx::from_ctx(&src[offset..(offset + $size)], le))
}
}
}
}
from_ctx_float_impl!(f32, 4, super::Endian);
from_ctx_float_impl!(f64, 8, super::Endian);
into_ctx_impl!(u8, 1, super::Endian);
into_ctx_impl!(i8, 1, super::Endian);
into_ctx_impl!(u16, 2, super::Endian);
into_ctx_impl!(i16, 2, super::Endian);
into_ctx_impl!(u32, 4, super::Endian);
into_ctx_impl!(i32, 4, super::Endian);
into_ctx_impl!(u64, 8, super::Endian);
into_ctx_impl!(i64, 8, super::Endian);
macro_rules! into_ctx_float_impl {
($typ:tt, $size:expr, $ctx:ty) => {
impl IntoCtx for $typ {
#[inline]
fn into_ctx(self, dst: &mut [u8], le: super::Endian) {
write_into!(signed_to_unsigned!($typ), $size, transmute::<$typ, signed_to_unsigned!($typ)>(self), dst, le);
}
}
impl TryIntoCtx<(usize, $ctx)> for $typ where $typ: IntoCtx<$ctx> {
type Error = error::Error;
#[inline]
fn try_into_ctx(self, dst: &mut [u8], (offset, le): (usize, super::Endian)) -> error::Result<()> {
if offset + $size > dst.len () {
return Err(error::Error::BadRange((offset..offset+$size), dst.len()))
}
<$typ as IntoCtx<$ctx>>::into_ctx(self, &mut dst[offset..(offset+$size)], le);
Ok(())
}
}
}
}
into_ctx_float_impl!(f32, 4, super::Endian);
into_ctx_float_impl!(f64, 8, super::Endian);
#[inline(always)]
fn get_str_delimiter_offset(bytes: &[u8], idx: usize, delimiter: u8) -> usize {
let len = bytes.len();
let mut i = idx;
let mut byte = bytes[i];
if byte == delimiter {
return i;
}
while byte != delimiter && i < len {
byte = bytes[i];
i += 1;
}
if i < len || bytes[i - 1] == delimiter {
i -= 1;
}
i
}
impl<'a> TryFromCtx<'a, (usize, StrCtx)> for &'a str {
type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a [u8], (offset, StrCtx {delimiter}): (usize, StrCtx)) -> error::Result<Self> {
let len = src.len();
if offset >= len {
return Err(error::Error::BadOffset(offset))
}
let delimiter_offset = get_str_delimiter_offset(src, offset, delimiter);
let count = delimiter_offset - offset;
if count == 0 { return Ok("") }
let bytes = &src[offset..(offset+count)];
str::from_utf8(bytes).map_err(| _err | {
error::Error::BadInput((offset..offset+count), bytes.len(), "invalid utf8")
})
}
}
impl<'a, T> TryFromCtx<'a, (usize, StrCtx), T> for &'a str where T: AsRef<[u8]> {
type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a T, ctx: (usize, StrCtx)) -> error::Result<Self> {
let src = src.as_ref();
TryFromCtx::try_from_ctx(src, ctx)
}
}
impl<'a> TryIntoCtx<(usize, DefaultCtx)> for &'a [u8] {
type Error = error::Error;
#[inline]
fn try_into_ctx(self, dst: &mut [u8], (uoffset, _): (usize, DefaultCtx)) -> error::Result<()> {
let src_len = self.len() as isize;
let dst_len = dst.len() as isize;
let offset = uoffset as isize;
if offset + src_len > dst_len {
return Err(error::Error::BadRange((uoffset..uoffset+self.len()), dst.len()))
}
unsafe { copy_nonoverlapping(self.as_ptr(), dst.as_mut_ptr().offset(offset as isize), src_len as usize) };
Ok(())
}
}
impl<'a> TryIntoCtx<(usize, StrCtx)> for &'a str {
type Error = error::Error;
#[inline]
fn try_into_ctx(self, dst: &mut [u8], (offset, _): (usize, StrCtx)) -> error::Result<()> {
let bytes = self.as_bytes();
TryIntoCtx::try_into_ctx(bytes, dst, (offset, CTX))
}
}
pub trait SizeWith<Ctx = DefaultCtx> {
type Units;
#[inline]
fn size_with(ctx: &Ctx) -> Self::Units;
}
macro_rules! sizeof_impl {
($ty:ty) => {
impl SizeWith for $ty {
type Units = usize;
#[inline]
fn size_with(_ctx: &DefaultCtx) -> usize {
size_of::<$ty>()
}
}
}
}
sizeof_impl!(u8);
sizeof_impl!(i8);
sizeof_impl!(u16);
sizeof_impl!(i16);
sizeof_impl!(u32);
sizeof_impl!(i32);
sizeof_impl!(u64);
sizeof_impl!(i64);
sizeof_impl!(f32);
sizeof_impl!(f64);