thruput-xdrs 0.17.1

XDR Serialization Library
Documentation
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
//! Provide a generic CXS implementation for any type
//! that implements some basic byte operations

#![allow(clippy::upper_case_acronyms)]

use super::limits::*;
use crate::serialize::writer::*;
use crate::serialize::writer_unchecked::*;

const V_MULTIBYTE: u8 = 63;
const S_8BYTE: u8 = 3 << 6;
const S_4BYTE: u8 = 2 << 6;
const S_2BYTE: u8 = 1 << 6;
const S_1BYTE: u8 = 0 << 6;
const S_HEADER: u8 = 0 << 6;

/// Implement these basic byte operations in order
/// to get a `Writer<CXS>` implementation for free
pub(crate) trait Put {
    // Required:
    fn put_byte(&mut self, b: u8) -> WriteResult<()>;
    fn put_bytes(&mut self, bytes: &[u8]) -> WriteResult<()>;
}

pub struct CXS;

impl<P> Writer<CXS> for P
where
    P: Put,
{
    #[inline]
    fn write_opaque_fixed(&mut self, bytes: &[u8]) -> WriteResult<()> {
        debug_assert!(bytes.len() <= u32::MAX as usize);
        self.put_bytes(bytes)
    }

    #[inline]
    fn write_opaque_unpadded(&mut self, bytes: &[u8]) -> WriteResult<()> {
        self.put_bytes(bytes)
    }

    // CXS does not pad opaques.
    #[inline]
    fn write_opaque_padding(&mut self, _len: usize) -> WriteResult<()> {
        Ok(())
    }

    // inline here slows down benchmarks
    fn write_opaque_variable(&mut self, bytes: &[u8]) -> WriteResult<()> {
        if bytes.len() > u32::MAX as usize {
            return Err(WriteError::InvalidArrayLength);
        }

        self.write_u32(bytes.len() as u32)?;
        self.put_bytes(bytes)
    }

    #[inline]
    fn write_bool(&mut self, v: bool) -> WriteResult<()> {
        self.write_i32(if v { 1 } else { 0 })
    }

    #[inline]
    fn write_i64(&mut self, n: i64) -> WriteResult<()> {
        if n > i32::MAX as i64 || n < i32::MIN as i64 {
            // Use the 8-byte representation
            let h = V_MULTIBYTE | S_8BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]];
            self.put_bytes(&combined)
        } else {
            self.write_i32(n as i32)
        }
    }

    #[inline]
    fn write_i32(&mut self, n: i32) -> WriteResult<()> {
        if n > i16::MAX as i32 || n < i16::MIN as i32 {
            // use 4-byte repr
            let h = V_MULTIBYTE | S_4BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1], b[2], b[3]];
            self.put_bytes(&combined)
        } else {
            self.write_i16(n as i16)
        }
    }

    #[inline]
    fn write_i16(&mut self, n: i16) -> WriteResult<()> {
        if n > i8::MAX as i16 || n < i8::MIN as i16 {
            // use 2-byte repr
            let h = V_MULTIBYTE | S_2BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1]];
            self.put_bytes(&combined)
        } else {
            write_i8(self, n as i8)
        }
    }

    #[inline]
    fn write_u64(&mut self, n: u64) -> WriteResult<()> {
        if n > u32::MAX as u64 {
            // Use 8-byte representation
            let h = V_MULTIBYTE | S_8BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]];
            self.put_bytes(&combined)
        } else {
            self.write_u32(n as u32)
        }
    }

    #[inline]
    fn write_u32(&mut self, n: u32) -> WriteResult<()> {
        if n > u16::MAX as u32 {
            // Use 4-byte representation
            let h = V_MULTIBYTE | S_4BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1], b[2], b[3]];
            self.put_bytes(&combined)
        } else {
            self.write_u16(n as u16)
        }
    }

    #[inline]
    fn write_u16(&mut self, n: u16) -> WriteResult<()> {
        if n > u8::MAX as u16 {
            // Use 2-byte representation
            let h = V_MULTIBYTE | S_2BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1]];
            self.put_bytes(&combined)
        } else {
            write_u8(self, n as u8)
        }
    }

    /// Writes a 32-bit IEEE 754 float as raw little-endian bytes.
    ///
    /// Unlike integers, floats are not compressed with CXS variable-width
    /// encoding — the bit pattern is written as-is. Little-endian byte order
    /// follows CXS convention (not RFC 4506 big-endian).
    #[inline]
    fn write_f32(&mut self, n: f32) -> WriteResult<()> {
        let bytes: [u8; 4] = n.to_le_bytes();
        self.put_bytes(&bytes)
    }

    /// Writes a 64-bit IEEE 754 double as raw little-endian bytes.
    ///
    /// Unlike integers, floats are not compressed with CXS variable-width
    /// encoding — the bit pattern is written as-is. Little-endian byte order
    /// follows CXS convention (not RFC 4506 big-endian).
    #[inline]
    fn write_f64(&mut self, n: f64) -> WriteResult<()> {
        let bytes: [u8; 8] = n.to_le_bytes();
        self.put_bytes(&bytes)
    }
}

#[inline]
fn write_u8<P>(p: &mut P, n: u8) -> WriteResult<()>
where
    P: Put,
{
    if n > UH_MAX {
        // Use 1-additional-byte representation
        let h = V_MULTIBYTE | S_1BYTE;
        let combined = [h, n];
        p.put_bytes(&combined)
    } else {
        // Use header-byte-only representation
        p.put_byte(n)
    }
}

#[inline]
fn write_i8<P>(p: &mut P, n: i8) -> WriteResult<()>
where
    P: Put,
{
    if !(IH_MIN..=IH_MAX).contains(&n) {
        // Use the 1-byte repr
        let h = V_MULTIBYTE | S_1BYTE;
        let combined = [h, n as u8];
        p.put_bytes(&combined)
    } else {
        // Represent this small, signed number in the header-byte with 6-bit,
        // one's-complement encoding
        let nu: u8 = n.unsigned_abs();
        let v = if n >= 0 {
            nu
        } else {
            // invert and set the high bit
            (!nu & 0b01_1111) | 0b0010_0000
        };

        let s = S_HEADER;
        let h = v | s;
        p.put_byte(h)
    }
}

/// The unchecked counterpart to [`Put`]: implement these basic byte
/// operations to get a `WriterUnchecked<CXS>` implementation for free.
pub(crate) trait PutUnchecked {
    /// # Safety
    /// The underlying buffer must have space for 1 more byte.
    unsafe fn put_byte(&mut self, b: u8);

    /// # Safety
    /// The underlying buffer must have space for `bytes.len()` more bytes.
    unsafe fn put_bytes(&mut self, bytes: &[u8]);
}

impl<P> WriterUnchecked<CXS> for P
where
    P: PutUnchecked,
{
    #[inline]
    unsafe fn write_opaque_fixed(&mut self, bytes: &[u8]) {
        debug_assert!(bytes.len() <= u32::MAX as usize);
        // SAFETY: forwarded caller contract.
        unsafe { self.put_bytes(bytes) }
    }

    #[inline]
    unsafe fn write_opaque_unpadded(&mut self, bytes: &[u8]) {
        // SAFETY: forwarded caller contract.
        unsafe { self.put_bytes(bytes) }
    }

    // CXS does not pad opaques.
    #[inline]
    unsafe fn write_opaque_padding(&mut self, _len: usize) {}

    unsafe fn write_opaque_variable(&mut self, bytes: &[u8]) {
        debug_assert!(bytes.len() <= u32::MAX as usize);
        // SAFETY: forwarded caller contract.
        unsafe {
            self.write_u32(bytes.len() as u32);
            self.put_bytes(bytes);
        }
    }

    #[inline]
    unsafe fn write_bool(&mut self, v: bool) {
        // SAFETY: forwarded caller contract.
        unsafe { self.write_i32(if v { 1 } else { 0 }) }
    }

    #[inline]
    unsafe fn write_i64(&mut self, n: i64) {
        if n > i32::MAX as i64 || n < i32::MIN as i64 {
            // Use the 8-byte representation
            let h = V_MULTIBYTE | S_8BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]];
            // SAFETY: forwarded caller contract.
            unsafe { self.put_bytes(&combined) }
        } else {
            // SAFETY: forwarded caller contract.
            unsafe { self.write_i32(n as i32) }
        }
    }

    #[inline]
    unsafe fn write_i32(&mut self, n: i32) {
        if n > i16::MAX as i32 || n < i16::MIN as i32 {
            // use 4-byte repr
            let h = V_MULTIBYTE | S_4BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1], b[2], b[3]];
            // SAFETY: forwarded caller contract.
            unsafe { self.put_bytes(&combined) }
        } else {
            // SAFETY: forwarded caller contract.
            unsafe { self.write_i16(n as i16) }
        }
    }

    #[inline]
    unsafe fn write_i16(&mut self, n: i16) {
        if n > i8::MAX as i16 || n < i8::MIN as i16 {
            // use 2-byte repr
            let h = V_MULTIBYTE | S_2BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1]];
            // SAFETY: forwarded caller contract.
            unsafe { self.put_bytes(&combined) }
        } else {
            // SAFETY: forwarded caller contract.
            unsafe { write_i8_unchecked(self, n as i8) }
        }
    }

    #[inline]
    unsafe fn write_u64(&mut self, n: u64) {
        if n > u32::MAX as u64 {
            // Use 8-byte representation
            let h = V_MULTIBYTE | S_8BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]];
            // SAFETY: forwarded caller contract.
            unsafe { self.put_bytes(&combined) }
        } else {
            // SAFETY: forwarded caller contract.
            unsafe { self.write_u32(n as u32) }
        }
    }

    #[inline]
    unsafe fn write_u32(&mut self, n: u32) {
        if n > u16::MAX as u32 {
            // Use 4-byte representation
            let h = V_MULTIBYTE | S_4BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1], b[2], b[3]];
            // SAFETY: forwarded caller contract.
            unsafe { self.put_bytes(&combined) }
        } else {
            // SAFETY: forwarded caller contract.
            unsafe { self.write_u16(n as u16) }
        }
    }

    #[inline]
    unsafe fn write_u16(&mut self, n: u16) {
        if n > u8::MAX as u16 {
            // Use 2-byte representation
            let h = V_MULTIBYTE | S_2BYTE;
            let b = n.to_le_bytes();
            let combined = [h, b[0], b[1]];
            // SAFETY: forwarded caller contract.
            unsafe { self.put_bytes(&combined) }
        } else {
            // SAFETY: forwarded caller contract.
            unsafe { write_u8_unchecked(self, n as u8) }
        }
    }

    /// Writes a 32-bit IEEE 754 float as raw little-endian bytes,
    /// mirroring the checked CXS writer.
    #[inline]
    unsafe fn write_f32(&mut self, n: f32) {
        let bytes: [u8; 4] = n.to_le_bytes();
        // SAFETY: forwarded caller contract.
        unsafe { self.put_bytes(&bytes) }
    }

    /// Writes a 64-bit IEEE 754 double as raw little-endian bytes,
    /// mirroring the checked CXS writer.
    #[inline]
    unsafe fn write_f64(&mut self, n: f64) {
        let bytes: [u8; 8] = n.to_le_bytes();
        // SAFETY: forwarded caller contract.
        unsafe { self.put_bytes(&bytes) }
    }
}

/// # Safety
/// The underlying buffer must have space for the encoded form (1-2 bytes).
#[inline]
unsafe fn write_u8_unchecked<P>(p: &mut P, n: u8)
where
    P: PutUnchecked,
{
    if n > UH_MAX {
        // Use 1-additional-byte representation
        let h = V_MULTIBYTE | S_1BYTE;
        let combined = [h, n];
        // SAFETY: forwarded caller contract.
        unsafe { p.put_bytes(&combined) }
    } else {
        // Use header-byte-only representation
        // SAFETY: forwarded caller contract.
        unsafe { p.put_byte(n) }
    }
}

/// # Safety
/// The underlying buffer must have space for the encoded form (1-2 bytes).
#[inline]
unsafe fn write_i8_unchecked<P>(p: &mut P, n: i8)
where
    P: PutUnchecked,
{
    if !(IH_MIN..=IH_MAX).contains(&n) {
        // Use the 1-byte repr
        let h = V_MULTIBYTE | S_1BYTE;
        let combined = [h, n as u8];
        // SAFETY: forwarded caller contract.
        unsafe { p.put_bytes(&combined) }
    } else {
        // Represent this small, signed number in the header-byte with 6-bit,
        // one's-complement encoding
        let nu: u8 = n.unsigned_abs();
        let v = if n >= 0 {
            nu
        } else {
            // invert and set the high bit
            (!nu & 0b01_1111) | 0b0010_0000
        };

        let s = S_HEADER;
        let h = v | s;
        // SAFETY: forwarded caller contract.
        unsafe { p.put_byte(h) }
    }
}