Encode `len` into 1–3 bytes: - 1 byte if < 2⁷ - 2 bytes if < 2¹⁴ - 3 bytes otherwise (uses all 8 bits of the final byte, giving 22 bits total)
(buf: &mut Vec<u8>, len: u32)
| 40 | /// - 2 bytes if < 2¹⁴ |
| 41 | /// - 3 bytes otherwise (uses all 8 bits of the final byte, giving 22 bits total) |
| 42 | pub fn write_len(buf: &mut Vec<u8>, len: u32) { |
| 43 | if len < (1 << 7) { |
| 44 | // [0vvvvvvv] |
| 45 | buf.push(len as u8); |
| 46 | } else if len < (1 << 14) { |
| 47 | // [1vvvvvvv] [0vvvvvvv] |
| 48 | buf.push(((len) as u8 & 0x7F) | 0x80); |
| 49 | buf.push((len >> 7) as u8); |
| 50 | } else { |
| 51 | // [1vvvvvvv] [1vvvvvvv] [vvvvvvvv] |
| 52 | buf.push(((len) as u8 & 0x7F) | 0x80); |
| 53 | buf.push(((len >> 7) as u8 & 0x7F) | 0x80); |
| 54 | buf.push((len >> 14) as u8); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// Decode a 1–3 byte varint back to `u32`. Any bits beyond 22 are ignored. |
| 59 | pub fn read_len(bufman: &FilelessBufferManager, cursor: u64) -> Result<u32, BufIoError> { |