summaryrefslogtreecommitdiff
path: root/lib/libcbor/src/cbor/common.c
blob: 77544ae810749f253d44ba29a9f4169ec7cc27be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (c) 2014-2017 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include "cbor/common.h"
#include "arrays.h"
#include "bytestrings.h"
#include "data.h"
#include "floats_ctrls.h"
#include "ints.h"
#include "maps.h"
#include "strings.h"
#include "tags.h"

bool cbor_isa_uint(const cbor_item_t *item)
{
	return item->type == CBOR_TYPE_UINT;
}

bool cbor_isa_negint(const cbor_item_t *item)
{
	return item->type == CBOR_TYPE_NEGINT;
}

bool cbor_isa_bytestring(const cbor_item_t *item)
{
	return item->type == CBOR_TYPE_BYTESTRING;
}

bool cbor_isa_string(const cbor_item_t *item)
{
	return item->type == CBOR_TYPE_STRING;
}

bool cbor_isa_array(const cbor_item_t *item)
{
	return item->type == CBOR_TYPE_ARRAY;
}

bool cbor_isa_map(const cbor_item_t *item)
{
	return item->type == CBOR_TYPE_MAP;
}

bool cbor_isa_tag(const cbor_item_t *item)
{
	return item->type == CBOR_TYPE_TAG;
}

bool cbor_isa_float_ctrl(const cbor_item_t *item)
{
	return item->type == CBOR_TYPE_FLOAT_CTRL;
}


cbor_type cbor_typeof(const cbor_item_t *item)
{
	return item->type;
}


bool cbor_is_int(const cbor_item_t *item)
{
	return cbor_isa_uint(item) || cbor_isa_negint(item);
}


bool cbor_is_bool(const cbor_item_t *item)
{
	return cbor_isa_float_ctrl(item) &&
		   (cbor_ctrl_value(item) == CBOR_CTRL_FALSE || cbor_ctrl_value(item) == CBOR_CTRL_TRUE);
}

bool cbor_is_null(const cbor_item_t *item)
{
	return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_NULL;
}

bool cbor_is_undef(const cbor_item_t *item)
{
	return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_UNDEF;
}

bool cbor_is_float(const cbor_item_t *item)
{
	return cbor_isa_float_ctrl(item) && !cbor_float_ctrl_is_ctrl(item);
}


cbor_item_t * cbor_incref(cbor_item_t *item)
{
	item->refcount++;
	return item;
}

void cbor_decref(cbor_item_t **item_ref)
{
	cbor_item_t * item = *item_ref;
	if (--item->refcount == 0) {
		switch (item->type) {
		case CBOR_TYPE_UINT:
			/* Fallthrough */
		case CBOR_TYPE_NEGINT:
			/* Combined allocation, freeing the item suffices */
		{
			break;
		}
		case CBOR_TYPE_BYTESTRING: {
			if (cbor_bytestring_is_definite(item)) {
				_CBOR_FREE(item->data);
			} else {
				/* We need to decref all chunks */
				cbor_item_t **handle = cbor_bytestring_chunks_handle(item);
				for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++)
					cbor_decref(&handle[i]);
				_CBOR_FREE(((struct cbor_indefinite_string_data *) item->data)->chunks);
				_CBOR_FREE(item->data);
			}
			break;
		}
		case CBOR_TYPE_STRING: {
			if (cbor_string_is_definite(item)) {
				_CBOR_FREE(item->data);
			} else {
				/* We need to decref all chunks */
				cbor_item_t **handle = cbor_string_chunks_handle(item);
				for (size_t i = 0; i < cbor_string_chunk_count(item); i++)
					cbor_decref(&handle[i]);
				_CBOR_FREE(((struct cbor_indefinite_string_data *) item->data)->chunks);
				_CBOR_FREE(item->data);
			}
			break;
		}
		case CBOR_TYPE_ARRAY: {
			/* Get all items and decref them */
			cbor_item_t **handle = cbor_array_handle(item);
			size_t size = cbor_array_size(item);
			for (size_t i = 0; i < size; i++)
				if (handle[i] != NULL)
					cbor_decref(&handle[i]);
			_CBOR_FREE(item->data);
			break;
		}
		case CBOR_TYPE_MAP: {
			struct cbor_pair *handle = cbor_map_handle(item);
			for (size_t i = 0; i < item->metadata.map_metadata.end_ptr; i++, handle++) {
				cbor_decref(&handle->key);
				if (handle->value != NULL)
					cbor_decref(&handle->value);
			}
			_CBOR_FREE(item->data);
			break;
		};
		case CBOR_TYPE_TAG: {
			if (item->metadata.tag_metadata.tagged_item != NULL)
				cbor_decref(&item->metadata.tag_metadata.tagged_item);
			_CBOR_FREE(item->data);
			break;
		}
		case CBOR_TYPE_FLOAT_CTRL: {
			/* Floats have combined allocation */
			break;
		}
		}
		_CBOR_FREE(item);
		//TODO
		*item_ref = NULL;
	}
}

void cbor_intermediate_decref(cbor_item_t * item)
{
	cbor_decref(&item);
}

size_t cbor_refcount(const cbor_item_t * item)
{
	return item->refcount;
}

cbor_item_t * cbor_move(cbor_item_t * item)
{
	item->refcount--;
	return item;
}