Path: blob/main/contrib/libcbor/src/cbor/bytestrings.h
39536 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#ifndef LIBCBOR_BYTESTRINGS_H8#define LIBCBOR_BYTESTRINGS_H910#include "cbor/cbor_export.h"11#include "cbor/common.h"1213#ifdef __cplusplus14extern "C" {15#endif1617/*18* ============================================================================19* Byte string manipulation20* ============================================================================21*/2223/** Returns the length of the binary data24*25* For definite byte strings only26*27* @param item a definite bytestring28* @return length of the binary data. Zero if no chunk has been attached yet29*/30_CBOR_NODISCARD31CBOR_EXPORT size_t cbor_bytestring_length(const cbor_item_t *item);3233/** Is the byte string definite?34*35* @param item a byte string36* @return Is the byte string definite?37*/38_CBOR_NODISCARD39CBOR_EXPORT bool cbor_bytestring_is_definite(const cbor_item_t *item);4041/** Is the byte string indefinite?42*43* @param item a byte string44* @return Is the byte string indefinite?45*/46_CBOR_NODISCARD47CBOR_EXPORT bool cbor_bytestring_is_indefinite(const cbor_item_t *item);4849/** Get the handle to the binary data50*51* Definite items only. Modifying the data is allowed. In that case, the caller52* takes responsibility for the effect on items this item might be a part of53*54* @param item A definite byte string55* @return The address of the underlying binary data56* @return `NULL` if no data have been assigned57* yet.58*/59_CBOR_NODISCARD60CBOR_EXPORT cbor_mutable_data cbor_bytestring_handle(const cbor_item_t *item);6162/** Set the handle to the binary data63*64* @param item A definite byte string65* @param data The memory block. The caller gives up the ownership of the block.66* libcbor will deallocate it when appropriate using the `free` implementation67* configured using #cbor_set_allocs68* @param length Length of the data block69*/70CBOR_EXPORT void cbor_bytestring_set_handle(71cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data,72size_t length);7374/** Get the handle to the array of chunks75*76* Manipulations with the memory block (e.g. sorting it) are allowed, but the77* validity and the number of chunks must be retained.78*79* @param item A indefinite byte string80* @return array of #cbor_bytestring_chunk_count definite bytestrings81*/82_CBOR_NODISCARD83CBOR_EXPORT cbor_item_t **cbor_bytestring_chunks_handle(84const cbor_item_t *item);8586/** Get the number of chunks this string consist of87*88* @param item A indefinite bytestring89* @return The chunk count. 0 for freshly created items.90*/91_CBOR_NODISCARD92CBOR_EXPORT size_t cbor_bytestring_chunk_count(const cbor_item_t *item);9394/** Appends a chunk to the bytestring95*96* Indefinite byte strings only.97*98* May realloc the chunk storage.99*100* @param item An indefinite byte string101* @param chunk A definite byte string. Its reference count will be be increased102* by one.103* @return true on success, false on realloc failure. In that case, the refcount104* of `chunk` is not increased and the `item` is left intact.105*/106_CBOR_NODISCARD107CBOR_EXPORT bool cbor_bytestring_add_chunk(cbor_item_t *item,108cbor_item_t *chunk);109110/** Creates a new definite byte string111*112* The handle is initialized to `NULL` and length to 0113*114* @return Reference to the new bytestring item. The item's reference count is115* initialized to one.116* @return `NULL` if memory allocation fails117*/118_CBOR_NODISCARD119CBOR_EXPORT cbor_item_t *cbor_new_definite_bytestring(void);120121/** Creates a new indefinite byte string122*123* The chunks array is initialized to `NULL` and chunk count to 0124*125* @return Reference to the new bytestring item. The item's reference count is126* initialized to one.127* @return `NULL` if memory allocation fails128*/129_CBOR_NODISCARD130CBOR_EXPORT cbor_item_t *cbor_new_indefinite_bytestring(void);131132/** Creates a new byte string and initializes it133*134* The `handle` will be copied to a newly allocated block135*136* @param handle Block of binary data137* @param length Length of `data`138* @return Reference to the new bytestring item. The item's reference count is139* initialized to one.140* @return `NULL` if memory allocation fails141*/142_CBOR_NODISCARD143CBOR_EXPORT cbor_item_t *cbor_build_bytestring(cbor_data handle, size_t length);144145#ifdef __cplusplus146}147#endif148149#endif // LIBCBOR_BYTESTRINGS_H150151152