110ff414cSEd Maste /* 210ff414cSEd Maste * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com> 310ff414cSEd Maste * 410ff414cSEd Maste * libcbor is free software; you can redistribute it and/or modify 510ff414cSEd Maste * it under the terms of the MIT license. See LICENSE for details. 610ff414cSEd Maste */ 710ff414cSEd Maste 810ff414cSEd Maste #ifndef LIBCBOR_BYTESTRINGS_H 910ff414cSEd Maste #define LIBCBOR_BYTESTRINGS_H 1010ff414cSEd Maste 1110ff414cSEd Maste #include "cbor/cbor_export.h" 1210ff414cSEd Maste #include "cbor/common.h" 1310ff414cSEd Maste 1410ff414cSEd Maste #ifdef __cplusplus 1510ff414cSEd Maste extern "C" { 1610ff414cSEd Maste #endif 1710ff414cSEd Maste 1810ff414cSEd Maste /* 1910ff414cSEd Maste * ============================================================================ 2010ff414cSEd Maste * Byte string manipulation 2110ff414cSEd Maste * ============================================================================ 2210ff414cSEd Maste */ 2310ff414cSEd Maste 2410ff414cSEd Maste /** Returns the length of the binary data 2510ff414cSEd Maste * 2610ff414cSEd Maste * For definite byte strings only 2710ff414cSEd Maste * 28*5d3e7166SEd Maste * @param item a definite bytestring 2910ff414cSEd Maste * @return length of the binary data. Zero if no chunk has been attached yet 3010ff414cSEd Maste */ 31*5d3e7166SEd Maste _CBOR_NODISCARD 3210ff414cSEd Maste CBOR_EXPORT size_t cbor_bytestring_length(const cbor_item_t *item); 3310ff414cSEd Maste 3410ff414cSEd Maste /** Is the byte string definite? 3510ff414cSEd Maste * 36*5d3e7166SEd Maste * @param item a byte string 3710ff414cSEd Maste * @return Is the byte string definite? 3810ff414cSEd Maste */ 39*5d3e7166SEd Maste _CBOR_NODISCARD 4010ff414cSEd Maste CBOR_EXPORT bool cbor_bytestring_is_definite(const cbor_item_t *item); 4110ff414cSEd Maste 4210ff414cSEd Maste /** Is the byte string indefinite? 4310ff414cSEd Maste * 44*5d3e7166SEd Maste * @param item a byte string 4510ff414cSEd Maste * @return Is the byte string indefinite? 4610ff414cSEd Maste */ 47*5d3e7166SEd Maste _CBOR_NODISCARD 4810ff414cSEd Maste CBOR_EXPORT bool cbor_bytestring_is_indefinite(const cbor_item_t *item); 4910ff414cSEd Maste 5010ff414cSEd Maste /** Get the handle to the binary data 5110ff414cSEd Maste * 5210ff414cSEd Maste * Definite items only. Modifying the data is allowed. In that case, the caller 5310ff414cSEd Maste * takes responsibility for the effect on items this item might be a part of 5410ff414cSEd Maste * 55*5d3e7166SEd Maste * @param item A definite byte string 56*5d3e7166SEd Maste * @return The address of the underlying binary data 57*5d3e7166SEd Maste * @return `NULL` if no data have been assigned 5810ff414cSEd Maste * yet. 5910ff414cSEd Maste */ 60*5d3e7166SEd Maste _CBOR_NODISCARD 6110ff414cSEd Maste CBOR_EXPORT cbor_mutable_data cbor_bytestring_handle(const cbor_item_t *item); 6210ff414cSEd Maste 6310ff414cSEd Maste /** Set the handle to the binary data 6410ff414cSEd Maste * 65*5d3e7166SEd Maste * @param item A definite byte string 6610ff414cSEd Maste * @param data The memory block. The caller gives up the ownership of the block. 67*5d3e7166SEd Maste * libcbor will deallocate it when appropriate using the `free` implementation 68*5d3e7166SEd Maste * configured using #cbor_set_allocs 6910ff414cSEd Maste * @param length Length of the data block 7010ff414cSEd Maste */ 7110ff414cSEd Maste CBOR_EXPORT void cbor_bytestring_set_handle( 7210ff414cSEd Maste cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data, 7310ff414cSEd Maste size_t length); 7410ff414cSEd Maste 7510ff414cSEd Maste /** Get the handle to the array of chunks 7610ff414cSEd Maste * 7710ff414cSEd Maste * Manipulations with the memory block (e.g. sorting it) are allowed, but the 7810ff414cSEd Maste * validity and the number of chunks must be retained. 7910ff414cSEd Maste * 80*5d3e7166SEd Maste * @param item A indefinite byte string 8110ff414cSEd Maste * @return array of #cbor_bytestring_chunk_count definite bytestrings 8210ff414cSEd Maste */ 83*5d3e7166SEd Maste _CBOR_NODISCARD 8410ff414cSEd Maste CBOR_EXPORT cbor_item_t **cbor_bytestring_chunks_handle( 8510ff414cSEd Maste const cbor_item_t *item); 8610ff414cSEd Maste 8710ff414cSEd Maste /** Get the number of chunks this string consist of 8810ff414cSEd Maste * 89*5d3e7166SEd Maste * @param item A indefinite bytestring 9010ff414cSEd Maste * @return The chunk count. 0 for freshly created items. 9110ff414cSEd Maste */ 92*5d3e7166SEd Maste _CBOR_NODISCARD 9310ff414cSEd Maste CBOR_EXPORT size_t cbor_bytestring_chunk_count(const cbor_item_t *item); 9410ff414cSEd Maste 9510ff414cSEd Maste /** Appends a chunk to the bytestring 9610ff414cSEd Maste * 9710ff414cSEd Maste * Indefinite byte strings only. 9810ff414cSEd Maste * 9910ff414cSEd Maste * May realloc the chunk storage. 10010ff414cSEd Maste * 101*5d3e7166SEd Maste * @param item An indefinite byte string 102*5d3e7166SEd Maste * @param chunk A definite byte string. Its reference count will be be increased 103*5d3e7166SEd Maste * by one. 10410ff414cSEd Maste * @return true on success, false on realloc failure. In that case, the refcount 10510ff414cSEd Maste * of `chunk` is not increased and the `item` is left intact. 10610ff414cSEd Maste */ 107*5d3e7166SEd Maste _CBOR_NODISCARD 10810ff414cSEd Maste CBOR_EXPORT bool cbor_bytestring_add_chunk(cbor_item_t *item, 10910ff414cSEd Maste cbor_item_t *chunk); 11010ff414cSEd Maste 11110ff414cSEd Maste /** Creates a new definite byte string 11210ff414cSEd Maste * 11310ff414cSEd Maste * The handle is initialized to `NULL` and length to 0 11410ff414cSEd Maste * 115*5d3e7166SEd Maste * @return Reference to the new bytestring item. The item's reference count is 116*5d3e7166SEd Maste * initialized to one. 117*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 11810ff414cSEd Maste */ 119*5d3e7166SEd Maste _CBOR_NODISCARD 120*5d3e7166SEd Maste CBOR_EXPORT cbor_item_t *cbor_new_definite_bytestring(void); 12110ff414cSEd Maste 12210ff414cSEd Maste /** Creates a new indefinite byte string 12310ff414cSEd Maste * 12410ff414cSEd Maste * The chunks array is initialized to `NULL` and chunk count to 0 12510ff414cSEd Maste * 126*5d3e7166SEd Maste * @return Reference to the new bytestring item. The item's reference count is 127*5d3e7166SEd Maste * initialized to one. 128*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 12910ff414cSEd Maste */ 130*5d3e7166SEd Maste _CBOR_NODISCARD 131*5d3e7166SEd Maste CBOR_EXPORT cbor_item_t *cbor_new_indefinite_bytestring(void); 13210ff414cSEd Maste 13310ff414cSEd Maste /** Creates a new byte string and initializes it 13410ff414cSEd Maste * 13510ff414cSEd Maste * The `handle` will be copied to a newly allocated block 13610ff414cSEd Maste * 13710ff414cSEd Maste * @param handle Block of binary data 13810ff414cSEd Maste * @param length Length of `data` 139*5d3e7166SEd Maste * @return Reference to the new bytestring item. The item's reference count is 140*5d3e7166SEd Maste * initialized to one. 141*5d3e7166SEd Maste * @return `NULL` if memory allocation fails 14210ff414cSEd Maste */ 143*5d3e7166SEd Maste _CBOR_NODISCARD 14410ff414cSEd Maste CBOR_EXPORT cbor_item_t *cbor_build_bytestring(cbor_data handle, size_t length); 14510ff414cSEd Maste 14610ff414cSEd Maste #ifdef __cplusplus 14710ff414cSEd Maste } 14810ff414cSEd Maste #endif 14910ff414cSEd Maste 15010ff414cSEd Maste #endif // LIBCBOR_BYTESTRINGS_H 151