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#include <string.h> 25 26#include "blob.h" 27#include "u_math.h" 28 29#ifdef HAVE_VALGRIND 30#include <valgrind.h> 31#include <memcheck.h> 32#define VG(x) x 33#else 34#define VG(x) 35#endif 36 37#define BLOB_INITIAL_SIZE 4096 38 39/* Ensure that \blob will be able to fit an additional object of size 40 * \additional. The growing (if any) will occur by doubling the existing 41 * allocation. 42 */ 43static bool 44grow_to_fit(struct blob *blob, size_t additional) 45{ 46 size_t to_allocate; 47 uint8_t *new_data; 48 49 if (blob->out_of_memory) 50 return false; 51 52 if (blob->size + additional <= blob->allocated) 53 return true; 54 55 if (blob->fixed_allocation) { 56 blob->out_of_memory = true; 57 return false; 58 } 59 60 if (blob->allocated == 0) 61 to_allocate = BLOB_INITIAL_SIZE; 62 else 63 to_allocate = blob->allocated * 2; 64 65 to_allocate = MAX2(to_allocate, blob->allocated + additional); 66 67 new_data = realloc(blob->data, to_allocate); 68 if (new_data == NULL) { 69 blob->out_of_memory = true; 70 return false; 71 } 72 73 blob->data = new_data; 74 blob->allocated = to_allocate; 75 76 return true; 77} 78 79/* Align the blob->size so that reading or writing a value at (blob->data + 80 * blob->size) will result in an access aligned to a granularity of \alignment 81 * bytes. 82 * 83 * \return True unless allocation fails 84 */ 85bool 86blob_align(struct blob *blob, size_t alignment) 87{ 88 const size_t new_size = align64(blob->size, alignment); 89 90 if (blob->size < new_size) { 91 if (!grow_to_fit(blob, new_size - blob->size)) 92 return false; 93 94 if (blob->data) 95 memset(blob->data + blob->size, 0, new_size - blob->size); 96 blob->size = new_size; 97 } 98 99 return true; 100} 101 102void 103blob_reader_align(struct blob_reader *blob, size_t alignment) 104{ 105 blob->current = blob->data + align64(blob->current - blob->data, alignment); 106} 107 108void 109blob_init(struct blob *blob) 110{ 111 blob->data = NULL; 112 blob->allocated = 0; 113 blob->size = 0; 114 blob->fixed_allocation = false; 115 blob->out_of_memory = false; 116} 117 118void 119blob_init_fixed(struct blob *blob, void *data, size_t size) 120{ 121 blob->data = data; 122 blob->allocated = size; 123 blob->size = 0; 124 blob->fixed_allocation = true; 125 blob->out_of_memory = false; 126} 127 128void 129blob_finish_get_buffer(struct blob *blob, void **buffer, size_t *size) 130{ 131 *buffer = blob->data; 132 *size = blob->size; 133 blob->data = NULL; 134 135 /* Trim the buffer. */ 136 *buffer = realloc(*buffer, *size); 137} 138 139bool 140blob_overwrite_bytes(struct blob *blob, 141 size_t offset, 142 const void *bytes, 143 size_t to_write) 144{ 145 /* Detect an attempt to overwrite data out of bounds. */ 146 if (offset + to_write < offset || blob->size < offset + to_write) 147 return false; 148 149 VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write)); 150 151 if (blob->data) 152 memcpy(blob->data + offset, bytes, to_write); 153 154 return true; 155} 156 157bool 158blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write) 159{ 160 if (! grow_to_fit(blob, to_write)) 161 return false; 162 163 VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write)); 164 165 if (blob->data && to_write > 0) 166 memcpy(blob->data + blob->size, bytes, to_write); 167 blob->size += to_write; 168 169 return true; 170} 171 172intptr_t 173blob_reserve_bytes(struct blob *blob, size_t to_write) 174{ 175 intptr_t ret; 176 177 if (! grow_to_fit (blob, to_write)) 178 return -1; 179 180 ret = blob->size; 181 blob->size += to_write; 182 183 return ret; 184} 185 186intptr_t 187blob_reserve_uint32(struct blob *blob) 188{ 189 blob_align(blob, sizeof(uint32_t)); 190 return blob_reserve_bytes(blob, sizeof(uint32_t)); 191} 192 193intptr_t 194blob_reserve_intptr(struct blob *blob) 195{ 196 blob_align(blob, sizeof(intptr_t)); 197 return blob_reserve_bytes(blob, sizeof(intptr_t)); 198} 199 200#define BLOB_WRITE_TYPE(name, type) \ 201bool \ 202name(struct blob *blob, type value) \ 203{ \ 204 blob_align(blob, sizeof(value)); \ 205 return blob_write_bytes(blob, &value, sizeof(value)); \ 206} 207 208BLOB_WRITE_TYPE(blob_write_uint8, uint8_t) 209BLOB_WRITE_TYPE(blob_write_uint16, uint16_t) 210BLOB_WRITE_TYPE(blob_write_uint32, uint32_t) 211BLOB_WRITE_TYPE(blob_write_uint64, uint64_t) 212BLOB_WRITE_TYPE(blob_write_intptr, intptr_t) 213 214#define ASSERT_ALIGNED(_offset, _align) \ 215 assert(align64((_offset), (_align)) == (_offset)) 216 217bool 218blob_overwrite_uint8 (struct blob *blob, 219 size_t offset, 220 uint8_t value) 221{ 222 ASSERT_ALIGNED(offset, sizeof(value)); 223 return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); 224} 225 226bool 227blob_overwrite_uint32 (struct blob *blob, 228 size_t offset, 229 uint32_t value) 230{ 231 ASSERT_ALIGNED(offset, sizeof(value)); 232 return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); 233} 234 235bool 236blob_overwrite_intptr (struct blob *blob, 237 size_t offset, 238 intptr_t value) 239{ 240 ASSERT_ALIGNED(offset, sizeof(value)); 241 return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); 242} 243 244bool 245blob_write_string(struct blob *blob, const char *str) 246{ 247 return blob_write_bytes(blob, str, strlen(str) + 1); 248} 249 250void 251blob_reader_init(struct blob_reader *blob, const void *data, size_t size) 252{ 253 blob->data = data; 254 blob->end = blob->data + size; 255 blob->current = data; 256 blob->overrun = false; 257} 258 259/* Check that an object of size \size can be read from this blob. 260 * 261 * If not, set blob->overrun to indicate that we attempted to read too far. 262 */ 263static bool 264ensure_can_read(struct blob_reader *blob, size_t size) 265{ 266 if (blob->overrun) 267 return false; 268 269 if (blob->current <= blob->end && blob->end - blob->current >= size) 270 return true; 271 272 blob->overrun = true; 273 274 return false; 275} 276 277const void * 278blob_read_bytes(struct blob_reader *blob, size_t size) 279{ 280 const void *ret; 281 282 if (! ensure_can_read (blob, size)) 283 return NULL; 284 285 ret = blob->current; 286 287 blob->current += size; 288 289 return ret; 290} 291 292void 293blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size) 294{ 295 const void *bytes; 296 297 bytes = blob_read_bytes(blob, size); 298 if (bytes == NULL || size == 0) 299 return; 300 301 memcpy(dest, bytes, size); 302} 303 304void 305blob_skip_bytes(struct blob_reader *blob, size_t size) 306{ 307 if (ensure_can_read (blob, size)) 308 blob->current += size; 309} 310 311#define BLOB_READ_TYPE(name, type) \ 312type \ 313name(struct blob_reader *blob) \ 314{ \ 315 type ret = 0; \ 316 int size = sizeof(ret); \ 317 blob_reader_align(blob, size); \ 318 blob_copy_bytes(blob, &ret, size); \ 319 return ret; \ 320} 321 322BLOB_READ_TYPE(blob_read_uint8, uint8_t) 323BLOB_READ_TYPE(blob_read_uint16, uint16_t) 324BLOB_READ_TYPE(blob_read_uint32, uint32_t) 325BLOB_READ_TYPE(blob_read_uint64, uint64_t) 326BLOB_READ_TYPE(blob_read_intptr, intptr_t) 327 328char * 329blob_read_string(struct blob_reader *blob) 330{ 331 int size; 332 char *ret; 333 uint8_t *nul; 334 335 /* If we're already at the end, then this is an overrun. */ 336 if (blob->current >= blob->end) { 337 blob->overrun = true; 338 return NULL; 339 } 340 341 /* Similarly, if there is no zero byte in the data remaining in this blob, 342 * we also consider that an overrun. 343 */ 344 nul = memchr(blob->current, 0, blob->end - blob->current); 345 346 if (nul == NULL) { 347 blob->overrun = true; 348 return NULL; 349 } 350 351 size = nul - blob->current + 1; 352 353 assert(ensure_can_read(blob, size)); 354 355 ret = (char *) blob->current; 356 357 blob->current += size; 358 359 return ret; 360} 361