{"id":16874777,"url":"https://github.com/bergzand/nanocbor","last_synced_at":"2025-04-24T02:49:56.637Z","repository":{"id":34537187,"uuid":"179025654","full_name":"bergzand/NanoCBOR","owner":"bergzand","description":"CBOR library aimed at heavily constrained devices","archived":false,"fork":false,"pushed_at":"2024-06-26T12:29:38.000Z","size":374,"stargazers_count":50,"open_issues_count":13,"forks_count":24,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-24T02:49:51.308Z","etag":null,"topics":["c","cbor","embedded","iot"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bergzand.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":"2019-04-02T07:46:00.000Z","updated_at":"2024-12-30T14:34:49.000Z","dependencies_parsed_at":"2024-06-26T15:40:23.046Z","dependency_job_id":null,"html_url":"https://github.com/bergzand/NanoCBOR","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergzand%2FNanoCBOR","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergzand%2FNanoCBOR/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergzand%2FNanoCBOR/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bergzand%2FNanoCBOR/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bergzand","download_url":"https://codeload.github.com/bergzand/NanoCBOR/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250552035,"owners_count":21449161,"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":["c","cbor","embedded","iot"],"created_at":"2024-10-13T15:34:06.081Z","updated_at":"2025-04-24T02:49:56.619Z","avatar_url":"https://github.com/bergzand.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NanoCBOR\n\nNanoCBOR is a tiny [CBOR](https://tools.ietf.org/html/rfc7049) library aimed at embedded and heavily constrained devices.\nIt is optimized for 32-bit architectures but should run fine on 8-bit and 16-bit architectures.\nNanoCBOR is optimized for decoding known CBOR structures while optimizing the flash footprint of both NanoCBOR and the code using NanoCBOR.\n\nThe decoder of NanoCBOR should compile to 600-800 bytes on a Cortex-M0+ MCU, depending on whether floating point decoding is required.\n\n## Compiling\n\nCompiling NanoCBOR as is from this repository requires:\n- [Meson] \n- [Ninja]\n\nFurthermore, the tests make use of [CUnit] as test framework.\n\nAll of these can usually be found in the package repository of your Linux distribution.\n\nBuilding NanoCBOR is a two-step process. First a build directory has to be created with the necessary Ninja build files:\n\n```\nmeson build\n```\n\nSecond step is to compile NanoCBOR:\n\n```\nninja -C build\n```\n\nThis results in a `libnanocbor.so` file inside the `build` directory and binaries for the examples and tests in their respective directories inside the `build` directory.\n\nWhen including NanoCBOR into a custom project, it is usually sufficient to only include the source and header files into the project, the meson build system used in the repo is not mandatory to use.\n\n## Usage\n\nTo achieve the small code size, two patterns are used throughout the decode library.\n\n - Every decode call will first check the type and refuse to decode if the CBOR element is not of the required type.\n - Every decode call will, on successful decode, advance the decode context to the next CBOR element.\n\nThis allows using code to call decode functions and check the return code of the function without requiring an if value of type, decode value, advance to next item dance, and requiring only a single call to decode an expected type and advance to the next element.\n\nStart the decoding of a buffer with:\n\n```C\nnanocbor_value_t decoder;\nnanocbor_decoder_init(\u0026decoder, buffer, buffer_len);\n```\n\nWhere `buffer` is a `const uint8_t` array containing a CBOR structure.\n\nTo decode an `int32_t` from a CBOR structure and bail out if the element is not of the integer type:\n\n```C\nint32_t value = 0;\nif (nanocbor_get_int32(\u0026decoder, \u0026value) \u003c 0) {\n  return ERR_INVALID_STRUCTURE;\n}\nreturn use_value(value);\n```\n\nIterating over an CBOR array and calling a function passing every element is as simple as:\n\n```C\nnanocbor_value_t arr; /* Array value instance */\n\nif (nanocbor_enter_array(\u0026decoder, \u0026arr) \u003c 0) {\n    return ERR_INVALID_STRUCTURE;\n}\nwhile (!nanocbor_at_end(\u0026arr)) {\n    handle_array_element(\u0026arr);\n}\n```\n\nDecoding a map is similar to an array, except that every map entry consists of two CBOR elements requiring separate decoding.\nFor example, a map using integers as keys and strings as values can be decoded with:\n\n```C\nwhile (!nanocbor_at_end(\u0026map)) {\n    int32_t key;\n    const char *value;\n    size_t value_len;\n    if (nanocbor_get_int32(\u0026map, \u0026integer_key) \u003c 0) {\n        return ERR_INVALID_STRUCTURE;\n    }\n    if (nanocbor_get_tstr(\u0026map, \u0026value, \u0026value_len) \u003c 0) {\n        return ERR_INVALID_STRUCTURE;\n    }\n    handle_map_element(key, value, value_len);\n}\n```\n\n\n### Dependencies:\n\nOnly dependency are two functions to provide endian conversion.\nThese are not provided by the library and have to be configured in the header file.\nOn a bare metal ARM platform, `__builtin_bswap64` and `__builtin_bswap32` can be used for this conversion.\n\n### Contributing\n\nOpen an issue, PR, the usual.\n\n[Meson]: https://mesonbuild.com/\n[Ninja]: https://ninja-build.org/\n[CUnit]: https://cunit.sourceforge.net/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbergzand%2Fnanocbor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbergzand%2Fnanocbor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbergzand%2Fnanocbor/lists"}