110ff414cSEd Maste /*
210ff414cSEd Maste * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
310ff414cSEd Maste *
410ff414cSEd Maste * libcbor is free software; you can redistribute it and/or modify
510ff414cSEd Maste * it under the terms of the MIT license. See LICENSE for details.
610ff414cSEd Maste */
710ff414cSEd Maste
810ff414cSEd Maste #include "ints.h"
910ff414cSEd Maste
cbor_int_get_width(const cbor_item_t * item)1010ff414cSEd Maste cbor_int_width cbor_int_get_width(const cbor_item_t *item) {
11*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
1210ff414cSEd Maste return item->metadata.int_metadata.width;
1310ff414cSEd Maste }
1410ff414cSEd Maste
cbor_get_uint8(const cbor_item_t * item)1510ff414cSEd Maste uint8_t cbor_get_uint8(const cbor_item_t *item) {
16*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
17*5d3e7166SEd Maste CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_8);
1810ff414cSEd Maste return *item->data;
1910ff414cSEd Maste }
2010ff414cSEd Maste
cbor_get_uint16(const cbor_item_t * item)2110ff414cSEd Maste uint16_t cbor_get_uint16(const cbor_item_t *item) {
22*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
23*5d3e7166SEd Maste CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_16);
2410ff414cSEd Maste return *(uint16_t *)item->data;
2510ff414cSEd Maste }
2610ff414cSEd Maste
cbor_get_uint32(const cbor_item_t * item)2710ff414cSEd Maste uint32_t cbor_get_uint32(const cbor_item_t *item) {
28*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
29*5d3e7166SEd Maste CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_32);
3010ff414cSEd Maste return *(uint32_t *)item->data;
3110ff414cSEd Maste }
3210ff414cSEd Maste
cbor_get_uint64(const cbor_item_t * item)3310ff414cSEd Maste uint64_t cbor_get_uint64(const cbor_item_t *item) {
34*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
35*5d3e7166SEd Maste CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_64);
3610ff414cSEd Maste return *(uint64_t *)item->data;
3710ff414cSEd Maste }
3810ff414cSEd Maste
cbor_get_int(const cbor_item_t * item)3910ff414cSEd Maste uint64_t cbor_get_int(const cbor_item_t *item) {
40*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
41*5d3e7166SEd Maste // cppcheck-suppress missingReturn
4210ff414cSEd Maste switch (cbor_int_get_width(item)) {
4310ff414cSEd Maste case CBOR_INT_8:
4410ff414cSEd Maste return cbor_get_uint8(item);
4510ff414cSEd Maste case CBOR_INT_16:
4610ff414cSEd Maste return cbor_get_uint16(item);
4710ff414cSEd Maste case CBOR_INT_32:
4810ff414cSEd Maste return cbor_get_uint32(item);
4910ff414cSEd Maste case CBOR_INT_64:
5010ff414cSEd Maste return cbor_get_uint64(item);
5110ff414cSEd Maste }
5210ff414cSEd Maste }
5310ff414cSEd Maste
cbor_set_uint8(cbor_item_t * item,uint8_t value)5410ff414cSEd Maste void cbor_set_uint8(cbor_item_t *item, uint8_t value) {
55*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
56*5d3e7166SEd Maste CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_8);
5710ff414cSEd Maste *item->data = value;
5810ff414cSEd Maste }
5910ff414cSEd Maste
cbor_set_uint16(cbor_item_t * item,uint16_t value)6010ff414cSEd Maste void cbor_set_uint16(cbor_item_t *item, uint16_t value) {
61*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
62*5d3e7166SEd Maste CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_16);
6310ff414cSEd Maste *(uint16_t *)item->data = value;
6410ff414cSEd Maste }
6510ff414cSEd Maste
cbor_set_uint32(cbor_item_t * item,uint32_t value)6610ff414cSEd Maste void cbor_set_uint32(cbor_item_t *item, uint32_t value) {
67*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
68*5d3e7166SEd Maste CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_32);
6910ff414cSEd Maste *(uint32_t *)item->data = value;
7010ff414cSEd Maste }
7110ff414cSEd Maste
cbor_set_uint64(cbor_item_t * item,uint64_t value)7210ff414cSEd Maste void cbor_set_uint64(cbor_item_t *item, uint64_t value) {
73*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
74*5d3e7166SEd Maste CBOR_ASSERT(cbor_int_get_width(item) == CBOR_INT_64);
7510ff414cSEd Maste *(uint64_t *)item->data = value;
7610ff414cSEd Maste }
7710ff414cSEd Maste
cbor_mark_uint(cbor_item_t * item)7810ff414cSEd Maste void cbor_mark_uint(cbor_item_t *item) {
79*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
8010ff414cSEd Maste item->type = CBOR_TYPE_UINT;
8110ff414cSEd Maste }
8210ff414cSEd Maste
cbor_mark_negint(cbor_item_t * item)8310ff414cSEd Maste void cbor_mark_negint(cbor_item_t *item) {
84*5d3e7166SEd Maste CBOR_ASSERT(cbor_is_int(item));
8510ff414cSEd Maste item->type = CBOR_TYPE_NEGINT;
8610ff414cSEd Maste }
8710ff414cSEd Maste
cbor_new_int8(void)88*5d3e7166SEd Maste cbor_item_t *cbor_new_int8(void) {
89*5d3e7166SEd Maste cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 1);
9010ff414cSEd Maste _CBOR_NOTNULL(item);
9110ff414cSEd Maste *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
9210ff414cSEd Maste .refcount = 1,
9310ff414cSEd Maste .metadata = {.int_metadata = {.width = CBOR_INT_8}},
9410ff414cSEd Maste .type = CBOR_TYPE_UINT};
9510ff414cSEd Maste return item;
9610ff414cSEd Maste }
9710ff414cSEd Maste
cbor_new_int16(void)98*5d3e7166SEd Maste cbor_item_t *cbor_new_int16(void) {
99*5d3e7166SEd Maste cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 2);
10010ff414cSEd Maste _CBOR_NOTNULL(item);
10110ff414cSEd Maste *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
10210ff414cSEd Maste .refcount = 1,
10310ff414cSEd Maste .metadata = {.int_metadata = {.width = CBOR_INT_16}},
10410ff414cSEd Maste .type = CBOR_TYPE_UINT};
10510ff414cSEd Maste return item;
10610ff414cSEd Maste }
10710ff414cSEd Maste
cbor_new_int32(void)108*5d3e7166SEd Maste cbor_item_t *cbor_new_int32(void) {
109*5d3e7166SEd Maste cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 4);
11010ff414cSEd Maste _CBOR_NOTNULL(item);
11110ff414cSEd Maste *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
11210ff414cSEd Maste .refcount = 1,
11310ff414cSEd Maste .metadata = {.int_metadata = {.width = CBOR_INT_32}},
11410ff414cSEd Maste .type = CBOR_TYPE_UINT};
11510ff414cSEd Maste return item;
11610ff414cSEd Maste }
11710ff414cSEd Maste
cbor_new_int64(void)118*5d3e7166SEd Maste cbor_item_t *cbor_new_int64(void) {
119*5d3e7166SEd Maste cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t) + 8);
12010ff414cSEd Maste _CBOR_NOTNULL(item);
12110ff414cSEd Maste *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
12210ff414cSEd Maste .refcount = 1,
12310ff414cSEd Maste .metadata = {.int_metadata = {.width = CBOR_INT_64}},
12410ff414cSEd Maste .type = CBOR_TYPE_UINT};
12510ff414cSEd Maste return item;
12610ff414cSEd Maste }
12710ff414cSEd Maste
cbor_build_uint8(uint8_t value)12810ff414cSEd Maste cbor_item_t *cbor_build_uint8(uint8_t value) {
12910ff414cSEd Maste cbor_item_t *item = cbor_new_int8();
13010ff414cSEd Maste _CBOR_NOTNULL(item);
13110ff414cSEd Maste cbor_set_uint8(item, value);
13210ff414cSEd Maste cbor_mark_uint(item);
13310ff414cSEd Maste return item;
13410ff414cSEd Maste }
13510ff414cSEd Maste
cbor_build_uint16(uint16_t value)13610ff414cSEd Maste cbor_item_t *cbor_build_uint16(uint16_t value) {
13710ff414cSEd Maste cbor_item_t *item = cbor_new_int16();
13810ff414cSEd Maste _CBOR_NOTNULL(item);
13910ff414cSEd Maste cbor_set_uint16(item, value);
14010ff414cSEd Maste cbor_mark_uint(item);
14110ff414cSEd Maste return item;
14210ff414cSEd Maste }
14310ff414cSEd Maste
cbor_build_uint32(uint32_t value)14410ff414cSEd Maste cbor_item_t *cbor_build_uint32(uint32_t value) {
14510ff414cSEd Maste cbor_item_t *item = cbor_new_int32();
14610ff414cSEd Maste _CBOR_NOTNULL(item);
14710ff414cSEd Maste cbor_set_uint32(item, value);
14810ff414cSEd Maste cbor_mark_uint(item);
14910ff414cSEd Maste return item;
15010ff414cSEd Maste }
15110ff414cSEd Maste
cbor_build_uint64(uint64_t value)15210ff414cSEd Maste cbor_item_t *cbor_build_uint64(uint64_t value) {
15310ff414cSEd Maste cbor_item_t *item = cbor_new_int64();
15410ff414cSEd Maste _CBOR_NOTNULL(item);
15510ff414cSEd Maste cbor_set_uint64(item, value);
15610ff414cSEd Maste cbor_mark_uint(item);
15710ff414cSEd Maste return item;
15810ff414cSEd Maste }
15910ff414cSEd Maste
cbor_build_negint8(uint8_t value)16010ff414cSEd Maste cbor_item_t *cbor_build_negint8(uint8_t value) {
16110ff414cSEd Maste cbor_item_t *item = cbor_new_int8();
16210ff414cSEd Maste _CBOR_NOTNULL(item);
16310ff414cSEd Maste cbor_set_uint8(item, value);
16410ff414cSEd Maste cbor_mark_negint(item);
16510ff414cSEd Maste return item;
16610ff414cSEd Maste }
16710ff414cSEd Maste
cbor_build_negint16(uint16_t value)16810ff414cSEd Maste cbor_item_t *cbor_build_negint16(uint16_t value) {
16910ff414cSEd Maste cbor_item_t *item = cbor_new_int16();
17010ff414cSEd Maste _CBOR_NOTNULL(item);
17110ff414cSEd Maste cbor_set_uint16(item, value);
17210ff414cSEd Maste cbor_mark_negint(item);
17310ff414cSEd Maste return item;
17410ff414cSEd Maste }
17510ff414cSEd Maste
cbor_build_negint32(uint32_t value)17610ff414cSEd Maste cbor_item_t *cbor_build_negint32(uint32_t value) {
17710ff414cSEd Maste cbor_item_t *item = cbor_new_int32();
17810ff414cSEd Maste _CBOR_NOTNULL(item);
17910ff414cSEd Maste cbor_set_uint32(item, value);
18010ff414cSEd Maste cbor_mark_negint(item);
18110ff414cSEd Maste return item;
18210ff414cSEd Maste }
18310ff414cSEd Maste
cbor_build_negint64(uint64_t value)18410ff414cSEd Maste cbor_item_t *cbor_build_negint64(uint64_t value) {
18510ff414cSEd Maste cbor_item_t *item = cbor_new_int64();
18610ff414cSEd Maste _CBOR_NOTNULL(item);
18710ff414cSEd Maste cbor_set_uint64(item, value);
18810ff414cSEd Maste cbor_mark_negint(item);
18910ff414cSEd Maste return item;
19010ff414cSEd Maste }
191