1da0d961cSdjm /* 2d3425be1Sdjm * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com> 3da0d961cSdjm * 4da0d961cSdjm * libcbor is free software; you can redistribute it and/or modify 5da0d961cSdjm * it under the terms of the MIT license. See LICENSE for details. 6da0d961cSdjm */ 7da0d961cSdjm 8da0d961cSdjm #ifndef LIBCBOR_INTS_H 9da0d961cSdjm #define LIBCBOR_INTS_H 10da0d961cSdjm 11*4dcc46c4Sdjm #include "cbor/cbor_export.h" 12da0d961cSdjm #include "cbor/common.h" 13da0d961cSdjm 14da0d961cSdjm #ifdef __cplusplus 15da0d961cSdjm extern "C" { 16da0d961cSdjm #endif 17da0d961cSdjm 18da0d961cSdjm /* 19da0d961cSdjm * ============================================================================ 20da0d961cSdjm * Integer (uints and negints) manipulation 21da0d961cSdjm * ============================================================================ 22da0d961cSdjm */ 23da0d961cSdjm 24da0d961cSdjm /** Extracts the integer value 25da0d961cSdjm * 26da0d961cSdjm * @param item[borrow] positive or negative integer 27da0d961cSdjm * @return the value 28da0d961cSdjm */ 29*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_get_uint8(const cbor_item_t *item); 30da0d961cSdjm 31da0d961cSdjm /** Extracts the integer value 32da0d961cSdjm * 33da0d961cSdjm * @param item[borrow] positive or negative integer 34da0d961cSdjm * @return the value 35da0d961cSdjm */ 36*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT uint16_t cbor_get_uint16(const cbor_item_t *item); 37da0d961cSdjm 38da0d961cSdjm /** Extracts the integer value 39da0d961cSdjm * 40da0d961cSdjm * @param item[borrow] positive or negative integer 41da0d961cSdjm * @return the value 42da0d961cSdjm */ 43*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT uint32_t cbor_get_uint32(const cbor_item_t *item); 44da0d961cSdjm 45da0d961cSdjm /** Extracts the integer value 46da0d961cSdjm * 47da0d961cSdjm * @param item[borrow] positive or negative integer 48da0d961cSdjm * @return the value 49da0d961cSdjm */ 50*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_uint64(const cbor_item_t *item); 51da0d961cSdjm 52da0d961cSdjm /** Extracts the integer value 53da0d961cSdjm * 54da0d961cSdjm * @param item[borrow] positive or negative integer 55da0d961cSdjm * @return the value, extended to `uint64_t` 56da0d961cSdjm */ 57*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_int(const cbor_item_t *item); 58da0d961cSdjm 59da0d961cSdjm /** Assigns the integer value 60da0d961cSdjm * 61da0d961cSdjm * @param item[borrow] positive or negative integer item 629e5c2ddcSdjm * @param value the value to assign. For negative integer, the logical value is 639e5c2ddcSdjm * `-value - 1` 64da0d961cSdjm */ 65*4dcc46c4Sdjm CBOR_EXPORT void cbor_set_uint8(cbor_item_t *item, uint8_t value); 66da0d961cSdjm 67da0d961cSdjm /** Assigns the integer value 68da0d961cSdjm * 69da0d961cSdjm * @param item[borrow] positive or negative integer item 709e5c2ddcSdjm * @param value the value to assign. For negative integer, the logical value is 719e5c2ddcSdjm * `-value - 1` 72da0d961cSdjm */ 73*4dcc46c4Sdjm CBOR_EXPORT void cbor_set_uint16(cbor_item_t *item, uint16_t value); 74da0d961cSdjm 75da0d961cSdjm /** Assigns the integer value 76da0d961cSdjm * 77da0d961cSdjm * @param item[borrow] positive or negative integer item 789e5c2ddcSdjm * @param value the value to assign. For negative integer, the logical value is 799e5c2ddcSdjm * `-value - 1` 80da0d961cSdjm */ 81*4dcc46c4Sdjm CBOR_EXPORT void cbor_set_uint32(cbor_item_t *item, uint32_t value); 82da0d961cSdjm 83da0d961cSdjm /** Assigns the integer value 84da0d961cSdjm * 85da0d961cSdjm * @param item[borrow] positive or negative integer item 869e5c2ddcSdjm * @param value the value to assign. For negative integer, the logical value is 879e5c2ddcSdjm * `-value - 1` 88da0d961cSdjm */ 89*4dcc46c4Sdjm CBOR_EXPORT void cbor_set_uint64(cbor_item_t *item, uint64_t value); 90da0d961cSdjm 91da0d961cSdjm /** Queries the integer width 92da0d961cSdjm * 93da0d961cSdjm * @param item[borrow] positive or negative integer item 94da0d961cSdjm * @return the width 95da0d961cSdjm */ 96*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_int_width 97*4dcc46c4Sdjm cbor_int_get_width(const cbor_item_t *item); 98da0d961cSdjm 99da0d961cSdjm /** Marks the integer item as a positive integer 100da0d961cSdjm * 101da0d961cSdjm * The data value is not changed 102da0d961cSdjm * 103da0d961cSdjm * @param item[borrow] positive or negative integer item 104da0d961cSdjm */ 105*4dcc46c4Sdjm CBOR_EXPORT void cbor_mark_uint(cbor_item_t *item); 106da0d961cSdjm 107da0d961cSdjm /** Marks the integer item as a negative integer 108da0d961cSdjm * 109da0d961cSdjm * The data value is not changed 110da0d961cSdjm * 111da0d961cSdjm * @param item[borrow] positive or negative integer item 112da0d961cSdjm */ 113*4dcc46c4Sdjm CBOR_EXPORT void cbor_mark_negint(cbor_item_t *item); 114da0d961cSdjm 115da0d961cSdjm /** Allocates new integer with 1B width 116da0d961cSdjm * 117da0d961cSdjm * The width cannot be changed once allocated 118da0d961cSdjm * 1199e5c2ddcSdjm * @return **new** positive integer or `NULL` on memory allocation failure. The 1209e5c2ddcSdjm * value is not initialized 121da0d961cSdjm */ 122*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int8(void); 123da0d961cSdjm 124da0d961cSdjm /** Allocates new integer with 2B width 125da0d961cSdjm * 126da0d961cSdjm * The width cannot be changed once allocated 127da0d961cSdjm * 1289e5c2ddcSdjm * @return **new** positive integer or `NULL` on memory allocation failure. The 1299e5c2ddcSdjm * value is not initialized 130da0d961cSdjm */ 131*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int16(void); 132da0d961cSdjm 133da0d961cSdjm /** Allocates new integer with 4B width 134da0d961cSdjm * 135da0d961cSdjm * The width cannot be changed once allocated 136da0d961cSdjm * 1379e5c2ddcSdjm * @return **new** positive integer or `NULL` on memory allocation failure. The 1389e5c2ddcSdjm * value is not initialized 139da0d961cSdjm */ 140*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int32(void); 141da0d961cSdjm 142da0d961cSdjm /** Allocates new integer with 8B width 143da0d961cSdjm * 144da0d961cSdjm * The width cannot be changed once allocated 145da0d961cSdjm * 1469e5c2ddcSdjm * @return **new** positive integer or `NULL` on memory allocation failure. The 1479e5c2ddcSdjm * value is not initialized 148da0d961cSdjm */ 149*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int64(void); 150da0d961cSdjm 151da0d961cSdjm /** Constructs a new positive integer 152da0d961cSdjm * 153da0d961cSdjm * @param value the value to use 1549e5c2ddcSdjm * @return **new** positive integer or `NULL` on memory allocation failure 155da0d961cSdjm */ 156*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint8(uint8_t value); 157da0d961cSdjm 158da0d961cSdjm /** Constructs a new positive integer 159da0d961cSdjm * 160da0d961cSdjm * @param value the value to use 1619e5c2ddcSdjm * @return **new** positive integer or `NULL` on memory allocation failure 162da0d961cSdjm */ 163*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint16(uint16_t value); 164da0d961cSdjm 165da0d961cSdjm /** Constructs a new positive integer 166da0d961cSdjm * 167da0d961cSdjm * @param value the value to use 1689e5c2ddcSdjm * @return **new** positive integer or `NULL` on memory allocation failure 169da0d961cSdjm */ 170*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint32(uint32_t value); 171da0d961cSdjm 172da0d961cSdjm /** Constructs a new positive integer 173da0d961cSdjm * 174da0d961cSdjm * @param value the value to use 1759e5c2ddcSdjm * @return **new** positive integer or `NULL` on memory allocation failure 176da0d961cSdjm */ 177*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint64(uint64_t value); 178da0d961cSdjm 179da0d961cSdjm /** Constructs a new negative integer 180da0d961cSdjm * 181da0d961cSdjm * @param value the value to use 1829e5c2ddcSdjm * @return **new** negative integer or `NULL` on memory allocation failure 183da0d961cSdjm */ 184*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint8(uint8_t value); 185da0d961cSdjm 186da0d961cSdjm /** Constructs a new negative integer 187da0d961cSdjm * 188da0d961cSdjm * @param value the value to use 1899e5c2ddcSdjm * @return **new** negative integer or `NULL` on memory allocation failure 190da0d961cSdjm */ 191*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint16(uint16_t value); 192da0d961cSdjm 193da0d961cSdjm /** Constructs a new negative integer 194da0d961cSdjm * 195da0d961cSdjm * @param value the value to use 1969e5c2ddcSdjm * @return **new** negative integer or `NULL` on memory allocation failure 197da0d961cSdjm */ 198*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint32(uint32_t value); 199da0d961cSdjm 200da0d961cSdjm /** Constructs a new negative integer 201da0d961cSdjm * 202da0d961cSdjm * @param value the value to use 2039e5c2ddcSdjm * @return **new** negative integer or `NULL` on memory allocation failure 204da0d961cSdjm */ 205*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint64(uint64_t value); 206da0d961cSdjm 207da0d961cSdjm #ifdef __cplusplus 208da0d961cSdjm } 209da0d961cSdjm #endif 210da0d961cSdjm 211da0d961cSdjm #endif // LIBCBOR_INTS_H 212