1/* 2 * Copyright © 2014 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#ifndef BLOB_H 25#define BLOB_H 26 27#include <stdbool.h> 28#include <stddef.h> 29#include <stdint.h> 30#include <stdlib.h> 31 32#ifdef __cplusplus 33extern "C" { 34#endif 35 36/* The blob functions implement a simple, low-level API for serializing and 37 * deserializing. 38 * 39 * All objects written to a blob will be serialized directly, (without any 40 * additional meta-data to describe the data written). Therefore, it is the 41 * caller's responsibility to ensure that any data can be read later, (either 42 * by knowing exactly what data is expected, or by writing to the blob 43 * sufficient meta-data to describe what has been written). 44 * 45 * A blob is efficient in that it dynamically grows by doubling in size, so 46 * allocation costs are logarithmic. 47 */ 48 49struct blob { 50 /* The data actually written to the blob. Never read or write this directly 51 * when serializing, use blob_reserve_* and blob_overwrite_* instead which 52 * check for out_of_memory and handle fixed-size blobs correctly. 53 */ 54 uint8_t *data; 55 56 /** Number of bytes that have been allocated for \c data. */ 57 size_t allocated; 58 59 /** The number of bytes that have actual data written to them. */ 60 size_t size; 61 62 /** True if \c data a fixed allocation that we cannot resize 63 * 64 * \see blob_init_fixed 65 */ 66 bool fixed_allocation; 67 68 /** 69 * True if we've ever failed to realloc or if we go pas the end of a fixed 70 * allocation blob. 71 */ 72 bool out_of_memory; 73}; 74 75/* When done reading, the caller can ensure that everything was consumed by 76 * checking the following: 77 * 78 * 1. blob->current should be equal to blob->end, (if not, too little was 79 * read). 80 * 81 * 2. blob->overrun should be false, (otherwise, too much was read). 82 */ 83struct blob_reader { 84 const uint8_t *data; 85 const uint8_t *end; 86 const uint8_t *current; 87 bool overrun; 88}; 89 90/** 91 * Init a new, empty blob. 92 */ 93void 94blob_init(struct blob *blob); 95 96/** 97 * Init a new, fixed-size blob. 98 * 99 * A fixed-size blob has a fixed block of data that will not be freed on 100 * blob_finish and will never be grown. If we hit the end, we simply start 101 * returning false from the write functions. 102 * 103 * If a fixed-size blob has a NULL data pointer then the data is written but 104 * it otherwise operates normally. This can be used to determine the size 105 * that will be required to write a given data structure. 106 */ 107void 108blob_init_fixed(struct blob *blob, void *data, size_t size); 109 110/** 111 * Finish a blob and free its memory. 112 * 113 * If \blob was initialized with blob_init_fixed, the data pointer is 114 * considered to be owned by the user and will not be freed. 115 */ 116static inline void 117blob_finish(struct blob *blob) 118{ 119 if (!blob->fixed_allocation) 120 free(blob->data); 121} 122 123void 124blob_finish_get_buffer(struct blob *blob, void **buffer, size_t *size); 125 126/** 127 * Aligns the blob to the given alignment. 128 * 129 * \see blob_reader_align 130 * 131 * \return True unless allocation fails 132 */ 133bool 134blob_align(struct blob *blob, size_t alignment); 135 136/** 137 * Add some unstructured, fixed-size data to a blob. 138 * 139 * \return True unless allocation failed. 140 */ 141bool 142blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write); 143 144/** 145 * Reserve space in \blob for a number of bytes. 146 * 147 * Space will be allocated within the blob for these byes, but the bytes will 148 * be left uninitialized. The caller is expected to use \sa 149 * blob_overwrite_bytes to write to these bytes. 150 * 151 * \return An offset to space allocated within \blob to which \to_write bytes 152 * can be written, (or -1 in case of any allocation error). 153 */ 154intptr_t 155blob_reserve_bytes(struct blob *blob, size_t to_write); 156 157/** 158 * Similar to \sa blob_reserve_bytes, but only reserves an uint32_t worth of 159 * space. Note that this must be used if later reading with \sa 160 * blob_read_uint32, since it aligns the offset correctly. 161 */ 162intptr_t 163blob_reserve_uint32(struct blob *blob); 164 165/** 166 * Similar to \sa blob_reserve_bytes, but only reserves an intptr_t worth of 167 * space. Note that this must be used if later reading with \sa 168 * blob_read_intptr, since it aligns the offset correctly. 169 */ 170intptr_t 171blob_reserve_intptr(struct blob *blob); 172 173/** 174 * Overwrite some data previously written to the blob. 175 * 176 * Writes data to an existing portion of the blob at an offset of \offset. 177 * This data range must have previously been written to the blob by one of the 178 * blob_write_* calls. 179 * 180 * For example usage, see blob_overwrite_uint32 181 * 182 * \return True unless the requested offset or offset+to_write lie outside 183 * the current blob's size. 184 */ 185bool 186blob_overwrite_bytes(struct blob *blob, 187 size_t offset, 188 const void *bytes, 189 size_t to_write); 190 191/** 192 * Add a uint8_t to a blob. 193 * 194 * \return True unless allocation failed. 195 */ 196bool 197blob_write_uint8(struct blob *blob, uint8_t value); 198 199/** 200 * Overwrite a uint8_t previously written to the blob. 201 * 202 * Writes a uint8_t value to an existing portion of the blob at an offset of 203 * \offset. This data range must have previously been written to the blob by 204 * one of the blob_write_* calls. 205 * 206 * \return True unless the requested position or position+to_write lie outside 207 * the current blob's size. 208 */ 209bool 210blob_overwrite_uint8(struct blob *blob, 211 size_t offset, 212 uint8_t value); 213 214/** 215 * Add a uint16_t to a blob. 216 * 217 * \note This function will only write to a uint16_t-aligned offset from the 218 * beginning of the blob's data, so some padding bytes may be added to the 219 * blob if this write follows some unaligned write (such as 220 * blob_write_string). 221 * 222 * \return True unless allocation failed. 223 */ 224bool 225blob_write_uint16(struct blob *blob, uint16_t value); 226 227/** 228 * Add a uint32_t to a blob. 229 * 230 * \note This function will only write to a uint32_t-aligned offset from the 231 * beginning of the blob's data, so some padding bytes may be added to the 232 * blob if this write follows some unaligned write (such as 233 * blob_write_string). 234 * 235 * \return True unless allocation failed. 236 */ 237bool 238blob_write_uint32(struct blob *blob, uint32_t value); 239 240/** 241 * Overwrite a uint32_t previously written to the blob. 242 * 243 * Writes a uint32_t value to an existing portion of the blob at an offset of 244 * \offset. This data range must have previously been written to the blob by 245 * one of the blob_write_* calls. 246 * 247 * 248 * The expected usage is something like the following pattern: 249 * 250 * size_t offset; 251 * 252 * offset = blob_reserve_uint32(blob); 253 * ... various blob write calls, writing N items ... 254 * blob_overwrite_uint32 (blob, offset, N); 255 * 256 * \return True unless the requested position or position+to_write lie outside 257 * the current blob's size. 258 */ 259bool 260blob_overwrite_uint32(struct blob *blob, 261 size_t offset, 262 uint32_t value); 263 264/** 265 * Add a uint64_t to a blob. 266 * 267 * \note This function will only write to a uint64_t-aligned offset from the 268 * beginning of the blob's data, so some padding bytes may be added to the 269 * blob if this write follows some unaligned write (such as 270 * blob_write_string). 271 * 272 * \return True unless allocation failed. 273 */ 274bool 275blob_write_uint64(struct blob *blob, uint64_t value); 276 277/** 278 * Add an intptr_t to a blob. 279 * 280 * \note This function will only write to an intptr_t-aligned offset from the 281 * beginning of the blob's data, so some padding bytes may be added to the 282 * blob if this write follows some unaligned write (such as 283 * blob_write_string). 284 * 285 * \return True unless allocation failed. 286 */ 287bool 288blob_write_intptr(struct blob *blob, intptr_t value); 289 290/** 291 * Overwrite an intptr_t previously written to the blob. 292 * 293 * Writes a intptr_t value to an existing portion of the blob at an offset of 294 * \offset. This data range must have previously been written to the blob by 295 * one of the blob_write_* calls. 296 * 297 * For example usage, see blob_overwrite_uint32 298 * 299 * \return True unless the requested position or position+to_write lie outside 300 * the current blob's size. 301 */ 302bool 303blob_overwrite_intptr(struct blob *blob, 304 size_t offset, 305 intptr_t value); 306 307/** 308 * Add a NULL-terminated string to a blob, (including the NULL terminator). 309 * 310 * \return True unless allocation failed. 311 */ 312bool 313blob_write_string(struct blob *blob, const char *str); 314 315/** 316 * Start reading a blob, (initializing the contents of \blob for reading). 317 * 318 * After this call, the caller can use the various blob_read_* functions to 319 * read elements from the data array. 320 * 321 * For all of the blob_read_* functions, if there is insufficient data 322 * remaining, the functions will do nothing, (perhaps returning default values 323 * such as 0). The caller can detect this by noting that the blob_reader's 324 * current value is unchanged before and after the call. 325 */ 326void 327blob_reader_init(struct blob_reader *blob, const void *data, size_t size); 328 329/** 330 * Align the current offset of the blob reader to the given alignment. 331 * 332 * This may be useful if you need the result of blob_read_bytes to have a 333 * particular alignment. Note that this only aligns relative to blob->data 334 * and the alignment of the resulting pointer is only guaranteed if blob->data 335 * is also aligned to the requested alignment. 336 */ 337void 338blob_reader_align(struct blob_reader *blob, size_t alignment); 339 340/** 341 * Read some unstructured, fixed-size data from the current location, (and 342 * update the current location to just past this data). 343 * 344 * \note The memory returned belongs to the data underlying the blob reader. The 345 * caller must copy the data in order to use it after the lifetime of the data 346 * underlying the blob reader. 347 * 348 * \return The bytes read (see note above about memory lifetime). 349 */ 350const void * 351blob_read_bytes(struct blob_reader *blob, size_t size); 352 353/** 354 * Read some unstructured, fixed-size data from the current location, copying 355 * it to \dest (and update the current location to just past this data) 356 */ 357void 358blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size); 359 360/** 361 * Skip \size bytes within the blob. 362 */ 363void 364blob_skip_bytes(struct blob_reader *blob, size_t size); 365 366/** 367 * Read a uint8_t from the current location, (and update the current location 368 * to just past this uint8_t). 369 * 370 * \return The uint8_t read 371 */ 372uint8_t 373blob_read_uint8(struct blob_reader *blob); 374 375/** 376 * Read a uint16_t from the current location, (and update the current location 377 * to just past this uint16_t). 378 * 379 * \note This function will only read from a uint16_t-aligned offset from the 380 * beginning of the blob's data, so some padding bytes may be skipped. 381 * 382 * \return The uint16_t read 383 */ 384uint16_t 385blob_read_uint16(struct blob_reader *blob); 386 387/** 388 * Read a uint32_t from the current location, (and update the current location 389 * to just past this uint32_t). 390 * 391 * \note This function will only read from a uint32_t-aligned offset from the 392 * beginning of the blob's data, so some padding bytes may be skipped. 393 * 394 * \return The uint32_t read 395 */ 396uint32_t 397blob_read_uint32(struct blob_reader *blob); 398 399/** 400 * Read a uint64_t from the current location, (and update the current location 401 * to just past this uint64_t). 402 * 403 * \note This function will only read from a uint64_t-aligned offset from the 404 * beginning of the blob's data, so some padding bytes may be skipped. 405 * 406 * \return The uint64_t read 407 */ 408uint64_t 409blob_read_uint64(struct blob_reader *blob); 410 411/** 412 * Read an intptr_t value from the current location, (and update the 413 * current location to just past this intptr_t). 414 * 415 * \note This function will only read from an intptr_t-aligned offset from the 416 * beginning of the blob's data, so some padding bytes may be skipped. 417 * 418 * \return The intptr_t read 419 */ 420intptr_t 421blob_read_intptr(struct blob_reader *blob); 422 423/** 424 * Read a NULL-terminated string from the current location, (and update the 425 * current location to just past this string). 426 * 427 * \note The memory returned belongs to the data underlying the blob reader. The 428 * caller must copy the string in order to use the string after the lifetime 429 * of the data underlying the blob reader. 430 * 431 * \return The string read (see note above about memory lifetime). However, if 432 * there is no NULL byte remaining within the blob, this function returns 433 * NULL. 434 */ 435char * 436blob_read_string(struct blob_reader *blob); 437 438#ifdef __cplusplus 439} 440#endif 441 442#endif /* BLOB_H */ 443