{"id":19391824,"url":"https://github.com/nDimensional/zig-multiformats","last_synced_at":"2025-09-13T11:31:30.599Z","repository":{"id":209120492,"uuid":"708604898","full_name":"nDimensional/zig-multiformats","owner":"nDimensional","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-11T17:50:04.000Z","size":63,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-10T21:42:18.083Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nDimensional.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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-23T02:11:37.000Z","updated_at":"2025-03-11T17:50:04.000Z","dependencies_parsed_at":"2023-11-25T08:22:59.486Z","dependency_job_id":"cfaaa75c-a52c-4188-871f-f0c9be468952","html_url":"https://github.com/nDimensional/zig-multiformats","commit_stats":null,"previous_names":["joeltg/zig-multiformats"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/nDimensional/zig-multiformats","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nDimensional%2Fzig-multiformats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nDimensional%2Fzig-multiformats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nDimensional%2Fzig-multiformats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nDimensional%2Fzig-multiformats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nDimensional","download_url":"https://codeload.github.com/nDimensional/zig-multiformats/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nDimensional%2Fzig-multiformats/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274956110,"owners_count":25380670,"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","status":"online","status_checked_at":"2025-09-13T02:00:10.085Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-10T10:29:21.400Z","updated_at":"2025-09-13T11:31:30.255Z","avatar_url":"https://github.com/nDimensional.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zig-multiformats\n\nZig modules for [unsigned varints](https://github.com/multiformats/unsigned-varint), [multicodec](https://github.com/multiformats/multicodec), [multibase](https://github.com/multiformats/multibase), [multihash](https://github.com/multiformats/multihash), and [CIDs](https://github.com/multiformats/cid).\n\n## Table of Contents\n\n- [Install](#install)\n- [Usage](#usage)\n  - [Varints](#varints)\n  - [Multibase](#multibase)\n  - [Multicodec](#multicodec)\n  - [Multihash](#multihash)\n  - [CIDs](#cids)\n\n## Install\n\nAdd to `build.zig.zon`:\n\n```zig\n.{\n    // ...\n    .dependencies = .{\n        .multiformats = .{\n            .url = \"https://github.com/nDimensional/zig-multiformats/archive/refs/tags/v0.1.0.tar.gz\",\n            // .hash = \"...\",\n        },\n    }\n}\n```\n\nThen in `build.zig`:\n\n```zig\npub fn build(b: *std.Build) !void {\n    // ...\n    const multiformats = b.dependency(\"multiformats\", .{});\n\n    const varint = multiformats.module(\"varint\");\n    const multicodec = multiformats.module(\"multicodec\");\n    const multibase = multiformats.module(\"multibase\");\n    const multihash = multiformats.module(\"multihash\");\n    const cid = multiformats.module(\"cid\");\n\n    // ... add as imports to exe or lib\n}\n```\n\n## Usage\n\n### Varints\n\n```zig\npub const MAX_BYTE_LENGTH = 9;\npub const MAX_VALUE: u64 = 1 \u003c\u003c 63;\n\npub fn encodingLength(val: u64) usize\n\npub fn decode(buf: []const u8, len: ?*usize) !usize\npub fn encode(buf: []u8, val: u64) usize\n\npub fn read(reader: std.io.AnyReader) !u64\npub fn write(writer: std.io.AnyWriter, val: u64) !void\n```\n\nThe `varint` module has `read`/`write` for usage with streams, and `encode`/`decode` for usage with slices. Trying to `encode` to a buffer that is too small will panic; always check that it has sufficient size using `encodingLength(val)` first.\n\n### Multibase\n\nThe different bases are exported as type-erased `Base` interface structs.\n\n```zig\npub const Base = struct {\n    code: Code,\n    name: []const u8,\n    impl: type,\n\n    pub fn writeAll(self: Base, writer: std.io.AnyWriter, bytes: []const u8) !void\n\n    pub fn encode(self: Base, allocator: std.mem.Allocator, bytes: []const u8) ![]const u8\n    pub fn baseEncode(self: Base, allocator: std.mem.Allocator, bytes: []const u8) ![]const u8\n    pub fn decode(self: Base, allocator: std.mem.Allocator, str: []const u8) ![]const u8\n    pub fn baseDecode(self: Base, allocator: std.mem.Allocator, str: []const u8) ![]const u8\n};\n\n// Base-X family - these are limited to encoding 256 bytes or less\npub const base10: Base;\npub const base36: Base;\npub const base36upper: Base;\npub const base58btc: Base;\npub const base58flickr: Base;\n\n// RFC 4648 family - these are suitable for streaming / arbitrarily long encodings\npub const base2: Base;\npub const base8: Base;\npub const base16: Base;\npub const base16upper: Base;\npub const base32: Base;\npub const base32upper: Base;\npub const base32pad: Base;\npub const base32padupper: Base;\npub const base32hex: Base;\npub const base32hexupper: Base;\npub const base32hexpad: Base;\npub const base32hexpadupper: Base;\npub const base32z: Base;\npub const base64: Base;\npub const base64pad: Base;\npub const base64url: Base;\npub const base64urlpad: Base;\n```\n\nAdditionally, there are \"generic\" encode/decode methods that use the `multibase.Code` enum.\n\n```zig\npub const Code = enum {\n    base10,\n    base36,\n    base36upper,\n    base58btc,\n    base58flickr,\n    base2,\n    base8,\n    base16,\n    base16upper,\n    base32,\n    base32upper,\n    base32pad,\n    base32padupper,\n    base32hex,\n    base32hexupper,\n    base32hexpad,\n    base32hexpadupper,\n    base32z,\n    base64,\n    base64pad,\n    base64url,\n    base64urlpad,\n};\n\npub const DecodeResult = struct {\n    code: Code,\n    data: []const u8,\n};\n\npub fn decode(allocator: std.mem.Allocator, str: []const u8) !DecodeResult\npub fn encode(allocator: std.mem.Allocator, bytes: []const u8, code: Code) ![]const u8\n```\n\n### Multicodec\n\n`multicodec.Codec` is an enum containing every entry in the [multicodec table](https://github.com/multiformats/multicodec/blob/master/table.csv). The enum type is `u32` and the enum value is the multicodec code. Many of the multicodec names have dashes and thus must be escaped in Zig code like this:\n\n```zig\nconst multicodec = @import(\"multicodec\");\n\nconst code = multicodec.Codec.@\"sha2-256\";\n```\n\n### Multihash\n\n```zig\nconst std = @import(\"std\");\nconst varint = @import(\"varint\");\nconst multibase = @import(\"multibase\");\nconst multicodec = @import(\"multicodec\");\n\npub const Digest = struct {\n    code: multicodec.Codec,\n    hash: []const u8,\n\n    /// decode a binary multihash from bytes\n    pub fn decode(allocator: std.mem.Allocator, bytes: []const u8) !Digest;\n\n    /// read a binary multihash from a reader\n    pub fn read(allocator: std.mem.Allocator, reader: std.io.AnyReader) !Digest;\n    pub fn deinit(self: Digest, allocator: std.mem.Allocator) void;\n\n    pub fn copy(self: Digest, allocator: std.mem.Allocator) !Digest;\n    pub fn eql(self: Digest, other: Digest) bool;\n    pub fn expectEqual(actual: Digest, expected: Digest) !void;\n    pub fn encodingLength(self: Digest) usize;\n    pub fn encode(self: Digest, allocator: std.mem.Allocator) ![]const u8;\n    pub fn write(self: Digest, writer: std.io.AnyWriter) !void;\n\n    /// Format a human-readable {code}-{len}-{hex} string for a Digest\n    pub fn format(self: Digest, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void;\n};\n```\n\n### CIDs\n\n```zig\npub const CID = struct {\n    pub const Version = enum { cidv0, cidv1 };\n\n    version: Version,\n    codec: Codec,\n    digest: Digest,\n\n    /// parse a CID from multibase-encoded string\n    pub fn parse(allocator: std.mem.Allocator, str: []const u8) !CID;\n\n    /// read a binary CID from a reader\n    pub fn read(allocator: std.mem.Allocator, reader: std.io.AnyReader) !CID;\n\n    /// decode a binary CID from bytes\n    pub fn decode(allocator: std.mem.Allocator, bytes: []const u8) !CID;\n\n    pub fn deinit(self: CID, allocator: std.mem.Allocator) void;\n\n    pub fn copy(self: CID, allocator: std.mem.Allocator) !CID;\n    pub fn eql(self: CID, other: CID) bool;\n    pub fn expectEqual(actual: CID, expected: CID) !void;\n    pub fn encodingLength(self: CID) usize;\n    pub fn write(self: CID, writer: std.io.AnyWriter) !void;\n    pub fn encode(self: CID, allocator: std.mem.Allocator) ![]const u8;\n\n    /// Return a multibase Formatter for a CID\n    pub fn formatBase(self: CID, base: multibase.Code) std.fmt.Formatter(formatBaseImpl);\n\n    /// Return a human-readable string Formatter for a CID\n    pub fn formatString(self: CID) std.fmt.Formatter(formatStringImpl);\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FnDimensional%2Fzig-multiformats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FnDimensional%2Fzig-multiformats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FnDimensional%2Fzig-multiformats/lists"}