{"id":18031727,"url":"https://github.com/dagronf/bytesparser","last_synced_at":"2025-10-26T06:48:00.162Z","repository":{"id":177720152,"uuid":"660424093","full_name":"dagronf/BytesParser","owner":"dagronf","description":"A simple byte-oriented parser/writer. Read and write formatted values to/from binary blobs/files with ease! ","archived":false,"fork":false,"pushed_at":"2025-04-22T05:04:08.000Z","size":201,"stargazers_count":8,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-04T00:06:52.638Z","etag":null,"topics":["binary","endian","endianness","parsing","reading","swift","writing"],"latest_commit_sha":null,"homepage":"","language":"Swift","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/dagronf.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":"2023-06-30T01:33:53.000Z","updated_at":"2025-07-31T15:14:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"b65ceb8a-709b-446e-a207-d4bf9647c71d","html_url":"https://github.com/dagronf/BytesParser","commit_stats":null,"previous_names":["dagronf/bytesparser"],"tags_count":21,"template":false,"template_full_name":null,"purl":"pkg:github/dagronf/BytesParser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FBytesParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FBytesParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FBytesParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FBytesParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dagronf","download_url":"https://codeload.github.com/dagronf/BytesParser/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FBytesParser/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274748362,"owners_count":25341942,"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-12T02:00:09.324Z","response_time":60,"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":["binary","endian","endianness","parsing","reading","swift","writing"],"created_at":"2024-10-30T10:10:35.803Z","updated_at":"2025-10-26T06:47:55.139Z","avatar_url":"https://github.com/dagronf.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BytesParser\n\nA simple byte-oriented parser/writer. Read and write formatted values to/from binary blobs/files with ease! \n\n![tag](https://img.shields.io/github/v/tag/dagronf/BytesParser)\n![Platform support](https://img.shields.io/badge/platform-ios%20%7C%20osx%20%7C%20tvos%20%7C%20watchos%20%7C%20macCatalyst%20%7C%20linux-lightgrey.svg?style=flat-square)\n[![License MIT](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/dagronf/BytesParser/blob/master/LICENSE) ![Build](https://img.shields.io/github/actions/workflow/status/dagronf/BytesParser/swift.yml)\n\nSupports :-\n\n* reading to/writing from a `Data` object, a file URL or an `InputStream` object\n* different endianness, even within the same file\n* different string encodings, including multi-byte encodings like UInt32\n* explicit string null-termination handling\n* explicit endian handling for appropriate types (eg. `Int32`) so no unintentional byte swaps.\n* padding to byte boundaries\n\n[Online Documentation](https://swiftpackageindex.com/dagronf/BytesParser/main/documentation/bytesparser)\n\n## Reading\n\nThe `BytesReader` class type provides the basic mechanism for reading and extracting useful information from binary data.\n\n### Using a BytesReader Object \n\n```swift\nlet reader = BytesReader(data: \u003csome data object\u003e)\n\n// The magic identifier for the file is an 8-byte ascii string (not null terminated)\nlet magic = try reader.readString(length: 8, encoding: .ascii)\nassert(\"8BCBAFFE\", magic)\n\n// Read 12-character little-endian encoded WCS2 (UTF16) string \nlet title = try reader.readStringUTF16(.little, length: 12)\nlet length = try reader.readInt16(.little)\nlet offset = try reader.readInt32(.little)\n```\n\n### Closure based\n\nThe closure based API hides some of the complexity of opening/closing a bytes file/blob.\n\n#### Example\n\n```swift\n// Parsing a (local) crossword file\ntry BytesReader.read(fileURL: url) { reader in\n   let checksum = try reader.readUInt16(.little)\n   let magic = try reader.readStringASCII(length: 12, lengthIncludesTerminator: true)\n   assert(magic, \"ACROSS\u0026DOWN\")\n\n   let width = try reader.readInt8()\n   let height = try reader.readInt8()\n\n   let clueCount = try reader.readUInt16(.little)\n   ...\n   let title = try parser.readStringNullTerminated(encoding: .ascii)\n}\n```\n\n## Writing\n\nThe `BytesWriter` class allows you to write formatted binary data to a file/data\n\nThe basic process is as follows :-\n\n1. Create a new `BytesWriter` writer object with the appropriate initializer \n2. Write to the writer object using the `.write(...)` methods\n3. Call `complete()` on the writer to close any open file(s) and make the data ready. **Failure to call `.complete()` may render your data unreadable.**\n\n* If using a `BytesWriter` to write to a data object, call the `.data()` method to retrieve the data. Calling `.data()` on a file-based writer will throw an error.\n\n### Using a BytesWriter Object \n\n```swift\nlet data: [UInt8] = [0x20, 0x40, 0x60, 0x80]\n\nlet writer = try BytesWriter()\ntry writer.writeBytes(data)\n\n// Mark the content as complete\nwriter.complete()\n\nlet myData = writer.data()\n```\n\n### Closure based\n\nThe closure based API hides some of the complexity of opening/closing a writing destination.\nYou won't need to call `complete()` at the end of an build block.\n\n```swift\n// Write to a Data object \nstatic func build(_ block: (BytesWriter) throws -\u003e Void) throws -\u003e Data\n```\n\n```swift\n// Write to a file\nstatic func build(fileURL: URL, _ block: (BytesWriter) throws -\u003e Void) throws\n```\n\n#### Example\n\n```swift\nlet message = \"10. 好き 【す・き】 (na-adj) – likable; desirable\"\ntry BytesWriter.build(fileURL: fileURL) { writer in\n   // Write out a 'message' block\n   // \u003e Write out the block type identifier\n   try writer.writeByte(0x78)\n\t// \u003e Write message length (big-endian UInt32)\n   try writer.writeUInt32(...message length in UTF32 chars..., .bigEndian)\n   // \u003e Write out the message in a big-endian, UTF32 format\n   try writer.writeStringUTF32BE(message)\n\n   // Write a 'data' block\n\t// \u003e Write out the block type identifier\n   try writer.writeByte(0x33)\n   // \u003e Write data length (big-endian UInt32)\n   try writer.writeUInt32(...length of data..., .bigEndian)\n   // \u003e Write raw data as bytes\n   try writer.writeData(...data...)\n}\n```\n\n## Notes\n\n* Forward reading only (no rewinding!)\n* Intentionally not thread safe (no calling from multiple different threads!)\n\n## License\n\n```\nMIT License\n\nCopyright (c) 2025 Darren Ford\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdagronf%2Fbytesparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdagronf%2Fbytesparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdagronf%2Fbytesparser/lists"}