bits
lib/bits.vklib/bits.vk Pure-Veko binary wire helpers (u24, signed i16/i32, IEEE-754 f64 bitcasts, LEB128/ZigZag varints), checksums (CRC32, Adler-32, FNV-1a), and constant-time byte primitives. Zero capabilities required.
VarintVal = struct { value i64, next_off i64 }
Result of decoding a varint at a byte offset.
get-u24-be = fn [b bytes, off i64] i64
Reads an unsigned 24-bit big-endian integer from b at off.
use [bits]
mut b = bytes/with-cap 4
_ = bits/push-u24-be mut b 0x123456
if (bits/get-u24-be b 0) != 0x123456 [ err 'u24-be mismatch' ] else [ .ok ]
get-u24-le = fn [b bytes, off i64] i64
Reads an unsigned 24-bit little-endian integer from b at off.
set-u24-be = fn [mut b bytes, off i64, v i64] i64
Writes an unsigned 24-bit big-endian integer into b at off.
set-u24-le = fn [mut b bytes, off i64, v i64] i64
Writes an unsigned 24-bit little-endian integer into b at off.
push-u24-be = fn [mut b bytes, v i64] i64
Appends an unsigned 24-bit big-endian integer to b, returning the new length.
push-u24-le = fn [mut b bytes, v i64] i64
Appends an unsigned 24-bit little-endian integer to b, returning the new length.
get-i16-le = fn [b bytes, off i64] i64
Reads a signed 16-bit little-endian integer from b at off.
get-i16-be = fn [b bytes, off i64] i64
Reads a signed 16-bit big-endian integer from b at off.
get-i32-le = fn [b bytes, off i64] i64
Reads a signed 32-bit little-endian integer from b at off.
get-i32-be = fn [b bytes, off i64] i64
Reads a signed 32-bit big-endian integer from b at off.
push-i16-le = fn [mut b bytes, v i64] i64
Appends a signed 16-bit little-endian integer to b.
push-i16-be = fn [mut b bytes, v i64] i64
Appends a signed 16-bit big-endian integer to b.
push-i32-le = fn [mut b bytes, v i64] i64
Appends a signed 32-bit little-endian integer to b.
push-i32-be = fn [mut b bytes, v i64] i64
Appends a signed 32-bit big-endian integer to b.
f64-to-bits = fn [x f64] i64
Converts an IEEE-754 f64 value into its 64-bit integer bit pattern.
use [bits]
b1 = bits/f64-to-bits 1.0
b2 = bits/f64-to-bits -2.5
rt = bits/bits-to-f64 b2
if b1 != 0x3ff0000000000000 or rt != -2.5 [ err 'f64 bitcast mismatch' ] else [ .ok ]
bits-to-f64 = fn [raw i64] f64
Reconstructs an IEEE-754 f64 value from its 64-bit integer bit pattern.
zigzag-encode = fn [n i64] i64
Encodes a signed i64 into an unsigned ZigZag i64 bit pattern.
use [bits]
z = bits/zigzag-encode -42
if (bits/zigzag-decode z) != -42 [ err 'zigzag mismatch' ] else [ .ok ]
push-varint-u64 = fn [mut out bytes, v i64] i64
Appends an unsigned LEB128 varint (64-bit) to out, returning the new length.
use [bits]
mut b = bytes/with-cap 16
_ = bits/push-varint-u64 mut b 300
r = bits/read-varint-u64 b 0
if r.value != 300 or r.next_off != 2 [ err 'varint mismatch' ] else [ .ok ]
read-varint-u64 = fn [b bytes, off i64] VarintVal
Reads an unsigned LEB128 varint from b starting at off.
crc32 = fn [data bytes] i64
Computes the IEEE 802.3 CRC-32 checksum (0xEDB88320 reflected polynomial).
use [bits]
c = bits/crc32 (bytes/from-str '123456789')
if c != 0xcbf43926 [ err 'crc32 mismatch' ] else [ .ok ]
adler32 = fn [data bytes] i64
Computes the RFC 1950 Adler-32 checksum of data.
use [bits]
a = bits/adler32 (bytes/from-str 'Wikipedia')
if a != 0x11e60398 [ err 'adler32 mismatch' ] else [ .ok ]
index-of = fn [hay bytes, needle bytes, start i64] i64
Finds the first index of needle inside hay at or after start, or -1 if absent.
use [bits]
h = bytes/from-str 'hello veko world'
n = bytes/from-str 'veko'
if (bits/index-of h n 0) != 6 [ err 'bits/index-of mismatch' ] else [ .ok ]
starts-with? = fn [data bytes, prefix bytes] bool
Returns true if data starts with prefix.
xor-in-place! = fn [mut dst bytes, mask bytes] i64
XORs mask into dst in-place across min(len dst, len mask) bytes.
ct-eq? = fn [a bytes, b bytes] bool
Constant-time equality comparison for equal-length byte buffers (returns false if lengths differ).
use [bits]
a = bytes/from-str 'secret-tag'
b = bytes/from-str 'secret-tag'
c = bytes/from-str 'secret-tah'
if (not (bits/ct-eq? a b)) or (bits/ct-eq? a c) [ err 'ct-eq mismatch' ] else [ .ok ]