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