All Projects → onkwon → cbor

onkwon / cbor

Licence: MIT license
An implementation of CBOR in C

Programming Languages

c
50402 projects - #5 most used programming language
Makefile
30231 projects
CMake
9771 projects

Projects that are alternatives of or similar to cbor

Dahomey.Cbor
High-performance CBOR (RFC 7049) serialization framework for .Net (C#)
Stars: ✭ 47 (+67.86%)
Mutual labels:  cbor
Rpc.py
A fast and powerful RPC framework based on ASGI/WSGI.
Stars: ✭ 98 (+250%)
Mutual labels:  cbor
Cbor
CBOR support for serde.
Stars: ✭ 238 (+750%)
Mutual labels:  cbor
Kotlinx.serialization
Kotlin multiplatform / multi-format serialization
Stars: ✭ 3,550 (+12578.57%)
Mutual labels:  cbor
Treefrog Framework
TreeFrog Framework : High-speed C++ MVC Framework for Web Application
Stars: ✭ 885 (+3060.71%)
Mutual labels:  cbor
Borer
Efficient CBOR and JSON (de)serialization in Scala
Stars: ✭ 131 (+367.86%)
Mutual labels:  cbor
nlohmann json release
json for modern c++ (single header file, release versions)
Stars: ✭ 40 (+42.86%)
Mutual labels:  cbor
fs2-data
streaming data parsing and transformation library
Stars: ✭ 103 (+267.86%)
Mutual labels:  cbor
Qcbor
QCBOR -- a small CBOR encoder/decoder oriented around C and native data representations
Stars: ✭ 48 (+71.43%)
Mutual labels:  cbor
Jackson Dataformats Binary
Uber-project for standard Jackson binary format backends: avro, cbor, ion, protobuf, smile
Stars: ✭ 221 (+689.29%)
Mutual labels:  cbor
Json
C++ header-only JSON library
Stars: ✭ 343 (+1125%)
Mutual labels:  cbor
Json
JSON for Modern C++
Stars: ✭ 27,824 (+99271.43%)
Mutual labels:  cbor
Libxo
The libxo library allows an application to generate text, XML, JSON, and HTML output using a common set of function calls. The application decides at run time which output style should be produced.
Stars: ✭ 185 (+560.71%)
Mutual labels:  cbor
Tinycbor
Concise Binary Object Representation (CBOR) Library
Stars: ✭ 263 (+839.29%)
Mutual labels:  cbor
Cbor
CBOR RFC 7049 (Go/Golang) - safe & fast with standard API + toarray & keyasint, CBOR tags, float64/32/16, fuzz tested.
Stars: ✭ 250 (+792.86%)
Mutual labels:  cbor
libcose
Constrained node COSE library
Stars: ✭ 16 (-42.86%)
Mutual labels:  cbor
Kripton
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.
Stars: ✭ 110 (+292.86%)
Mutual labels:  cbor
jwt-compact
Compact JWT implementation in Rust
Stars: ✭ 26 (-7.14%)
Mutual labels:  cbor
restish
Restish is a CLI for interacting with REST-ish HTTP APIs with some nice features built-in
Stars: ✭ 453 (+1517.86%)
Mutual labels:  cbor
Libcbor
CBOR protocol implementation for C
Stars: ✭ 215 (+667.86%)
Mutual labels:  cbor

CBOR

This is a minimalistic implementation for CBOR, the Concise Binary Object Representation. CBOR is defined by IETF RFC 8949, and Wikipedia has a good description.

Features

  • C99
  • No dynamic memory allocation
  • Small code footprint

Build

Make

CBOR_ROOT ?= <THIRD_PARTY_DIR>/cbor
include $(CBOR_ROOT)/cbor.mk

CMake

include(FetchContent)
FetchContent_Declare(cbor
                      GIT_REPOSITORY https://github.com/libmcu/cbor.git
                      GIT_TAG main
)
FetchContent_MakeAvailable(cbor)

or

set(CBOR_ROOT <THIRD_PARTY_DIR>/cbor)
include(${CBOR_ROOT}/cbor.cmake)

Usage

Please see the examples.

  • CBOR_BIG_ENDIAN
    • Define the macro for big endian machine. The default is little endian.
  • CBOR_RECURSION_MAX_LEVEL
    • This is set to avoid stack overflow from recursion. The default maximum depth is 8.

Parser

The parser takes 626 bytes on ARM Cortex-M0 optimizing for code size -Os. arm-none-eabi-gcc 10-2020-q4-major was used for the check.

Stack usage per the major type functions:

Major type Bytes
0: unsigned integer 12
1: negative integer 12
2: byte string 32
3: text string 32
4: array 32
5: map 32
6: tag(not implemented yet) 0
7: floating-point numbers, simple values and break 32

And the call stack for each recursion is 24 bytes.

cbor_reader_t reader;
cbor_item_t items[MAX_ITEMS];
size_t n;

cbor_reader_init(&reader, items, sizeof(items) / sizeof(*items));
cbor_parse(&reader, cbor_message, cbor_message_len, &n);

for (i = 0; i < n; i++) {
	printf("item: %s, size: %zu\n",
			cbor_stringify_item(&items[i]),
			cbor_get_item_size(&items[i]);
}

Decoder

union {
	int8_t i8;
	int16_t i16;
	int32_t i32;
	int64_t i64;
	float f32;
	double f64;
	uint8_t s[MTU];
} val;

cbor_decode(&reader, &items[i], &val, sizeof(val));

Encoder

cbor_writer_t writer;

cbor_writer_init(&reader, buf, sizeof(buf));

cbor_encode_map(&writer, 2);
  /* 1st */
  cbor_encode_text_string(&writer, "key");
  cbor_encode_text_string(&writer, "value");
  /* 2nd */
  cbor_encode_text_string(&writer, "age");
  cbor_encode_negative_integer(&writer, -1);

Limitation

  • The maximum item length is size_t because the interface return type is size_t. The argument's value in the specification can go up to uint64_t though
  • A negative integer ranges down to -2^63-1 other than -2^64 in the specification
  • Sorting of encoded map keys is not supported
  • Tag item is not implemented yet
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].