{"id":2097,"library":"leb128","title":"LEB128 (Little Endian Base 128) Encoder/Decoder","description":"The `leb128` Python library provides functionality for encoding and decoding integers using the LEB128 (Little Endian Base 128) variable-length compression format. This format is commonly used in contexts like the DWARF debug file format and WebAssembly binary encoding for integer literals. The library supports both unsigned and signed LEB128 operations. It is currently at version 1.0.9 and appears to have a stable, though infrequent, release cadence, with the latest release in January 2026.","status":"active","version":"1.0.9","language":"en","source_language":"en","source_url":"https://github.com/mohanson/leb128","tags":["encoding","decoding","leb128","variable-length integer","binary","webassembly","dwarf"],"install":[{"cmd":"pip install leb128","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"Imports the unsigned LEB128 encoder/decoder object.","symbol":"u","correct":"from leb128 import u"},{"note":"Imports the signed LEB128 encoder/decoder object.","symbol":"i","correct":"from leb128 import i"},{"note":"Imports the module, then use `leb128.u` and `leb128.i`.","symbol":"leb128","correct":"import leb128"}],"quickstart":{"code":"import io\nimport leb128\n\n# --- Unsigned LEB128 ---\n# Encode an unsigned integer\nunsigned_value = 624485\nbuf_u = io.BytesIO()\nleb128.u.encode(buf_u, unsigned_value)\nprint(f\"Encoded {unsigned_value} (unsigned): {buf_u.getvalue().hex()}\")\n\n# To decode, reset buffer position and read\nbuf_u.seek(0)\ndecoded_u = leb128.u.decode(buf_u)\nprint(f\"Decoded unsigned: {decod_u}\")\nassert decoded_u == unsigned_value\n\n# --- Signed LEB128 ---\n# Encode a signed integer\nsigned_value = -12345\nbuf_i = io.BytesIO()\nleb128.i.encode(buf_i, signed_value)\nprint(f\"Encoded {signed_value} (signed): {buf_i.getvalue().hex()}\")\n\n# To decode, reset buffer position and read\nbuf_i.seek(0)\ndecoded_i = leb128.i.decode(buf_i)\nprint(f\"Decoded signed: {decod_i}\")\nassert decoded_i == signed_value\n\nprint(\"LEB128 encoding and decoding successful!\")","lang":"python","description":"This quickstart demonstrates encoding and decoding both unsigned and signed integers using the `leb128` library. It uses `io.BytesIO` as a file-like object for writing and reading the LEB128 encoded bytes, highlighting the necessity to `seek(0)` the buffer before decoding after an encode operation."},"warnings":[{"fix":"Use `leb128.u.encode`/`leb128.u.decode` for unsigned integers and `leb128.i.encode`/`leb128.i.decode` for signed integers.","message":"Always distinguish between unsigned (`leb128.u`) and signed (`leb128.i`) encoding/decoding. Using the wrong one will result in incorrect values or decoding errors, as LEB128's interpretation of the sign bit differs.","severity":"gotcha","affected_versions":"All versions"},{"fix":"After `encode()` and before `decode()` on the same `io.BytesIO` object, insert `buffer_object.seek(0)`.","message":"When using `io.BytesIO` or any file-like object, remember to call `seek(0)` on the buffer after encoding to reset its position before attempting to decode the written bytes. Failure to do so will result in reading from the end of the buffer, leading to unexpected results or errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Validate the magnitude of integers being encoded against the limitations of the system that will ultimately decode and use them. Python itself will handle arbitrary sizes without `OverflowError` for its own operations.","message":"While Python integers support arbitrary precision, be mindful of the maximum integer size supported by the *target system* if these LEB128 encoded bytes are being exchanged with other languages or environments (e.g., C, WebAssembly). Encoding excessively large numbers that exceed the recipient's integer type could lead to truncation or overflow on the receiving end.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}