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
bytestrings.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 "bytestrings.h"
9#include <string.h>
11
16
17unsigned char* cbor_bytestring_handle(const cbor_item_t* item) {
19 return item->data;
20}
21
26
30
32 cbor_item_t* item = _cbor_malloc(sizeof(cbor_item_t));
33 _CBOR_NOTNULL(item);
34 *item = (cbor_item_t){
35 .refcount = 1,
37 .metadata = {.bytestring_metadata = {.type = _CBOR_METADATA_DEFINITE,
38 .length = 0}}};
39 return item;
40}
41
43 cbor_item_t* item = _cbor_malloc(sizeof(cbor_item_t));
44 _CBOR_NOTNULL(item);
45 *item = (cbor_item_t){
46 .refcount = 1,
48 .metadata = {.bytestring_metadata = {.type = _CBOR_METADATA_INDEFINITE,
49 .length = 0}},
50 .data = _cbor_malloc(sizeof(struct cbor_indefinite_string_data))};
51 _CBOR_DEPENDENT_NOTNULL(item, item->data);
52 *((struct cbor_indefinite_string_data*)item->data) =
54 .chunk_count = 0,
55 .chunk_capacity = 0,
56 .chunks = NULL,
57 };
58 return item;
59}
60
63 _CBOR_NOTNULL(item);
64 void* content = _cbor_malloc(length);
65 _CBOR_DEPENDENT_NOTNULL(item, content);
66 memcpy(content, handle, length);
67 cbor_bytestring_set_handle(item, content, length);
68 return item;
69}
70
79
85
89 return ((struct cbor_indefinite_string_data*)item->data)->chunk_count;
90}
91
97 struct cbor_indefinite_string_data* data =
98 (struct cbor_indefinite_string_data*)item->data;
99 if (data->chunk_count == data->chunk_capacity) {
100 if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {
101 return false;
102 }
103
104 size_t new_chunk_capacity =
105 data->chunk_capacity == 0 ? 1
106 : CBOR_BUFFER_GROWTH * (data->chunk_capacity);
107
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 data->chunk_capacity = new_chunk_capacity;
115 data->chunks = new_chunks_data;
116 }
117 data->chunks[data->chunk_count++] = cbor_incref(chunk);
118 return true;
119}
_cbor_malloc_t _cbor_malloc
Definition allocators.c:10
bool cbor_bytestring_is_definite(const cbor_item_t *item)
Is the byte string definite?
Definition bytestrings.c:22
bool cbor_bytestring_is_indefinite(const cbor_item_t *item)
Is the byte string indefinite?
Definition bytestrings.c:27
cbor_item_t * cbor_new_definite_bytestring(void)
Creates a new definite byte string.
Definition bytestrings.c:31
bool cbor_bytestring_add_chunk(cbor_item_t *item, cbor_item_t *chunk)
Appends a chunk to the bytestring.
Definition bytestrings.c:92
size_t cbor_bytestring_length(const cbor_item_t *item)
Returns the length of the binary data.
Definition bytestrings.c:12
cbor_item_t ** cbor_bytestring_chunks_handle(const cbor_item_t *item)
Get the handle to the array of chunks.
Definition bytestrings.c:80
cbor_item_t * cbor_new_indefinite_bytestring(void)
Creates a new indefinite byte string.
Definition bytestrings.c:42
cbor_item_t * cbor_build_bytestring(cbor_data handle, size_t length)
Creates a new byte string and initializes it.
Definition bytestrings.c:61
size_t cbor_bytestring_chunk_count(const cbor_item_t *item)
Get the number of chunks this string consist of.
Definition bytestrings.c:86
unsigned char * cbor_bytestring_handle(const cbor_item_t *item)
Get the handle to the binary data.
Definition bytestrings.c:17
void cbor_bytestring_set_handle(cbor_item_t *item, cbor_mutable_data data, size_t length)
Set the handle to the binary data.
Definition bytestrings.c:71
cbor_item_t * cbor_incref(cbor_item_t *item)
Increases the item's reference count by one.
Definition common.c:78
bool cbor_isa_bytestring(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition common.c:30
#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
const unsigned char * cbor_data
Definition data.h:20
@ CBOR_TYPE_BYTESTRING
2 - byte strings
Definition data.h:29
@ _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_dst_metadata type
Definition data.h:104
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
struct _cbor_bytestring_metadata bytestring_metadata
Definition data.h:162