# HG changeset patch # User Matti Hamalainen # Date 1352426342 -7200 # Node ID 051ef70d01236e546318118b2f83a0fb2cb4adba # Parent 6b47d9813067f249c6aab3136f7b629b10ffc8bb Improve xs_fload_buffer() and add xs_fload_buffer_path(). diff -r 6b47d9813067 -r 051ef70d0123 src/xs_support.c --- a/src/xs_support.c Fri Nov 09 03:57:59 2012 +0200 +++ b/src/xs_support.c Fri Nov 09 03:59:02 2012 +0200 @@ -63,56 +63,71 @@ /* Load a file to a buffer, return 0 on success, negative value on error */ -gint xs_fload_buffer(const gchar *filename, guint8 **buf, size_t *bufSize) +gboolean xs_fload_buffer(const gchar *filename, + guint8 **pbuf, size_t *bufSize, const size_t maxSize, gboolean failMaxSize) { - XSFile *f; - glong seekPos; + XSFile *fp = NULL; + size_t readSize, fileSize; + gboolean res = FALSE; if (filename == NULL) - return -15; + return FALSE; - if ((f = xs_fopen(filename, "rb")) == NULL) - return -1; + if ((fp = xs_fopen(filename, "rb")) == NULL) + { + xs_error("Could not open '%s' for reading.\n", filename); + goto error; + } - xs_fseek(f, 0, SEEK_END); - seekPos = xs_ftell(f); - - if (seekPos > 0) + fileSize = xs_fsize(fp); + if (failMaxSize && fileSize > maxSize) + { + xs_error("File '%s' size %d exceeds maxSize %d.\n", + fileSize, maxSize); + goto error; + } + + readSize = fileSize < maxSize ? fileSize : maxSize; + + if ((*pbuf = (guint8 *) g_malloc(readSize * sizeof(guint8))) == NULL) { - size_t readSize = seekPos; - if (readSize >= *bufSize || *buf == NULL) - { - /* Only re-allocate if the required size > current */ - if (*buf != NULL) { - g_free(*buf); - *buf = NULL; - } - - *bufSize = seekPos; - - *buf = (guint8 *) g_malloc(*bufSize * sizeof(guint8)); - if (*buf == NULL) { - xs_fclose(f); - return -2; - } - } - - /* Read data */ - xs_fseek(f, 0, SEEK_SET); - readSize = xs_fread(*buf, sizeof(guint8), *bufSize, f); - xs_fclose(f); - - if (readSize != *bufSize) - return -3; - else - return 0; - } else { - xs_fclose(f); - return -4; + xs_error("Could not allocate %d bytes for filebuffer '%s'.\n", + readSize, filename); + goto error; } + + *bufSize = xs_fread(*pbuf, sizeof(guint8), readSize, fp); + res = (readSize == *bufSize); + +error: + if (fp != NULL) + xs_fclose(fp); + + if (!res) + { + xs_error("File '%s', expected %d bytes, read %d bytes.\n", + readSize, *bufSize); + } + + return res; } +gboolean xs_fload_buffer_path(const gchar *ppath, const gchar *pfilename, + guint8 **pbuf, size_t *bufSize, const size_t maxSize, gboolean failMaxSize) +{ + gchar *filename = g_strdup_printf("%s%s", ppath != NULL ? ppath : "", pfilename); + gboolean res; + + if (filename == NULL) + return FALSE; + + res = xs_fload_buffer(filename, pbuf, bufSize, maxSize, failMaxSize); + + g_free(filename); + return res; +} + /* Copy a given string over in *result. */ gint xs_pstrcpy(gchar **result, const gchar *str) @@ -122,8 +137,8 @@ return -1; /* Allocate memory for destination */ - if (*result) - g_free(*result); + g_free(*result); + *result = (gchar *) g_malloc(strlen(str) + 1); if (!*result) return -2; @@ -143,12 +158,15 @@ if (!result || !str) return -1; - if (*result != NULL) { + if (*result != NULL) + { *result = (gchar *) g_realloc(*result, strlen(*result) + strlen(str) + 1); if (*result == NULL) return -1; strcat(*result, str); - } else { + } + else + { *result = (gchar *) g_malloc(strlen(str) + 1); if (*result == NULL) return -1; diff -r 6b47d9813067 -r 051ef70d0123 src/xs_support.h --- a/src/xs_support.h Fri Nov 09 03:57:59 2012 +0200 +++ b/src/xs_support.h Fri Nov 09 03:59:02 2012 +0200 @@ -116,7 +116,10 @@ #endif guint16 xs_fread_be16(XSFile *); guint32 xs_fread_be32(XSFile *); -gint xs_fload_buffer(const gchar *, guint8 **, size_t *); +gboolean xs_fload_buffer(const gchar *filename, + guint8 **pbuf, size_t *bufSize, const size_t maxSize, gboolean failMaxSize); +gboolean xs_fload_buffer_path(const gchar *ppath, const gchar *pfilename, + guint8 **pbuf, size_t *bufSize, const size_t maxSize, gboolean failMaxSize); /* Misc functions