2014-12-04 22:16:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 Intel Corporation
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "main/macros.h"
|
|
|
|
#include "blob.h"
|
|
|
|
|
2017-06-24 09:26:29 +01:00
|
|
|
#ifdef HAVE_VALGRIND
|
|
|
|
#include <valgrind.h>
|
|
|
|
#include <memcheck.h>
|
|
|
|
#define VG(x) x
|
|
|
|
#else
|
|
|
|
#define VG(x)
|
|
|
|
#endif
|
|
|
|
|
2014-12-04 22:16:47 +00:00
|
|
|
#define BLOB_INITIAL_SIZE 4096
|
|
|
|
|
|
|
|
/* Ensure that \blob will be able to fit an additional object of size
|
|
|
|
* \additional. The growing (if any) will occur by doubling the existing
|
|
|
|
* allocation.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
grow_to_fit(struct blob *blob, size_t additional)
|
|
|
|
{
|
|
|
|
size_t to_allocate;
|
|
|
|
uint8_t *new_data;
|
|
|
|
|
2017-10-11 21:32:45 +01:00
|
|
|
if (blob->out_of_memory)
|
|
|
|
return false;
|
|
|
|
|
2014-12-04 22:16:47 +00:00
|
|
|
if (blob->size + additional <= blob->allocated)
|
|
|
|
return true;
|
|
|
|
|
2017-10-11 17:52:07 +01:00
|
|
|
if (blob->fixed_allocation) {
|
|
|
|
blob->out_of_memory = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-04 22:16:47 +00:00
|
|
|
if (blob->allocated == 0)
|
|
|
|
to_allocate = BLOB_INITIAL_SIZE;
|
|
|
|
else
|
|
|
|
to_allocate = blob->allocated * 2;
|
|
|
|
|
|
|
|
to_allocate = MAX2(to_allocate, blob->allocated + additional);
|
|
|
|
|
2017-03-10 00:30:01 +00:00
|
|
|
new_data = realloc(blob->data, to_allocate);
|
2017-10-11 21:32:45 +01:00
|
|
|
if (new_data == NULL) {
|
|
|
|
blob->out_of_memory = true;
|
2014-12-04 22:16:47 +00:00
|
|
|
return false;
|
2017-10-11 21:32:45 +01:00
|
|
|
}
|
2014-12-04 22:16:47 +00:00
|
|
|
|
|
|
|
blob->data = new_data;
|
|
|
|
blob->allocated = to_allocate;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Align the blob->size so that reading or writing a value at (blob->data +
|
|
|
|
* blob->size) will result in an access aligned to a granularity of \alignment
|
|
|
|
* bytes.
|
|
|
|
*
|
|
|
|
* \return True unless allocation fails
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
align_blob(struct blob *blob, size_t alignment)
|
|
|
|
{
|
|
|
|
const size_t new_size = ALIGN(blob->size, alignment);
|
|
|
|
|
2017-03-02 23:59:57 +00:00
|
|
|
if (blob->size < new_size) {
|
|
|
|
if (!grow_to_fit(blob, new_size - blob->size))
|
|
|
|
return false;
|
2014-12-04 22:16:47 +00:00
|
|
|
|
2017-10-13 05:02:48 +01:00
|
|
|
if (blob->data)
|
|
|
|
memset(blob->data + blob->size, 0, new_size - blob->size);
|
2017-03-02 23:59:57 +00:00
|
|
|
blob->size = new_size;
|
|
|
|
}
|
2014-12-04 22:16:47 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
align_blob_reader(struct blob_reader *blob, size_t alignment)
|
|
|
|
{
|
|
|
|
blob->current = blob->data + ALIGN(blob->current - blob->data, alignment);
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:44:33 +01:00
|
|
|
void
|
|
|
|
blob_init(struct blob *blob)
|
2014-12-04 22:16:47 +00:00
|
|
|
{
|
|
|
|
blob->data = NULL;
|
|
|
|
blob->allocated = 0;
|
|
|
|
blob->size = 0;
|
2017-10-11 17:52:07 +01:00
|
|
|
blob->fixed_allocation = false;
|
|
|
|
blob->out_of_memory = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
blob_init_fixed(struct blob *blob, void *data, size_t size)
|
|
|
|
{
|
|
|
|
blob->data = data;
|
|
|
|
blob->allocated = size;
|
|
|
|
blob->size = 0;
|
|
|
|
blob->fixed_allocation = true;
|
2017-10-11 21:32:45 +01:00
|
|
|
blob->out_of_memory = false;
|
2014-12-04 22:16:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 07:16:51 +00:00
|
|
|
bool
|
|
|
|
blob_overwrite_bytes(struct blob *blob,
|
|
|
|
size_t offset,
|
|
|
|
const void *bytes,
|
|
|
|
size_t to_write)
|
|
|
|
{
|
|
|
|
/* Detect an attempt to overwrite data out of bounds. */
|
2017-09-15 05:29:46 +01:00
|
|
|
if (offset + to_write < offset || blob->size < offset + to_write)
|
2014-11-13 07:16:51 +00:00
|
|
|
return false;
|
|
|
|
|
2017-06-24 09:26:29 +01:00
|
|
|
VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
|
|
|
|
|
2017-10-13 05:02:48 +01:00
|
|
|
if (blob->data)
|
|
|
|
memcpy(blob->data + offset, bytes, to_write);
|
2014-11-13 07:16:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-04 22:16:47 +00:00
|
|
|
bool
|
|
|
|
blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
|
|
|
|
{
|
|
|
|
if (! grow_to_fit(blob, to_write))
|
|
|
|
return false;
|
|
|
|
|
2017-06-24 09:26:29 +01:00
|
|
|
VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
|
|
|
|
|
2017-10-13 05:02:48 +01:00
|
|
|
if (blob->data)
|
|
|
|
memcpy(blob->data + blob->size, bytes, to_write);
|
2014-12-04 22:16:47 +00:00
|
|
|
blob->size += to_write;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-13 18:40:45 +01:00
|
|
|
intptr_t
|
2014-12-04 22:16:47 +00:00
|
|
|
blob_reserve_bytes(struct blob *blob, size_t to_write)
|
|
|
|
{
|
2017-10-13 18:40:45 +01:00
|
|
|
intptr_t ret;
|
2014-12-04 22:16:47 +00:00
|
|
|
|
|
|
|
if (! grow_to_fit (blob, to_write))
|
2017-09-15 05:29:46 +01:00
|
|
|
return -1;
|
2014-12-04 22:16:47 +00:00
|
|
|
|
2017-09-15 05:29:46 +01:00
|
|
|
ret = blob->size;
|
2014-12-04 22:16:47 +00:00
|
|
|
blob->size += to_write;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-13 18:40:45 +01:00
|
|
|
intptr_t
|
2017-10-13 04:58:43 +01:00
|
|
|
blob_reserve_uint32(struct blob *blob)
|
|
|
|
{
|
|
|
|
align_blob(blob, sizeof(uint32_t));
|
|
|
|
return blob_reserve_bytes(blob, sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
2017-10-13 18:40:45 +01:00
|
|
|
intptr_t
|
2017-10-13 04:58:43 +01:00
|
|
|
blob_reserve_intptr(struct blob *blob)
|
|
|
|
{
|
|
|
|
align_blob(blob, sizeof(intptr_t));
|
|
|
|
return blob_reserve_bytes(blob, sizeof(intptr_t));
|
|
|
|
}
|
|
|
|
|
2014-12-04 22:16:47 +00:00
|
|
|
bool
|
|
|
|
blob_write_uint32(struct blob *blob, uint32_t value)
|
|
|
|
{
|
|
|
|
align_blob(blob, sizeof(value));
|
|
|
|
|
|
|
|
return blob_write_bytes(blob, &value, sizeof(value));
|
2014-11-13 07:16:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 04:58:43 +01:00
|
|
|
#define ASSERT_ALIGNED(_offset, _align) \
|
|
|
|
assert(ALIGN((_offset), (_align)) == (_offset))
|
|
|
|
|
2014-11-13 07:16:51 +00:00
|
|
|
bool
|
|
|
|
blob_overwrite_uint32 (struct blob *blob,
|
|
|
|
size_t offset,
|
|
|
|
uint32_t value)
|
|
|
|
{
|
2017-10-13 04:58:43 +01:00
|
|
|
ASSERT_ALIGNED(offset, sizeof(value));
|
2014-11-13 07:16:51 +00:00
|
|
|
return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
|
2014-12-04 22:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
blob_write_uint64(struct blob *blob, uint64_t value)
|
|
|
|
{
|
|
|
|
align_blob(blob, sizeof(value));
|
|
|
|
|
|
|
|
return blob_write_bytes(blob, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
blob_write_intptr(struct blob *blob, intptr_t value)
|
|
|
|
{
|
|
|
|
align_blob(blob, sizeof(value));
|
|
|
|
|
|
|
|
return blob_write_bytes(blob, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
2017-10-13 04:58:43 +01:00
|
|
|
bool
|
|
|
|
blob_overwrite_intptr (struct blob *blob,
|
|
|
|
size_t offset,
|
|
|
|
intptr_t value)
|
|
|
|
{
|
|
|
|
ASSERT_ALIGNED(offset, sizeof(value));
|
|
|
|
return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
2014-12-04 22:16:47 +00:00
|
|
|
bool
|
|
|
|
blob_write_string(struct blob *blob, const char *str)
|
|
|
|
{
|
|
|
|
return blob_write_bytes(blob, str, strlen(str) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-11 20:10:08 +01:00
|
|
|
blob_reader_init(struct blob_reader *blob, const void *data, size_t size)
|
2014-12-04 22:16:47 +00:00
|
|
|
{
|
|
|
|
blob->data = data;
|
2017-10-11 20:09:02 +01:00
|
|
|
blob->end = blob->data + size;
|
2014-12-04 22:16:47 +00:00
|
|
|
blob->current = data;
|
|
|
|
blob->overrun = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that an object of size \size can be read from this blob.
|
|
|
|
*
|
|
|
|
* If not, set blob->overrun to indicate that we attempted to read too far.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
ensure_can_read(struct blob_reader *blob, size_t size)
|
|
|
|
{
|
2017-10-11 18:56:48 +01:00
|
|
|
if (blob->overrun)
|
|
|
|
return false;
|
|
|
|
|
2017-10-21 10:23:30 +01:00
|
|
|
if (blob->current <= blob->end && blob->end - blob->current >= size)
|
2014-12-04 22:16:47 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
blob->overrun = true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:09:02 +01:00
|
|
|
const void *
|
2014-12-04 22:16:47 +00:00
|
|
|
blob_read_bytes(struct blob_reader *blob, size_t size)
|
|
|
|
{
|
2017-10-11 20:09:02 +01:00
|
|
|
const void *ret;
|
2014-12-04 22:16:47 +00:00
|
|
|
|
|
|
|
if (! ensure_can_read (blob, size))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = blob->current;
|
|
|
|
|
|
|
|
blob->current += size;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-11 20:10:08 +01:00
|
|
|
blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size)
|
2014-12-04 22:16:47 +00:00
|
|
|
{
|
2017-10-11 20:10:08 +01:00
|
|
|
const void *bytes;
|
2014-12-04 22:16:47 +00:00
|
|
|
|
|
|
|
bytes = blob_read_bytes(blob, size);
|
|
|
|
if (bytes == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(dest, bytes, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These next three read functions have identical form. If we add any beyond
|
|
|
|
* these first three we should probably switch to generating these with a
|
|
|
|
* preprocessor macro.
|
|
|
|
*/
|
|
|
|
uint32_t
|
|
|
|
blob_read_uint32(struct blob_reader *blob)
|
|
|
|
{
|
|
|
|
uint32_t ret;
|
|
|
|
int size = sizeof(ret);
|
|
|
|
|
|
|
|
align_blob_reader(blob, size);
|
|
|
|
|
|
|
|
if (! ensure_can_read(blob, size))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = *((uint32_t*) blob->current);
|
|
|
|
|
|
|
|
blob->current += size;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
blob_read_uint64(struct blob_reader *blob)
|
|
|
|
{
|
|
|
|
uint64_t ret;
|
|
|
|
int size = sizeof(ret);
|
|
|
|
|
|
|
|
align_blob_reader(blob, size);
|
|
|
|
|
|
|
|
if (! ensure_can_read(blob, size))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = *((uint64_t*) blob->current);
|
|
|
|
|
|
|
|
blob->current += size;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
intptr_t
|
|
|
|
blob_read_intptr(struct blob_reader *blob)
|
|
|
|
{
|
|
|
|
intptr_t ret;
|
|
|
|
int size = sizeof(ret);
|
|
|
|
|
|
|
|
align_blob_reader(blob, size);
|
|
|
|
|
|
|
|
if (! ensure_can_read(blob, size))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = *((intptr_t *) blob->current);
|
|
|
|
|
|
|
|
blob->current += size;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
blob_read_string(struct blob_reader *blob)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
char *ret;
|
|
|
|
uint8_t *nul;
|
|
|
|
|
|
|
|
/* If we're already at the end, then this is an overrun. */
|
|
|
|
if (blob->current >= blob->end) {
|
|
|
|
blob->overrun = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Similarly, if there is no zero byte in the data remaining in this blob,
|
|
|
|
* we also consider that an overrun.
|
|
|
|
*/
|
|
|
|
nul = memchr(blob->current, 0, blob->end - blob->current);
|
|
|
|
|
|
|
|
if (nul == NULL) {
|
|
|
|
blob->overrun = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = nul - blob->current + 1;
|
|
|
|
|
|
|
|
assert(ensure_can_read(blob, size));
|
|
|
|
|
|
|
|
ret = (char *) blob->current;
|
|
|
|
|
|
|
|
blob->current += size;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|