{"id":29691965,"url":"https://github.com/devlarley/binarystruct","last_synced_at":"2026-02-13T09:17:49.649Z","repository":{"id":305810180,"uuid":"911529502","full_name":"DevLARLEY/BinaryStruct","owner":"DevLARLEY","description":"Declarative data structures that allow for binary parsing and building","archived":false,"fork":false,"pushed_at":"2025-01-03T10:42:56.000Z","size":26,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-06T04:22:12.028Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DevLARLEY.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,"zenodo":null}},"created_at":"2025-01-03T08:29:46.000Z","updated_at":"2025-09-29T03:58:06.000Z","dependencies_parsed_at":"2025-07-22T06:35:51.387Z","dependency_job_id":"d2cf8686-5bbb-4bb2-927e-b645bf62f488","html_url":"https://github.com/DevLARLEY/BinaryStruct","commit_stats":null,"previous_names":["devlarley/binarystruct"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/DevLARLEY/BinaryStruct","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevLARLEY%2FBinaryStruct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevLARLEY%2FBinaryStruct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevLARLEY%2FBinaryStruct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevLARLEY%2FBinaryStruct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DevLARLEY","download_url":"https://codeload.github.com/DevLARLEY/BinaryStruct/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevLARLEY%2FBinaryStruct/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29400512,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-13T06:24:03.484Z","status":"ssl_error","status_checked_at":"2026-02-13T06:23:12.830Z","response_time":78,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":"2025-07-23T07:08:42.856Z","updated_at":"2026-02-13T09:17:49.635Z","avatar_url":"https://github.com/DevLARLEY.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BinaryStruct\nDeclarative data structures that allow for binary parsing and building. \\\nHeavily based on the base functions of the python construct library.\n\n# Installing\n```\ndotnet add package BinaryStruct\n```\n\n# Examples\n\n## General Usage\n\n```csharp\nusing BinaryStruct;\nusing static BinaryStruct.ParserBuilder;\n\nvar example = new Struct(\n    Int16ub(\"size\"),\n    Bytes(\"data\", ctx =\u003e ctx[\"size\"]),\n    Int8ub(\"count\"),\n    Array(\"items\", Int16ul(string.Empty), ctx =\u003e ctx[\"count\"])\n);\n\n// Input data: 001000000000000000000000000000000000040100020003000400\n\nvar result = example.Parse(new byte[] { ... });\n// OR\n// using var stream = File.OpenRead(args[0]);\n// var result = example.Parse(stream);\n\n// Results in:\n/*\n *  new Dictionary\u003cstring, object\u003e{\n *      { \"size\", (ushort)16 },\n *      { \"data\", new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },\n *      { \"count\", (byte)4 },\n *      { \"items\", new List\u003cobject\u003e{ (ushort)1, (ushort)2, (ushort)3, (ushort)4 },\n *  }\n */\n\nvar bytes = example.Build(result);\n// Output data: 001000000000000000000000000000000000040100020003000400\n```\n\n## Fields\n\n### IntField\n```csharp\nvar example = new Struct(\n    Int8ub(\"Int8ub\"),   // big-endian byte\n    Int8ul(\"Int8ul\"),   // little-endian byte\n    Int8sb(\"Int8sb\"),   // big-endian sbyte\n    Int8sl(\"Int8sl\"),   // little-endian sbyte\n    Int16ub(\"Int16ub\"), // big-endian ushort\n    Int16ul(\"Int16ul\"), // little-endian ushort\n    Int16sb(\"Int16sb\"), // big-endian short\n    Int16sl(\"Int16sl\"), // little-endian short\n    Int32ub(\"Int32ub\"), // big-endian uint\n    Int32ul(\"Int32ul\"), // little-endian uint\n    Int32sb(\"Int32sb\"), // big-endian int\n    Int32sl(\"Int32sl\"), // little-endian int\n    Int64ub(\"Int64ub\"), // big-endian ulong\n    Int64ul(\"Int64ul\"), // little-endian ulong\n    Int64sb(\"Int64sb\"), // big-endian long\n    Int64sl(\"Int64sl\")  // little-endian long\n);\n```\n\n### BytesField\n```csharp\nvar example = new Struct(\n    Bytes(\"data1\", 16), // static length\n    Bytes(\"data2\", ctx =\u003e ctx[\"Int8sl\"]) // get length from field named 'Int8sl'\n);\n```\n\n### ConstField\n```csharp\nvar example = new Struct(\n    Const(\"const\", \"CONST\"u8.ToArray()) // always expects 'CONST'\n);\n```\n\n### StringField\n```csharp\nvar example = new Struct(\n    ASCIIString(\"asciiString1\", 16), // static length ASCII string\n    ASCIIString(\"asciiString2\", ctx =\u003e ctx[\"Int8sl\"]), // get length from field named 'Int8sl'\n    UTF8String(\"utf8String1\", 16), // static length UTF-8 string\n    UTF8String(\"utf8String2\", ctx =\u003e ctx[\"Int8sl\"]), // get length from field named 'Int8sl'\n    UTF16String(\"utf16String1\", 18), // static length UTF-16/Unicode string\n    UTF16String(\"utf16String2\", ctx =\u003e (sbyte)ctx[\"Int8sl\"] + 2) // cast for addition, etc...\n);\n```\n\n### ArrayField\n```csharp\nvar example = new Struct(\n    Array(\"array\", Int16ub(string.Empty), ctx =\u003e ctx[\"Int8sl\"]), // get count from field named 'Int8sl'\n    Array(\"array2\", Int16ub(string.Empty), 2) // static count\n);\n```\n\n### SwitchField\n```csharp\nvar example = new Struct(\n    Switch(\"switch\", ctx =\u003e ctx[\"Int8sl\"], i =\u003e i switch // switch based on value of field named 'Int8sl'\n    {\n        1 =\u003e Int32ub(string.Empty),\n        _ =\u003e throw new ArgumentOutOfRangeException(nameof(i), i, null)\n    }),\n    Switch(\"switch2\", 2, i =\u003e i switch // static value\n    {\n        1 =\u003e Int32ub(string.Empty),\n        _ =\u003e Bytes(string.Empty, 4)\n    })\n);\n```\n\n### ConditionalField\n```csharp\nvar example = new Struct(\n    IfThenElse(\"ifThenElse\", ctx =\u003e ctx[\"Int8sl\"], Bytes(string.Empty, 6), Bytes(string.Empty, 4)), // basically ctx[\"Int8sl\"] ? Bytes(string.Empty, 6) : Bytes(string.Empty, 4))\n    IfThenElse(\"ifThenElse2\", false, Bytes(string.Empty, 6), Bytes(string.Empty, 4)), // basically ctx[\"Int8sl\"] ? Bytes(string.Empty, 6) : Bytes(string.Empty, 4))\n    If(\"if\", ctx =\u003e ctx[\"Int8sl\"], Bytes(string.Empty, 6)), // writes nothing and returns new object() if ctx[\"Int8sl\"] is false\n    If(\"if2\", false, Bytes(string.Empty, 4)) // writes nothing and returns new object()\n);\n```\n\n### RangeField\n```csharp\nvar example = new Struct(\n    Range(\"range\", Int32ub(string.Empty), 1, 2) // will repeat Int32ub two times but only throws an error if it crashes earlier and has read less than one item\n    Range(\"range\", Int32ub(string.Empty), 1, ctx =\u003e ctx[\"Int16ub\"]) // variable counts\n    Range(\"range\", Int32ub(string.Empty), ctx =\u003e ctx[\"Int8sl\"], 2) // variable counts\n    Range(\"range\", Int32ub(string.Empty), ctx =\u003e ctx[\"Int8sl\"], ctx =\u003e ctx[\"Int16ub\"]) // variable counts\n    GreedyRange(\"greedy\", Const(string.Empty, \"string\"u8.ToArray())) // reads as much as possible until it crashes (errors are catched, so debugging is difficult)\n);\n```\n\n### StructField\n```csharp\nvar example = new Struct(\n    Child(\"subStruct\", SubStruct) // include another Struct\n);\n```\n\n```csharp\n// Recursiveness\nprivate static readonly Struct Record = new(\n    Int16ub(\"flags\"),\n    Int16ub(\"type\"),\n    Int32ub(\"length\"),\n    Switch(\"data\", ctx =\u003e ctx[\"type\"], i =\u003e i switch \n    {\n        1 =\u003e Child(string.Empty, SubStruct1),\n        2 =\u003e Child(string.Empty, SubStruct2),\n        _ =\u003e Child(string.Empty, () =\u003e Record!) // References itself (must be a lambda)\n    })\n);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevlarley%2Fbinarystruct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevlarley%2Fbinarystruct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevlarley%2Fbinarystruct/lists"}