xref: /freebsd/contrib/libcbor/README.md (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1# [libcbor](https://github.com/PJK/libcbor)
2
3[![Build Status](https://travis-ci.org/PJK/libcbor.svg?branch=master)](https://travis-ci.org/PJK/libcbor)
4[![Build status](https://ci.appveyor.com/api/projects/status/8kkmvmefelsxp5u2?svg=true)](https://ci.appveyor.com/project/PJK/libcbor)
5[![Documentation Status](https://readthedocs.org/projects/libcbor/badge/?version=latest)](https://readthedocs.org/projects/libcbor/?badge=latest)
6[![latest packaged version(s)](https://repology.org/badge/latest-versions/libcbor.svg)](https://repology.org/project/libcbor/versions)
7[![codecov](https://codecov.io/gh/PJK/libcbor/branch/master/graph/badge.svg)](https://codecov.io/gh/PJK/libcbor)
8
9**libcbor** is a C library for parsing and generating [CBOR](http://tools.ietf.org/html/rfc7049), the general-purpose schema-less binary data format.
10
11## Main features
12 - Complete RFC conformance
13 - Robust C99 implementation
14 - Layered architecture offers both control and convenience
15 - Flexible memory management
16 - No shared global state - threading friendly
17 - Proper handling of UTF-8
18 - Full support for streams & incremental processing
19 - Extensive documentation and test suite
20 - No runtime dependencies, small footprint
21
22## Getting started
23
24### Compile from source
25
26```bash
27git clone https://github.com/PJK/libcbor
28cmake -DCMAKE_BUILD_TYPE=Release -DCBOR_CUSTOM_ALLOC=ON libcbor
29make
30make install
31```
32
33### Homebrew
34
35```bash
36brew install libcbor
37```
38
39### Ubuntu 18.04 and above
40
41```bash
42sudo add-apt-repository universe
43sudo apt-get install libcbor-dev
44```
45
46### Fedora & RPM friends
47
48```bash
49yum install libcbor-devel
50```
51
52### Others
53
54<details>
55  <summary>Packaged libcbor is available from 15+ major repositories. Click here for more detail</summary>
56
57  [![Packaging status](https://repology.org/badge/vertical-allrepos/libcbor.svg)](https://repology.org/project/libcbor/versions)
58</details>
59
60## Usage example
61
62```c
63#include <cbor.h>
64#include <stdio.h>
65
66int main(int argc, char * argv[])
67{
68	/* Preallocate the map structure */
69	cbor_item_t * root = cbor_new_definite_map(2);
70	/* Add the content */
71	cbor_map_add(root, (struct cbor_pair) {
72		.key = cbor_move(cbor_build_string("Is CBOR awesome?")),
73		.value = cbor_move(cbor_build_bool(true))
74	});
75	cbor_map_add(root, (struct cbor_pair) {
76		.key = cbor_move(cbor_build_uint8(42)),
77		.value = cbor_move(cbor_build_string("Is the answer"))
78	});
79	/* Output: `length` bytes of data in the `buffer` */
80	unsigned char * buffer;
81	size_t buffer_size,
82		length = cbor_serialize_alloc(root, &buffer, &buffer_size);
83
84	fwrite(buffer, 1, length, stdout);
85	free(buffer);
86
87	fflush(stdout);
88	cbor_decref(&root);
89}
90```
91
92## Documentation
93Get the latest documentation at [libcbor.readthedocs.org](http://libcbor.readthedocs.org/)
94
95## Contributions
96
97All bug reports and contributions are welcome. Please see https://github.com/PJK/libcbor for more info.
98
99Kudos to all the [contributors](https://github.com/PJK/libcbor/graphs/contributors)!
100
101## License
102The MIT License (MIT)
103
104Copyright (c) Pavel Kalvoda, 2014-2020
105
106Permission is hereby granted, free of charge, to any person obtaining a copy
107of this software and associated documentation files (the "Software"), to deal
108in the Software without restriction, including without limitation the rights
109to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
110copies of the Software, and to permit persons to whom the Software is
111furnished to do so, subject to the following conditions:
112
113The above copyright notice and this permission notice shall be included in all
114copies or substantial portions of the Software.
115
116THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
117IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
118FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
119AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
120LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
121OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
122SOFTWARE.
123