110ff414cSEd Maste /* 210ff414cSEd Maste * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com> 310ff414cSEd Maste * 410ff414cSEd Maste * libcbor is free software; you can redistribute it and/or modify 510ff414cSEd Maste * it under the terms of the MIT license. See LICENSE for details. 610ff414cSEd Maste */ 710ff414cSEd Maste 810ff414cSEd Maste #ifndef LIBCBOR_ENCODING_H 910ff414cSEd Maste #define LIBCBOR_ENCODING_H 1010ff414cSEd Maste 1110ff414cSEd Maste #include "cbor/cbor_export.h" 1210ff414cSEd Maste #include "cbor/common.h" 1310ff414cSEd Maste 1410ff414cSEd Maste #ifdef __cplusplus 1510ff414cSEd Maste extern "C" { 1610ff414cSEd Maste #endif 1710ff414cSEd Maste 1810ff414cSEd Maste /* 19*5d3e7166SEd Maste * All cbor_encode_* methods take 2 or 3 arguments: 20*5d3e7166SEd Maste * - a logical `value` to encode (except for trivial items such as NULLs) 21*5d3e7166SEd Maste * - an output `buffer` pointer 22*5d3e7166SEd Maste * - a `buffer_size` specification 23*5d3e7166SEd Maste * 24*5d3e7166SEd Maste * They serialize the `value` into one or more bytes and write the bytes to the 25*5d3e7166SEd Maste * output `buffer` and return either the number of bytes written, or 0 if the 26*5d3e7166SEd Maste * `buffer_size` was too small to small to fit the serialized value (in which 27*5d3e7166SEd Maste * case it is not modified). 2810ff414cSEd Maste */ 2910ff414cSEd Maste 30*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint8(uint8_t, unsigned char *, 3110ff414cSEd Maste size_t); 3210ff414cSEd Maste 33*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint16(uint16_t, unsigned char *, 34*5d3e7166SEd Maste size_t); 3510ff414cSEd Maste 36*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint32(uint32_t, unsigned char *, 37*5d3e7166SEd Maste size_t); 3810ff414cSEd Maste 39*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint64(uint64_t, unsigned char *, 40*5d3e7166SEd Maste size_t); 4110ff414cSEd Maste 42*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint(uint64_t, unsigned char *, 43*5d3e7166SEd Maste size_t); 4410ff414cSEd Maste 45*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint8(uint8_t, unsigned char *, 46*5d3e7166SEd Maste size_t); 4710ff414cSEd Maste 48*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint16(uint16_t, 49*5d3e7166SEd Maste unsigned char *, 50*5d3e7166SEd Maste size_t); 5110ff414cSEd Maste 52*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint32(uint32_t, 53*5d3e7166SEd Maste unsigned char *, 54*5d3e7166SEd Maste size_t); 5510ff414cSEd Maste 56*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint64(uint64_t, 57*5d3e7166SEd Maste unsigned char *, 58*5d3e7166SEd Maste size_t); 5910ff414cSEd Maste 60*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint(uint64_t, unsigned char *, 61*5d3e7166SEd Maste size_t); 6210ff414cSEd Maste 63*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bytestring_start(size_t, 64*5d3e7166SEd Maste unsigned char *, 65*5d3e7166SEd Maste size_t); 6610ff414cSEd Maste 67*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t 68*5d3e7166SEd Maste cbor_encode_indef_bytestring_start(unsigned char *, size_t); 69*5d3e7166SEd Maste 70*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_string_start(size_t, 71*5d3e7166SEd Maste unsigned char *, 72*5d3e7166SEd Maste size_t); 73*5d3e7166SEd Maste 74*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t 75*5d3e7166SEd Maste cbor_encode_indef_string_start(unsigned char *, size_t); 76*5d3e7166SEd Maste 77*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_array_start(size_t, 78*5d3e7166SEd Maste unsigned char *, 79*5d3e7166SEd Maste size_t); 80*5d3e7166SEd Maste 81*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t 82*5d3e7166SEd Maste cbor_encode_indef_array_start(unsigned char *, size_t); 83*5d3e7166SEd Maste 84*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_map_start(size_t, 85*5d3e7166SEd Maste unsigned char *, 86*5d3e7166SEd Maste size_t); 87*5d3e7166SEd Maste 88*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_indef_map_start(unsigned char *, 89*5d3e7166SEd Maste size_t); 90*5d3e7166SEd Maste 91*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_tag(uint64_t, unsigned char *, 92*5d3e7166SEd Maste size_t); 93*5d3e7166SEd Maste 94*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bool(bool, unsigned char *, 95*5d3e7166SEd Maste size_t); 96*5d3e7166SEd Maste 97*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_null(unsigned char *, size_t); 98*5d3e7166SEd Maste 99*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_undef(unsigned char *, size_t); 10010ff414cSEd Maste 10110ff414cSEd Maste /** Encodes a half-precision float 10210ff414cSEd Maste * 10310ff414cSEd Maste * Since there is no native representation or semantics for half floats 10410ff414cSEd Maste * in the language, we use single-precision floats, as every value that 10510ff414cSEd Maste * can be expressed as a half-float can also be expressed as a float. 10610ff414cSEd Maste * 10710ff414cSEd Maste * This however means that not all floats passed to this function can be 10810ff414cSEd Maste * unambiguously encoded. The behavior is as follows: 10910ff414cSEd Maste * - Infinity, NaN are preserved 11010ff414cSEd Maste * - Zero is preserved 11110ff414cSEd Maste * - Denormalized numbers keep their sign bit and 10 most significant bit of 11210ff414cSEd Maste * the significand 11310ff414cSEd Maste * - All other numbers 11410ff414cSEd Maste * - If the logical value of the exponent is < -24, the output is zero 11510ff414cSEd Maste * - If the logical value of the exponent is between -23 and -14, the output 11610ff414cSEd Maste * is cut off to represent the 'magnitude' of the input, by which we 11710ff414cSEd Maste * mean (-1)^{signbit} x 1.0e{exponent}. The value in the significand is 11810ff414cSEd Maste * lost. 11910ff414cSEd Maste * - In all other cases, the sign bit, the exponent, and 10 most significant 12010ff414cSEd Maste * bits of the significand are kept 12110ff414cSEd Maste */ 122*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_half(float, unsigned char *, 123*5d3e7166SEd Maste size_t); 12410ff414cSEd Maste 125*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_single(float, unsigned char *, 126*5d3e7166SEd Maste size_t); 12710ff414cSEd Maste 128*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_double(double, unsigned char *, 129*5d3e7166SEd Maste size_t); 13010ff414cSEd Maste 131*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_break(unsigned char *, size_t); 13210ff414cSEd Maste 133*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_ctrl(uint8_t, unsigned char *, 134*5d3e7166SEd Maste size_t); 13510ff414cSEd Maste 13610ff414cSEd Maste #ifdef __cplusplus 13710ff414cSEd Maste } 13810ff414cSEd Maste #endif 13910ff414cSEd Maste 14010ff414cSEd Maste #endif // LIBCBOR_ENCODING_H 141