libcbor  0.5.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
ints.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2017 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 
11 {
12  assert(cbor_is_int(item));
13  return item->metadata.int_metadata.width;
14 }
15 
16 uint8_t cbor_get_uint8(const cbor_item_t *item)
17 {
18  assert(cbor_is_int(item));
19  assert(cbor_int_get_width(item) == CBOR_INT_8);
20  return *item->data;
21 }
22 
23 uint16_t cbor_get_uint16(const cbor_item_t *item)
24 {
25  assert(cbor_is_int(item));
26  assert(cbor_int_get_width(item) == CBOR_INT_16);
27  return *(uint16_t *) item->data;
28 }
29 
30 uint32_t cbor_get_uint32(const cbor_item_t *item)
31 {
32  assert(cbor_is_int(item));
33  assert(cbor_int_get_width(item) == CBOR_INT_32);
34  return *(uint32_t *) item->data;
35 }
36 
37 uint64_t cbor_get_uint64(const cbor_item_t *item)
38 {
39  assert(cbor_is_int(item));
40  assert(cbor_int_get_width(item) == CBOR_INT_64);
41  return *(uint64_t *) item->data;
42 }
43 
44 uint64_t cbor_get_int(const cbor_item_t * item)
45 {
46  assert(cbor_is_int(item));
47  switch(cbor_int_get_width(item)) {
48  case CBOR_INT_8: return cbor_get_uint8(item);
49  case CBOR_INT_16: return cbor_get_uint16(item);
50  case CBOR_INT_32: return cbor_get_uint32(item);
51  case CBOR_INT_64: return cbor_get_uint64(item);
52  }
53  return 0xDEADBEEF; /* Compiler complaints */
54 }
55 
56 void cbor_set_uint8(cbor_item_t *item, uint8_t value)
57 {
58  assert(cbor_is_int(item));
59  assert(cbor_int_get_width(item) == CBOR_INT_8);
60  *item->data = value;
61 }
62 
63 void cbor_set_uint16(cbor_item_t *item, uint16_t value)
64 {
65  assert(cbor_is_int(item));
66  assert(cbor_int_get_width(item) == CBOR_INT_16);
67  *(uint16_t *) item->data = value;
68 }
69 
70 
71 void cbor_set_uint32(cbor_item_t *item, uint32_t value)
72 {
73  assert(cbor_is_int(item));
74  assert(cbor_int_get_width(item) == CBOR_INT_32);
75  *(uint32_t *) item->data = value;
76 }
77 
78 void cbor_set_uint64(cbor_item_t *item, uint64_t value)
79 {
80  assert(cbor_is_int(item));
81  assert(cbor_int_get_width(item) == CBOR_INT_64);
82  *(uint64_t *) item->data = value;
83 }
84 
86 {
87  assert(cbor_is_int(item));
88  item->type = CBOR_TYPE_UINT;
89 }
90 
92 {
93  assert(cbor_is_int(item));
94  item->type = CBOR_TYPE_NEGINT;
95 }
96 
98 {
99  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 1);
100  *item = (cbor_item_t) {
101  .data = (unsigned char *) item + sizeof(cbor_item_t),
102  .refcount = 1,
103  .metadata = {.int_metadata = {.width = CBOR_INT_8}},
104  .type = CBOR_TYPE_UINT
105  };
106  return item;
107 }
108 
110 {
111  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 2);
112  *item = (cbor_item_t) {
113  .data = (unsigned char *) item + sizeof(cbor_item_t),
114  .refcount = 1,
115  .metadata = {.int_metadata = {.width = CBOR_INT_16}},
116  .type = CBOR_TYPE_UINT
117  };
118  return item;
119 }
120 
122 {
123  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 4);
124  *item = (cbor_item_t) {
125  .data = (unsigned char *) item + sizeof(cbor_item_t),
126  .refcount = 1,
127  .metadata = {.int_metadata = {.width = CBOR_INT_32}},
128  .type = CBOR_TYPE_UINT
129  };
130  return item;
131 }
132 
134 {
135  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 8);
136  *item = (cbor_item_t) {
137  .data = (unsigned char *) item + sizeof(cbor_item_t),
138  .refcount = 1,
139  .metadata = {.int_metadata = {.width = CBOR_INT_64}},
140  .type = CBOR_TYPE_UINT
141  };
142  return item;
143 }
144 
146 {
147  cbor_item_t *item = cbor_new_int8();
148  cbor_set_uint8(item, value);
149  cbor_mark_uint(item);
150  return item;
151 }
152 
154 {
155  cbor_item_t *item = cbor_new_int16();
156  cbor_set_uint16(item, value);
157  cbor_mark_uint(item);
158  return item;
159 }
160 
162 {
163  cbor_item_t *item = cbor_new_int32();
164  cbor_set_uint32(item, value);
165  cbor_mark_uint(item);
166  return item;
167 }
168 
170 {
171  cbor_item_t *item = cbor_new_int64();
172  cbor_set_uint64(item, value);
173  cbor_mark_uint(item);
174  return item;
175 }
176 
178 {
179  cbor_item_t *item = cbor_new_int8();
180  cbor_set_uint8(item, value);
181  cbor_mark_negint(item);
182  return item;
183 }
184 
186 {
187  cbor_item_t *item = cbor_new_int16();
188  cbor_set_uint16(item, value);
189  cbor_mark_negint(item);
190  return item;
191 }
192 
194 {
195  cbor_item_t *item = cbor_new_int32();
196  cbor_set_uint32(item, value);
197  cbor_mark_negint(item);
198  return item;
199 }
200 
202 {
203  cbor_item_t *item = cbor_new_int64();
204  cbor_set_uint64(item, value);
205  cbor_mark_negint(item);
206  return item;
207 }
cbor_item_t * cbor_build_uint8(uint8_t value)
Constructs a new positive integer.
Definition: ints.c:145
struct cbor_item_t cbor_item_t
The item handle.
cbor_item_t * cbor_build_negint64(uint64_t value)
Constructs a new negative integer.
Definition: ints.c:201
cbor_item_t * cbor_new_int64()
Allocates new integer with 8B width.
Definition: ints.c:133
uint64_t cbor_get_int(const cbor_item_t *item)
Extracts the integer value.
Definition: ints.c:44
union cbor_item_metadata metadata
Discriminated by type.
Definition: data.h:151
void cbor_set_uint16(cbor_item_t *item, uint16_t value)
Assigns the integer value.
Definition: ints.c:63
cbor_int_width cbor_int_get_width(const cbor_item_t *item)
Queries the integer width.
Definition: ints.c:10
cbor_item_t * cbor_new_int8()
Allocates new integer with 1B width.
Definition: ints.c:97
bool cbor_is_int(const cbor_item_t *item)
Is the item an integer, either positive or negative?
Definition: common.c:65
cbor_item_t * cbor_build_negint16(uint16_t value)
Constructs a new negative integer.
Definition: ints.c:185
cbor_item_t * cbor_build_negint32(uint32_t value)
Constructs a new negative integer.
Definition: ints.c:193
void cbor_set_uint8(cbor_item_t *item, uint8_t value)
Assigns the integer value.
Definition: ints.c:56
cbor_type type
Major type discriminator.
Definition: data.h:155
uint32_t cbor_get_uint32(const cbor_item_t *item)
Extracts the integer value.
Definition: ints.c:30
void cbor_mark_negint(cbor_item_t *item)
Marks the integer item as a negative integer.
Definition: ints.c:91
#define _CBOR_MALLOC
Definition: common.h:84
size_t refcount
Reference count - initialize to 0.
Definition: data.h:153
cbor_item_t * cbor_build_uint32(uint32_t value)
Constructs a new positive integer.
Definition: ints.c:161
uint16_t cbor_get_uint16(const cbor_item_t *item)
Extracts the integer value.
Definition: ints.c:23
cbor_int_width
Possible widths of CBOR_TYPE_UINT items.
Definition: data.h:46
void cbor_set_uint32(cbor_item_t *item, uint32_t value)
Assigns the integer value.
Definition: ints.c:71
0 - positive integers
Definition: data.h:25
cbor_item_t * cbor_build_uint16(uint16_t value)
Constructs a new positive integer.
Definition: ints.c:153
uint8_t cbor_get_uint8(const cbor_item_t *item)
Extracts the integer value.
Definition: ints.c:16
uint64_t cbor_get_uint64(const cbor_item_t *item)
Extracts the integer value.
Definition: ints.c:37
void cbor_mark_uint(cbor_item_t *item)
Marks the integer item as a positive integer.
Definition: ints.c:85
cbor_item_t * cbor_build_uint64(uint64_t value)
Constructs a new positive integer.
Definition: ints.c:169
struct _cbor_int_metadata int_metadata
Definition: data.h:139
unsigned char * data
Raw data block - interpretation depends on metadata.
Definition: data.h:157
cbor_item_t * cbor_new_int32()
Allocates new integer with 4B width.
Definition: ints.c:121
cbor_item_t * cbor_build_negint8(uint8_t value)
Constructs a new negative integer.
Definition: ints.c:177
cbor_int_width width
Definition: data.h:78
The item handle.
Definition: data.h:149
1 - negative integers
Definition: data.h:26
void cbor_set_uint64(cbor_item_t *item, uint64_t value)
Assigns the integer value.
Definition: ints.c:78
cbor_item_t * cbor_new_int16()
Allocates new integer with 2B width.
Definition: ints.c:109