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