xref: /freebsd/contrib/libcbor/src/cbor/ints.h (revision 188bf8876fdc6e3edebeb76f80e764e419a65f1e)
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 positive or negative integer
27  * @return the value
28  */
29 _CBOR_NODISCARD CBOR_EXPORT uint8_t cbor_get_uint8(const cbor_item_t *item);
30 
31 /** Extracts the integer value
32  *
33  * @param item positive or negative integer
34  * @return the value
35  */
36 _CBOR_NODISCARD CBOR_EXPORT uint16_t cbor_get_uint16(const cbor_item_t *item);
37 
38 /** Extracts the integer value
39  *
40  * @param item positive or negative integer
41  * @return the value
42  */
43 _CBOR_NODISCARD CBOR_EXPORT uint32_t cbor_get_uint32(const cbor_item_t *item);
44 
45 /** Extracts the integer value
46  *
47  * @param item positive or negative integer
48  * @return the value
49  */
50 _CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_uint64(const cbor_item_t *item);
51 
52 /** Extracts the integer value
53  *
54  * @param item positive or negative integer
55  * @return the value, extended to `uint64_t`
56  */
57 _CBOR_NODISCARD CBOR_EXPORT uint64_t cbor_get_int(const cbor_item_t *item);
58 
59 /** Assigns the integer value
60  *
61  * @param item 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 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 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 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  positive or negative integer item
94  *  @return the width
95  */
96 _CBOR_NODISCARD CBOR_EXPORT cbor_int_width
97 cbor_int_get_width(const cbor_item_t *item);
98 
99 /** Marks the integer item as a positive integer
100  *
101  * The data value is not changed
102  *
103  * @param item positive or negative integer item
104  */
105 CBOR_EXPORT void cbor_mark_uint(cbor_item_t *item);
106 
107 /** Marks the integer item as a negative integer
108  *
109  * The data value is not changed
110  *
111  * @param item positive or negative integer item
112  */
113 CBOR_EXPORT void cbor_mark_negint(cbor_item_t *item);
114 
115 /** Allocates new integer with 1B width
116  *
117  * The width cannot be changed once allocated
118  *
119  * @return **new** positive integer or `NULL` on memory allocation failure. The
120  * value is not initialized
121  */
122 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int8(void);
123 
124 /** Allocates new integer with 2B width
125  *
126  * The width cannot be changed once allocated
127  *
128  * @return **new** positive integer or `NULL` on memory allocation failure. The
129  * value is not initialized
130  */
131 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int16(void);
132 
133 /** Allocates new integer with 4B width
134  *
135  * The width cannot be changed once allocated
136  *
137  * @return **new** positive integer or `NULL` on memory allocation failure. The
138  * value is not initialized
139  */
140 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int32(void);
141 
142 /** Allocates new integer with 8B width
143  *
144  * The width cannot be changed once allocated
145  *
146  * @return **new** positive integer or `NULL` on memory allocation failure. The
147  * value is not initialized
148  */
149 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_int64(void);
150 
151 /** Constructs a new positive integer
152  *
153  * @param value the value to use
154  * @return **new** positive integer or `NULL` on memory allocation failure
155  */
156 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint8(uint8_t value);
157 
158 /** Constructs a new positive integer
159  *
160  * @param value the value to use
161  * @return **new** positive integer or `NULL` on memory allocation failure
162  */
163 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint16(uint16_t value);
164 
165 /** Constructs a new positive integer
166  *
167  * @param value the value to use
168  * @return **new** positive integer or `NULL` on memory allocation failure
169  */
170 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint32(uint32_t value);
171 
172 /** Constructs a new positive integer
173  *
174  * @param value the value to use
175  * @return **new** positive integer or `NULL` on memory allocation failure
176  */
177 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_uint64(uint64_t value);
178 
179 /** Constructs a new negative integer
180  *
181  * @param value the value to use
182  * @return **new** negative integer or `NULL` on memory allocation failure
183  */
184 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint8(uint8_t value);
185 
186 /** Constructs a new negative integer
187  *
188  * @param value the value to use
189  * @return **new** negative integer or `NULL` on memory allocation failure
190  */
191 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint16(uint16_t value);
192 
193 /** Constructs a new negative integer
194  *
195  * @param value the value to use
196  * @return **new** negative integer or `NULL` on memory allocation failure
197  */
198 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint32(uint32_t value);
199 
200 /** Constructs a new negative integer
201  *
202  * @param value the value to use
203  * @return **new** negative integer or `NULL` on memory allocation failure
204  */
205 _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_negint64(uint64_t value);
206 
207 #ifdef __cplusplus
208 }
209 #endif
210 
211 #endif  // LIBCBOR_INTS_H
212