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
|
/*
* Copyright (c) 2014-2020 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 "strings.h"
#include <string.h>
#include "internal/memory_utils.h"
cbor_item_t *cbor_new_definite_string() {
cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
_CBOR_NOTNULL(item);
*item = (cbor_item_t){
.refcount = 1,
.type = CBOR_TYPE_STRING,
.metadata = {.string_metadata = {_CBOR_METADATA_DEFINITE, 0}}};
return item;
}
cbor_item_t *cbor_new_indefinite_string() {
cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
_CBOR_NOTNULL(item);
*item = (cbor_item_t){
.refcount = 1,
.type = CBOR_TYPE_STRING,
.metadata = {.string_metadata = {.type = _CBOR_METADATA_INDEFINITE,
.length = 0}},
.data = _CBOR_MALLOC(sizeof(struct cbor_indefinite_string_data))};
_CBOR_DEPENDENT_NOTNULL(item, item->data);
*((struct cbor_indefinite_string_data *)item->data) =
(struct cbor_indefinite_string_data){
.chunk_count = 0,
.chunk_capacity = 0,
.chunks = NULL,
};
return item;
}
cbor_item_t *cbor_build_string(const char *val) {
cbor_item_t *item = cbor_new_definite_string();
_CBOR_NOTNULL(item);
size_t len = strlen(val);
unsigned char *handle = _CBOR_MALLOC(len);
_CBOR_DEPENDENT_NOTNULL(item, handle);
memcpy(handle, val, len);
cbor_string_set_handle(item, handle, len);
return item;
}
cbor_item_t *cbor_build_stringn(const char *val, size_t length) {
cbor_item_t *item = cbor_new_definite_string();
_CBOR_NOTNULL(item);
unsigned char *handle = _CBOR_MALLOC(length);
_CBOR_DEPENDENT_NOTNULL(item, handle);
memcpy(handle, val, length);
cbor_string_set_handle(item, handle, length);
return item;
}
void cbor_string_set_handle(cbor_item_t *item,
cbor_mutable_data CBOR_RESTRICT_POINTER data,
size_t length) {
assert(cbor_isa_string(item));
assert(cbor_string_is_definite(item));
item->data = data;
item->metadata.string_metadata.length = length;
}
cbor_item_t **cbor_string_chunks_handle(const cbor_item_t *item) {
assert(cbor_isa_string(item));
assert(cbor_string_is_indefinite(item));
return ((struct cbor_indefinite_string_data *)item->data)->chunks;
}
size_t cbor_string_chunk_count(const cbor_item_t *item) {
assert(cbor_isa_string(item));
assert(cbor_string_is_indefinite(item));
return ((struct cbor_indefinite_string_data *)item->data)->chunk_count;
}
bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk) {
assert(cbor_isa_string(item));
assert(cbor_string_is_indefinite(item));
struct cbor_indefinite_string_data *data =
(struct cbor_indefinite_string_data *)item->data;
if (data->chunk_count == data->chunk_capacity) {
// TODO: Add a test for this
if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {
return false;
}
size_t new_chunk_capacity =
data->chunk_capacity == 0 ? 1
: CBOR_BUFFER_GROWTH * (data->chunk_capacity);
cbor_item_t **new_chunks_data = _cbor_realloc_multiple(
data->chunks, sizeof(cbor_item_t *), new_chunk_capacity);
if (new_chunks_data == NULL) {
return false;
}
data->chunk_capacity = new_chunk_capacity;
data->chunks = new_chunks_data;
}
data->chunks[data->chunk_count++] = cbor_incref(chunk);
return true;
}
size_t cbor_string_length(const cbor_item_t *item) {
assert(cbor_isa_string(item));
return item->metadata.string_metadata.length;
}
unsigned char *cbor_string_handle(const cbor_item_t *item) {
assert(cbor_isa_string(item));
return item->data;
}
size_t cbor_string_codepoint_count(const cbor_item_t *item) {
assert(cbor_isa_string(item));
return item->metadata.string_metadata.codepoint_count;
}
bool cbor_string_is_definite(const cbor_item_t *item) {
assert(cbor_isa_string(item));
return item->metadata.string_metadata.type == _CBOR_METADATA_DEFINITE;
}
bool cbor_string_is_indefinite(const cbor_item_t *item) {
return !cbor_string_is_definite(item);
}
|