/src/libcbor/src/cbor/ints.c
Line | Count | Source |
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 | | #include "ints.h" |
9 | | |
10 | 4.23M | cbor_int_width cbor_int_get_width(const cbor_item_t *item) { |
11 | 4.23M | CBOR_ASSERT(cbor_is_int(item)); |
12 | 4.23M | return item->metadata.int_metadata.width; |
13 | 4.23M | } |
14 | | |
15 | 4.18M | uint8_t cbor_get_uint8(const cbor_item_t *item) { |
16 | 4.18M | CBOR_ASSERT(cbor_is_int(item)); |
17 | 4.18M | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_8); |
18 | 4.18M | return *item->data; |
19 | 4.18M | } |
20 | | |
21 | 20.9k | uint16_t cbor_get_uint16(const cbor_item_t *item) { |
22 | 20.9k | CBOR_ASSERT(cbor_is_int(item)); |
23 | 20.9k | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_16); |
24 | 20.9k | return *(uint16_t *)item->data; |
25 | 20.9k | } |
26 | | |
27 | 286 | uint32_t cbor_get_uint32(const cbor_item_t *item) { |
28 | 286 | CBOR_ASSERT(cbor_is_int(item)); |
29 | 286 | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_32); |
30 | 286 | return *(uint32_t *)item->data; |
31 | 286 | } |
32 | | |
33 | 687 | uint64_t cbor_get_uint64(const cbor_item_t *item) { |
34 | 687 | CBOR_ASSERT(cbor_is_int(item)); |
35 | 687 | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_64); |
36 | 687 | return *(uint64_t *)item->data; |
37 | 687 | } |
38 | | |
39 | 265k | uint64_t cbor_get_int(const cbor_item_t *item) { |
40 | 265k | CBOR_ASSERT(cbor_is_int(item)); |
41 | | // cppcheck-suppress missingReturn |
42 | 265k | switch (cbor_int_get_width(item)) { |
43 | 264k | case CBOR_INT_8: |
44 | 264k | return cbor_get_uint8(item); |
45 | 280 | case CBOR_INT_16: |
46 | 280 | return cbor_get_uint16(item); |
47 | 286 | case CBOR_INT_32: |
48 | 286 | return cbor_get_uint32(item); |
49 | 341 | case CBOR_INT_64: |
50 | 341 | return cbor_get_uint64(item); |
51 | 265k | } |
52 | 265k | } |
53 | | |
54 | 3.76M | void cbor_set_uint8(cbor_item_t *item, uint8_t value) { |
55 | 3.76M | CBOR_ASSERT(cbor_is_int(item)); |
56 | 3.76M | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_8); |
57 | 3.76M | *item->data = value; |
58 | 3.76M | } |
59 | | |
60 | 46.2k | void cbor_set_uint16(cbor_item_t *item, uint16_t value) { |
61 | 46.2k | CBOR_ASSERT(cbor_is_int(item)); |
62 | 46.2k | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_16); |
63 | 46.2k | *(uint16_t *)item->data = value; |
64 | 46.2k | } |
65 | | |
66 | 4.96k | void cbor_set_uint32(cbor_item_t *item, uint32_t value) { |
67 | 4.96k | CBOR_ASSERT(cbor_is_int(item)); |
68 | 4.96k | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_32); |
69 | 4.96k | *(uint32_t *)item->data = value; |
70 | 4.96k | } |
71 | | |
72 | 14.9k | void cbor_set_uint64(cbor_item_t *item, uint64_t value) { |
73 | 14.9k | CBOR_ASSERT(cbor_is_int(item)); |
74 | 14.9k | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_64); |
75 | 14.9k | *(uint64_t *)item->data = value; |
76 | 14.9k | } |
77 | | |
78 | 2.66M | void cbor_mark_uint(cbor_item_t *item) { |
79 | 2.66M | CBOR_ASSERT(cbor_is_int(item)); |
80 | 2.66M | item->type = CBOR_TYPE_UINT; |
81 | 2.66M | } |
82 | | |
83 | 203k | void cbor_mark_negint(cbor_item_t *item) { |
84 | 203k | CBOR_ASSERT(cbor_is_int(item)); |
85 | 203k | item->type = CBOR_TYPE_NEGINT; |
86 | 203k | } |
87 | | |
88 | 3.76M | cbor_item_t *cbor_new_int8(void) { |
89 | 3.76M | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 1); |
90 | 3.76M | _CBOR_NOTNULL(item); |
91 | 3.76M | *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), |
92 | 3.76M | .refcount = 1, |
93 | 3.76M | .metadata = {.int_metadata = {.width = CBOR_INT_8}}, |
94 | 3.76M | .type = CBOR_TYPE_UINT}; |
95 | 3.76M | return item; |
96 | 3.76M | } |
97 | | |
98 | 46.2k | cbor_item_t *cbor_new_int16(void) { |
99 | 46.2k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 2); |
100 | 46.2k | _CBOR_NOTNULL(item); |
101 | 46.2k | *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), |
102 | 46.2k | .refcount = 1, |
103 | 46.2k | .metadata = {.int_metadata = {.width = CBOR_INT_16}}, |
104 | 46.2k | .type = CBOR_TYPE_UINT}; |
105 | 46.2k | return item; |
106 | 46.2k | } |
107 | | |
108 | 4.96k | cbor_item_t *cbor_new_int32(void) { |
109 | 4.96k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4); |
110 | 4.96k | _CBOR_NOTNULL(item); |
111 | 4.96k | *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), |
112 | 4.96k | .refcount = 1, |
113 | 4.96k | .metadata = {.int_metadata = {.width = CBOR_INT_32}}, |
114 | 4.96k | .type = CBOR_TYPE_UINT}; |
115 | 4.96k | return item; |
116 | 4.96k | } |
117 | | |
118 | 14.9k | cbor_item_t *cbor_new_int64(void) { |
119 | 14.9k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 8); |
120 | 14.9k | _CBOR_NOTNULL(item); |
121 | 14.9k | *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), |
122 | 14.9k | .refcount = 1, |
123 | 14.9k | .metadata = {.int_metadata = {.width = CBOR_INT_64}}, |
124 | 14.9k | .type = CBOR_TYPE_UINT}; |
125 | 14.9k | return item; |
126 | 14.9k | } |
127 | | |
128 | 1.20M | cbor_item_t *cbor_build_uint8(uint8_t value) { |
129 | 1.20M | cbor_item_t *item = cbor_new_int8(); |
130 | 1.20M | _CBOR_NOTNULL(item); |
131 | 1.20M | cbor_set_uint8(item, value); |
132 | 1.20M | cbor_mark_uint(item); |
133 | 1.20M | return item; |
134 | 1.20M | } |
135 | | |
136 | 20.6k | cbor_item_t *cbor_build_uint16(uint16_t value) { |
137 | 20.6k | cbor_item_t *item = cbor_new_int16(); |
138 | 20.6k | _CBOR_NOTNULL(item); |
139 | 20.6k | cbor_set_uint16(item, value); |
140 | 20.6k | cbor_mark_uint(item); |
141 | 20.6k | return item; |
142 | 20.6k | } |
143 | | |
144 | 0 | cbor_item_t *cbor_build_uint32(uint32_t value) { |
145 | 0 | cbor_item_t *item = cbor_new_int32(); |
146 | 0 | _CBOR_NOTNULL(item); |
147 | 0 | cbor_set_uint32(item, value); |
148 | 0 | cbor_mark_uint(item); |
149 | 0 | return item; |
150 | 0 | } |
151 | | |
152 | 346 | cbor_item_t *cbor_build_uint64(uint64_t value) { |
153 | 346 | cbor_item_t *item = cbor_new_int64(); |
154 | 346 | _CBOR_NOTNULL(item); |
155 | 346 | cbor_set_uint64(item, value); |
156 | 346 | cbor_mark_uint(item); |
157 | 346 | return item; |
158 | 346 | } |
159 | | |
160 | 0 | cbor_item_t *cbor_build_negint8(uint8_t value) { |
161 | 0 | cbor_item_t *item = cbor_new_int8(); |
162 | 0 | _CBOR_NOTNULL(item); |
163 | 0 | cbor_set_uint8(item, value); |
164 | 0 | cbor_mark_negint(item); |
165 | 0 | return item; |
166 | 0 | } |
167 | | |
168 | 0 | cbor_item_t *cbor_build_negint16(uint16_t value) { |
169 | 0 | cbor_item_t *item = cbor_new_int16(); |
170 | 0 | _CBOR_NOTNULL(item); |
171 | 0 | cbor_set_uint16(item, value); |
172 | 0 | cbor_mark_negint(item); |
173 | 0 | return item; |
174 | 0 | } |
175 | | |
176 | 0 | cbor_item_t *cbor_build_negint32(uint32_t value) { |
177 | 0 | cbor_item_t *item = cbor_new_int32(); |
178 | 0 | _CBOR_NOTNULL(item); |
179 | 0 | cbor_set_uint32(item, value); |
180 | 0 | cbor_mark_negint(item); |
181 | 0 | return item; |
182 | 0 | } |
183 | | |
184 | 0 | cbor_item_t *cbor_build_negint64(uint64_t value) { |
185 | 0 | cbor_item_t *item = cbor_new_int64(); |
186 | 0 | _CBOR_NOTNULL(item); |
187 | 0 | cbor_set_uint64(item, value); |
188 | 0 | cbor_mark_negint(item); |
189 | 0 | return item; |
190 | 0 | } |