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*, size_t); 50 51 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint32(uint32_t, 52 unsigned char*, size_t); 53 54 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint64(uint64_t, 55 unsigned char*, size_t); 56 57 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint(uint64_t, unsigned char*, 58 size_t); 59 60 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bytestring_start(size_t, 61 unsigned char*, 62 size_t); 63 64 _CBOR_NODISCARD CBOR_EXPORT size_t 65 cbor_encode_indef_bytestring_start(unsigned char*, size_t); 66 67 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_string_start(size_t, 68 unsigned char*, 69 size_t); 70 71 _CBOR_NODISCARD CBOR_EXPORT size_t 72 cbor_encode_indef_string_start(unsigned char*, size_t); 73 74 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_array_start(size_t, 75 unsigned char*, 76 size_t); 77 78 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_indef_array_start(unsigned char*, 79 size_t); 80 81 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_map_start(size_t, unsigned char*, 82 size_t); 83 84 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_indef_map_start(unsigned char*, 85 size_t); 86 87 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_tag(uint64_t, unsigned char*, 88 size_t); 89 90 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bool(bool, unsigned char*, 91 size_t); 92 93 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_null(unsigned char*, size_t); 94 95 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_undef(unsigned char*, size_t); 96 97 /** Encodes a half-precision float 98 * 99 * Since there is no native representation or semantics for half floats 100 * in the language, we use single-precision floats, as every value that 101 * can be expressed as a half-float can also be expressed as a float. 102 * 103 * This however means that not all floats passed to this function can be 104 * unambiguously encoded. The behavior is as follows: 105 * - Infinity, NaN are preserved 106 * - Zero is preserved 107 * - Denormalized numbers keep their sign bit and 10 most significant bit of 108 * the significand 109 * - All other numbers 110 * - If the logical value of the exponent is < -24, the output is zero 111 * - If the logical value of the exponent is between -23 and -14, the output 112 * is cut off to represent the 'magnitude' of the input, by which we 113 * mean (-1)^{signbit} x 1.0e{exponent}. The value in the significand is 114 * lost. 115 * - In all other cases, the sign bit, the exponent, and 10 most significant 116 * bits of the significand are kept 117 * 118 * Note: Signaling NaNs are encoded as a standard, "quiet" NaN. 119 */ 120 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_half(float, unsigned char*, 121 size_t); 122 /** Encodes a single precision float 123 * 124 * Note: Signaling NaNs are encoded as a standard, "quiet" NaN. 125 */ 126 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_single(float, unsigned char*, 127 size_t); 128 129 /** Encodes a double precision float 130 * 131 * Note: Signaling NaNs are encoded as a standard, "quiet" NaN. 132 */ 133 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_double(double, unsigned char*, 134 size_t); 135 136 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_break(unsigned char*, size_t); 137 138 _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_ctrl(uint8_t, unsigned char*, 139 size_t); 140 141 #ifdef __cplusplus 142 } 143 #endif 144 145 #endif // LIBCBOR_ENCODING_H 146