/*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_INTS_H8#define LIBCBOR_INTS_H910#include "cbor/cbor_export.h"11#include "cbor/common.h"1213#ifdef __cplusplus14extern "C" {15#endif1617/*18* ============================================================================19* Integer (uints and negints) manipulation20* ============================================================================21*/2223/** Extracts the integer value24*25* @param item positive or negative integer26* @return the value27*/28_CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_get_uint8(const cbor_item_t *item);2930/** Extracts the integer value31*32* @param item positive or negative integer33* @return the value34*/35_CBOR_NODISCARD CBOR_EXPORT uint16_t cbor_get_uint16(const cbor_item_t *item);3637/** Extracts the integer value38*39* @param item positive or negative integer40* @return the value41*/42_CBOR_NODISCARD CBOR_EXPORT uint32_t cbor_get_uint32(const cbor_item_t *item);4344/** Extracts the integer value45*46* @param item positive or negative integer47* @return the value48*/49_CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_uint64(const cbor_item_t *item);5051/** Extracts the integer value52*53* @param item positive or negative integer54* @return the value, extended to `uint64_t`55*/56_CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_int(const cbor_item_t *item);5758/** Assigns the integer value59*60* @param item positive or negative integer item61* @param value the value to assign. For negative integer, the logical value is62* `-value - 1`63*/64CBOR_EXPORT void cbor_set_uint8(cbor_item_t *item, uint8_t value);6566/** Assigns the integer value67*68* @param item positive or negative integer item69* @param value the value to assign. For negative integer, the logical value is70* `-value - 1`71*/72CBOR_EXPORT void cbor_set_uint16(cbor_item_t *item, uint16_t value);7374/** Assigns the integer value75*76* @param item positive or negative integer item77* @param value the value to assign. For negative integer, the logical value is78* `-value - 1`79*/80CBOR_EXPORT void cbor_set_uint32(cbor_item_t *item, uint32_t value);8182/** Assigns the integer value83*84* @param item positive or negative integer item85* @param value the value to assign. For negative integer, the logical value is86* `-value - 1`87*/88CBOR_EXPORT void cbor_set_uint64(cbor_item_t *item, uint64_t value);8990/** Queries the integer width91*92* @param item positive or negative integer item93* @return the width94*/95_CBOR_NODISCARD CBOR_EXPORT cbor_int_width96cbor_int_get_width(const cbor_item_t *item);9798/** Marks the integer item as a positive integer99*100* The data value is not changed101*102* @param item positive or negative integer item103*/104CBOR_EXPORT void cbor_mark_uint(cbor_item_t *item);105106/** Marks the integer item as a negative integer107*108* The data value is not changed109*110* @param item positive or negative integer item111*/112CBOR_EXPORT void cbor_mark_negint(cbor_item_t *item);113114/** Allocates new integer with 1B width115*116* The width cannot be changed once allocated117*118* @return **new** positive integer or `NULL` on memory allocation failure. The119* value is not initialized120*/121_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int8(void);122123/** Allocates new integer with 2B width124*125* The width cannot be changed once allocated126*127* @return **new** positive integer or `NULL` on memory allocation failure. The128* value is not initialized129*/130_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int16(void);131132/** Allocates new integer with 4B width133*134* The width cannot be changed once allocated135*136* @return **new** positive integer or `NULL` on memory allocation failure. The137* value is not initialized138*/139_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int32(void);140141/** Allocates new integer with 8B width142*143* The width cannot be changed once allocated144*145* @return **new** positive integer or `NULL` on memory allocation failure. The146* value is not initialized147*/148_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int64(void);149150/** Constructs a new positive integer151*152* @param value the value to use153* @return **new** positive integer or `NULL` on memory allocation failure154*/155_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint8(uint8_t value);156157/** Constructs a new positive integer158*159* @param value the value to use160* @return **new** positive integer or `NULL` on memory allocation failure161*/162_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint16(uint16_t value);163164/** Constructs a new positive integer165*166* @param value the value to use167* @return **new** positive integer or `NULL` on memory allocation failure168*/169_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint32(uint32_t value);170171/** Constructs a new positive integer172*173* @param value the value to use174* @return **new** positive integer or `NULL` on memory allocation failure175*/176_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint64(uint64_t value);177178/** Constructs a new negative integer179*180* @param value the value to use181* @return **new** negative integer or `NULL` on memory allocation failure182*/183_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint8(uint8_t value);184185/** Constructs a new negative integer186*187* @param value the value to use188* @return **new** negative integer or `NULL` on memory allocation failure189*/190_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint16(uint16_t value);191192/** Constructs a new negative integer193*194* @param value the value to use195* @return **new** negative integer or `NULL` on memory allocation failure196*/197_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint32(uint32_t value);198199/** Constructs a new negative integer200*201* @param value the value to use202* @return **new** negative integer or `NULL` on memory allocation failure203*/204_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint64(uint64_t value);205206#ifdef __cplusplus207}208#endif209210#endif // LIBCBOR_INTS_H211212213