xref: /freebsd/contrib/libcbor/README.md (revision e0c4386e)
1# [libcbor](https://github.com/PJK/libcbor)
2
3[![CircleCI](https://circleci.com/gh/PJK/libcbor/tree/master.svg?style=svg)](https://circleci.com/gh/PJK/libcbor/tree/master)
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](https://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 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(void) {
67  /* Preallocate the map structure */
68  cbor_item_t* root = cbor_new_definite_map(2);
69  /* Add the content */
70  bool success = cbor_map_add(
71      root, (struct cbor_pair){
72                .key = cbor_move(cbor_build_string("Is CBOR awesome?")),
73                .value = cbor_move(cbor_build_bool(true))});
74  success &= cbor_map_add(
75      root, (struct cbor_pair){
76                .key = cbor_move(cbor_build_uint8(42)),
77                .value = cbor_move(cbor_build_string("Is the answer"))});
78  if (!success) return 1;
79  /* Output: `length` bytes of data in the `buffer` */
80  unsigned char* buffer;
81  size_t buffer_size;
82  cbor_serialize_alloc(root, &buffer, &buffer_size);
83
84  fwrite(buffer, 1, buffer_size, 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
97Bug reports and contributions are welcome. Please see [CONTRIBUTING.md](https://github.com/PJK/libcbor/blob/master/CONTRIBUTING.md) 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