Path: blob/main/contrib/libcbor/src/cbor/encoding.h
39536 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#ifndef LIBCBOR_ENCODING_H8#define LIBCBOR_ENCODING_H910#include "cbor/cbor_export.h"11#include "cbor/common.h"1213#ifdef __cplusplus14extern "C" {15#endif1617/*18* All cbor_encode_* methods take 2 or 3 arguments:19* - a logical `value` to encode (except for trivial items such as NULLs)20* - an output `buffer` pointer21* - a `buffer_size` specification22*23* They serialize the `value` into one or more bytes and write the bytes to the24* output `buffer` and return either the number of bytes written, or 0 if the25* `buffer_size` was too small to small to fit the serialized value (in which26* case it is not modified).27*/2829_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint8(uint8_t, unsigned char *,30size_t);3132_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint16(uint16_t, unsigned char *,33size_t);3435_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint32(uint32_t, unsigned char *,36size_t);3738_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint64(uint64_t, unsigned char *,39size_t);4041_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_uint(uint64_t, unsigned char *,42size_t);4344_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint8(uint8_t, unsigned char *,45size_t);4647_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint16(uint16_t,48unsigned char *,49size_t);5051_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint32(uint32_t,52unsigned char *,53size_t);5455_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint64(uint64_t,56unsigned char *,57size_t);5859_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_negint(uint64_t, unsigned char *,60size_t);6162_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bytestring_start(size_t,63unsigned char *,64size_t);6566_CBOR_NODISCARD CBOR_EXPORT size_t67cbor_encode_indef_bytestring_start(unsigned char *, size_t);6869_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_string_start(size_t,70unsigned char *,71size_t);7273_CBOR_NODISCARD CBOR_EXPORT size_t74cbor_encode_indef_string_start(unsigned char *, size_t);7576_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_array_start(size_t,77unsigned char *,78size_t);7980_CBOR_NODISCARD CBOR_EXPORT size_t81cbor_encode_indef_array_start(unsigned char *, size_t);8283_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_map_start(size_t,84unsigned char *,85size_t);8687_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_indef_map_start(unsigned char *,88size_t);8990_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_tag(uint64_t, unsigned char *,91size_t);9293_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_bool(bool, unsigned char *,94size_t);9596_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_null(unsigned char *, size_t);9798_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_undef(unsigned char *, size_t);99100/** Encodes a half-precision float101*102* Since there is no native representation or semantics for half floats103* in the language, we use single-precision floats, as every value that104* can be expressed as a half-float can also be expressed as a float.105*106* This however means that not all floats passed to this function can be107* unambiguously encoded. The behavior is as follows:108* - Infinity, NaN are preserved109* - Zero is preserved110* - Denormalized numbers keep their sign bit and 10 most significant bit of111* the significand112* - All other numbers113* - If the logical value of the exponent is < -24, the output is zero114* - If the logical value of the exponent is between -23 and -14, the output115* is cut off to represent the 'magnitude' of the input, by which we116* mean (-1)^{signbit} x 1.0e{exponent}. The value in the significand is117* lost.118* - In all other cases, the sign bit, the exponent, and 10 most significant119* bits of the significand are kept120*/121_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_half(float, unsigned char *,122size_t);123124_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_single(float, unsigned char *,125size_t);126127_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_double(double, unsigned char *,128size_t);129130_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_break(unsigned char *, size_t);131132_CBOR_NODISCARD CBOR_EXPORT size_t cbor_encode_ctrl(uint8_t, unsigned char *,133size_t);134135#ifdef __cplusplus136}137#endif138139#endif // LIBCBOR_ENCODING_H140141142