summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2012-05-19 13:53:13 +0200
committerAmaury Pouly <amaury.pouly@gmail.com>2012-05-19 16:10:52 +0200
commitb62ec2ba809f093f299111cf263ce51ca4d4a665 (patch)
tree2441ca6d9bb45faa2a9c8b3d542524edb98b9355
parentb422be9bd0ddf517c6e7b804cb161f843939b1e0 (diff)
downloadrockbox-b62ec2ba809f093f299111cf263ce51ca4d4a665.tar.gz
rockbox-b62ec2ba809f093f299111cf263ce51ca4d4a665.tar.bz2
rockbox-b62ec2ba809f093f299111cf263ce51ca4d4a665.zip
imxtools: make sb api work with in-memory files
Change-Id: I517244802efb118be214437bbaea2ef811e87aea
-rw-r--r--utils/imxtools/sb.c46
-rw-r--r--utils/imxtools/sb.h5
2 files changed, 39 insertions, 12 deletions
diff --git a/utils/imxtools/sb.c b/utils/imxtools/sb.c
index 8e4b98b1a9..db6763c400 100644
--- a/utils/imxtools/sb.c
+++ b/utils/imxtools/sb.c
@@ -630,32 +630,55 @@ static uint32_t guess_alignment(uint32_t off)
struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u,
sb_color_printf cprintf, enum sb_error_t *err)
{
- uint8_t *buf = NULL;
- struct sb_file_t *sb_file = NULL;
-
- #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
+ return sb_read_file_ex(filename, 0, -1, raw_mode, u, cprintf, err);
+}
+
+struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t size, bool raw_mode, void *u,
+ sb_color_printf cprintf, enum sb_error_t *err)
+{
#define fatal(e, ...) \
do { if(err) *err = e; \
cprintf(u, true, GREY, __VA_ARGS__); \
free(buf); \
- sb_free(sb_file); \
return NULL; } while(0)
- #define print_hex(c, p, len, nl) \
- do { printf(c, ""); print_hex(p, len, nl); } while(0)
FILE *f = fopen(filename, "rb");
+ void *buf = NULL;
if(f == NULL)
fatal(SB_OPEN_ERROR, "Cannot open file for reading\n");
fseek(f, 0, SEEK_END);
- long filesize = ftell(f);
- fseek(f, 0, SEEK_SET);
- buf = xmalloc(filesize);
- if(fread(buf, filesize, 1, f) != 1)
+ size_t read_size = ftell(f);
+ fseek(f, offset, SEEK_SET);
+ if(size != (size_t)-1)
+ read_size = size;
+ buf = xmalloc(read_size);
+ if(fread(buf, read_size, 1, f) != 1)
{
fclose(f);
fatal(SB_READ_ERROR, "Cannot read file\n");
}
fclose(f);
+
+ struct sb_file_t *ret = sb_read_memory(buf, read_size, raw_mode, u, cprintf, err);
+ free(buf);
+ return ret;
+
+ #undef fatal
+}
+
+struct sb_file_t *sb_read_memory(void *buf, size_t filesize, bool raw_mode, void *u,
+ sb_color_printf cprintf, enum sb_error_t *err)
+{
+ struct sb_file_t *sb_file = NULL;
+
+ #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
+ #define fatal(e, ...) \
+ do { if(err) *err = e; \
+ cprintf(u, true, GREY, __VA_ARGS__); \
+ sb_free(sb_file); \
+ return NULL; } while(0)
+ #define print_hex(c, p, len, nl) \
+ do { printf(c, ""); print_hex(p, len, nl); } while(0)
struct sha_1_params_t sha_1_params;
sb_file = xmalloc(sizeof(struct sb_file_t));
@@ -1048,7 +1071,6 @@ struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u,
printf(RED, " Failed\n");
fatal(SB_CHECKSUM_ERROR, "File SHA-1 error\n");
}
- free(buf);
return sb_file;
#undef printf
diff --git a/utils/imxtools/sb.h b/utils/imxtools/sb.h
index e0f25b68a4..74d86f303a 100644
--- a/utils/imxtools/sb.h
+++ b/utils/imxtools/sb.h
@@ -228,6 +228,11 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename);
typedef void (*sb_color_printf)(void *u, bool err, color_t c, const char *f, ...);
struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u,
sb_color_printf printf, enum sb_error_t *err);
+/* use size_t(-1) to use maximum size */
+struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t size, bool raw_mode, void *u,
+ sb_color_printf printf, enum sb_error_t *err);
+struct sb_file_t *sb_read_memory(void *buffer, size_t size, bool raw_mode, void *u,
+ sb_color_printf printf, enum sb_error_t *err);
void sb_fill_section_name(char name[5], uint32_t identifier);
void sb_dump(struct sb_file_t *file, void *u, sb_color_printf printf);