Patrick Steinhardt <ps@xxxxxx> writes: > diff --git a/src/varint.rs b/src/varint.rs > new file mode 100644 > index 00000000000..3d41760a555 > --- /dev/null > +++ b/src/varint.rs > @@ -0,0 +1,92 @@ > +use std::os::raw::c_int; > +use std::os::raw::c_uchar; > + > +#[no_mangle] > +pub unsafe extern "C" fn decode_varint(bufp: *mut *const c_uchar) -> usize { > + let mut buf = *bufp; > + let mut c = *buf; > + let mut val = usize::from(c & 127); > + > + buf = buf.add(1); > + > + while (c & 128) != 0 { > + val += 1; > + if val == 0 || val.leading_zeros() < 7 { > + return 0; // overflow > + } > + > + c = *buf; > + buf = buf.add(1); > + > + val = (val << 7) + usize::from(c & 127); > + } > + > + *bufp = buf; > + val > +} This (and the encoding side) looks quite faithful translation of the original in C. Interestingly, disassembly I saw looked a lot more optimized than the C variant compiled with clang-19 -O2. The difference probably is largely due to its omitting frame pointer. The comparison to detect overflow compiled to direct comparison with 0x1ffffffffffffff (both in C and rustc/LLVM), which was amusing, too.