bswap 1.0.0

A Rust library for byte-swapping integers, pointers, and slices.
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
414
415
416
417
418
419
420
421
422
#![cfg(test)]
use test::Bencher;

const LOREM_IPSUM: &'static [u8] = b"\t\n\n\tSed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit, amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur?\t\n\n\tAt vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt mollitia animi, id est laborum et dolorum fuga. et harum quidem rerum facilis est et expedita distinctio. nam libero tempore, cum soluta nobis est eligendi optio, cumque nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae non recusandae. itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.\t\n\n\t";

const MUSPI_MEROL: &'static [u8] = b"\t\n\n\t.talleper seroirepsa subirolod sidnerefrep tua rutauqesnoc saila seroiam subitatpulov sidneicier tua tu ,sutceled etneipas a rutenet cih murer murae euqati .eadnasucer non eaitselom te tnis eadnaiduper setatpulov te tu ,teineve epeas subitatissecen murer tua sitibed siiciffo tua te madsubiuq metua subiropmet .sudnelleper rolod sinmo ,tse adnemussa satpulov sinmo ,sumissop erecaf ,taecalp emixam douq ,di sunim ouq ,tidepmi lihin euqmuc ,oitpo idnegile tse sibon atulos muc ,eropmet orebil man .oitcnitsid atidepxe te tse silicaf murer mediuq murah te .aguf murolod te murobal tse di ,imina aitillom tnuresed aiciffo iuq ,apluc ni tnus euqilimis ,tnedivorp non etatidipuc itaceacbo ,tnis irutpecxe saitselom sauq te serolod souq ,itpurroc euqta itineled mutatpulov muitnesearp siitidnalb iuq ,sumicud somissingid oido otsui te sumasucca te soe orev tA\t\n\n\t?rutairap allun satpulov ouq ,taiguf mue merolod iuq ,mulli lev ,rutauqesnoc eaitselom lihin mauq ,esse tilev etatpulov ae ni iuq ,tiredneherper erui mue lev metua siuq ?rutauqesnoc idommoc ae xe diuqila tu isin ,masoirobal tipicsus siroproc mallu menoitaticrexe murtson siuq ,mainev aminim da mine tu .metatpulov tareauq mauqila mangam erolod te erobal tu ,tnudicni aropmet idom suie mauqmun non aiuq des ,tilev icsipida ,rutetcesnoc ,tema ,tis rolod aiuq ,muspi merolod iuq ,tse mauqsiuq orrop euqen ,tnuicsen iuqes metatpulov enoitar iuq ,soe serolod ingam rutnuuqesnoc aiuq des ,tiguf tua tido tua rutanrepsa ,tis satpulov aiuq ,metatpulov maspi mine omen .obacilpxe ,tnus atcid eativ eataeb otcetihcra isauq te sitatirev erotnevni olli ba eauq ,aspi euqae mairepa mer matot ,muitnadual euqmerolod muitnasucca metatpulov tis rorre sutan etsi sinmo ednu ,sitaicipsrep tu deS\t\n\n\t";

const LOREM_IPSUM_160_U64_SWAP_MEMORY: &'static [u8] = b" deS\t\n\n\tpsrep tu,sitaicimo ednu etsi sine sutan tis rorratpulov ucca met muitnasqmerolodadual eut ,muitnmer matomairepa i euqae auq ,asplli ba etnevni oirev ero";

#[test]
fn test_u8_reverse_memory_u8() {
    let mut dst = [0u8; 11];
    let src: &[u8] = b"hello world";
    unsafe {
        super::u8::reverse_memory(
            (&mut dst[..]).as_mut_ptr(),
            src.as_ptr(), src.len());
    }
    assert_eq!(&dst[..], b"dlrow olleh");
}

#[test]
fn test_u8_reverse_memory_u64() {
    let mut dst = [0u8; 8];
    let src: &[u8] = b"hi world";
    unsafe {
        super::u8::reverse_memory(
            (&mut dst[..]).as_mut_ptr(),
            src.as_ptr(), src.len());
    }
    assert_eq!(&dst[..], b"dlrow ih");
}

#[test]
fn test_u8_reverse_memory_u8x16_lorem_ipsum() {
    let mut dst = [0u8; 160];
    let src: &[u8] = &LOREM_IPSUM[..160];
    unsafe {
        super::u8::reverse_memory(
            (&mut dst[..]).as_mut_ptr(),
            src.as_ptr(), src.len());
    }
    //println!("src = '{}'", String::from_utf8(src.to_vec()).unwrap().as_slice());
    //println!("dst = '{}'", String::from_utf8(dst.to_vec()).unwrap().as_slice());
    //println!("exp = '{}'", String::from_utf8((&MUSPI_MEROL[1582..]).to_vec()).unwrap().as_slice());
    assert_eq!(&dst[..], &MUSPI_MEROL[1582..]);
}

#[test]
fn test_u8_reverse_memory_lorem_ipsum() {
    let mut dst = [0u8; 1742];
    let src: &[u8] = LOREM_IPSUM;
    assert_eq!(dst.len(), LOREM_IPSUM.len());
    assert_eq!(dst.len(), MUSPI_MEROL.len());
    unsafe {
        super::u8::reverse_memory(
            (&mut dst[..]).as_mut_ptr(),
            src.as_ptr(), src.len());
    }
    assert_eq!(&dst[..], MUSPI_MEROL);
}

#[test]
fn test_u8_reverse_memory_muspi_merol() {
    let mut dst = [0u8; 1742];
    let src: &[u8] = MUSPI_MEROL;
    assert_eq!(dst.len(), LOREM_IPSUM.len());
    assert_eq!(dst.len(), MUSPI_MEROL.len());
    unsafe {
        super::u8::reverse_memory(
            (&mut dst[..]).as_mut_ptr(),
            src.as_ptr(), src.len());
    }
    assert_eq!(&dst[..], LOREM_IPSUM);
}

#[bench]
fn bench_u8_reverse_memory_lorem_ipsum(b: & mut Bencher) {
    let mut dst = [0u8; 1742];
    let src: &[u8] = LOREM_IPSUM;
    b.iter(
        || {
            unsafe {
                super::u8::reverse_memory(
                    (&mut dst[..]).as_mut_ptr(),
                    src.as_ptr(), src.len());
            }
        }
    );
    b.bytes = src.len() as u64;
}

#[bench]
fn bench_u8_reverse_memory_muspi_merol(b: & mut Bencher) {
    let mut dst = [0u8; 1742];
    let src: &[u8] = MUSPI_MEROL;
    b.iter(
        || {
            unsafe {
                super::u8::reverse_memory(
                    (&mut dst[..]).as_mut_ptr(),
                    src.as_ptr(), src.len());
            }
        }
    );
    b.bytes = src.len() as u64;
}

#[bench]
fn bench_u8_reverse_memory_ones_1000(b: & mut Bencher) {
    let mut dst = [0u8; 1000];
    let src = [1u8; 1000];
    b.iter(
        || {
            unsafe {
                super::u8::reverse_memory(
                    (&mut dst[..]).as_mut_ptr(),
                    (&src[..]).as_ptr(), src.len());
            }
        }
    );
    b.bytes = src.len() as u64;
}

#[bench]
fn bench_u8_reverse_memory_ones_1024(b: & mut Bencher) {
    let mut dst = [0u8; 1024];
    let src = [1u8; 1024];
    b.iter(
        || {
            unsafe {
                super::u8::reverse_memory(
                    (&mut dst[..]).as_mut_ptr(),
                    (&src[..]).as_ptr(), src.len());
            }
        }
    );
    b.bytes = src.len() as u64;
}

#[test]
fn test_u64_swap_memory_hi() {
    let mut dst = [0u8; 8];
    let src: &[u8] = b"hi world";
    unsafe {
        super::u64::swap_memory(
            (&mut dst[..]).as_mut_ptr(),
            src.as_ptr(), src.len()/8);
    }
    //println!("src = '{}'", String::from_utf8(src.to_vec()).unwrap().as_slice());
    //println!("dst = '{}'", String::from_utf8(dst.to_vec()).unwrap().as_slice());
    assert_eq!(&dst[..], b"dlrow ih");
}

#[test]
fn test_u64_swap_memory_inplace_hi() {
    let mut dst = [0u8; 8];
    let src: &[u8] = b"hi world";
    ::std::slice::bytes::copy_memory(&mut dst, src);
    unsafe {
        super::u64::swap_memory_inplace(
            (&mut dst[..]).as_mut_ptr(),
            dst.len()/8);
    }
    //println!("src = '{}'", String::from_utf8(src.to_vec()).unwrap().as_slice());
    //println!("dst = '{}'", String::from_utf8(dst.to_vec()).unwrap().as_slice());
    assert_eq!(&dst[..], b"dlrow ih");
}

#[test]
fn test_u64_swap_memory_pangram() {
    let mut dst = [0u8; 32];
    let src: &[u8] = b"Five boxing wizards jump quickly";
    unsafe {
        super::u64::swap_memory(
            (&mut dst[..]).as_mut_ptr(),
            src.as_ptr(), src.len()/8);
    }
    //println!("src = '{}'", String::from_utf8(src.to_vec()).unwrap().as_slice());
    //println!("dst = '{}'", String::from_utf8(dst.to_vec()).unwrap().as_slice());
    assert_eq!(&dst[..], b"xob eviFaziw gnipmuj sdrylkciuq ");
}

#[test]
fn test_u64_swap_memory_inplace_pangram() {
    let mut dst = [0u8; 32];
    let src: &[u8] = b"Five boxing wizards jump quickly";
    ::std::slice::bytes::copy_memory(&mut dst, src);
    unsafe {
        super::u64::swap_memory_inplace(
            (&mut dst[..]).as_mut_ptr(),
            dst.len()/8);
    }
    //println!("src = '{}'", String::from_utf8(src.to_vec()).unwrap().as_slice());
    //println!("dst = '{}'", String::from_utf8(dst.to_vec()).unwrap().as_slice());
    assert_eq!(&dst[..], b"xob eviFaziw gnipmuj sdrylkciuq ");
}

#[test]
fn test_u64_swap_memory_lorem_ipsum() {
    let mut dst = [0u8; 160];
    let src: &[u8] = &LOREM_IPSUM[..160];
    unsafe {
        super::u64::swap_memory(
            (&mut dst[..]).as_mut_ptr(),
            src.as_ptr(), src.len()/8);
    }
    //println!("src = '{}'", String::from_utf8(src.to_vec()).unwrap().as_slice());
    //println!("dst = '{}'", String::from_utf8(dst.to_vec()).unwrap().as_slice());
    //println!("exp = '{}'", String::from_utf8((LOREM_IPSUM_160_U64_SWAP_MEMORY).to_vec()).unwrap().as_slice());
    assert_eq!(&dst[..], LOREM_IPSUM_160_U64_SWAP_MEMORY);
}

#[test]
fn test_u64_swap_memory_inplace_lorem_ipsum() {
    let mut dst = [0u8; 160];
    let src: &[u8] = &LOREM_IPSUM[..160];
    ::std::slice::bytes::copy_memory(&mut dst, src);
    unsafe {
        super::u64::swap_memory_inplace(
            (&mut dst[..]).as_mut_ptr(),
            dst.len()/8);
    }
    //println!("src = '{}'", String::from_utf8(src.to_vec()).unwrap().as_slice());
    //println!("dst = '{}'", String::from_utf8(dst.to_vec()).unwrap().as_slice());
    //println!("exp = '{}'", String::from_utf8((LOREM_IPSUM_160_U64_SWAP_MEMORY).to_vec()).unwrap().as_slice());
    assert_eq!(&dst[..], LOREM_IPSUM_160_U64_SWAP_MEMORY);
}

//
// tests from Burntsushi/byteorder (modified)
//

fn qc_sized<T: Sized>(f: fn(n: T) -> bool, size: T) {
    assert!(f(size));
}

macro_rules! qc_byte_order {
    ($name:ident, $ty_int:ident, $ty_uint:ident, $bytes:expr, $bemod:ident, $lemod:ident) => (
        mod $name {
            use super::qc_sized;

            #[test]
            fn big_endian() {
                let max = ::std::$ty_int::MAX as u64 - 1 >> (8 * (8 - $bytes));
                fn prop(n: $ty_int) -> bool {
                    let mut buf = [0; 8];
                    //println!("big_endian({:016x})", n);
                    super::super::beusize::encode(&mut buf[8 - $bytes..], n as u64, $bytes);
                    //println!("{}", (&buf[..]).to_hex());
                    let m = super::super::beusize::decode(&mut buf[8 - $bytes..], $bytes) as $ty_int;
                    //println!("{:016x}", m);
                    n == m
                }
                qc_sized(prop as fn($ty_int) -> bool, max as $ty_int - 1);
            }

            #[test]
            fn little_endian() {
                let max = ::std::$ty_int::MAX as u64 - 1 >> (8 * (8 - $bytes));
                fn prop(n: $ty_int) -> bool {
                    let mut buf = [0; 8];
                    //println!("little_endian({:016x})", n);
                    super::super::leusize::encode(&mut buf[..$bytes], n as u64, $bytes);
                    //println!("{}", (&buf[..]).to_hex());
                    let m = super::super::leusize::decode(&mut buf[..$bytes], $bytes) as $ty_int;
                    //println!("{:016x}", m);
                    n == m
                }
                qc_sized(prop as fn($ty_int) -> bool, max as $ty_int - 1);
            }
        }
    );
    ($name:ident, $ty_int:ident, $ty_uint:ident, $bemod:ident, $lemod:ident) => (
        mod $name {
            use super::qc_sized;

            #[test]
            fn big_endian() {
                fn prop(n: $ty_int) -> bool {
                    let mut buf = [0; 8];
                    super::super::$bemod::encode(&mut buf[(8 - (::std::$ty_int::BYTES as usize))..], n as $ty_uint);
                    n == super::super::$bemod::decode(&mut buf[(8 - (::std::$ty_int::BYTES as usize))..]) as $ty_int
                }
                qc_sized(prop as fn($ty_int) -> bool,
                         ::std::$ty_int::MAX as $ty_int - 1);
            }

            #[test]
            fn little_endian() {
                fn prop(n: $ty_int) -> bool {
                    let mut buf = [0; 8];
                    super::super::$lemod::encode(&mut buf[..((::std::$ty_int::BYTES as usize))], n as $ty_uint);
                    n == super::super::$lemod::decode(&mut buf[..((::std::$ty_int::BYTES as usize))]) as $ty_int
                }
                qc_sized(prop as fn($ty_int) -> bool,
                         ::std::$ty_int::MAX as $ty_int - 1);
            }
        }
    );
}

qc_byte_order!(prop_u16, u16, u16, beu16, leu16);
qc_byte_order!(prop_i16, i16, u16, beu16, leu16);
qc_byte_order!(prop_u32, u32, u32, beu32, leu32);
qc_byte_order!(prop_i32, i32, u32, beu32, leu32);
qc_byte_order!(prop_u64, u64, u64, beu64, leu64);
qc_byte_order!(prop_i64, i64, u64, beu64, leu64);

qc_byte_order!(prop_uint_1, u64, u64, 1, beusize, leusize);
qc_byte_order!(prop_uint_2, u64, u64, 2, beusize, leusize);
qc_byte_order!(prop_uint_3, u64, u64, 3, beusize, leusize);
qc_byte_order!(prop_uint_4, u64, u64, 4, beusize, leusize);
qc_byte_order!(prop_uint_5, u64, u64, 5, beusize, leusize);
qc_byte_order!(prop_uint_6, u64, u64, 6, beusize, leusize);
qc_byte_order!(prop_uint_7, u64, u64, 7, beusize, leusize);
qc_byte_order!(prop_uint_8, u64, u64, 8, beusize, leusize);

qc_byte_order!(prop_int_1, i64, u64, 1, beusize, leusize);
qc_byte_order!(prop_int_2, i64, u64, 2, beusize, leusize);
qc_byte_order!(prop_int_3, i64, u64, 3, beusize, leusize);
qc_byte_order!(prop_int_4, i64, u64, 4, beusize, leusize);
qc_byte_order!(prop_int_5, i64, u64, 5, beusize, leusize);
qc_byte_order!(prop_int_6, i64, u64, 6, beusize, leusize);
qc_byte_order!(prop_int_7, i64, u64, 7, beusize, leusize);
qc_byte_order!(prop_int_8, i64, u64, 8, beusize, leusize);

// Test that all of the byte conversion functions panic when given a
// buffer that is too small.
//
// These tests are critical to ensure safety, otherwise we might end up
// with a buffer overflow.
macro_rules! too_small {
    ($name:ident, $maximally_small:expr, $zero:expr, $bemod:ident, $lemod:ident) => (
        mod $name {

            #[test]
            #[should_panic]
            fn read_big_endian() {
                let buf = [0; $maximally_small];
                super::super::$bemod::decode(&buf);
            }

            #[test]
            #[should_panic]
            fn read_little_endian() {
                let buf = [0; $maximally_small];
                super::super::$lemod::decode(&buf);
            }

            #[test]
            #[should_panic]
            fn write_big_endian() {
                let mut buf = [0; $maximally_small];
                super::super::$bemod::encode(&mut buf, $zero);
            }

            #[test]
            #[should_panic]
            fn write_little_endian() {
                let mut buf = [0; $maximally_small];
                super::super::$lemod::encode(&mut buf, $zero);
            }
        }
    );
    ($name:ident, $maximally_small:expr, $bemod:ident, $lemod:ident) => (
        mod $name {

            #[test]
            #[should_panic]
            fn read_big_endian() {
                let buf = [0; $maximally_small];
                super::super::$bemod::decode(&buf, $maximally_small + 1);
            }

            #[test]
            #[should_panic]
            fn read_little_endian() {
                let buf = [0; $maximally_small];
                super::super::$lemod::decode(&buf, $maximally_small + 1);
            }
        }
    );
}

too_small!(small_u16, 1, 0, beu16, leu16);
too_small!(small_i16, 1, 0, beu16, leu16);
too_small!(small_u32, 3, 0, beu32, leu32);
too_small!(small_i32, 3, 0, beu32, leu32);
too_small!(small_u64, 7, 0, beu64, leu64);
too_small!(small_i64, 7, 0, beu64, leu64);

too_small!(small_uint_1, 1, beusize, leusize);
too_small!(small_uint_2, 2, beusize, leusize);
too_small!(small_uint_3, 3, beusize, leusize);
too_small!(small_uint_4, 4, beusize, leusize);
too_small!(small_uint_5, 5, beusize, leusize);
too_small!(small_uint_6, 6, beusize, leusize);
too_small!(small_uint_7, 7, beusize, leusize);

too_small!(small_int_1, 1, beusize, leusize);
too_small!(small_int_2, 2, beusize, leusize);
too_small!(small_int_3, 3, beusize, leusize);
too_small!(small_int_4, 4, beusize, leusize);
too_small!(small_int_5, 5, beusize, leusize);
too_small!(small_int_6, 6, beusize, leusize);
too_small!(small_int_7, 7, beusize, leusize);

#[test]
fn test_read_bytes_ext() {
    let rdr = vec![2, 5, 3, 0];
    assert_eq!(517, super::beu16::decode(&rdr[0..2]));
    assert_eq!(768, super::beu16::decode(&rdr[2..4]));
}

#[test]
fn test_write_bytes_ext() {
    let mut wtr = vec![0; 4];
    super::beu16::encode(&mut wtr[0..2], 517);
    super::beu16::encode(&mut wtr[2..4], 768);
    assert_eq!(wtr, vec![2, 5, 3, 0]);
}