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
strings.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 "strings.h"
9#include <string.h>
11#include "internal/unicode.h"
12
14 cbor_item_t* item = _cbor_malloc(sizeof(cbor_item_t));
15 _CBOR_NOTNULL(item);
16 *item = (cbor_item_t){
17 .refcount = 1,
18 .type = CBOR_TYPE_STRING,
19 .metadata = {.string_metadata = {.type = _CBOR_METADATA_DEFINITE,
20 .codepoint_count = 0,
21 .length = 0}}};
22 return item;
23}
24
26 cbor_item_t* item = _cbor_malloc(sizeof(cbor_item_t));
27 _CBOR_NOTNULL(item);
28 *item = (cbor_item_t){
29 .refcount = 1,
30 .type = CBOR_TYPE_STRING,
31 .metadata = {.string_metadata = {.type = _CBOR_METADATA_INDEFINITE,
32 .length = 0}},
33 .data = _cbor_malloc(sizeof(struct cbor_indefinite_string_data))};
34 _CBOR_DEPENDENT_NOTNULL(item, item->data);
35 *((struct cbor_indefinite_string_data*)item->data) =
37 .chunk_count = 0,
38 .chunk_capacity = 0,
39 .chunks = NULL,
40 };
41 return item;
42}
43
44cbor_item_t* cbor_build_string(const char* val) {
46 _CBOR_NOTNULL(item);
47 size_t len = strlen(val);
48 unsigned char* handle = _cbor_malloc(len);
49 _CBOR_DEPENDENT_NOTNULL(item, handle);
50 memcpy(handle, val, len);
51 cbor_string_set_handle(item, handle, len);
52 return item;
53}
54
55cbor_item_t* cbor_build_stringn(const char* val, size_t length) {
57 _CBOR_NOTNULL(item);
58 unsigned char* handle = _cbor_malloc(length);
59 _CBOR_DEPENDENT_NOTNULL(item, handle);
60 memcpy(handle, val, length);
61 cbor_string_set_handle(item, handle, length);
62 return item;
63}
64
67 size_t length) {
70 item->data = data;
71 item->metadata.string_metadata.length = length;
72 struct _cbor_unicode_status unicode_status;
73 size_t codepoint_count =
74 _cbor_unicode_codepoint_count(data, length, &unicode_status);
75 CBOR_ASSERT(codepoint_count <= length);
76 if (unicode_status.status == _CBOR_UNICODE_OK) {
77 item->metadata.string_metadata.codepoint_count = codepoint_count;
78 } else {
80 }
81}
82
88
92 return ((struct cbor_indefinite_string_data*)item->data)->chunk_count;
93}
94
98 struct cbor_indefinite_string_data* data =
99 (struct cbor_indefinite_string_data*)item->data;
100 if (data->chunk_count == data->chunk_capacity) {
101 if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {
102 return false;
103 }
104
105 size_t new_chunk_capacity =
106 data->chunk_capacity == 0 ? 1
107 : CBOR_BUFFER_GROWTH * (data->chunk_capacity);
108 cbor_item_t** new_chunks_data = _cbor_realloc_multiple(
109 data->chunks, sizeof(cbor_item_t*), new_chunk_capacity);
110
111 if (new_chunks_data == NULL) {
112 return false;
113 }
114
115 data->chunk_capacity = new_chunk_capacity;
116 data->chunks = new_chunks_data;
117 }
118 data->chunks[data->chunk_count++] = cbor_incref(chunk);
119 return true;
120}
121
122size_t cbor_string_length(const cbor_item_t* item) {
124 return item->metadata.string_metadata.length;
125}
126
127unsigned char* cbor_string_handle(const cbor_item_t* item) {
129 return item->data;
130}
131
136
141
143 return !cbor_string_is_definite(item);
144}
_cbor_malloc_t _cbor_malloc
Definition allocators.c:10
bool cbor_isa_string(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:34
cbor_item_t * cbor_incref(cbor_item_t *item)
Increases the item's reference count by one.
Definition common.c:78
#define _CBOR_DEPENDENT_NOTNULL(cbor_item, pointer)
Definition common.h:119
#define CBOR_RESTRICT_POINTER
Definition common.h:36
#define _CBOR_NOTNULL(cbor_item)
Definition common.h:111
#define CBOR_ASSERT(e)
Definition common.h:73
@ CBOR_TYPE_STRING
3 - strings
Definition data.h:31
@ _CBOR_METADATA_DEFINITE
Definition data.h:78
@ _CBOR_METADATA_INDEFINITE
Definition data.h:79
unsigned char * cbor_mutable_data
Definition data.h:21
void * _cbor_realloc_multiple(void *pointer, size_t item_size, size_t item_count)
Overflow-proof contiguous array reallocation.
bool _cbor_safe_to_multiply(size_t a, size_t b)
Can a and b be multiplied without overflowing size_t?
cbor_item_t * cbor_new_definite_string(void)
Creates a new definite string.
Definition strings.c:13
void cbor_string_set_handle(cbor_item_t *item, cbor_mutable_data data, size_t length)
Set the handle to the underlying string.
Definition strings.c:65
cbor_item_t * cbor_build_string(const char *val)
Creates a new string and initializes it.
Definition strings.c:44
bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk)
Appends a chunk to the string.
Definition strings.c:95
cbor_item_t * cbor_new_indefinite_string(void)
Creates a new indefinite string.
Definition strings.c:25
size_t cbor_string_codepoint_count(const cbor_item_t *item)
The number of codepoints in this string.
Definition strings.c:132
cbor_item_t * cbor_build_stringn(const char *val, size_t length)
Creates a new string and initializes it.
Definition strings.c:55
size_t cbor_string_length(const cbor_item_t *item)
Returns the length of the underlying string in bytes.
Definition strings.c:122
bool cbor_string_is_indefinite(const cbor_item_t *item)
Is the string indefinite?
Definition strings.c:142
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
unsigned char * cbor_string_handle(const cbor_item_t *item)
Get the handle to the underlying string.
Definition strings.c:127
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 codepoint_count
Definition data.h:110
_cbor_dst_metadata type
Definition data.h:112
Signals unicode validation error and possibly its location.
Definition unicode.h:20
enum _cbor_unicode_status_error status
Definition unicode.h:21
Defines cbor_item_t::data structure for indefinite strings and bytestrings.
Definition data.h:186
cbor_item_t ** chunks
Definition data.h:189
The item handle.
Definition data.h:171
unsigned char * data
Raw data block - interpretation depends on metadata.
Definition data.h:179
union cbor_item_metadata metadata
Discriminated by type.
Definition data.h:173
size_t _cbor_unicode_codepoint_count(cbor_data source, size_t source_length, struct _cbor_unicode_status *status)
Definition unicode.c:69
@ _CBOR_UNICODE_OK
Definition unicode.h:17
struct _cbor_string_metadata string_metadata
Definition data.h:163