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
15extern "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
68cbor_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
75cbor_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
82cbor_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