xref: /freebsd/contrib/libcbor/src/cbor/ints.c (revision 10ff414c14eef433d8157f0c17904d740693933b)
1*10ff414cSEd Maste /*
2*10ff414cSEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3*10ff414cSEd Maste  *
4*10ff414cSEd Maste  * libcbor is free software; you can redistribute it and/or modify
5*10ff414cSEd Maste  * it under the terms of the MIT license. See LICENSE for details.
6*10ff414cSEd Maste  */
7*10ff414cSEd Maste 
8*10ff414cSEd Maste #include "ints.h"
9*10ff414cSEd Maste 
10*10ff414cSEd Maste cbor_int_width cbor_int_get_width(const cbor_item_t *item) {
11*10ff414cSEd Maste   assert(cbor_is_int(item));
12*10ff414cSEd Maste   return item->metadata.int_metadata.width;
13*10ff414cSEd Maste }
14*10ff414cSEd Maste 
15*10ff414cSEd Maste uint8_t cbor_get_uint8(const cbor_item_t *item) {
16*10ff414cSEd Maste   assert(cbor_is_int(item));
17*10ff414cSEd Maste   assert(cbor_int_get_width(item) == CBOR_INT_8);
18*10ff414cSEd Maste   return *item->data;
19*10ff414cSEd Maste }
20*10ff414cSEd Maste 
21*10ff414cSEd Maste uint16_t cbor_get_uint16(const cbor_item_t *item) {
22*10ff414cSEd Maste   assert(cbor_is_int(item));
23*10ff414cSEd Maste   assert(cbor_int_get_width(item) == CBOR_INT_16);
24*10ff414cSEd Maste   return *(uint16_t *)item->data;
25*10ff414cSEd Maste }
26*10ff414cSEd Maste 
27*10ff414cSEd Maste uint32_t cbor_get_uint32(const cbor_item_t *item) {
28*10ff414cSEd Maste   assert(cbor_is_int(item));
29*10ff414cSEd Maste   assert(cbor_int_get_width(item) == CBOR_INT_32);
30*10ff414cSEd Maste   return *(uint32_t *)item->data;
31*10ff414cSEd Maste }
32*10ff414cSEd Maste 
33*10ff414cSEd Maste uint64_t cbor_get_uint64(const cbor_item_t *item) {
34*10ff414cSEd Maste   assert(cbor_is_int(item));
35*10ff414cSEd Maste   assert(cbor_int_get_width(item) == CBOR_INT_64);
36*10ff414cSEd Maste   return *(uint64_t *)item->data;
37*10ff414cSEd Maste }
38*10ff414cSEd Maste 
39*10ff414cSEd Maste uint64_t cbor_get_int(const cbor_item_t *item) {
40*10ff414cSEd Maste   assert(cbor_is_int(item));
41*10ff414cSEd Maste   switch (cbor_int_get_width(item)) {
42*10ff414cSEd Maste     case CBOR_INT_8:
43*10ff414cSEd Maste       return cbor_get_uint8(item);
44*10ff414cSEd Maste     case CBOR_INT_16:
45*10ff414cSEd Maste       return cbor_get_uint16(item);
46*10ff414cSEd Maste     case CBOR_INT_32:
47*10ff414cSEd Maste       return cbor_get_uint32(item);
48*10ff414cSEd Maste     case CBOR_INT_64:
49*10ff414cSEd Maste       return cbor_get_uint64(item);
50*10ff414cSEd Maste   }
51*10ff414cSEd Maste   // TODO: This should be handled in a default branch
52*10ff414cSEd Maste   return 0xDEADBEEF; /* Compiler complaints */
53*10ff414cSEd Maste }
54*10ff414cSEd Maste 
55*10ff414cSEd Maste void cbor_set_uint8(cbor_item_t *item, uint8_t value) {
56*10ff414cSEd Maste   assert(cbor_is_int(item));
57*10ff414cSEd Maste   assert(cbor_int_get_width(item) == CBOR_INT_8);
58*10ff414cSEd Maste   *item->data = value;
59*10ff414cSEd Maste }
60*10ff414cSEd Maste 
61*10ff414cSEd Maste void cbor_set_uint16(cbor_item_t *item, uint16_t value) {
62*10ff414cSEd Maste   assert(cbor_is_int(item));
63*10ff414cSEd Maste   assert(cbor_int_get_width(item) == CBOR_INT_16);
64*10ff414cSEd Maste   *(uint16_t *)item->data = value;
65*10ff414cSEd Maste }
66*10ff414cSEd Maste 
67*10ff414cSEd Maste void cbor_set_uint32(cbor_item_t *item, uint32_t value) {
68*10ff414cSEd Maste   assert(cbor_is_int(item));
69*10ff414cSEd Maste   assert(cbor_int_get_width(item) == CBOR_INT_32);
70*10ff414cSEd Maste   *(uint32_t *)item->data = value;
71*10ff414cSEd Maste }
72*10ff414cSEd Maste 
73*10ff414cSEd Maste void cbor_set_uint64(cbor_item_t *item, uint64_t value) {
74*10ff414cSEd Maste   assert(cbor_is_int(item));
75*10ff414cSEd Maste   assert(cbor_int_get_width(item) == CBOR_INT_64);
76*10ff414cSEd Maste   *(uint64_t *)item->data = value;
77*10ff414cSEd Maste }
78*10ff414cSEd Maste 
79*10ff414cSEd Maste void cbor_mark_uint(cbor_item_t *item) {
80*10ff414cSEd Maste   assert(cbor_is_int(item));
81*10ff414cSEd Maste   item->type = CBOR_TYPE_UINT;
82*10ff414cSEd Maste }
83*10ff414cSEd Maste 
84*10ff414cSEd Maste void cbor_mark_negint(cbor_item_t *item) {
85*10ff414cSEd Maste   assert(cbor_is_int(item));
86*10ff414cSEd Maste   item->type = CBOR_TYPE_NEGINT;
87*10ff414cSEd Maste }
88*10ff414cSEd Maste 
89*10ff414cSEd Maste cbor_item_t *cbor_new_int8() {
90*10ff414cSEd Maste   cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 1);
91*10ff414cSEd Maste   _CBOR_NOTNULL(item);
92*10ff414cSEd Maste   *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
93*10ff414cSEd Maste                         .refcount = 1,
94*10ff414cSEd Maste                         .metadata = {.int_metadata = {.width = CBOR_INT_8}},
95*10ff414cSEd Maste                         .type = CBOR_TYPE_UINT};
96*10ff414cSEd Maste   return item;
97*10ff414cSEd Maste }
98*10ff414cSEd Maste 
99*10ff414cSEd Maste cbor_item_t *cbor_new_int16() {
100*10ff414cSEd Maste   cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 2);
101*10ff414cSEd Maste   _CBOR_NOTNULL(item);
102*10ff414cSEd Maste   *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
103*10ff414cSEd Maste                         .refcount = 1,
104*10ff414cSEd Maste                         .metadata = {.int_metadata = {.width = CBOR_INT_16}},
105*10ff414cSEd Maste                         .type = CBOR_TYPE_UINT};
106*10ff414cSEd Maste   return item;
107*10ff414cSEd Maste }
108*10ff414cSEd Maste 
109*10ff414cSEd Maste cbor_item_t *cbor_new_int32() {
110*10ff414cSEd Maste   cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 4);
111*10ff414cSEd Maste   _CBOR_NOTNULL(item);
112*10ff414cSEd Maste   *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
113*10ff414cSEd Maste                         .refcount = 1,
114*10ff414cSEd Maste                         .metadata = {.int_metadata = {.width = CBOR_INT_32}},
115*10ff414cSEd Maste                         .type = CBOR_TYPE_UINT};
116*10ff414cSEd Maste   return item;
117*10ff414cSEd Maste }
118*10ff414cSEd Maste 
119*10ff414cSEd Maste cbor_item_t *cbor_new_int64() {
120*10ff414cSEd Maste   cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 8);
121*10ff414cSEd Maste   _CBOR_NOTNULL(item);
122*10ff414cSEd Maste   *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
123*10ff414cSEd Maste                         .refcount = 1,
124*10ff414cSEd Maste                         .metadata = {.int_metadata = {.width = CBOR_INT_64}},
125*10ff414cSEd Maste                         .type = CBOR_TYPE_UINT};
126*10ff414cSEd Maste   return item;
127*10ff414cSEd Maste }
128*10ff414cSEd Maste 
129*10ff414cSEd Maste cbor_item_t *cbor_build_uint8(uint8_t value) {
130*10ff414cSEd Maste   cbor_item_t *item = cbor_new_int8();
131*10ff414cSEd Maste   _CBOR_NOTNULL(item);
132*10ff414cSEd Maste   cbor_set_uint8(item, value);
133*10ff414cSEd Maste   cbor_mark_uint(item);
134*10ff414cSEd Maste   return item;
135*10ff414cSEd Maste }
136*10ff414cSEd Maste 
137*10ff414cSEd Maste cbor_item_t *cbor_build_uint16(uint16_t value) {
138*10ff414cSEd Maste   cbor_item_t *item = cbor_new_int16();
139*10ff414cSEd Maste   _CBOR_NOTNULL(item);
140*10ff414cSEd Maste   cbor_set_uint16(item, value);
141*10ff414cSEd Maste   cbor_mark_uint(item);
142*10ff414cSEd Maste   return item;
143*10ff414cSEd Maste }
144*10ff414cSEd Maste 
145*10ff414cSEd Maste cbor_item_t *cbor_build_uint32(uint32_t value) {
146*10ff414cSEd Maste   cbor_item_t *item = cbor_new_int32();
147*10ff414cSEd Maste   _CBOR_NOTNULL(item);
148*10ff414cSEd Maste   cbor_set_uint32(item, value);
149*10ff414cSEd Maste   cbor_mark_uint(item);
150*10ff414cSEd Maste   return item;
151*10ff414cSEd Maste }
152*10ff414cSEd Maste 
153*10ff414cSEd Maste cbor_item_t *cbor_build_uint64(uint64_t value) {
154*10ff414cSEd Maste   cbor_item_t *item = cbor_new_int64();
155*10ff414cSEd Maste   _CBOR_NOTNULL(item);
156*10ff414cSEd Maste   cbor_set_uint64(item, value);
157*10ff414cSEd Maste   cbor_mark_uint(item);
158*10ff414cSEd Maste   return item;
159*10ff414cSEd Maste }
160*10ff414cSEd Maste 
161*10ff414cSEd Maste cbor_item_t *cbor_build_negint8(uint8_t value) {
162*10ff414cSEd Maste   cbor_item_t *item = cbor_new_int8();
163*10ff414cSEd Maste   _CBOR_NOTNULL(item);
164*10ff414cSEd Maste   cbor_set_uint8(item, value);
165*10ff414cSEd Maste   cbor_mark_negint(item);
166*10ff414cSEd Maste   return item;
167*10ff414cSEd Maste }
168*10ff414cSEd Maste 
169*10ff414cSEd Maste cbor_item_t *cbor_build_negint16(uint16_t value) {
170*10ff414cSEd Maste   cbor_item_t *item = cbor_new_int16();
171*10ff414cSEd Maste   _CBOR_NOTNULL(item);
172*10ff414cSEd Maste   cbor_set_uint16(item, value);
173*10ff414cSEd Maste   cbor_mark_negint(item);
174*10ff414cSEd Maste   return item;
175*10ff414cSEd Maste }
176*10ff414cSEd Maste 
177*10ff414cSEd Maste cbor_item_t *cbor_build_negint32(uint32_t value) {
178*10ff414cSEd Maste   cbor_item_t *item = cbor_new_int32();
179*10ff414cSEd Maste   _CBOR_NOTNULL(item);
180*10ff414cSEd Maste   cbor_set_uint32(item, value);
181*10ff414cSEd Maste   cbor_mark_negint(item);
182*10ff414cSEd Maste   return item;
183*10ff414cSEd Maste }
184*10ff414cSEd Maste 
185*10ff414cSEd Maste cbor_item_t *cbor_build_negint64(uint64_t value) {
186*10ff414cSEd Maste   cbor_item_t *item = cbor_new_int64();
187*10ff414cSEd Maste   _CBOR_NOTNULL(item);
188*10ff414cSEd Maste   cbor_set_uint64(item, value);
189*10ff414cSEd Maste   cbor_mark_negint(item);
190*10ff414cSEd Maste   return item;
191*10ff414cSEd Maste }
192