xref: /netbsd-src/external/mit/libcbor/dist/src/cbor/encoding.h (revision 5dd36a3bc8bf2a9dec29ceb6349550414570c447)
1 /*
2  * Copyright (c) 2014-2019 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/common.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /*
18  * ============================================================================
19  * Primitives encoding
20  * ============================================================================
21  */
22 
23 size_t cbor_encode_uint8(uint8_t, unsigned char *, size_t);
24 
25 size_t cbor_encode_uint16(uint16_t, unsigned char *, size_t);
26 
27 size_t cbor_encode_uint32(uint32_t, unsigned char *, size_t);
28 
29 size_t cbor_encode_uint64(uint64_t, unsigned char *, size_t);
30 
31 size_t cbor_encode_uint(uint64_t, unsigned char *, size_t);
32 
33 size_t cbor_encode_negint8(uint8_t, unsigned char *, size_t);
34 
35 size_t cbor_encode_negint16(uint16_t, unsigned char *, size_t);
36 
37 size_t cbor_encode_negint32(uint32_t, unsigned char *, size_t);
38 
39 size_t cbor_encode_negint64(uint64_t, unsigned char *, size_t);
40 
41 size_t cbor_encode_negint(uint64_t, unsigned char *, size_t);
42 
43 size_t cbor_encode_bytestring_start(size_t, unsigned char *, size_t);
44 
45 size_t cbor_encode_indef_bytestring_start(unsigned char *, size_t);
46 
47 size_t cbor_encode_string_start(size_t, unsigned char *, size_t);
48 
49 size_t cbor_encode_indef_string_start(unsigned char *, size_t);
50 
51 size_t cbor_encode_array_start(size_t, unsigned char *, size_t);
52 
53 size_t cbor_encode_indef_array_start(unsigned char *, size_t);
54 
55 size_t cbor_encode_map_start(size_t, unsigned char *, size_t);
56 
57 size_t cbor_encode_indef_map_start(unsigned char *, size_t);
58 
59 size_t cbor_encode_tag(uint64_t, unsigned char *, size_t);
60 
61 size_t cbor_encode_bool(bool, unsigned char *, size_t);
62 
63 size_t cbor_encode_null(unsigned char *, size_t);
64 
65 size_t cbor_encode_undef(unsigned char *, size_t);
66 
67 /** Encodes a half-precision float
68  *
69  * Since there is no native representation or semantics for half floats
70  * in the language, we use single-precision floats, as every value that
71  * can be expressed as a half-float can also be expressed as a float.
72  *
73  * This however means that not all floats passed to this function can be
74  * unambiguously encoded. The behavior is as follows:
75  *  - Infinity, NaN are preserved
76  *  - Zero is preserved
77  *  - Denormalized numbers keep their sign bit and 10 most significant bit of
78  * the significand
79  *  - All other numbers
80  *   - If the logical value of the exponent is < -24, the output is zero
81  *   - If the logical value of the exponent is between -23 and -14, the output
82  *     is cut off to represent the 'magnitude' of the input, by which we
83  *     mean (-1)^{signbit} x 1.0e{exponent}. The value in the significand is
84  * lost.
85  *   - In all other cases, the sign bit, the exponent, and 10 most significant
86  * bits of the significand are kept
87  *
88  * @param value
89  * @param buffer Target buffer
90  * @param buffer_size Available space in the buffer
91  * @return number of bytes written
92  */
93 size_t cbor_encode_half(float, unsigned char *, size_t);
94 
95 size_t cbor_encode_single(float, unsigned char *, size_t);
96 
97 size_t cbor_encode_double(double, unsigned char *, size_t);
98 
99 size_t cbor_encode_break(unsigned char *, size_t);
100 
101 size_t cbor_encode_ctrl(uint8_t, unsigned char *, size_t);
102 
103 #ifdef __cplusplus
104 }
105 #endif
106 
107 #endif  // LIBCBOR_ENCODING_H
108