Skip to content

Add checked ULEB128 encoding and decoding for byte cursors #674

Closed

Description

@LunaStev

Motivation

The byte cursor API provides fixed-width integer reads and writes and advances the cursor only after an operation succeeds. Add a bounded variable-length unsigned integer codec using the same caller-owned storage and error conventions.

Code references:

Scope

Add unsigned 64-bit ULEB128 reading and writing, preferably in a focused std/bytes/leb128.wave module. Follow the existing cursor API shape: a reader plus an output pointer for decoding, and a writer plus a u64 value for encoding, returning an i32 status.

Suggested names are bytes_reader_read_uleb128_u64 and bytes_writer_write_uleb128_u64.

The contract is:

  • Encode seven payload bits per byte, least-significant group first, with bit 7 indicating continuation. Writers emit the shortest representation.
  • Decode at most ten bytes. In the tenth byte, only payload bit 0 may be set and continuation must be clear.
  • Accept terminating non-minimal representations within that ten-byte bound, such as 80 00 for zero; document this separately from canonical writer output.
  • Return BYTES_ERROR_EOF when input ends before termination within the permitted length, and BYTES_ERROR_OVERFLOW when the encoding cannot represent a u64 or would require more than ten bytes. Validate the tenth byte before shifting.
  • Preserve the reader position and output value on every read failure. Validate null pointers and cursor invariants using the existing invalid-input convention.
  • Preflight the encoded length and available capacity before writing. Insufficient space returns BYTES_ERROR_NO_SPACE without changing the writer position or any destination bytes.

Completion criteria

  • Implement both operations without heap allocation, libc, or a new external dependency.
  • Cover exact byte vectors for 0, 127, 128, every seven-bit length transition, and u64::MAX.
  • Test truncated encodings, ten continuation bytes, a tenth-byte payload greater than one, valid non-minimal encodings, and decoding consecutive values from a nonzero cursor position.
  • Assert unchanged cursor/output state for failed reads and unchanged cursor/buffer state for failed writes, including one-byte-short capacity.
  • Test null output pointers and invalid reader/writer state without reading or writing out of bounds.
  • Add runnable standard-library regressions under tests/cases/ and a short API example documenting byte counts and error behavior.
  • Keep all existing fixed-width cursor behavior unchanged.

SLEB128, other integer widths, and WebAssembly-specific parsing are separate extensions.

Activity

  1. added
    enhancement
    help wanted
    needs testing
    on Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementhelp wantedneeds testing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions