Decoding
libcbor provides two decoding interfaces:
Default |
Streaming |
|
|---|---|---|
Function |
||
Returns |
A fully-built |
Nothing. Fires a callback for each decoded value. |
Callbacks |
None. Results available after the call returns. |
One callback per primitive, string chunk, or
collection boundary (e.g. |
Nesting |
Handled by the library. |
Caller’s responsibility. Nested structures (e.g. arrays containing maps) require the caller to maintain an explicit stack to track the current depth and accumulate children. |
Memory |
Allocates the |
No library allocations. |
Best for |
|
|
cbor_load() is implemented on top of cbor_stream_decode() — it
installs its own internal callbacks that build the cbor_item_t tree as
events arrive.
![digraph decoding {
graph [rankdir=TB, nodesep=1.4, ranksep=0.8, fontname="Helvetica"]
node [shape=box, style="filled,rounded", fontname="Helvetica", margin="0.3,0.15"]
edge [color="#555555", fontname="Helvetica", fontsize=10]
client [label="Client application", fillcolor="#AED6F1"]
cbor_load [label="cbor_load", fillcolor="#FAD7A0"]
streaming [label="cbor_stream_decode", fillcolor="#FAD7A0"]
item [label="cbor_item_t", fillcolor="#A9DFBF"]
manip [label="Manipulation routines\n(cbor_item_t API)", fillcolor="#D2B4DE"]
{ rank=same; cbor_load; streaming }
{ rank=same; item; manip }
client -> cbor_load [label=" bytes "]
client -> streaming [label=" bytes + callbacks "]
cbor_load -> streaming [label=" internal callbacks ",
style=dashed, constraint=false]
cbor_load -> item
item:e -> manip:w [style=dashed, dir=both, constraint=false,
label=" use items "]
}](../_images/graphviz-adb9e4c0a20d5aeb03c8a77c27ed7e115255554c.png)
This section covers the Default driver — cbor_load() and related
routines that decode a complete CBOR input into a cbor_item_t tree in one
call.
Warning
cbor_load allocates memory sized by lengths declared in the CBOR header
before reading the corresponding data:
Definite-length arrays and maps — storage for the declared element count.
Definite-length strings and bytestrings — a buffer for the declared byte length.
All of these lengths are encoded as a 64-bit integer, so cbor_load may
attempt an allocation of up to 264−1 bytes before any payload data
is read. malloc will normally refuse such a request and cbor_load
will return CBOR_ERR_MEMERROR, but on platforms with memory overcommit
(Linux by default) the allocation may silently appear to succeed.
Mitigations:
Install a capping allocator via
cbor_set_allocs()to bound total memory consumption (seeexamples/capped_alloc.cfor a self-contained example).Use the streaming decoder (Streaming Decoding), which gives the application full control over memory allocation for each decoded item.
-
cbor_item_t *cbor_load(cbor_data source, size_t source_size, struct cbor_load_result *result)
Loads data item from a buffer.
- param source:
The buffer
- param source_size:
- param result:
[out] Result indicator. CBOR_ERR_NONE on success
- return:
Decoded CBOR item. The item’s reference count is initialized to one.
- return:
NULLon failure. In that case,resultcontains the location and description of the error.
Associated data structures
-
enum cbor_error_code
Possible decoding errors.
Values:
-
enumerator CBOR_ERR_NONE
-
enumerator CBOR_ERR_NOTENOUGHDATA
-
enumerator CBOR_ERR_NODATA
-
enumerator CBOR_ERR_MALFORMATED
-
enumerator CBOR_ERR_MEMERROR
Memory error - item allocation failed.
Is it too big for your allocator?
-
enumerator CBOR_ERR_SYNTAXERROR
Stack parsing algorithm failed.
-
enumerator CBOR_ERR_NONE
-
struct cbor_load_result
High-level decoding result.
-
struct cbor_error
High-level decoding error.