]> git.lizzy.rs Git - rust.git/blob - src/libserialize/leb128.rs
Auto merge of #69115 - ehuss:update-books, r=Dylan-DPC
[rust.git] / src / libserialize / leb128.rs
1 macro_rules! impl_write_unsigned_leb128 {
2     ($fn_name:ident, $int_ty:ident) => {
3         #[inline]
4         pub fn $fn_name(out: &mut Vec<u8>, mut value: $int_ty) {
5             loop {
6                 if value < 0x80 {
7                     out.push(value as u8);
8                     break;
9                 } else {
10                     out.push(((value & 0x7f) | 0x80) as u8);
11                     value >>= 7;
12                 }
13             }
14         }
15     };
16 }
17
18 impl_write_unsigned_leb128!(write_u16_leb128, u16);
19 impl_write_unsigned_leb128!(write_u32_leb128, u32);
20 impl_write_unsigned_leb128!(write_u64_leb128, u64);
21 impl_write_unsigned_leb128!(write_u128_leb128, u128);
22 impl_write_unsigned_leb128!(write_usize_leb128, usize);
23
24 macro_rules! impl_read_unsigned_leb128 {
25     ($fn_name:ident, $int_ty:ident) => {
26         #[inline]
27         pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
28             let mut result = 0;
29             let mut shift = 0;
30             let mut position = 0;
31             loop {
32                 let byte = slice[position];
33                 position += 1;
34                 if (byte & 0x80) == 0 {
35                     result |= (byte as $int_ty) << shift;
36                     return (result, position);
37                 } else {
38                     result |= ((byte & 0x7F) as $int_ty) << shift;
39                 }
40                 shift += 7;
41             }
42         }
43     };
44 }
45
46 impl_read_unsigned_leb128!(read_u16_leb128, u16);
47 impl_read_unsigned_leb128!(read_u32_leb128, u32);
48 impl_read_unsigned_leb128!(read_u64_leb128, u64);
49 impl_read_unsigned_leb128!(read_u128_leb128, u128);
50 impl_read_unsigned_leb128!(read_usize_leb128, usize);
51
52 #[inline]
53 /// encodes an integer using signed leb128 encoding and stores
54 /// the result using a callback function.
55 ///
56 /// The callback `write` is called once for each position
57 /// that is to be written to with the byte to be encoded
58 /// at that position.
59 pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W)
60 where
61     W: FnMut(u8),
62 {
63     loop {
64         let mut byte = (value as u8) & 0x7f;
65         value >>= 7;
66         let more =
67             !(((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && ((byte & 0x40) != 0)));
68
69         if more {
70             byte |= 0x80; // Mark this byte to show that more bytes will follow.
71         }
72
73         write(byte);
74
75         if !more {
76             break;
77         }
78     }
79 }
80
81 #[inline]
82 pub fn write_signed_leb128(out: &mut Vec<u8>, value: i128) {
83     write_signed_leb128_to(value, |v| out.push(v))
84 }
85
86 #[inline]
87 pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
88     let mut result = 0;
89     let mut shift = 0;
90     let mut position = start_position;
91     let mut byte;
92
93     loop {
94         byte = data[position];
95         position += 1;
96         result |= i128::from(byte & 0x7F) << shift;
97         shift += 7;
98
99         if (byte & 0x80) == 0 {
100             break;
101         }
102     }
103
104     if (shift < 64) && ((byte & 0x40) != 0) {
105         // sign extend
106         result |= -(1 << shift);
107     }
108
109     (result, position - start_position)
110 }