{"id":23416189,"url":"https://github.com/bluk/leb128fmt","last_synced_at":"2025-04-09T06:43:51.123Z","repository":{"id":197650029,"uuid":"699047943","full_name":"bluk/leb128fmt","owner":"bluk","description":"A library to encode and decode LEB128 compressed values.","archived":false,"fork":false,"pushed_at":"2025-03-16T21:11:07.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"trunk","last_synced_at":"2025-03-16T22:24:11.100Z","etag":null,"topics":["compression","dwarf","leb128","parser","rust","variable-length-encoding","wasm","webassembly"],"latest_commit_sha":null,"homepage":"https://docs.rs/leb128fmt/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bluk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-01T19:03:37.000Z","updated_at":"2025-03-16T21:11:12.000Z","dependencies_parsed_at":"2025-03-16T22:23:51.219Z","dependency_job_id":"f1deeb53-5218-4aad-bf5b-f4eb4ab6eea9","html_url":"https://github.com/bluk/leb128fmt","commit_stats":null,"previous_names":["bluk/leb128fmt"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluk%2Fleb128fmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluk%2Fleb128fmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluk%2Fleb128fmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluk%2Fleb128fmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bluk","download_url":"https://codeload.github.com/bluk/leb128fmt/tar.gz/refs/heads/trunk","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247994102,"owners_count":21030049,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compression","dwarf","leb128","parser","rust","variable-length-encoding","wasm","webassembly"],"created_at":"2024-12-22T22:13:58.963Z","updated_at":"2025-04-09T06:43:51.106Z","avatar_url":"https://github.com/bluk.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Leb128fmt\n\nLeb128fmt is a library to decode and encode [LEB128][leb128_wiki] formatted numbers.\nLEB128 is a variable length integer compression format.\n\n* [Latest API Documentation][api_docs]\n\nThe library does not allocate memory and can be used in `no_std` and\n`no_std::no_alloc` environments.\n\nVarious functions are provided which encode and decode signed and unsigned\nintegers with the number of bits in the function name. There are generic\nfunctions provided to read and write slices of encoded values as well.\n\nThere are encoding functions with the word `fixed` in the name which will\nwrite out a value using the maximum number of bytes for a given bit size.\nFor instance, using `encode_fixed_u32` will always use 5 bytes to\nwrite out the value. While always using the maximum number of bytes removes\nthe benefit of compression, in some scenarios, it is beneficial to have a\nfixed encoding size.\n\nFinally, there are macros provided which you can use to build your own\nencoding and decoding functions for unusual variants like signed 33 bit\nvalues.\n\n## Installation\n\n```sh\ncargo add leb128fmt\n```\n\n## Examples\n\n### Functions using Arrays\n\n```rust\n// Encode an unsigned 32 bit number:\nlet (output, written_len) = leb128fmt::encode_u32(43110).unwrap();\n// The number of bytes written in the output array\nassert_eq!(written_len, 3);\nassert_eq!(\u0026output[..written_len], \u0026[0xE6, 0xD0, 0x02]);\n// The entire output array. Note you should only use \u0026output[..written_len] to copy\n// into your output buffer\nassert_eq!(output, [0xE6, 0xD0, 0x02, 0x00, 0x00]);\n\n// Decode an unsigned 32 bit number:\nlet input = [0xE6, 0xD0, 0x02, 0x00, 0x00];\nlet (result, read_len) = leb128fmt::decode_u32(input).unwrap();\nassert_eq!(result, 43110);\nassert_eq!(read_len, 3);\n```\n\n#### Helper Functions\n\nIf you are reading from an input buffer, you can use `is_last` and\n`max_len` to determine the bytes to copy into the array.\n\n```rust\nlet buffer = vec![0xFE, 0xFE, 0xE6, 0xD0, 0x02, 0xFE, 0xFE, 0xFE];\nlet pos = 2;\nlet end = buffer.iter().skip(pos).copied().position(leb128fmt::is_last).map(|p| pos + p);\nif let Some(end) = end {\n    if end \u003c= pos + leb128fmt::max_len::\u003c32\u003e() {\n        let mut input = [0u8; leb128fmt::max_len::\u003c32\u003e()];\n        input[..=end - pos].copy_from_slice(\u0026buffer[pos..=end]);\n        let (result, read_len) = leb128fmt::decode_u32(input).unwrap();\n        assert_eq!(result, 43110);\n        assert_eq!(read_len, 3);\n    } else {\n        // invalid LEB128 encoding\n        panic!();\n    }\n} else {\n  if buffer.len() - pos \u003c leb128fmt::max_len::\u003c32\u003e() {\n     // Need more bytes in the buffer\n     panic!();\n  } else {\n     // invalid LEB128 encoding\n     panic!();\n  }\n}\n\n```\n\n### Functions Using Slices\n\n```rust\nlet mut buffer = vec![0xFE; 10];\nlet mut pos = 1;\n\n// Encode an unsigned 64 bit number with a mutable slice:\nlet result = leb128fmt::encode_uint_slice::\u003cu64, 64\u003e(43110u64, \u0026mut buffer, \u0026mut pos);\n// The number of bytes written in the output array\nassert_eq!(result, Some(3));\nassert_eq!(pos, 4);\n\nassert_eq!(buffer, [0xFE, 0xE6, 0xD0, 0x02, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE]);\n\n// Decode an unsigned 64 bit number with a slice:\npos = 1;\nlet result = leb128fmt::decode_uint_slice::\u003cu64, 64\u003e(\u0026buffer, \u0026mut pos);\nassert_eq!(result, Ok(43110));\nassert_eq!(pos, 4);\n```\n\n### Functions Using Fixed Sized Encoding\n\nThere may be several different ways to encode a value. For instance, `0` can\nbe encoded as 32 bits unsigned:\n\n```rust\nlet mut pos = 0;\nassert_eq!(leb128fmt::decode_uint_slice::\u003cu32, 32\u003e(\u0026[0x00], \u0026mut pos), Ok(0));\npos = 0;\nassert_eq!(leb128fmt::decode_uint_slice::\u003cu32, 32\u003e(\u0026[0x80, 0x00], \u0026mut pos), Ok(0));\npos = 0;\nassert_eq!(leb128fmt::decode_uint_slice::\u003cu32, 32\u003e(\u0026[0x80, 0x80, 0x00], \u0026mut pos), Ok(0));\npos = 0;\nassert_eq!(leb128fmt::decode_uint_slice::\u003cu32, 32\u003e(\u0026[0x80, 0x80, 0x80, 0x00], \u0026mut pos), Ok(0));\npos = 0;\nassert_eq!(leb128fmt::decode_uint_slice::\u003cu32, 32\u003e(\u0026[0x80, 0x80, 0x80, 0x80, 0x00], \u0026mut pos), Ok(0));\n```\n\nThere are functions provided to encode a value using the maximum number of\nbytes possible for a given bit size. Using the maximum number of bytes\nremoves the benefit of compression, but it may be useful in a few scenarios.\n\nFor instance, if a binary format needs to store the size or offset of some\ndata before the size of data is known, it can be beneficial to write a fixed\nsized `0` placeholder value first. Then, once the real value is known, the\n`0` placeholder can be overwritten without moving other bytes. The real\nvalue is also written out using the fixed maximum number of bytes.\n\n```rust\n// Encode an unsigned 32 bit number with all 5 bytes:\nlet output = leb128fmt::encode_fixed_u32(43110).unwrap();\nassert_eq!(output, [0xE6, 0xD0, 0x82, 0x80, 0x00]);\n\n// Decode an unsigned 32 bit number:\nlet input = output;\nlet (result, read_len) = leb128fmt::decode_u32(input).unwrap();\nassert_eq!(result, 43110);\n\n// Note that all 5 bytes are read\nassert_eq!(read_len, 5);\n```\n\n## License\n\nLicensed under either of [Apache License, Version 2.0][LICENSE_APACHE] or [MIT\nLicense][LICENSE_MIT] at your option.\n\n### Contributions\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be\ndual licensed as above, without any additional terms or conditions.\n\n[LICENSE_APACHE]: LICENSE-APACHE\n[LICENSE_MIT]: LICENSE-MIT\n[leb128_wiki]: https://en.wikipedia.org/wiki/LEB128\n[api_docs]: https://docs.rs/leb128fmt/","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluk%2Fleb128fmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbluk%2Fleb128fmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluk%2Fleb128fmt/lists"}