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