/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 | 7.36M | cbor_int_width cbor_int_get_width(const cbor_item_t *item) { |
11 | 7.36M | CBOR_ASSERT(cbor_is_int(item)); |
12 | 7.36M | return item->metadata.int_metadata.width; |
13 | 7.36M | } |
14 | | |
15 | 7.30M | uint8_t cbor_get_uint8(const cbor_item_t *item) { |
16 | 7.30M | CBOR_ASSERT(cbor_is_int(item)); |
17 | 7.30M | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_8); |
18 | 7.30M | return *item->data; |
19 | 7.30M | } |
20 | | |
21 | 29.0k | uint16_t cbor_get_uint16(const cbor_item_t *item) { |
22 | 29.0k | CBOR_ASSERT(cbor_is_int(item)); |
23 | 29.0k | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_16); |
24 | 29.0k | return *(uint16_t *)item->data; |
25 | 29.0k | } |
26 | | |
27 | 435 | uint32_t cbor_get_uint32(const cbor_item_t *item) { |
28 | 435 | CBOR_ASSERT(cbor_is_int(item)); |
29 | 435 | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_32); |
30 | 435 | return *(uint32_t *)item->data; |
31 | 435 | } |
32 | | |
33 | 1.42k | uint64_t cbor_get_uint64(const cbor_item_t *item) { |
34 | 1.42k | CBOR_ASSERT(cbor_is_int(item)); |
35 | 1.42k | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_64); |
36 | 1.42k | return *(uint64_t *)item->data; |
37 | 1.42k | } |
38 | | |
39 | 368k | uint64_t cbor_get_int(const cbor_item_t *item) { |
40 | 368k | CBOR_ASSERT(cbor_is_int(item)); |
41 | | // cppcheck-suppress missingReturn |
42 | 368k | switch (cbor_int_get_width(item)) { |
43 | 362k | case CBOR_INT_8: |
44 | 362k | return cbor_get_uint8(item); |
45 | 4.53k | case CBOR_INT_16: |
46 | 4.53k | return cbor_get_uint16(item); |
47 | 435 | case CBOR_INT_32: |
48 | 435 | return cbor_get_uint32(item); |
49 | 926 | case CBOR_INT_64: |
50 | 926 | return cbor_get_uint64(item); |
51 | 368k | } |
52 | 368k | } |
53 | | |
54 | 7.29M | void cbor_set_uint8(cbor_item_t *item, uint8_t value) { |
55 | 7.29M | CBOR_ASSERT(cbor_is_int(item)); |
56 | 7.29M | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_8); |
57 | 7.29M | *item->data = value; |
58 | 7.29M | } |
59 | | |
60 | 72.6k | void cbor_set_uint16(cbor_item_t *item, uint16_t value) { |
61 | 72.6k | CBOR_ASSERT(cbor_is_int(item)); |
62 | 72.6k | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_16); |
63 | 72.6k | *(uint16_t *)item->data = value; |
64 | 72.6k | } |
65 | | |
66 | 19.6k | void cbor_set_uint32(cbor_item_t *item, uint32_t value) { |
67 | 19.6k | CBOR_ASSERT(cbor_is_int(item)); |
68 | 19.6k | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_32); |
69 | 19.6k | *(uint32_t *)item->data = value; |
70 | 19.6k | } |
71 | | |
72 | 22.2k | void cbor_set_uint64(cbor_item_t *item, uint64_t value) { |
73 | 22.2k | CBOR_ASSERT(cbor_is_int(item)); |
74 | 22.2k | CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_64); |
75 | 22.2k | *(uint64_t *)item->data = value; |
76 | 22.2k | } |
77 | | |
78 | 4.61M | void cbor_mark_uint(cbor_item_t *item) { |
79 | 4.61M | CBOR_ASSERT(cbor_is_int(item)); |
80 | 4.61M | item->type = CBOR_TYPE_UINT; |
81 | 4.61M | } |
82 | | |
83 | 1.15M | void cbor_mark_negint(cbor_item_t *item) { |
84 | 1.15M | CBOR_ASSERT(cbor_is_int(item)); |
85 | 1.15M | item->type = CBOR_TYPE_NEGINT; |
86 | 1.15M | } |
87 | | |
88 | 7.29M | cbor_item_t *cbor_new_int8(void) { |
89 | 7.29M | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 1); |
90 | 7.29M | _CBOR_NOTNULL(item); |
91 | 7.29M | *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), |
92 | 7.29M | .refcount = 1, |
93 | 7.29M | .metadata = {.int_metadata = {.width = CBOR_INT_8}}, |
94 | 7.29M | .type = CBOR_TYPE_UINT}; |
95 | 7.29M | return item; |
96 | 7.29M | } |
97 | | |
98 | 72.6k | cbor_item_t *cbor_new_int16(void) { |
99 | 72.6k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 2); |
100 | 72.6k | _CBOR_NOTNULL(item); |
101 | 72.6k | *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), |
102 | 72.6k | .refcount = 1, |
103 | 72.6k | .metadata = {.int_metadata = {.width = CBOR_INT_16}}, |
104 | 72.6k | .type = CBOR_TYPE_UINT}; |
105 | 72.6k | return item; |
106 | 72.6k | } |
107 | | |
108 | 19.6k | cbor_item_t *cbor_new_int32(void) { |
109 | 19.6k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4); |
110 | 19.6k | _CBOR_NOTNULL(item); |
111 | 19.6k | *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), |
112 | 19.6k | .refcount = 1, |
113 | 19.6k | .metadata = {.int_metadata = {.width = CBOR_INT_32}}, |
114 | 19.6k | .type = CBOR_TYPE_UINT}; |
115 | 19.6k | return item; |
116 | 19.6k | } |
117 | | |
118 | 22.2k | cbor_item_t *cbor_new_int64(void) { |
119 | 22.2k | cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 8); |
120 | 22.2k | _CBOR_NOTNULL(item); |
121 | 22.2k | *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t), |
122 | 22.2k | .refcount = 1, |
123 | 22.2k | .metadata = {.int_metadata = {.width = CBOR_INT_64}}, |
124 | 22.2k | .type = CBOR_TYPE_UINT}; |
125 | 22.2k | return item; |
126 | 22.2k | } |
127 | | |
128 | 1.94M | cbor_item_t *cbor_build_uint8(uint8_t value) { |
129 | 1.94M | cbor_item_t *item = cbor_new_int8(); |
130 | 1.94M | _CBOR_NOTNULL(item); |
131 | 1.94M | cbor_set_uint8(item, value); |
132 | 1.94M | cbor_mark_uint(item); |
133 | 1.94M | return item; |
134 | 1.94M | } |
135 | | |
136 | 24.5k | cbor_item_t *cbor_build_uint16(uint16_t value) { |
137 | 24.5k | cbor_item_t *item = cbor_new_int16(); |
138 | 24.5k | _CBOR_NOTNULL(item); |
139 | 24.5k | cbor_set_uint16(item, value); |
140 | 24.5k | cbor_mark_uint(item); |
141 | 24.5k | return item; |
142 | 24.5k | } |
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 | 504 | cbor_item_t *cbor_build_uint64(uint64_t value) { |
153 | 504 | cbor_item_t *item = cbor_new_int64(); |
154 | 504 | _CBOR_NOTNULL(item); |
155 | 504 | cbor_set_uint64(item, value); |
156 | 504 | cbor_mark_uint(item); |
157 | 504 | return item; |
158 | 504 | } |
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 | } |