xref: /openbsd-src/lib/libcbor/src/cbor/bytestrings.h (revision 4dcc46c4d04180142eda526ce521dfb137776d05)
1da0d961cSdjm /*
2d3425be1Sdjm  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3da0d961cSdjm  *
4da0d961cSdjm  * libcbor is free software; you can redistribute it and/or modify
5da0d961cSdjm  * it under the terms of the MIT license. See LICENSE for details.
6da0d961cSdjm  */
7da0d961cSdjm 
8da0d961cSdjm #ifndef LIBCBOR_BYTESTRINGS_H
9da0d961cSdjm #define LIBCBOR_BYTESTRINGS_H
10da0d961cSdjm 
11*4dcc46c4Sdjm #include "cbor/cbor_export.h"
12da0d961cSdjm #include "cbor/common.h"
13da0d961cSdjm 
14da0d961cSdjm #ifdef __cplusplus
15da0d961cSdjm extern "C" {
16da0d961cSdjm #endif
17da0d961cSdjm 
18da0d961cSdjm /*
19da0d961cSdjm  * ============================================================================
20da0d961cSdjm  * Byte string manipulation
21da0d961cSdjm  * ============================================================================
22da0d961cSdjm  */
23da0d961cSdjm 
24da0d961cSdjm /** Returns the length of the binary data
25da0d961cSdjm  *
26da0d961cSdjm  * For definite byte strings only
27da0d961cSdjm  *
28da0d961cSdjm  * @param item[borrow] a definite bytestring
29da0d961cSdjm  * @return length of the binary data. Zero if no chunk has been attached yet
30da0d961cSdjm  */
31*4dcc46c4Sdjm _CBOR_NODISCARD
32*4dcc46c4Sdjm CBOR_EXPORT size_t cbor_bytestring_length(const cbor_item_t *item);
33da0d961cSdjm 
34da0d961cSdjm /** Is the byte string definite?
35da0d961cSdjm  *
36da0d961cSdjm  * @param item[borrow] a byte string
37da0d961cSdjm  * @return Is the byte string definite?
38da0d961cSdjm  */
39*4dcc46c4Sdjm _CBOR_NODISCARD
40*4dcc46c4Sdjm CBOR_EXPORT bool cbor_bytestring_is_definite(const cbor_item_t *item);
41da0d961cSdjm 
42da0d961cSdjm /** Is the byte string indefinite?
43da0d961cSdjm  *
44da0d961cSdjm  * @param item[borrow] a byte string
45da0d961cSdjm  * @return Is the byte string indefinite?
46da0d961cSdjm  */
47*4dcc46c4Sdjm _CBOR_NODISCARD
48*4dcc46c4Sdjm CBOR_EXPORT bool cbor_bytestring_is_indefinite(const cbor_item_t *item);
49da0d961cSdjm 
50da0d961cSdjm /** Get the handle to the binary data
51da0d961cSdjm  *
529e5c2ddcSdjm  * Definite items only. Modifying the data is allowed. In that case, the caller
539e5c2ddcSdjm  * takes responsibility for the effect on items this item might be a part of
54da0d961cSdjm  *
55da0d961cSdjm  * @param item[borrow] A definite byte string
569e5c2ddcSdjm  * @return The address of the binary data. `NULL` if no data have been assigned
579e5c2ddcSdjm  * yet.
58da0d961cSdjm  */
59*4dcc46c4Sdjm _CBOR_NODISCARD
60*4dcc46c4Sdjm CBOR_EXPORT cbor_mutable_data cbor_bytestring_handle(const cbor_item_t *item);
61da0d961cSdjm 
62da0d961cSdjm /** Set the handle to the binary data
63da0d961cSdjm  *
64da0d961cSdjm  * @param item[borrow] A definite byte string
659e5c2ddcSdjm  * @param data The memory block. The caller gives up the ownership of the block.
669e5c2ddcSdjm  * libcbor will deallocate it when appropriate using its free function
67da0d961cSdjm  * @param length Length of the data block
68da0d961cSdjm  */
69*4dcc46c4Sdjm CBOR_EXPORT void cbor_bytestring_set_handle(
70*4dcc46c4Sdjm     cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data,
719e5c2ddcSdjm     size_t length);
72da0d961cSdjm 
73da0d961cSdjm /** Get the handle to the array of chunks
74da0d961cSdjm  *
759e5c2ddcSdjm  * Manipulations with the memory block (e.g. sorting it) are allowed, but the
769e5c2ddcSdjm  * validity and the number of chunks must be retained.
77da0d961cSdjm  *
78da0d961cSdjm  * @param item[borrow] A indefinite byte string
79da0d961cSdjm  * @return array of #cbor_bytestring_chunk_count definite bytestrings
80da0d961cSdjm  */
81*4dcc46c4Sdjm _CBOR_NODISCARD
82*4dcc46c4Sdjm CBOR_EXPORT cbor_item_t **cbor_bytestring_chunks_handle(
83*4dcc46c4Sdjm     const cbor_item_t *item);
84da0d961cSdjm 
85da0d961cSdjm /** Get the number of chunks this string consist of
86da0d961cSdjm  *
87da0d961cSdjm  * @param item[borrow] A indefinite bytestring
88da0d961cSdjm  * @return The chunk count. 0 for freshly created items.
89da0d961cSdjm  */
90*4dcc46c4Sdjm _CBOR_NODISCARD
91*4dcc46c4Sdjm CBOR_EXPORT size_t cbor_bytestring_chunk_count(const cbor_item_t *item);
92da0d961cSdjm 
93da0d961cSdjm /** Appends a chunk to the bytestring
94da0d961cSdjm  *
95da0d961cSdjm  * Indefinite byte strings only.
96da0d961cSdjm  *
97da0d961cSdjm  * May realloc the chunk storage.
98da0d961cSdjm  *
99da0d961cSdjm  * @param item[borrow] An indefinite byte string
100da0d961cSdjm  * @param item[incref] A definite byte string
1019e5c2ddcSdjm  * @return true on success, false on realloc failure. In that case, the refcount
1029e5c2ddcSdjm  * of `chunk` is not increased and the `item` is left intact.
103da0d961cSdjm  */
104*4dcc46c4Sdjm _CBOR_NODISCARD
105*4dcc46c4Sdjm CBOR_EXPORT bool cbor_bytestring_add_chunk(cbor_item_t *item,
106*4dcc46c4Sdjm                                            cbor_item_t *chunk);
107da0d961cSdjm 
108da0d961cSdjm /** Creates a new definite byte string
109da0d961cSdjm  *
110da0d961cSdjm  * The handle is initialized to `NULL` and length to 0
111da0d961cSdjm  *
112da0d961cSdjm  * @return **new** definite bytestring. `NULL` on malloc failure.
113da0d961cSdjm  */
114*4dcc46c4Sdjm _CBOR_NODISCARD
115*4dcc46c4Sdjm CBOR_EXPORT cbor_item_t *cbor_new_definite_bytestring(void);
116da0d961cSdjm 
117da0d961cSdjm /** Creates a new indefinite byte string
118da0d961cSdjm  *
119da0d961cSdjm  * The chunks array is initialized to `NULL` and chunk count to 0
120da0d961cSdjm  *
121da0d961cSdjm  * @return **new** indefinite bytestring. `NULL` on malloc failure.
122da0d961cSdjm  */
123*4dcc46c4Sdjm _CBOR_NODISCARD
124*4dcc46c4Sdjm CBOR_EXPORT cbor_item_t *cbor_new_indefinite_bytestring(void);
125da0d961cSdjm 
126da0d961cSdjm /** Creates a new byte string and initializes it
127da0d961cSdjm  *
128da0d961cSdjm  * The `handle` will be copied to a newly allocated block
129da0d961cSdjm  *
130da0d961cSdjm  * @param handle Block of binary data
131da0d961cSdjm  * @param length Length of `data`
1329e5c2ddcSdjm  * @return A **new** byte string with content `handle`. `NULL` on malloc
1339e5c2ddcSdjm  * failure.
134da0d961cSdjm  */
135*4dcc46c4Sdjm _CBOR_NODISCARD
136*4dcc46c4Sdjm CBOR_EXPORT cbor_item_t *cbor_build_bytestring(cbor_data handle, size_t length);
137da0d961cSdjm 
138da0d961cSdjm #ifdef __cplusplus
139da0d961cSdjm }
140da0d961cSdjm #endif
141da0d961cSdjm 
142da0d961cSdjm #endif  // LIBCBOR_BYTESTRINGS_H
143