diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-20 22:17:20 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-20 22:17:20 +0200 |
commit | 1a6b70f3702d3bdb0f1cb8e4669ec5ed21ea923b (patch) | |
tree | e35835e7497b12e96dbddd072b2fdd189f448990 /lib/sqfshelper | |
parent | e43be03653011f507901986cd9ff559cf20eb9cc (diff) |
Split data reader constructor and fragment table loading
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfshelper')
-rw-r--r-- | lib/sqfshelper/data_reader.c | 65 |
1 files changed, 36 insertions, 29 deletions
diff --git a/lib/sqfshelper/data_reader.c b/lib/sqfshelper/data_reader.c index 9a31fa4..d054069 100644 --- a/lib/sqfshelper/data_reader.c +++ b/lib/sqfshelper/data_reader.c @@ -130,51 +130,58 @@ static int precache_fragment_block(data_reader_t *data, size_t idx) return 0; } -data_reader_t *data_reader_create(sqfs_file_t *file, sqfs_super_t *super, +data_reader_t *data_reader_create(sqfs_file_t *file, size_t block_size, sqfs_compressor_t *cmp) { - data_reader_t *data = alloc_flex(sizeof(*data), 1, super->block_size); - size_t i, size; + data_reader_t *data = alloc_flex(sizeof(*data), 1, block_size); + + if (data != NULL) { + data->file = file; + data->block_size = block_size; + data->cmp = cmp; + } + + return data; +} + +int data_reader_load_fragment_table(data_reader_t *data, + const sqfs_super_t *super) +{ void *raw_frag; + size_t size; + uint32_t i; int ret; - if (data == NULL) { - perror("creating data reader"); - return data; - } + free(data->frag_block); + free(data->frag); - data->num_fragments = super->fragment_entry_count; - data->current_frag_index = super->fragment_entry_count; - data->file = file; - data->block_size = super->block_size; - data->cmp = cmp; + data->frag = NULL; + data->frag_block = NULL; + data->num_fragments = 0; + data->current_frag_index = 0; if (super->fragment_entry_count == 0 || (super->flags & SQFS_FLAG_NO_FRAGMENTS) != 0) { - return data; + return 0; } - if (super->fragment_table_start >= super->bytes_used) { - fputs("Fragment table start is past end of file\n", stderr); - free(data); - return NULL; - } + if (super->fragment_table_start >= super->bytes_used) + return SQFS_ERROR_OUT_OF_BOUNDS; - if (SZ_MUL_OV(sizeof(data->frag[0]), data->num_fragments, &size)) { - fputs("Too many fragments: overflow\n", stderr); - free(data); - return NULL; + if (SZ_MUL_OV(sizeof(data->frag[0]), super->fragment_entry_count, + &size)) { + return SQFS_ERROR_OVERFLOW; } - ret = sqfs_read_table(file, cmp, size, super->fragment_table_start, + ret = sqfs_read_table(data->file, data->cmp, size, + super->fragment_table_start, super->directory_table_start, super->fragment_table_start, &raw_frag); + if (ret) + return ret; - if (ret) { - free(data); - return NULL; - } - + data->num_fragments = super->fragment_entry_count; + data->current_frag_index = super->fragment_entry_count; data->frag = raw_frag; for (i = 0; i < data->num_fragments; ++i) { @@ -183,7 +190,7 @@ data_reader_t *data_reader_create(sqfs_file_t *file, sqfs_super_t *super, le64toh(data->frag[i].start_offset); } - return data; + return 0; } void data_reader_destroy(data_reader_t *data) |