From 6e8c2790bbc9190cf8b7ff83787b5328a2b8828e Mon Sep 17 00:00:00 2001 From: Aaron Durbin Date: Tue, 4 Mar 2014 22:01:12 -0600 Subject: 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 Reviewed-on: http://review.coreboot.org/5367 Tested-by: build bot (Jenkins) Reviewed-by: Stefan Reinauer --- util/cbfstool/common.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'util/cbfstool/common.h') diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h index d0069c3d58..60ffb51816 100644 --- a/util/cbfstool/common.h +++ b/util/cbfstool/common.h @@ -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); -- cgit v1.2.3