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_INTS_H 910ff414cSEd Maste #define LIBCBOR_INTS_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 /* 1910ff414cSEd Maste * ============================================================================ 2010ff414cSEd Maste * Integer (uints and negints) manipulation 2110ff414cSEd Maste * ============================================================================ 2210ff414cSEd Maste */ 2310ff414cSEd Maste 2410ff414cSEd Maste /** Extracts the integer value 2510ff414cSEd Maste * 26*5d3e7166SEd Maste * @param item positive or negative integer 2710ff414cSEd Maste * @return the value 2810ff414cSEd Maste */ 29*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_get_uint8(const cbor_item_t *item); 3010ff414cSEd Maste 3110ff414cSEd Maste /** Extracts the integer value 3210ff414cSEd Maste * 33*5d3e7166SEd Maste * @param item positive or negative integer 3410ff414cSEd Maste * @return the value 3510ff414cSEd Maste */ 36*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT uint16_t cbor_get_uint16(const cbor_item_t *item); 3710ff414cSEd Maste 3810ff414cSEd Maste /** Extracts the integer value 3910ff414cSEd Maste * 40*5d3e7166SEd Maste * @param item positive or negative integer 4110ff414cSEd Maste * @return the value 4210ff414cSEd Maste */ 43*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT uint32_t cbor_get_uint32(const cbor_item_t *item); 4410ff414cSEd Maste 4510ff414cSEd Maste /** Extracts the integer value 4610ff414cSEd Maste * 47*5d3e7166SEd Maste * @param item positive or negative integer 4810ff414cSEd Maste * @return the value 4910ff414cSEd Maste */ 50*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_uint64(const cbor_item_t *item); 5110ff414cSEd Maste 5210ff414cSEd Maste /** Extracts the integer value 5310ff414cSEd Maste * 54*5d3e7166SEd Maste * @param item positive or negative integer 5510ff414cSEd Maste * @return the value, extended to `uint64_t` 5610ff414cSEd Maste */ 57*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_int(const cbor_item_t *item); 5810ff414cSEd Maste 5910ff414cSEd Maste /** Assigns the integer value 6010ff414cSEd Maste * 61*5d3e7166SEd Maste * @param item positive or negative integer item 6210ff414cSEd Maste * @param value the value to assign. For negative integer, the logical value is 6310ff414cSEd Maste * `-value - 1` 6410ff414cSEd Maste */ 6510ff414cSEd Maste CBOR_EXPORT void cbor_set_uint8(cbor_item_t *item, uint8_t value); 6610ff414cSEd Maste 6710ff414cSEd Maste /** Assigns the integer value 6810ff414cSEd Maste * 69*5d3e7166SEd Maste * @param item positive or negative integer item 7010ff414cSEd Maste * @param value the value to assign. For negative integer, the logical value is 7110ff414cSEd Maste * `-value - 1` 7210ff414cSEd Maste */ 7310ff414cSEd Maste CBOR_EXPORT void cbor_set_uint16(cbor_item_t *item, uint16_t value); 7410ff414cSEd Maste 7510ff414cSEd Maste /** Assigns the integer value 7610ff414cSEd Maste * 77*5d3e7166SEd Maste * @param item positive or negative integer item 7810ff414cSEd Maste * @param value the value to assign. For negative integer, the logical value is 7910ff414cSEd Maste * `-value - 1` 8010ff414cSEd Maste */ 8110ff414cSEd Maste CBOR_EXPORT void cbor_set_uint32(cbor_item_t *item, uint32_t value); 8210ff414cSEd Maste 8310ff414cSEd Maste /** Assigns the integer value 8410ff414cSEd Maste * 85*5d3e7166SEd Maste * @param item positive or negative integer item 8610ff414cSEd Maste * @param value the value to assign. For negative integer, the logical value is 8710ff414cSEd Maste * `-value - 1` 8810ff414cSEd Maste */ 8910ff414cSEd Maste CBOR_EXPORT void cbor_set_uint64(cbor_item_t *item, uint64_t value); 9010ff414cSEd Maste 9110ff414cSEd Maste /** Queries the integer width 9210ff414cSEd Maste * 93*5d3e7166SEd Maste * @param item positive or negative integer item 9410ff414cSEd Maste * @return the width 9510ff414cSEd Maste */ 96*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_int_width 97*5d3e7166SEd Maste cbor_int_get_width(const cbor_item_t *item); 9810ff414cSEd Maste 9910ff414cSEd Maste /** Marks the integer item as a positive integer 10010ff414cSEd Maste * 10110ff414cSEd Maste * The data value is not changed 10210ff414cSEd Maste * 103*5d3e7166SEd Maste * @param item positive or negative integer item 10410ff414cSEd Maste */ 10510ff414cSEd Maste CBOR_EXPORT void cbor_mark_uint(cbor_item_t *item); 10610ff414cSEd Maste 10710ff414cSEd Maste /** Marks the integer item as a negative integer 10810ff414cSEd Maste * 10910ff414cSEd Maste * The data value is not changed 11010ff414cSEd Maste * 111*5d3e7166SEd Maste * @param item positive or negative integer item 11210ff414cSEd Maste */ 11310ff414cSEd Maste CBOR_EXPORT void cbor_mark_negint(cbor_item_t *item); 11410ff414cSEd Maste 11510ff414cSEd Maste /** Allocates new integer with 1B width 11610ff414cSEd Maste * 11710ff414cSEd Maste * The width cannot be changed once allocated 11810ff414cSEd Maste * 11910ff414cSEd Maste * @return **new** positive integer or `NULL` on memory allocation failure. The 12010ff414cSEd Maste * value is not initialized 12110ff414cSEd Maste */ 122*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int8(void); 12310ff414cSEd Maste 12410ff414cSEd Maste /** Allocates new integer with 2B width 12510ff414cSEd Maste * 12610ff414cSEd Maste * The width cannot be changed once allocated 12710ff414cSEd Maste * 12810ff414cSEd Maste * @return **new** positive integer or `NULL` on memory allocation failure. The 12910ff414cSEd Maste * value is not initialized 13010ff414cSEd Maste */ 131*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int16(void); 13210ff414cSEd Maste 13310ff414cSEd Maste /** Allocates new integer with 4B width 13410ff414cSEd Maste * 13510ff414cSEd Maste * The width cannot be changed once allocated 13610ff414cSEd Maste * 13710ff414cSEd Maste * @return **new** positive integer or `NULL` on memory allocation failure. The 13810ff414cSEd Maste * value is not initialized 13910ff414cSEd Maste */ 140*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int32(void); 14110ff414cSEd Maste 14210ff414cSEd Maste /** Allocates new integer with 8B width 14310ff414cSEd Maste * 14410ff414cSEd Maste * The width cannot be changed once allocated 14510ff414cSEd Maste * 14610ff414cSEd Maste * @return **new** positive integer or `NULL` on memory allocation failure. The 14710ff414cSEd Maste * value is not initialized 14810ff414cSEd Maste */ 149*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int64(void); 15010ff414cSEd Maste 15110ff414cSEd Maste /** Constructs a new positive integer 15210ff414cSEd Maste * 15310ff414cSEd Maste * @param value the value to use 15410ff414cSEd Maste * @return **new** positive integer or `NULL` on memory allocation failure 15510ff414cSEd Maste */ 156*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint8(uint8_t value); 15710ff414cSEd Maste 15810ff414cSEd Maste /** Constructs a new positive integer 15910ff414cSEd Maste * 16010ff414cSEd Maste * @param value the value to use 16110ff414cSEd Maste * @return **new** positive integer or `NULL` on memory allocation failure 16210ff414cSEd Maste */ 163*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint16(uint16_t value); 16410ff414cSEd Maste 16510ff414cSEd Maste /** Constructs a new positive integer 16610ff414cSEd Maste * 16710ff414cSEd Maste * @param value the value to use 16810ff414cSEd Maste * @return **new** positive integer or `NULL` on memory allocation failure 16910ff414cSEd Maste */ 170*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint32(uint32_t value); 17110ff414cSEd Maste 17210ff414cSEd Maste /** Constructs a new positive integer 17310ff414cSEd Maste * 17410ff414cSEd Maste * @param value the value to use 17510ff414cSEd Maste * @return **new** positive integer or `NULL` on memory allocation failure 17610ff414cSEd Maste */ 177*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint64(uint64_t value); 17810ff414cSEd Maste 17910ff414cSEd Maste /** Constructs a new negative integer 18010ff414cSEd Maste * 18110ff414cSEd Maste * @param value the value to use 18210ff414cSEd Maste * @return **new** negative integer or `NULL` on memory allocation failure 18310ff414cSEd Maste */ 184*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint8(uint8_t value); 18510ff414cSEd Maste 18610ff414cSEd Maste /** Constructs a new negative integer 18710ff414cSEd Maste * 18810ff414cSEd Maste * @param value the value to use 18910ff414cSEd Maste * @return **new** negative integer or `NULL` on memory allocation failure 19010ff414cSEd Maste */ 191*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint16(uint16_t value); 19210ff414cSEd Maste 19310ff414cSEd Maste /** Constructs a new negative integer 19410ff414cSEd Maste * 19510ff414cSEd Maste * @param value the value to use 19610ff414cSEd Maste * @return **new** negative integer or `NULL` on memory allocation failure 19710ff414cSEd Maste */ 198*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint32(uint32_t value); 19910ff414cSEd Maste 20010ff414cSEd Maste /** Constructs a new negative integer 20110ff414cSEd Maste * 20210ff414cSEd Maste * @param value the value to use 20310ff414cSEd Maste * @return **new** negative integer or `NULL` on memory allocation failure 20410ff414cSEd Maste */ 205*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint64(uint64_t value); 20610ff414cSEd Maste 20710ff414cSEd Maste #ifdef __cplusplus 20810ff414cSEd Maste } 20910ff414cSEd Maste #endif 21010ff414cSEd Maste 21110ff414cSEd Maste #endif // LIBCBOR_INTS_H 212