oxiproto-core 0.1.2

Pure Rust protobuf core types
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
//! LEB128 variable-length integer encoding and decoding.
//!
//! Protobuf uses the LEB128 (Little-Endian Base 128) encoding for varint
//! fields. Each byte uses 7 bits for the value and 1 bit (the MSB) as a
//! continuation flag.
//!
//! The maximum encoded length is 10 bytes (for a 64-bit value that uses all
//! bits).

use super::WireError;
use prost::alloc::{format, vec::Vec};

/// Maximum number of bytes a varint can occupy (ceil(64/7) = 10).
const MAX_VARINT_BYTES: usize = 10;

/// Encode a `u64` value as a varint and append the bytes to `buf`.
///
/// Returns the number of bytes written (1..=10).
///
/// # Example
///
/// ```
/// use oxiproto_core::wire::encode_varint;
///
/// let mut buf = Vec::new();
/// let n = encode_varint(300, &mut buf);
/// assert_eq!(n, 2);
/// assert_eq!(buf, &[0xAC, 0x02]);
/// ```
pub fn encode_varint(mut value: u64, buf: &mut Vec<u8>) -> usize {
    let mut count = 0;
    loop {
        let byte = (value & 0x7F) as u8;
        value >>= 7;
        if value == 0 {
            buf.push(byte);
            count += 1;
            break;
        } else {
            buf.push(byte | 0x80);
            count += 1;
        }
    }
    count
}

/// Encode a `u64` value as a varint into a fixed-size buffer.
///
/// Returns `(bytes_written, buffer)` where `buffer` is a 10-byte array and
/// `bytes_written` indicates how many leading bytes are valid.
///
/// This is useful when you need to write to a `&mut [u8]` or `Write` without
/// heap allocation.
pub fn encode_varint_fixed(mut value: u64) -> (usize, [u8; MAX_VARINT_BYTES]) {
    let mut buf = [0u8; MAX_VARINT_BYTES];
    let mut i = 0;
    loop {
        let byte = (value & 0x7F) as u8;
        value >>= 7;
        if value == 0 {
            buf[i] = byte;
            i += 1;
            break;
        } else {
            buf[i] = byte | 0x80;
            i += 1;
        }
    }
    (i, buf)
}

/// Decode a varint from the beginning of `buf`.
///
/// Returns `(value, bytes_consumed)` on success.
///
/// # Errors
///
/// - [`WireError::UnexpectedEof`] if the buffer is empty or ends mid-varint.
/// - [`WireError::Overflow`] if the varint exceeds 10 bytes.
///
/// # Example
///
/// ```
/// use oxiproto_core::wire::decode_varint;
///
/// let buf = [0xAC, 0x02];
/// let (value, consumed) = decode_varint(&buf).unwrap();
/// assert_eq!(value, 300);
/// assert_eq!(consumed, 2);
/// ```
pub fn decode_varint(buf: &[u8]) -> Result<(u64, usize), WireError> {
    if buf.is_empty() {
        return Err(WireError::UnexpectedEof);
    }

    let mut result: u64 = 0;
    let mut shift: u32 = 0;

    for (i, &byte) in buf.iter().enumerate() {
        if i >= MAX_VARINT_BYTES {
            return Err(WireError::Overflow);
        }

        let value_bits = u64::from(byte & 0x7F);

        // Check for overflow: if shift >= 63 and value_bits > 1, the result
        // would exceed u64::MAX. The 10th byte (shift=63) can only contribute
        // bit 0 (value 0 or 1).
        if shift >= 63 && value_bits > 1 {
            return Err(WireError::Overflow);
        }

        result |= value_bits << shift;
        shift += 7;

        if byte & 0x80 == 0 {
            return Ok((result, i + 1));
        }
    }

    Err(WireError::UnexpectedEof)
}

/// Compute the number of bytes needed to encode `value` as a varint.
///
/// # Example
///
/// ```
/// use oxiproto_core::wire::varint::encoded_len_varint;
///
/// assert_eq!(encoded_len_varint(0), 1);
/// assert_eq!(encoded_len_varint(127), 1);
/// assert_eq!(encoded_len_varint(128), 2);
/// assert_eq!(encoded_len_varint(300), 2);
/// assert_eq!(encoded_len_varint(u64::MAX), 10);
/// ```
pub fn encoded_len_varint(value: u64) -> usize {
    // Each byte encodes 7 bits. We need ceil((bits_needed)/7) bytes.
    // The minimum is 1 byte (for value 0).
    if value == 0 {
        return 1;
    }
    let bits = 64 - value.leading_zeros() as usize;
    bits.div_ceil(7)
}

/// Encode a `u32` value as a varint and append to `buf`.
///
/// Convenience wrapper around [`encode_varint`] for 32-bit values.
pub fn encode_varint32(value: u32, buf: &mut Vec<u8>) -> usize {
    encode_varint(u64::from(value), buf)
}

/// Decode a varint and truncate to `u32`.
///
/// # Errors
///
/// Returns [`WireError::OutOfRange`] if the decoded value exceeds `u32::MAX`.
pub fn decode_varint32(buf: &[u8]) -> Result<(u32, usize), WireError> {
    let (val, consumed) = decode_varint(buf)?;
    let val32 = u32::try_from(val)
        .map_err(|_| WireError::OutOfRange(format!("varint value {val} exceeds u32::MAX")))?;
    Ok((val32, consumed))
}

/// Encode an `i64` value as a varint (two's complement cast to u64).
///
/// Protobuf `int64` fields use this encoding. For signed fields where negative
/// values are common, prefer zigzag + varint instead.
pub fn encode_varint_i64(value: i64, buf: &mut Vec<u8>) -> usize {
    encode_varint(value as u64, buf)
}

/// Decode a varint as `i64` (two's complement reinterpretation).
pub fn decode_varint_i64(buf: &[u8]) -> Result<(i64, usize), WireError> {
    let (val, consumed) = decode_varint(buf)?;
    Ok((val as i64, consumed))
}

/// Encode an `i32` value as a varint.
///
/// Protobuf `int32` fields sign-extend to 64 bits before encoding, which
/// means negative `int32` values always occupy 10 bytes. For signed fields
/// where negative values are common, prefer zigzag encoding.
pub fn encode_varint_i32(value: i32, buf: &mut Vec<u8>) -> usize {
    // Sign-extend to i64, then reinterpret as u64 — this matches protobuf
    // spec for int32 encoding.
    encode_varint(value as i64 as u64, buf)
}

/// Decode a varint as `i32`.
///
/// # Errors
///
/// Returns [`WireError::OutOfRange`] if the value does not fit in an `i32`
/// after reinterpretation.
pub fn decode_varint_i32(buf: &[u8]) -> Result<(i32, usize), WireError> {
    let (val, consumed) = decode_varint(buf)?;
    // Protobuf int32 encoding sign-extends to 64 bits, so we truncate
    // back to 32 bits.
    Ok((val as i32, consumed))
}

/// Encode a `bool` as a single-byte varint (0 or 1).
pub fn encode_varint_bool(value: bool, buf: &mut Vec<u8>) -> usize {
    encode_varint(u64::from(value), buf)
}

/// Decode a varint as `bool` (0 → false, nonzero → true).
pub fn decode_varint_bool(buf: &[u8]) -> Result<(bool, usize), WireError> {
    let (val, consumed) = decode_varint(buf)?;
    Ok((val != 0, consumed))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn encode_decode_zero() {
        let mut buf = Vec::new();
        encode_varint(0, &mut buf);
        assert_eq!(buf, &[0x00]);
        let (val, consumed) = decode_varint(&buf).expect("decode");
        assert_eq!(val, 0);
        assert_eq!(consumed, 1);
    }

    #[test]
    fn encode_decode_one() {
        let mut buf = Vec::new();
        encode_varint(1, &mut buf);
        assert_eq!(buf, &[0x01]);
        let (val, consumed) = decode_varint(&buf).expect("decode");
        assert_eq!(val, 1);
        assert_eq!(consumed, 1);
    }

    #[test]
    fn encode_decode_127() {
        let mut buf = Vec::new();
        encode_varint(127, &mut buf);
        assert_eq!(buf, &[0x7F]);
        let (val, consumed) = decode_varint(&buf).expect("decode");
        assert_eq!(val, 127);
        assert_eq!(consumed, 1);
    }

    #[test]
    fn encode_decode_128() {
        let mut buf = Vec::new();
        encode_varint(128, &mut buf);
        assert_eq!(buf, &[0x80, 0x01]);
        let (val, consumed) = decode_varint(&buf).expect("decode");
        assert_eq!(val, 128);
        assert_eq!(consumed, 2);
    }

    #[test]
    fn encode_decode_300() {
        let mut buf = Vec::new();
        encode_varint(300, &mut buf);
        assert_eq!(buf, &[0xAC, 0x02]);
        let (val, consumed) = decode_varint(&buf).expect("decode");
        assert_eq!(val, 300);
        assert_eq!(consumed, 2);
    }

    #[test]
    fn encode_decode_u64_max() {
        let mut buf = Vec::new();
        let n = encode_varint(u64::MAX, &mut buf);
        assert_eq!(n, 10);
        let (val, consumed) = decode_varint(&buf).expect("decode");
        assert_eq!(val, u64::MAX);
        assert_eq!(consumed, 10);
    }

    #[test]
    fn encode_decode_u32_max() {
        let mut buf = Vec::new();
        encode_varint(u64::from(u32::MAX), &mut buf);
        let (val, consumed) = decode_varint(&buf).expect("decode");
        assert_eq!(val, u64::from(u32::MAX));
        assert_eq!(consumed, 5);
    }

    #[test]
    fn decode_empty_returns_eof() {
        assert!(matches!(decode_varint(&[]), Err(WireError::UnexpectedEof)));
    }

    #[test]
    fn decode_truncated_returns_eof() {
        // Byte with continuation bit set but no follow-up byte
        assert!(matches!(
            decode_varint(&[0x80]),
            Err(WireError::UnexpectedEof)
        ));
    }

    #[test]
    fn decode_overflow_returns_error() {
        // 11 continuation bytes — varint too long
        let buf = [0x80; 11];
        assert!(matches!(decode_varint(&buf), Err(WireError::Overflow)));
    }

    #[test]
    fn encode_decode_i64_negative() {
        let mut buf = Vec::new();
        encode_varint_i64(-1, &mut buf);
        // -1 as u64 is u64::MAX, which takes 10 bytes
        assert_eq!(buf.len(), 10);
        let (val, consumed) = decode_varint_i64(&buf).expect("decode");
        assert_eq!(val, -1);
        assert_eq!(consumed, 10);
    }

    #[test]
    fn encode_decode_i32_negative() {
        let mut buf = Vec::new();
        encode_varint_i32(-1, &mut buf);
        // int32 sign-extends to 64 bits, so -1 takes 10 bytes
        assert_eq!(buf.len(), 10);
        let (val, consumed) = decode_varint_i32(&buf).expect("decode");
        assert_eq!(val, -1);
        assert_eq!(consumed, 10);
    }

    #[test]
    fn encode_decode_bool() {
        let mut buf = Vec::new();
        encode_varint_bool(false, &mut buf);
        encode_varint_bool(true, &mut buf);
        let (val, consumed) = decode_varint_bool(&buf).expect("decode false");
        assert!(!val);
        assert_eq!(consumed, 1);
        let (val, consumed) = decode_varint_bool(&buf[1..]).expect("decode true");
        assert!(val);
        assert_eq!(consumed, 1);
    }

    #[test]
    fn encoded_len_varint_values() {
        assert_eq!(encoded_len_varint(0), 1);
        assert_eq!(encoded_len_varint(1), 1);
        assert_eq!(encoded_len_varint(127), 1);
        assert_eq!(encoded_len_varint(128), 2);
        assert_eq!(encoded_len_varint(300), 2);
        assert_eq!(encoded_len_varint(16383), 2);
        assert_eq!(encoded_len_varint(16384), 3);
        assert_eq!(encoded_len_varint(u64::MAX), 10);
    }

    #[test]
    fn decode_varint32_in_range() {
        let mut buf = Vec::new();
        encode_varint(1000, &mut buf);
        let (val, consumed) = decode_varint32(&buf).expect("decode");
        assert_eq!(val, 1000);
        assert_eq!(consumed, 2);
    }

    #[test]
    fn decode_varint32_out_of_range() {
        let mut buf = Vec::new();
        encode_varint(u64::from(u32::MAX) + 1, &mut buf);
        assert!(matches!(
            decode_varint32(&buf),
            Err(WireError::OutOfRange(_))
        ));
    }

    #[test]
    fn encode_fixed_matches_vec() {
        for value in [0u64, 1, 127, 128, 300, u32::MAX as u64, u64::MAX] {
            let mut vec_buf = Vec::new();
            let vec_len = encode_varint(value, &mut vec_buf);
            let (fixed_len, fixed_buf) = encode_varint_fixed(value);
            assert_eq!(vec_len, fixed_len);
            assert_eq!(&vec_buf[..], &fixed_buf[..fixed_len]);
        }
    }

    #[test]
    fn round_trip_various_values() {
        let test_values: &[u64] = &[
            0,
            1,
            127,
            128,
            255,
            256,
            16383,
            16384,
            2_097_151,
            2_097_152,
            0xFFFF_FFFF,
            0x1_0000_0000,
            u64::MAX / 2,
            u64::MAX - 1,
            u64::MAX,
        ];
        for &value in test_values {
            let mut buf = Vec::new();
            encode_varint(value, &mut buf);
            let (decoded, consumed) = decode_varint(&buf).expect("decode");
            assert_eq!(decoded, value, "round-trip failed for {value}");
            assert_eq!(consumed, buf.len());
        }
    }
}