libcbor  0.5.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
common.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 "cbor/common.h"
9 #include "arrays.h"
10 #include "bytestrings.h"
11 #include "data.h"
12 #include "floats_ctrls.h"
13 #include "ints.h"
14 #include "maps.h"
15 #include "strings.h"
16 #include "tags.h"
17 
18 bool cbor_isa_uint(const cbor_item_t *item)
19 {
20  return item->type == CBOR_TYPE_UINT;
21 }
22 
23 bool cbor_isa_negint(const cbor_item_t *item)
24 {
25  return item->type == CBOR_TYPE_NEGINT;
26 }
27 
29 {
30  return item->type == CBOR_TYPE_BYTESTRING;
31 }
32 
33 bool cbor_isa_string(const cbor_item_t *item)
34 {
35  return item->type == CBOR_TYPE_STRING;
36 }
37 
38 bool cbor_isa_array(const cbor_item_t *item)
39 {
40  return item->type == CBOR_TYPE_ARRAY;
41 }
42 
43 bool cbor_isa_map(const cbor_item_t *item)
44 {
45  return item->type == CBOR_TYPE_MAP;
46 }
47 
48 bool cbor_isa_tag(const cbor_item_t *item)
49 {
50  return item->type == CBOR_TYPE_TAG;
51 }
52 
54 {
55  return item->type == CBOR_TYPE_FLOAT_CTRL;
56 }
57 
58 
60 {
61  return item->type;
62 }
63 
64 
65 bool cbor_is_int(const cbor_item_t *item)
66 {
67  return cbor_isa_uint(item) || cbor_isa_negint(item);
68 }
69 
70 
71 bool cbor_is_bool(const cbor_item_t *item)
72 {
73  return cbor_isa_float_ctrl(item) &&
75 }
76 
77 bool cbor_is_null(const cbor_item_t *item)
78 {
79  return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_NULL;
80 }
81 
82 bool cbor_is_undef(const cbor_item_t *item)
83 {
84  return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_UNDEF;
85 }
86 
87 bool cbor_is_float(const cbor_item_t *item)
88 {
89  return cbor_isa_float_ctrl(item) && !cbor_float_ctrl_is_ctrl(item);
90 }
91 
92 
94 {
95  item->refcount++;
96  return item;
97 }
98 
99 void cbor_decref(cbor_item_t **item_ref)
100 {
101  cbor_item_t * item = *item_ref;
102  if (--item->refcount == 0) {
103  switch (item->type) {
104  case CBOR_TYPE_UINT:
105  /* Fallthrough */
106  case CBOR_TYPE_NEGINT:
107  /* Combined allocation, freeing the item suffices */
108  {
109  break;
110  }
111  case CBOR_TYPE_BYTESTRING: {
112  if (cbor_bytestring_is_definite(item)) {
113  _CBOR_FREE(item->data);
114  } else {
115  /* We need to decref all chunks */
117  for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++)
118  cbor_decref(&handle[i]);
119  _CBOR_FREE(((struct cbor_indefinite_string_data *) item->data)->chunks);
120  _CBOR_FREE(item->data);
121  }
122  break;
123  }
124  case CBOR_TYPE_STRING: {
125  if (cbor_string_is_definite(item)) {
126  _CBOR_FREE(item->data);
127  } else {
128  /* We need to decref all chunks */
129  cbor_item_t **handle = cbor_string_chunks_handle(item);
130  for (size_t i = 0; i < cbor_string_chunk_count(item); i++)
131  cbor_decref(&handle[i]);
132  _CBOR_FREE(((struct cbor_indefinite_string_data *) item->data)->chunks);
133  _CBOR_FREE(item->data);
134  }
135  break;
136  }
137  case CBOR_TYPE_ARRAY: {
138  /* Get all items and decref them */
139  cbor_item_t **handle = cbor_array_handle(item);
140  size_t size = cbor_array_size(item);
141  for (size_t i = 0; i < size; i++)
142  if (handle[i] != NULL)
143  cbor_decref(&handle[i]);
144  _CBOR_FREE(item->data);
145  break;
146  }
147  case CBOR_TYPE_MAP: {
148  struct cbor_pair *handle = cbor_map_handle(item);
149  for (size_t i = 0; i < item->metadata.map_metadata.end_ptr; i++, handle++) {
150  cbor_decref(&handle->key);
151  if (handle->value != NULL)
152  cbor_decref(&handle->value);
153  }
154  _CBOR_FREE(item->data);
155  break;
156  };
157  case CBOR_TYPE_TAG: {
158  if (item->metadata.tag_metadata.tagged_item != NULL)
160  _CBOR_FREE(item->data);
161  break;
162  }
163  case CBOR_TYPE_FLOAT_CTRL: {
164  /* Floats have combined allocation */
165  break;
166  }
167  }
168  _CBOR_FREE(item);
169  //TODO
170  *item_ref = NULL;
171  }
172 }
173 
175 {
176  cbor_decref(&item);
177 }
178 
179 size_t cbor_refcount(const cbor_item_t * item)
180 {
181  return item->refcount;
182 }
183 
185 {
186  item->refcount--;
187  return item;
188 }
bool cbor_bytestring_is_definite(const cbor_item_t *item)
Is the byte string definite?
Definition: bytestrings.c:24
6 - tags
Definition: data.h:31
3 - strings
Definition: data.h:28
union cbor_item_metadata metadata
Discriminated by type.
Definition: data.h:151
void cbor_intermediate_decref(cbor_item_t *item)
Decreases the reference count by one, deallocating the item if needed.
Definition: common.c:174
2 - byte strings
Definition: data.h:27
struct _cbor_map_metadata map_metadata
Definition: data.h:143
bool cbor_isa_negint(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:23
cbor_item_t * value
Definition: data.h:180
void cbor_decref(cbor_item_t **item_ref)
Decreases the reference count by one, deallocating the item if needed.
Definition: common.c:99
bool cbor_isa_string(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:33
size_t cbor_bytestring_chunk_count(const cbor_item_t *item)
Get the number of chunks this string consist of.
Definition: bytestrings.c:87
cbor_item_t ** cbor_string_chunks_handle(const cbor_item_t *item)
Get the handle to the array of chunks.
Definition: strings.c:67
bool cbor_is_int(const cbor_item_t *item)
Is the item an integer, either positive or negative?
Definition: common.c:65
bool cbor_isa_bytestring(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:28
cbor_item_t ** cbor_bytestring_chunks_handle(const cbor_item_t *item)
Get the handle to the array of chunks.
Definition: bytestrings.c:80
4 - arrays
Definition: data.h:29
size_t cbor_refcount(const cbor_item_t *item)
Get the reference count.
Definition: common.c:179
cbor_item_t * key
Definition: data.h:180
size_t end_ptr
Definition: data.h:104
cbor_type type
Major type discriminator.
Definition: data.h:155
7 - decimals and special values (true, false, nil, ...)
Definition: data.h:32
#define _CBOR_FREE
Definition: common.h:86
Simple pair of items for use in maps.
Definition: data.h:179
size_t refcount
Reference count - initialize to 0.
Definition: data.h:153
bool cbor_is_null(const cbor_item_t *item)
Does this item represent null
Definition: common.c:77
size_t cbor_string_chunk_count(const cbor_item_t *item)
Get the number of chunks this string consist of.
Definition: strings.c:74
size_t cbor_array_size(const cbor_item_t *item)
Get the number of members.
Definition: arrays.c:12
0 - positive integers
Definition: data.h:25
bool cbor_is_float(const cbor_item_t *item)
Is the item an a floating point number?
Definition: common.c:87
cbor_item_t * cbor_incref(cbor_item_t *item)
Increases the reference count by one.
Definition: common.c:93
struct _cbor_tag_metadata tag_metadata
Definition: data.h:144
uint8_t cbor_ctrl_value(const cbor_item_t *item)
Reads the control value.
Definition: floats_ctrls.c:18
Defines cbor_item_t::data structure for indefinite strings and bytestrings.
Definition: data.h:164
bool cbor_isa_float_ctrl(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:53
bool cbor_isa_array(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:38
struct cbor_item_t * tagged_item
Definition: data.h:115
bool cbor_isa_map(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:43
cbor_item_t * cbor_move(cbor_item_t *item)
Provides CPP-like move construct.
Definition: common.c:184
bool cbor_isa_uint(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:18
bool cbor_is_bool(const cbor_item_t *item)
Is the item an a boolean?
Definition: common.c:71
bool cbor_isa_tag(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:48
cbor_type
Specifies the Major type of cbor_item_t.
Definition: data.h:24
bool cbor_is_undef(const cbor_item_t *item)
Does this item represent undefined
Definition: common.c:82
cbor_type cbor_typeof(const cbor_item_t *item)
Get the type of the item.
Definition: common.c:59
bool cbor_string_is_definite(const cbor_item_t *item)
Is the string definite?
Definition: strings.c:124
unsigned char * data
Raw data block - interpretation depends on metadata.
Definition: data.h:157
bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item)
Is this a ctrl value?
Definition: floats_ctrls.c:25
The item handle.
Definition: data.h:149
1 - negative integers
Definition: data.h:26
struct cbor_pair * cbor_map_handle(const cbor_item_t *item)
Get the pairs storage.
Definition: maps.c:137
cbor_item_t ** cbor_array_handle(const cbor_item_t *item)
Get the array contents.
Definition: arrays.c:100
5 - maps
Definition: data.h:30