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