aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/blob.c
diff options
context:
space:
mode:
authorMarek Olšák <[email protected]>2019-11-19 19:36:36 -0500
committerMarek Olšák <[email protected]>2019-11-23 00:02:10 -0500
commit4fe1d7822b07bf78975c12a2e27db6e5817b1a6b (patch)
treef0fcdcb3775c194936a1b62b06e24a74f5545f3e /src/util/blob.c
parent59b489f44be8ae64a701568f2ae5247933e94fe7 (diff)
util/blob: add 8-bit and 16-bit reads and writes
Reviewed-by: Connor Abbott <[email protected]>
Diffstat (limited to 'src/util/blob.c')
-rw-r--r--src/util/blob.c101
1 files changed, 29 insertions, 72 deletions
diff --git a/src/util/blob.c b/src/util/blob.c
index 050df8351f8..94d5a9dea74 100644
--- a/src/util/blob.c
+++ b/src/util/blob.c
@@ -197,14 +197,20 @@ blob_reserve_intptr(struct blob *blob)
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) \
assert(ALIGN((_offset), (_align)) == (_offset))
@@ -218,22 +224,6 @@ blob_overwrite_uint32 (struct blob *blob,
}
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
blob_overwrite_intptr (struct blob *blob,
size_t offset,
intptr_t value)
@@ -313,59 +303,26 @@ blob_skip_bytes(struct blob_reader *blob, size_t size)
* 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;
+#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 *
blob_read_string(struct blob_reader *blob)