summaryrefslogtreecommitdiff
path: root/util/cbfstool
diff options
context:
space:
mode:
Diffstat (limited to 'util/cbfstool')
-rw-r--r--util/cbfstool/common.c5
-rw-r--r--util/cbfstool/common.h22
2 files changed, 22 insertions, 5 deletions
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index e8c2ccc456..cdc04f333e 100644
--- a/util/cbfstool/common.c
+++ b/util/cbfstool/common.c
@@ -57,6 +57,7 @@ static off_t get_file_size(FILE *f)
int buffer_create(struct buffer *buffer, size_t size, const char *name)
{
buffer->name = strdup(name);
+ buffer->offset = 0;
buffer->size = size;
buffer->data = (char *)malloc(buffer->size);
if (!buffer->data) {
@@ -73,6 +74,7 @@ int buffer_from_file(struct buffer *buffer, const char *filename)
perror(filename);
return -1;
}
+ buffer->offset = 0;
buffer->size = get_file_size(fp);
if (buffer->size == -1u) {
fprintf(stderr, "could not determine size of %s\n", filename);
@@ -116,9 +118,10 @@ void buffer_delete(struct buffer *buffer)
buffer->name = NULL;
}
if (buffer->data) {
- free(buffer->data);
+ free(buffer->data - buffer->offset);
buffer->data = NULL;
}
+ buffer->offset = 0;
buffer->size = 0;
}
diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h
index 78ec40aaa1..0cf6b6e31f 100644
--- a/util/cbfstool/common.h
+++ b/util/cbfstool/common.h
@@ -59,6 +59,7 @@ static inline uint32_t align_up(uint32_t value, uint32_t align)
struct buffer {
char *name;
char *data;
+ size_t offset;
size_t size;
};
@@ -72,6 +73,9 @@ static inline size_t buffer_size(const struct buffer *b)
return b->size;
}
+/*
+ * Shrink a buffer toward the beginning of its previous space.
+ * Afterward, buffer_delete() remains the means of cleaning it up. */
static inline void buffer_set_size(struct buffer *b, size_t size)
{
b->size = size;
@@ -87,22 +91,32 @@ static inline void buffer_init(struct buffer *b, char *name, void *data,
}
/* Splice a buffer into another buffer. Note that it's up to the caller to
- * bounds check the offset and size. */
+ * bounds check the offset and size. The resulting buffer is backed by the same
+ * storage as the original, so although it is valid to buffer_delete() either
+ * one of them, doing so releases both simultaneously. */
static inline void buffer_splice(struct buffer *dest, const struct buffer *src,
size_t offset, size_t size)
{
- buffer_init(dest, src->name, src->data, src->size);
- dest->data += offset;
- buffer_set_size(dest, size);
+ dest->name = src->name;
+ dest->data = src->data + offset;
+ dest->offset = src->offset + offset;
+ dest->size = size;
}
+/*
+ * Shallow copy a buffer. To clean up the resources, buffer_delete()
+ * either one, but not both. */
static inline void buffer_clone(struct buffer *dest, const struct buffer *src)
{
buffer_splice(dest, src, 0, src->size);
}
+/*
+ * Shrink a buffer toward the end of its previous space.
+ * Afterward, buffer_delete() remains the means of cleaning it up. */
static inline void buffer_seek(struct buffer *b, size_t size)
{
+ b->offset += size;
b->size -= size;
b->data += size;
}