/*1* Copyright © 2014 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef BLOB_H24#define BLOB_H2526#include <stdbool.h>27#include <stddef.h>28#include <stdint.h>29#include <stdlib.h>3031#ifdef __cplusplus32extern "C" {33#endif3435/* The blob functions implement a simple, low-level API for serializing and36* deserializing.37*38* All objects written to a blob will be serialized directly, (without any39* additional meta-data to describe the data written). Therefore, it is the40* caller's responsibility to ensure that any data can be read later, (either41* by knowing exactly what data is expected, or by writing to the blob42* sufficient meta-data to describe what has been written).43*44* A blob is efficient in that it dynamically grows by doubling in size, so45* allocation costs are logarithmic.46*/4748struct blob {49/* The data actually written to the blob. */50uint8_t *data;5152/** Number of bytes that have been allocated for \c data. */53size_t allocated;5455/** The number of bytes that have actual data written to them. */56size_t size;5758/** True if \c data a fixed allocation that we cannot resize59*60* \see blob_init_fixed61*/62bool fixed_allocation;6364/**65* True if we've ever failed to realloc or if we go pas the end of a fixed66* allocation blob.67*/68bool out_of_memory;69};7071/* When done reading, the caller can ensure that everything was consumed by72* checking the following:73*74* 1. blob->current should be equal to blob->end, (if not, too little was75* read).76*77* 2. blob->overrun should be false, (otherwise, too much was read).78*/79struct blob_reader {80const uint8_t *data;81const uint8_t *end;82const uint8_t *current;83bool overrun;84};8586/**87* Init a new, empty blob.88*/89void90blob_init(struct blob *blob);9192/**93* Init a new, fixed-size blob.94*95* A fixed-size blob has a fixed block of data that will not be freed on96* blob_finish and will never be grown. If we hit the end, we simply start97* returning false from the write functions.98*99* If a fixed-size blob has a NULL data pointer then the data is written but100* it otherwise operates normally. This can be used to determine the size101* that will be required to write a given data structure.102*/103void104blob_init_fixed(struct blob *blob, void *data, size_t size);105106/**107* Finish a blob and free its memory.108*109* If \blob was initialized with blob_init_fixed, the data pointer is110* considered to be owned by the user and will not be freed.111*/112static inline void113blob_finish(struct blob *blob)114{115if (!blob->fixed_allocation)116free(blob->data);117}118119void120blob_finish_get_buffer(struct blob *blob, void **buffer, size_t *size);121122/**123* Add some unstructured, fixed-size data to a blob.124*125* \return True unless allocation failed.126*/127bool128blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write);129130/**131* Reserve space in \blob for a number of bytes.132*133* Space will be allocated within the blob for these byes, but the bytes will134* be left uninitialized. The caller is expected to use \sa135* blob_overwrite_bytes to write to these bytes.136*137* \return An offset to space allocated within \blob to which \to_write bytes138* can be written, (or -1 in case of any allocation error).139*/140intptr_t141blob_reserve_bytes(struct blob *blob, size_t to_write);142143/**144* Similar to \sa blob_reserve_bytes, but only reserves an uint32_t worth of145* space. Note that this must be used if later reading with \sa146* blob_read_uint32, since it aligns the offset correctly.147*/148intptr_t149blob_reserve_uint32(struct blob *blob);150151/**152* Similar to \sa blob_reserve_bytes, but only reserves an intptr_t worth of153* space. Note that this must be used if later reading with \sa154* blob_read_intptr, since it aligns the offset correctly.155*/156intptr_t157blob_reserve_intptr(struct blob *blob);158159/**160* Overwrite some data previously written to the blob.161*162* Writes data to an existing portion of the blob at an offset of \offset.163* This data range must have previously been written to the blob by one of the164* blob_write_* calls.165*166* For example usage, see blob_overwrite_uint32167*168* \return True unless the requested offset or offset+to_write lie outside169* the current blob's size.170*/171bool172blob_overwrite_bytes(struct blob *blob,173size_t offset,174const void *bytes,175size_t to_write);176177/**178* Add a uint8_t to a blob.179*180* \return True unless allocation failed.181*/182bool183blob_write_uint8(struct blob *blob, uint8_t value);184185/**186* Overwrite a uint8_t previously written to the blob.187*188* Writes a uint8_t value to an existing portion of the blob at an offset of189* \offset. This data range must have previously been written to the blob by190* one of the blob_write_* calls.191*192* \return True unless the requested position or position+to_write lie outside193* the current blob's size.194*/195bool196blob_overwrite_uint8(struct blob *blob,197size_t offset,198uint8_t value);199200/**201* Add a uint16_t to a blob.202*203* \note This function will only write to a uint16_t-aligned offset from the204* beginning of the blob's data, so some padding bytes may be added to the205* blob if this write follows some unaligned write (such as206* blob_write_string).207*208* \return True unless allocation failed.209*/210bool211blob_write_uint16(struct blob *blob, uint16_t value);212213/**214* Add a uint32_t to a blob.215*216* \note This function will only write to a uint32_t-aligned offset from the217* beginning of the blob's data, so some padding bytes may be added to the218* blob if this write follows some unaligned write (such as219* blob_write_string).220*221* \return True unless allocation failed.222*/223bool224blob_write_uint32(struct blob *blob, uint32_t value);225226/**227* Overwrite a uint32_t previously written to the blob.228*229* Writes a uint32_t value to an existing portion of the blob at an offset of230* \offset. This data range must have previously been written to the blob by231* one of the blob_write_* calls.232*233*234* The expected usage is something like the following pattern:235*236* size_t offset;237*238* offset = blob_reserve_uint32(blob);239* ... various blob write calls, writing N items ...240* blob_overwrite_uint32 (blob, offset, N);241*242* \return True unless the requested position or position+to_write lie outside243* the current blob's size.244*/245bool246blob_overwrite_uint32(struct blob *blob,247size_t offset,248uint32_t value);249250/**251* Add a uint64_t to a blob.252*253* \note This function will only write to a uint64_t-aligned offset from the254* beginning of the blob's data, so some padding bytes may be added to the255* blob if this write follows some unaligned write (such as256* blob_write_string).257*258* \return True unless allocation failed.259*/260bool261blob_write_uint64(struct blob *blob, uint64_t value);262263/**264* Add an intptr_t to a blob.265*266* \note This function will only write to an intptr_t-aligned offset from the267* beginning of the blob's data, so some padding bytes may be added to the268* blob if this write follows some unaligned write (such as269* blob_write_string).270*271* \return True unless allocation failed.272*/273bool274blob_write_intptr(struct blob *blob, intptr_t value);275276/**277* Overwrite an intptr_t previously written to the blob.278*279* Writes a intptr_t value to an existing portion of the blob at an offset of280* \offset. This data range must have previously been written to the blob by281* one of the blob_write_* calls.282*283* For example usage, see blob_overwrite_uint32284*285* \return True unless the requested position or position+to_write lie outside286* the current blob's size.287*/288bool289blob_overwrite_intptr(struct blob *blob,290size_t offset,291intptr_t value);292293/**294* Add a NULL-terminated string to a blob, (including the NULL terminator).295*296* \return True unless allocation failed.297*/298bool299blob_write_string(struct blob *blob, const char *str);300301/**302* Start reading a blob, (initializing the contents of \blob for reading).303*304* After this call, the caller can use the various blob_read_* functions to305* read elements from the data array.306*307* For all of the blob_read_* functions, if there is insufficient data308* remaining, the functions will do nothing, (perhaps returning default values309* such as 0). The caller can detect this by noting that the blob_reader's310* current value is unchanged before and after the call.311*/312void313blob_reader_init(struct blob_reader *blob, const void *data, size_t size);314315/**316* Read some unstructured, fixed-size data from the current location, (and317* update the current location to just past this data).318*319* \note The memory returned belongs to the data underlying the blob reader. The320* caller must copy the data in order to use it after the lifetime of the data321* underlying the blob reader.322*323* \return The bytes read (see note above about memory lifetime).324*/325const void *326blob_read_bytes(struct blob_reader *blob, size_t size);327328/**329* Read some unstructured, fixed-size data from the current location, copying330* it to \dest (and update the current location to just past this data)331*/332void333blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size);334335/**336* Skip \size bytes within the blob.337*/338void339blob_skip_bytes(struct blob_reader *blob, size_t size);340341/**342* Read a uint8_t from the current location, (and update the current location343* to just past this uint8_t).344*345* \return The uint8_t read346*/347uint8_t348blob_read_uint8(struct blob_reader *blob);349350/**351* Read a uint16_t from the current location, (and update the current location352* to just past this uint16_t).353*354* \note This function will only read from a uint16_t-aligned offset from the355* beginning of the blob's data, so some padding bytes may be skipped.356*357* \return The uint16_t read358*/359uint16_t360blob_read_uint16(struct blob_reader *blob);361362/**363* Read a uint32_t from the current location, (and update the current location364* to just past this uint32_t).365*366* \note This function will only read from a uint32_t-aligned offset from the367* beginning of the blob's data, so some padding bytes may be skipped.368*369* \return The uint32_t read370*/371uint32_t372blob_read_uint32(struct blob_reader *blob);373374/**375* Read a uint64_t from the current location, (and update the current location376* to just past this uint64_t).377*378* \note This function will only read from a uint64_t-aligned offset from the379* beginning of the blob's data, so some padding bytes may be skipped.380*381* \return The uint64_t read382*/383uint64_t384blob_read_uint64(struct blob_reader *blob);385386/**387* Read an intptr_t value from the current location, (and update the388* current location to just past this intptr_t).389*390* \note This function will only read from an intptr_t-aligned offset from the391* beginning of the blob's data, so some padding bytes may be skipped.392*393* \return The intptr_t read394*/395intptr_t396blob_read_intptr(struct blob_reader *blob);397398/**399* Read a NULL-terminated string from the current location, (and update the400* current location to just past this string).401*402* \note The memory returned belongs to the data underlying the blob reader. The403* caller must copy the string in order to use the string after the lifetime404* of the data underlying the blob reader.405*406* \return The string read (see note above about memory lifetime). However, if407* there is no NULL byte remaining within the blob, this function returns408* NULL.409*/410char *411blob_read_string(struct blob_reader *blob);412413#ifdef __cplusplus414}415#endif416417#endif /* BLOB_H */418419420