cbfstool: add struct buffer helper routines

There are some open-coded manipulation of the struct buffer
innards in the elf parsing code. Add helper functions to avoid
reaching into the struct itself.

Change-Id: I0d5300afa1a3549f87f588f976184e880d071682
Signed-off-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: http://review.coreboot.org/5367
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
This commit is contained in:
Aaron Durbin 2014-03-04 22:01:12 -06:00 committed by Aaron Durbin
parent 1240d29209
commit 6e8c2790bb
1 changed files with 43 additions and 0 deletions

View File

@ -48,6 +48,49 @@ struct buffer {
size_t size;
};
static inline void *buffer_get(const struct buffer *b)
{
return b->data;
}
static inline size_t buffer_size(const struct buffer *b)
{
return b->size;
}
static inline void buffer_set_size(struct buffer *b, size_t size)
{
b->size = size;
}
/*
* Splice a buffer into another buffer. If size is zero the entire buffer
* is spliced while if size is non-zero the buffer is spliced starting at
* offset for size bytes. Note that it's up to caller to bounds check.
*/
static inline void buffer_splice(struct buffer *dest, const struct buffer *src,
size_t offset, size_t size)
{
dest->name = src->name;
dest->data = src->data;
dest->size = src->size;
if (size != 0) {
dest->data += offset;
buffer_set_size(dest, size);
}
}
static inline void buffer_clone(struct buffer *dest, const struct buffer *src)
{
buffer_splice(dest, src, 0, 0);
}
static inline void buffer_seek(struct buffer *b, size_t size)
{
b->size -= size;
b->data += size;
}
/* Creates an empty memory buffer with given size.
* Returns 0 on success, otherwise non-zero. */
int buffer_create(struct buffer *buffer, size_t size, const char *name);