110ff414cSEd Maste# [libcbor](https://github.com/PJK/libcbor) 210ff414cSEd Maste 35d3e7166SEd Maste[](https://circleci.com/gh/PJK/libcbor/tree/master) 410ff414cSEd Maste[](https://readthedocs.org/projects/libcbor/?badge=latest) 510ff414cSEd Maste[](https://repology.org/project/libcbor/versions) 610ff414cSEd Maste[](https://codecov.io/gh/PJK/libcbor) 710ff414cSEd Maste 8*abd87254SEd Maste**libcbor** is a C library for parsing and generating [CBOR](https://cbor.io/), the general-purpose schema-less binary data format. 910ff414cSEd Maste 1010ff414cSEd Maste## Main features 11*abd87254SEd Maste - Complete [IETF RFC 8949 (STD 94)](https://www.rfc-editor.org/info/std94) conformance 12*abd87254SEd Maste - Robust platform-independent C99 implementation 1310ff414cSEd Maste - Layered architecture offers both control and convenience 1410ff414cSEd Maste - Flexible memory management 1510ff414cSEd Maste - No shared global state - threading friendly 1610ff414cSEd Maste - Proper handling of UTF-8 1710ff414cSEd Maste - Full support for streams & incremental processing 1810ff414cSEd Maste - Extensive documentation and test suite 1910ff414cSEd Maste - No runtime dependencies, small footprint 2010ff414cSEd Maste 2110ff414cSEd Maste## Getting started 2210ff414cSEd Maste 2310ff414cSEd Maste### Compile from source 2410ff414cSEd Maste 2510ff414cSEd Maste```bash 2610ff414cSEd Mastegit clone https://github.com/PJK/libcbor 275d3e7166SEd Mastecmake -DCMAKE_BUILD_TYPE=Release libcbor 2810ff414cSEd Mastemake 2910ff414cSEd Mastemake install 3010ff414cSEd Maste``` 3110ff414cSEd Maste 3210ff414cSEd Maste### Homebrew 3310ff414cSEd Maste 3410ff414cSEd Maste```bash 3510ff414cSEd Mastebrew install libcbor 3610ff414cSEd Maste``` 3710ff414cSEd Maste 3810ff414cSEd Maste### Ubuntu 18.04 and above 3910ff414cSEd Maste 4010ff414cSEd Maste```bash 4110ff414cSEd Mastesudo add-apt-repository universe 4210ff414cSEd Mastesudo apt-get install libcbor-dev 4310ff414cSEd Maste``` 4410ff414cSEd Maste 4510ff414cSEd Maste### Fedora & RPM friends 4610ff414cSEd Maste 4710ff414cSEd Maste```bash 4810ff414cSEd Masteyum install libcbor-devel 4910ff414cSEd Maste``` 5010ff414cSEd Maste 5110ff414cSEd Maste### Others 5210ff414cSEd Maste 5310ff414cSEd Maste<details> 5410ff414cSEd Maste <summary>Packaged libcbor is available from 15+ major repositories. Click here for more detail</summary> 5510ff414cSEd Maste 5610ff414cSEd Maste [](https://repology.org/project/libcbor/versions) 5710ff414cSEd Maste</details> 5810ff414cSEd Maste 5910ff414cSEd Maste## Usage example 6010ff414cSEd Maste 6110ff414cSEd Maste```c 6210ff414cSEd Maste#include <cbor.h> 6310ff414cSEd Maste#include <stdio.h> 6410ff414cSEd Maste 655d3e7166SEd Masteint main(void) { 6610ff414cSEd Maste /* Preallocate the map structure */ 6710ff414cSEd Maste cbor_item_t* root = cbor_new_definite_map(2); 6810ff414cSEd Maste /* Add the content */ 695d3e7166SEd Maste bool success = cbor_map_add( 705d3e7166SEd Maste root, (struct cbor_pair){ 7110ff414cSEd Maste .key = cbor_move(cbor_build_string("Is CBOR awesome?")), 725d3e7166SEd Maste .value = cbor_move(cbor_build_bool(true))}); 735d3e7166SEd Maste success &= cbor_map_add( 745d3e7166SEd Maste root, (struct cbor_pair){ 7510ff414cSEd Maste .key = cbor_move(cbor_build_uint8(42)), 765d3e7166SEd Maste .value = cbor_move(cbor_build_string("Is the answer"))}); 775d3e7166SEd Maste if (!success) return 1; 7810ff414cSEd Maste /* Output: `length` bytes of data in the `buffer` */ 7910ff414cSEd Maste unsigned char* buffer; 805d3e7166SEd Maste size_t buffer_size; 815d3e7166SEd Maste cbor_serialize_alloc(root, &buffer, &buffer_size); 8210ff414cSEd Maste 835d3e7166SEd Maste fwrite(buffer, 1, buffer_size, stdout); 8410ff414cSEd Maste free(buffer); 8510ff414cSEd Maste 8610ff414cSEd Maste fflush(stdout); 8710ff414cSEd Maste cbor_decref(&root); 8810ff414cSEd Maste} 8910ff414cSEd Maste``` 9010ff414cSEd Maste 9110ff414cSEd Maste## Documentation 9210ff414cSEd MasteGet the latest documentation at [libcbor.readthedocs.org](http://libcbor.readthedocs.org/) 9310ff414cSEd Maste 9410ff414cSEd Maste## Contributions 9510ff414cSEd Maste 965d3e7166SEd MasteBug reports and contributions are welcome. Please see [CONTRIBUTING.md](https://github.com/PJK/libcbor/blob/master/CONTRIBUTING.md) for more info. 9710ff414cSEd Maste 9810ff414cSEd MasteKudos to all the [contributors](https://github.com/PJK/libcbor/graphs/contributors)! 9910ff414cSEd Maste 10010ff414cSEd Maste## License 10110ff414cSEd MasteThe MIT License (MIT) 10210ff414cSEd Maste 10310ff414cSEd MasteCopyright (c) Pavel Kalvoda, 2014-2020 10410ff414cSEd Maste 10510ff414cSEd MastePermission is hereby granted, free of charge, to any person obtaining a copy 10610ff414cSEd Masteof this software and associated documentation files (the "Software"), to deal 10710ff414cSEd Mastein the Software without restriction, including without limitation the rights 10810ff414cSEd Masteto use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10910ff414cSEd Mastecopies of the Software, and to permit persons to whom the Software is 11010ff414cSEd Mastefurnished to do so, subject to the following conditions: 11110ff414cSEd Maste 11210ff414cSEd MasteThe above copyright notice and this permission notice shall be included in all 11310ff414cSEd Mastecopies or substantial portions of the Software. 11410ff414cSEd Maste 11510ff414cSEd MasteTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 11610ff414cSEd MasteIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 11710ff414cSEd MasteFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 11810ff414cSEd MasteAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 11910ff414cSEd MasteLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 12010ff414cSEd MasteOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 12110ff414cSEd MasteSOFTWARE. 122