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