libcbor  0.5.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
bytestrings.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2017 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 <string.h>
9 #include "bytestrings.h"
10 #include "internal/memory_utils.h"
11 
13 {
14  assert(cbor_isa_bytestring(item));
15  return item->metadata.bytestring_metadata.length;
16 }
17 
18 unsigned char *cbor_bytestring_handle(const cbor_item_t *item)
19 {
20  assert(cbor_isa_bytestring(item));
21  return item->data;
22 }
23 
25 {
26  assert(cbor_isa_bytestring(item));
28 }
29 
31 {
32  return !cbor_bytestring_is_definite(item);
33 }
34 
36 {
37  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
38  *item = (cbor_item_t) {
39  .refcount = 1,
40  .type = CBOR_TYPE_BYTESTRING,
41  .metadata = {.bytestring_metadata = {_CBOR_METADATA_DEFINITE, 0}}
42  };
43  return item;
44 }
45 
47 {
48  cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
49  *item = (cbor_item_t) {
50  .refcount = 1,
51  .type = CBOR_TYPE_BYTESTRING,
52  .metadata = {.bytestring_metadata = {.type = _CBOR_METADATA_INDEFINITE, .length = 0}},
53  .data = _CBOR_MALLOC(sizeof(struct cbor_indefinite_string_data))
54  };
55  *((struct cbor_indefinite_string_data *) item->data) = (struct cbor_indefinite_string_data) {
56  .chunk_count = 0,
57  .chunk_capacity = 0,
58  .chunks = NULL,
59  };
60  return item;
61 }
62 
64 {
66  void * content = _CBOR_MALLOC(length);
67  memcpy(content, handle, length);
68  cbor_bytestring_set_handle(res, content, length);
69  return res;
70 }
71 
73 {
74  assert(cbor_isa_bytestring(item));
75  assert(cbor_bytestring_is_definite(item));
76  item->data = data;
77  item->metadata.bytestring_metadata.length = length;
78 }
79 
81 {
82  assert(cbor_isa_bytestring(item));
83  assert(cbor_bytestring_is_indefinite(item));
84  return ((struct cbor_indefinite_string_data *) item->data)->chunks;
85 }
86 
88 {
89  assert(cbor_isa_bytestring(item));
90  assert(cbor_bytestring_is_indefinite(item));
91  return ((struct cbor_indefinite_string_data *) item->data)->chunk_count;
92 
93 }
94 
96 {
97  assert(cbor_isa_bytestring(item));
98  assert(cbor_bytestring_is_indefinite(item));
99  struct cbor_indefinite_string_data *data = (struct cbor_indefinite_string_data *) item->data;
100  if (data->chunk_count == data->chunk_capacity) {
101  /* We need more space */
102  if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {
103  return false;
104  }
105 
106  data->chunk_capacity = data->chunk_capacity == 0 ? 1 : CBOR_BUFFER_GROWTH * (data->chunk_capacity);
107 
108  cbor_item_t **new_chunks_data = _cbor_realloc_multiple(data->chunks, sizeof(cbor_item_t *), data->chunk_capacity);
109 
110  if (new_chunks_data == NULL) {
111  return false;
112  }
113 
114  data->chunks = new_chunks_data;
115  }
116  data->chunks[data->chunk_count++] = cbor_incref(chunk);
117  return true;
118 }
struct cbor_item_t cbor_item_t
The item handle.
bool cbor_bytestring_is_definite(const cbor_item_t *item)
Is the byte string definite?
Definition: bytestrings.c:24
cbor_item_t ** chunks
Definition: data.h:167
union cbor_item_metadata metadata
Discriminated by type.
Definition: data.h:151
2 - byte strings
Definition: data.h:27
cbor_item_t * cbor_new_indefinite_bytestring()
Creates a new indefinite byte string.
Definition: bytestrings.c:46
void * _cbor_realloc_multiple(void *pointer, size_t item_size, size_t item_count)
Overflow-proof contiguous array reallocation.
Definition: memory_utils.c:39
size_t cbor_bytestring_chunk_count(const cbor_item_t *item)
Get the number of chunks this string consist of.
Definition: bytestrings.c:87
cbor_item_t * cbor_new_definite_bytestring()
Creates a new definite byte string.
Definition: bytestrings.c:35
struct _cbor_bytestring_metadata bytestring_metadata
Definition: data.h:140
bool cbor_isa_bytestring(const cbor_item_t *item)
Does the item have the appropriate major type?
Definition: common.c:28
_cbor_dst_metadata type
Definition: data.h:84
#define CBOR_RESTRICT_POINTER
Definition: common.h:33
cbor_item_t ** cbor_bytestring_chunks_handle(const cbor_item_t *item)
Get the handle to the array of chunks.
Definition: bytestrings.c:80
bool cbor_bytestring_is_indefinite(const cbor_item_t *item)
Is the byte string indefinite?
Definition: bytestrings.c:30
#define _CBOR_MALLOC
Definition: common.h:84
size_t refcount
Reference count - initialize to 0.
Definition: data.h:153
unsigned char * cbor_mutable_data
Definition: data.h:21
bool cbor_bytestring_add_chunk(cbor_item_t *item, cbor_item_t *chunk)
Appends a chunk to the bytestring.
Definition: bytestrings.c:95
cbor_item_t * cbor_incref(cbor_item_t *item)
Increases the reference count by one.
Definition: common.c:93
Defines cbor_item_t::data structure for indefinite strings and bytestrings.
Definition: data.h:164
const unsigned char * cbor_data
Definition: data.h:20
void cbor_bytestring_set_handle(cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data, size_t length)
Set the handle to the binary data.
Definition: bytestrings.c:72
unsigned char * cbor_bytestring_handle(const cbor_item_t *item)
Get the handle to the binary data.
Definition: bytestrings.c:18
unsigned char * data
Raw data block - interpretation depends on metadata.
Definition: data.h:157
bool _cbor_safe_to_multiply(size_t a, size_t b)
Can a and b be multiplied without overflowing size_t?
Definition: memory_utils.c:25
The item handle.
Definition: data.h:149
cbor_item_t * cbor_build_bytestring(cbor_data handle, size_t length)
Creates a new byte string and initializes it.
Definition: bytestrings.c:63
size_t cbor_bytestring_length(const cbor_item_t *item)
Returns the length of the binary data.
Definition: bytestrings.c:12