summaryrefslogtreecommitdiffstats
path: root/lib/libcbor/src/cbor/ints.h
blob: f965c2989487f06f8d9817af33c839626054a2bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#ifndef LIBCBOR_INTS_H
#define LIBCBOR_INTS_H

#include "cbor/common.h"

#ifdef __cplusplus
extern "C" {
#endif

/*
 * ============================================================================
 * Integer (uints and negints) manipulation
 * ============================================================================
 */

/** Extracts the integer value
 *
 * @param item[borrow] positive or negative integer
 * @return the value
 */
uint8_t cbor_get_uint8(const cbor_item_t *item);

/** Extracts the integer value
 *
 * @param item[borrow] positive or negative integer
 * @return the value
 */
uint16_t cbor_get_uint16(const cbor_item_t *item);

/** Extracts the integer value
 *
 * @param item[borrow] positive or negative integer
 * @return the value
 */
uint32_t cbor_get_uint32(const cbor_item_t *item);

/** Extracts the integer value
 *
 * @param item[borrow] positive or negative integer
 * @return the value
 */
uint64_t cbor_get_uint64(const cbor_item_t *item);

/** Extracts the integer value
 *
 * @param item[borrow] positive or negative integer
 * @return the value, extended to `uint64_t`
 */
uint64_t cbor_get_int(const cbor_item_t *item);

/** Assigns the integer value
 *
 * @param item[borrow] positive or negative integer item
 * @param value the value to assign. For negative integer, the logical value is
 * `-value - 1`
 */
void cbor_set_uint8(cbor_item_t *item, uint8_t value);

/** Assigns the integer value
 *
 * @param item[borrow] positive or negative integer item
 * @param value the value to assign. For negative integer, the logical value is
 * `-value - 1`
 */
void cbor_set_uint16(cbor_item_t *item, uint16_t value);

/** Assigns the integer value
 *
 * @param item[borrow] positive or negative integer item
 * @param value the value to assign. For negative integer, the logical value is
 * `-value - 1`
 */
void cbor_set_uint32(cbor_item_t *item, uint32_t value);

/** Assigns the integer value
 *
 * @param item[borrow] positive or negative integer item
 * @param value the value to assign. For negative integer, the logical value is
 * `-value - 1`
 */
void cbor_set_uint64(cbor_item_t *item, uint64_t value);

/** Queries the integer width
 *
 *  @param item[borrow] positive or negative integer item
 *  @return the width
 */
cbor_int_width cbor_int_get_width(const cbor_item_t *item);

/** Marks the integer item as a positive integer
 *
 * The data value is not changed
 *
 * @param item[borrow] positive or negative integer item
 */
void cbor_mark_uint(cbor_item_t *item);

/** Marks the integer item as a negative integer
 *
 * The data value is not changed
 *
 * @param item[borrow] positive or negative integer item
 */
void cbor_mark_negint(cbor_item_t *item);

/** Allocates new integer with 1B width
 *
 * The width cannot be changed once allocated
 *
 * @return **new** positive integer or `NULL` on memory allocation failure. The
 * value is not initialized
 */
cbor_item_t *cbor_new_int8();

/** Allocates new integer with 2B width
 *
 * The width cannot be changed once allocated
 *
 * @return **new** positive integer or `NULL` on memory allocation failure. The
 * value is not initialized
 */
cbor_item_t *cbor_new_int16();

/** Allocates new integer with 4B width
 *
 * The width cannot be changed once allocated
 *
 * @return **new** positive integer or `NULL` on memory allocation failure. The
 * value is not initialized
 */
cbor_item_t *cbor_new_int32();

/** Allocates new integer with 8B width
 *
 * The width cannot be changed once allocated
 *
 * @return **new** positive integer or `NULL` on memory allocation failure. The
 * value is not initialized
 */
cbor_item_t *cbor_new_int64();

/** Constructs a new positive integer
 *
 * @param value the value to use
 * @return **new** positive integer or `NULL` on memory allocation failure
 */
cbor_item_t *cbor_build_uint8(uint8_t value);

/** Constructs a new positive integer
 *
 * @param value the value to use
 * @return **new** positive integer or `NULL` on memory allocation failure
 */
cbor_item_t *cbor_build_uint16(uint16_t value);

/** Constructs a new positive integer
 *
 * @param value the value to use
 * @return **new** positive integer or `NULL` on memory allocation failure
 */
cbor_item_t *cbor_build_uint32(uint32_t value);

/** Constructs a new positive integer
 *
 * @param value the value to use
 * @return **new** positive integer or `NULL` on memory allocation failure
 */
cbor_item_t *cbor_build_uint64(uint64_t value);

/** Constructs a new negative integer
 *
 * @param value the value to use
 * @return **new** negative integer or `NULL` on memory allocation failure
 */
cbor_item_t *cbor_build_negint8(uint8_t value);

/** Constructs a new negative integer
 *
 * @param value the value to use
 * @return **new** negative integer or `NULL` on memory allocation failure
 */
cbor_item_t *cbor_build_negint16(uint16_t value);

/** Constructs a new negative integer
 *
 * @param value the value to use
 * @return **new** negative integer or `NULL` on memory allocation failure
 */
cbor_item_t *cbor_build_negint32(uint32_t value);

/** Constructs a new negative integer
 *
 * @param value the value to use
 * @return **new** negative integer or `NULL` on memory allocation failure
 */
cbor_item_t *cbor_build_negint64(uint64_t value);

#ifdef __cplusplus
}
#endif

#endif  // LIBCBOR_INTS_H