Browse Source

util/blob: add 8-bit and 16-bit reads and writes

Reviewed-by: Connor Abbott <cwabbott0@gmail.com>
master
Marek Olšák 6 years ago
parent
commit
4fe1d7822b
2 changed files with 71 additions and 72 deletions
  1. 29
    72
      src/util/blob.c
  2. 42
    0
      src/util/blob.h

+ 29
- 72
src/util/blob.c View File

return blob_reserve_bytes(blob, sizeof(intptr_t)); return blob_reserve_bytes(blob, sizeof(intptr_t));
} }


bool
blob_write_uint32(struct blob *blob, uint32_t value)
{
align_blob(blob, sizeof(value));
return blob_write_bytes(blob, &value, sizeof(value));
#define BLOB_WRITE_TYPE(name, type) \
bool \
name(struct blob *blob, type value) \
{ \
align_blob(blob, sizeof(value)); \
return blob_write_bytes(blob, &value, sizeof(value)); \
} }


BLOB_WRITE_TYPE(blob_write_uint8, uint8_t)
BLOB_WRITE_TYPE(blob_write_uint16, uint16_t)
BLOB_WRITE_TYPE(blob_write_uint32, uint32_t)
BLOB_WRITE_TYPE(blob_write_uint64, uint64_t)
BLOB_WRITE_TYPE(blob_write_intptr, intptr_t)

#define ASSERT_ALIGNED(_offset, _align) \ #define ASSERT_ALIGNED(_offset, _align) \
assert(ALIGN((_offset), (_align)) == (_offset)) assert(ALIGN((_offset), (_align)) == (_offset))


return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
} }


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));
}

bool bool
blob_overwrite_intptr (struct blob *blob, blob_overwrite_intptr (struct blob *blob,
size_t offset, size_t offset,
* these first three we should probably switch to generating these with a * these first three we should probably switch to generating these with a
* preprocessor macro. * 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;
#define BLOB_READ_TYPE(name, type) \
type \
name(struct blob_reader *blob) \
{ \
type ret; \
int size = sizeof(ret); \
align_blob_reader(blob, size); \
if (! ensure_can_read(blob, size)) \
return 0; \
ret = *((type*) 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;
}
BLOB_READ_TYPE(blob_read_uint8, uint8_t)
BLOB_READ_TYPE(blob_read_uint16, uint16_t)
BLOB_READ_TYPE(blob_read_uint32, uint32_t)
BLOB_READ_TYPE(blob_read_uint64, uint64_t)
BLOB_READ_TYPE(blob_read_intptr, intptr_t)


char * char *
blob_read_string(struct blob_reader *blob) blob_read_string(struct blob_reader *blob)

+ 42
- 0
src/util/blob.h View File

const void *bytes, const void *bytes,
size_t to_write); size_t to_write);


/**
* Add a uint8_t to a blob.
*
* \return True unless allocation failed.
*/
bool
blob_write_uint8(struct blob *blob, uint8_t value);

/**
* Add a uint16_t to a blob.
*
* \note This function will only write to a uint16_t-aligned offset from the
* beginning of the blob's data, so some padding bytes may be added to the
* blob if this write follows some unaligned write (such as
* blob_write_string).
*
* \return True unless allocation failed.
*/
bool
blob_write_uint16(struct blob *blob, uint16_t value);

/** /**
* Add a uint32_t to a blob. * Add a uint32_t to a blob.
* *
void void
blob_skip_bytes(struct blob_reader *blob, size_t size); blob_skip_bytes(struct blob_reader *blob, size_t size);


/**
* Read a uint8_t from the current location, (and update the current location
* to just past this uint8_t).
*
* \return The uint8_t read
*/
uint8_t
blob_read_uint8(struct blob_reader *blob);

/**
* Read a uint16_t from the current location, (and update the current location
* to just past this uint16_t).
*
* \note This function will only read from a uint16_t-aligned offset from the
* beginning of the blob's data, so some padding bytes may be skipped.
*
* \return The uint16_t read
*/
uint16_t
blob_read_uint16(struct blob_reader *blob);

/** /**
* Read a uint32_t from the current location, (and update the current location * Read a uint32_t from the current location, (and update the current location
* to just past this uint32_t). * to just past this uint32_t).

Loading…
Cancel
Save