1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
* Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
*
* libcbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef LIBCBOR_ENCODING_H
#define LIBCBOR_ENCODING_H
#include "cbor/cbor_export.h"
#include "cbor/common.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* All cbor_encode_* methods take 2 or 3 arguments:
* - a logical `value` to encode (except for trivial items such as NULLs)
* - an output `buffer` pointer
* - a `buffer_size` specification
*
* They serialize the `value` into one or more bytes and write the bytes to the
* output `buffer` and return either the number of bytes written, or 0 if the
* `buffer_size` was too small to small to fit the serialized value (in which
* case it is not modified).
*/
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint8(uint8_t, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint16(uint16_t, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint32(uint32_t, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint64(uint64_t, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint(uint64_t, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint8(uint8_t, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint16(uint16_t,
unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint32(uint32_t,
unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint64(uint64_t,
unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint(uint64_t, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bytestring_start(size_t,
unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t
cbor_encode_indef_bytestring_start(unsigned char *, size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_string_start(size_t,
unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t
cbor_encode_indef_string_start(unsigned char *, size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_array_start(size_t,
unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t
cbor_encode_indef_array_start(unsigned char *, size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_map_start(size_t,
unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_indef_map_start(unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_tag(uint64_t, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bool(bool, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_null(unsigned char *, size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_undef(unsigned char *, size_t);
/** Encodes a half-precision float
*
* Since there is no native representation or semantics for half floats
* in the language, we use single-precision floats, as every value that
* can be expressed as a half-float can also be expressed as a float.
*
* This however means that not all floats passed to this function can be
* unambiguously encoded. The behavior is as follows:
* - Infinity, NaN are preserved
* - Zero is preserved
* - Denormalized numbers keep their sign bit and 10 most significant bit of
* the significand
* - All other numbers
* - If the logical value of the exponent is < -24, the output is zero
* - If the logical value of the exponent is between -23 and -14, the output
* is cut off to represent the 'magnitude' of the input, by which we
* mean (-1)^{signbit} x 1.0e{exponent}. The value in the significand is
* lost.
* - In all other cases, the sign bit, the exponent, and 10 most significant
* bits of the significand are kept
*/
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_half(float, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_single(float, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_double(double, unsigned char *,
size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_break(unsigned char *, size_t);
_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_ctrl(uint8_t, unsigned char *,
size_t);
#ifdef __cplusplus
}
#endif
#endif // LIBCBOR_ENCODING_H
|