diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/cbfstool/common.c | 63 | ||||
-rw-r--r-- | util/cbfstool/common.h | 22 |
2 files changed, 85 insertions, 0 deletions
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c index d4560f691f..aa986963c1 100644 --- a/util/cbfstool/common.c +++ b/util/cbfstool/common.c @@ -40,6 +40,69 @@ int is_big_endian(void) return 0; } +/* Buffer and file I/O */ + +int buffer_create(struct buffer *buffer, size_t size, const char *name) { + buffer->name = strdup(name); + buffer->size = size; + buffer->data = (char *)malloc(buffer->size); + if (!buffer->data) { + fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n", + size); + } + return (buffer->data == NULL); +} + +int buffer_from_file(struct buffer *buffer, const char *filename) { + FILE *fp = fopen(filename, "rb"); + if (!fp) { + perror(filename); + return -1; + } + fseek(fp, 0, SEEK_END); + buffer->size = ftell(fp); + buffer->name = strdup(filename); + rewind(fp); + buffer->data = (char *)malloc(buffer->size); + assert(buffer->data); + if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) { + fprintf(stderr, "incomplete read: %s\n", filename); + fclose(fp); + return -1; + } + fclose(fp); + return 0; +} + +int buffer_write_file(struct buffer *buffer, const char *filename) { + FILE *fp = fopen(filename, "wb"); + if (!fp) { + perror(filename); + return -1; + } + assert(buffer && buffer->data); + if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) { + fprintf(stderr, "incomplete write: %s\n", filename); + fclose(fp); + return -1; + } + fclose(fp); + return 0; +} + +void buffer_delete(struct buffer *buffer) { + assert(buffer); + if (buffer->name) { + free(buffer->name); + buffer->name = NULL; + } + if (buffer->data) { + free(buffer->data); + buffer->data = NULL; + } + buffer->size = 0; +} + size_t getfilesize(const char *filename) { size_t size; diff --git a/util/cbfstool/common.h b/util/cbfstool/common.h index 5315e69bed..a859e103f2 100644 --- a/util/cbfstool/common.h +++ b/util/cbfstool/common.h @@ -21,6 +21,7 @@ #define __CBFSTOOL_COMMON_H #include <stdint.h> +#include <assert.h> /* Endianess */ #include "swab.h" @@ -40,6 +41,27 @@ extern int verbose; #define INFO(x...) { if (verbose > 0) fprintf(stderr, "INFO: " x); } #define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); } +/* Buffer and file I/O */ +struct buffer { + char *name; + char *data; + size_t 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); + +/* Loads a file into memory buffer. Returns 0 on success, otherwise non-zero. */ +int buffer_from_file(struct buffer *buffer, const char *filename); + +/* Writes memory buffer content into file. + * Returns 0 on success, otherwise non-zero. */ +int buffer_write_file(struct buffer *buffer, const char *filename); + +/* Destroys a memory buffer. */ +void buffer_delete(struct buffer *buffer); + extern void *offset; extern uint32_t romsize; extern int host_bigendian; |