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