Skip to content

Commit

Permalink
reader: make offset uint64_t instead of platform dependend sqsh_index_t
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Aug 12, 2024
1 parent e282d24 commit 17099d7
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 34 deletions.
6 changes: 3 additions & 3 deletions common/include/sqsh_reader_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extern "C" {

struct SqshReaderIteratorImpl {
bool (*next)(void *iterator, size_t desired_size, int *err);
int (*skip)(void *iterator, sqsh_index_t *offset, size_t desired_size);
int (*skip)(void *iterator, uint64_t *offset, size_t desired_size);
const uint8_t *(*data)(const void *iterator);
size_t (*size)(const void *iterator);
};
Expand Down Expand Up @@ -151,8 +151,8 @@ SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__reader_init(
*
* @return 0 on success, less than zero on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__reader_advance(
struct SqshReader *reader, sqsh_index_t offset, size_t size);
SQSH_NO_EXPORT SQSH_NO_UNUSED int
sqsh__reader_advance(struct SqshReader *reader, uint64_t offset, size_t size);

/**
* @internal
Expand Down
13 changes: 6 additions & 7 deletions common/src/reader/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ reader_fill_buffer(struct SqshReader *reader, size_t size) {
}

static int
handle_buffered(struct SqshReader *reader, sqsh_index_t offset, size_t size) {
handle_buffered(struct SqshReader *reader, uint64_t offset, size_t size) {
int rv = 0;
struct CxBuffer new_buffer = {0};
sqsh_index_t iterator_offset = reader->iterator_offset;

struct CxBuffer *buffer = &reader->buffer;
const uint8_t *buffer_data = cx_buffer_data(buffer);
size_t buffer_size = cx_buffer_size(buffer);
const size_t copy_size = buffer_size - offset;
const size_t copy_size = buffer_size - (size_t)offset;

if (offset != 0) {
rv = cx_buffer_init(&new_buffer);
Expand All @@ -152,7 +152,7 @@ handle_buffered(struct SqshReader *reader, sqsh_index_t offset, size_t size) {
}

static int
handle_mapped(struct SqshReader *reader, sqsh_index_t offset, size_t size) {
handle_mapped(struct SqshReader *reader, uint64_t offset, size_t size) {
int rv = 0;
void *iterator = reader->iterator;
const struct SqshReaderIteratorImpl *impl = reader->iterator_impl;
Expand All @@ -168,8 +168,8 @@ handle_mapped(struct SqshReader *reader, sqsh_index_t offset, size_t size) {
}
reader->iterator_size = impl->size(iterator);

reader->offset = offset;
sqsh_index_t end_offset;
reader->offset = (size_t)offset;
size_t end_offset;
if (SQSH_ADD_OVERFLOW(offset, size, &end_offset)) {
rv = -SQSH_ERROR_INTEGER_OVERFLOW;
goto out;
Expand All @@ -189,8 +189,7 @@ handle_mapped(struct SqshReader *reader, sqsh_index_t offset, size_t size) {
}

int
sqsh__reader_advance(
struct SqshReader *reader, sqsh_index_t offset, size_t size) {
sqsh__reader_advance(struct SqshReader *reader, uint64_t offset, size_t size) {
if (offset >= reader->iterator_offset) {
offset -= reader->iterator_offset;
reader->iterator_offset = 0;
Expand Down
2 changes: 1 addition & 1 deletion doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ struct SqshFile *file = sqsh_open(archive, "/path/to/file.txt", &err);
assert(err == 0);
struct SqshFileReader *reader = sqsh_file_reader_new(file, &err);
assert(err == 0);
err = sqsh_file_reader_advance(reader, 0, 10);
err = sqsh_file_reader_advance2(reader, 0, 10);
assert(err == 0);

const uint8_t *data = sqsh_file_reader_data(reader);
Expand Down
2 changes: 1 addition & 1 deletion fuzzer/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ read_file(const struct SqshTreeTraversal *traversal) {
if (chunk_size > size) {
chunk_size = size;
}
rv = sqsh_file_reader_advance(file, advance, chunk_size);
rv = sqsh_file_reader_advance2(file, advance, chunk_size);
if (rv < 0) {
goto out;
}
Expand Down
62 changes: 60 additions & 2 deletions include/sqsh_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct SqshFileReader *
sqsh_file_reader_new(const struct SqshFile *file, int *err);

/**
* @deprecated Since 1.5.0. Use sqsh_file_reader_advance2() instead.
* @brief Advances the file reader by a certain amount of data and presents
* `size` bytes of data to the user.
* @memberof SqshFileReader
Expand All @@ -76,9 +77,25 @@ sqsh_file_reader_new(const struct SqshFile *file, int *err);
*
* @return 0 on success, less than 0 on error.
*/
int sqsh_file_reader_advance(
__attribute__((deprecated(
"Since 1.5.0. Use sqsh_file_reader_advance2() instead."))) int
sqsh_file_reader_advance(
struct SqshFileReader *reader, sqsh_index_t offset, size_t size);

/**
* @brief Advances the file reader by a certain amount of data and presents
* `size` bytes of data to the user.
* @memberof SqshFileReader
*
* @param[in,out] reader The file reader to skip data in.
* @param[in] offset The offset to skip.
* @param[in] size The size of the data to skip.
*
* @return 0 on success, less than 0 on error.
*/
int sqsh_file_reader_advance2(
struct SqshFileReader *reader, uint64_t offset, size_t size);

/**
* @brief Gets a pointer to the current data in the file reader.
* @memberof SqshFileReader
Expand Down Expand Up @@ -145,6 +162,7 @@ SQSH_NO_UNUSED bool sqsh_file_iterator_next(
struct SqshFileIterator *iterator, size_t desired_size, int *err);

/**
* @deprecated Since 1.5.0. Use sqsh_file_iterator_skip2() instead.
* @memberof SqshFileIterator
* @brief Skips blocks until the block containing the offset is reached.
* Note that calling this function will invalidate the data pointer returned by
Expand Down Expand Up @@ -177,10 +195,50 @@ SQSH_NO_UNUSED bool sqsh_file_iterator_next(
*
* @return 0 on success, less than 0 on error.
*/
SQSH_NO_UNUSED int sqsh_file_iterator_skip(
__attribute__((
deprecated("Since 1.5.0. Use sqsh_file_iterator_skip2() instead.")))
SQSH_NO_UNUSED int
sqsh_file_iterator_skip(
struct SqshFileIterator *iterator, sqsh_index_t *offset,
size_t desired_size);

/**
* @memberof SqshFileIterator
* @brief Skips blocks until the block containing the offset is reached.
* Note that calling this function will invalidate the data pointer returned by
* sqsh_file_iterator_data().
*
* The offset is relative to the beginning of the current block or, if the
* iterator hasn't been forwarded with previous calls to
* sqsh_file_iterator_skip() or sqsh_file_iterator_next() the beginning of the
* first block.
*
* After calling this function `offset` is updated to the same position relative
* to the new block. See this visualisation:
*
* ```
* current_block: |<--- block 8000 --->|
* offset = 10000 --^
* --> sqsh_file_iterator_skip(i, &offset, 1)
* current_block: |<--- block 8000 --->|
* offset = 2000 --^
* ```
*
* If libsqsh can map more than one block at once, it will do so until
* `desired_size` is reached. Note that `desired_size` is only a hint and
* libsqsh may return more or less data than requested.
*
* @param[in,out] iterator The file iterator to skip data in.
* @param[in,out] offset The offset that is contained in the block to
* skip to.
* @param[in] desired_size The desired size of the data to read.
*
* @return 0 on success, less than 0 on error.
*/
SQSH_NO_UNUSED int sqsh_file_iterator_skip2(
struct SqshFileIterator *iterator, uint64_t *offset,
size_t desired_size);

/**
* @brief Gets a pointer to the current data in the file iterator.
* @memberof SqshFileIterator
Expand Down
4 changes: 2 additions & 2 deletions libsqsh/include/sqsh_mapper_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ sqsh__map_iterator_next(struct SqshMapIterator *iterator, int *err);
* @return 0 on success, less than 0 on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int
sqsh__map_iterator_skip(struct SqshMapIterator *iterator, sqsh_index_t *offset);
sqsh__map_iterator_skip(struct SqshMapIterator *iterator, uint64_t *offset);

/**
* @internal
Expand Down Expand Up @@ -437,7 +437,7 @@ sqsh__map_reader_address(const struct SqshMapReader *reader);
* @return 0 on success, negative on error
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__map_reader_advance(
struct SqshMapReader *reader, sqsh_index_t offset, size_t size);
struct SqshMapReader *reader, uint64_t offset, size_t size);

/**
* @internal
Expand Down
4 changes: 2 additions & 2 deletions libsqsh/include/sqsh_metablock_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ sqsh__metablock_iterator_next(struct SqshMetablockIterator *iterator, int *err);
* @return 0 on success, less than 0 on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__metablock_iterator_skip(
struct SqshMetablockIterator *iterator, sqsh_index_t *offset);
struct SqshMetablockIterator *iterator, uint64_t *offset);

/**
* @internal
Expand Down Expand Up @@ -184,7 +184,7 @@ SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__metablock_reader_init(
* @return 0 on success, less than zero on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__metablock_reader_advance(
struct SqshMetablockReader *reader, sqsh_index_t offset, size_t size);
struct SqshMetablockReader *reader, uint64_t offset, size_t size);

/**
* @internal
Expand Down
12 changes: 11 additions & 1 deletion libsqsh/src/file/file_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ int
sqsh_file_iterator_skip(
struct SqshFileIterator *iterator, sqsh_index_t *offset,
size_t desired_size) {
uint64_t offset64 = *offset;
int rv = sqsh_file_iterator_skip2(iterator, &offset64, desired_size);
*offset = (size_t)offset64;
return rv;
}

int
sqsh_file_iterator_skip2(
struct SqshFileIterator *iterator, uint64_t *offset,
size_t desired_size) {
int rv = 0;
const size_t block_size = iterator->block_size;
const size_t current_block_size = sqsh_file_iterator_size(iterator);
Expand All @@ -315,7 +325,7 @@ sqsh_file_iterator_skip(

*offset -= current_block_size;

sqsh_index_t skip_index = *offset / block_size;
uint64_t skip_index = *offset / block_size;
if (current_block_size != 0) {
skip_index += 1;
}
Expand Down
10 changes: 8 additions & 2 deletions libsqsh/src/file/file_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ file_iterator_next(void *iterator, size_t desired_size, int *err) {
return sqsh_file_iterator_next(iterator, desired_size, err);
}
static int
file_iterator_skip(void *iterator, sqsh_index_t *offset, size_t desired_size) {
return sqsh_file_iterator_skip(iterator, offset, desired_size);
file_iterator_skip(void *iterator, uint64_t *offset, size_t desired_size) {
return sqsh_file_iterator_skip2(iterator, offset, desired_size);
}
static const uint8_t *
file_iterator_data(const void *iterator) {
Expand Down Expand Up @@ -88,6 +88,12 @@ sqsh_file_reader_advance(
return sqsh__reader_advance(&reader->reader, offset, size);
}

int
sqsh_file_reader_advance2(
struct SqshFileReader *reader, uint64_t offset, size_t size) {
return sqsh__reader_advance(&reader->reader, offset, size);
}

const uint8_t *
sqsh_file_reader_data(const struct SqshFileReader *reader) {
return sqsh__reader_data(&reader->reader);
Expand Down
12 changes: 7 additions & 5 deletions libsqsh/src/mapper/map_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ sqsh__map_iterator_init(
return 0;
}

#include <stdio.h>
int
sqsh__map_iterator_skip(
struct SqshMapIterator *iterator, sqsh_index_t *offset) {
sqsh__map_iterator_skip(struct SqshMapIterator *iterator, uint64_t *offset) {
int rv = 0;
sqsh_index_t index;
uint64_t index;
size_t block_size = sqsh__map_manager_block_size(iterator->map_manager);

size_t current_size = sqsh__map_iterator_size(iterator);
Expand All @@ -88,8 +86,12 @@ sqsh__map_iterator_skip(
*/
index--;
}
if (index > SIZE_MAX) {
rv = -SQSH_ERROR_INTEGER_OVERFLOW;
goto out;
}

iterator->next_index = index;
iterator->next_index = (size_t)index;
bool has_next = sqsh__map_iterator_next(iterator, &rv);
if (rv < 0) {
goto out;
Expand Down
4 changes: 2 additions & 2 deletions libsqsh/src/mapper/map_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ map_iterator_next(void *iterator, size_t desired_size, int *err) {
return sqsh__map_iterator_next(iterator, err);
}
static int
map_iterator_skip(void *iterator, sqsh_index_t *offset, size_t desired_size) {
map_iterator_skip(void *iterator, uint64_t *offset, size_t desired_size) {
(void)desired_size;
return sqsh__map_iterator_skip(iterator, offset);
}
Expand Down Expand Up @@ -110,7 +110,7 @@ sqsh__map_reader_address(const struct SqshMapReader *reader) {

int
sqsh__map_reader_advance(
struct SqshMapReader *reader, sqsh_index_t offset, size_t size) {
struct SqshMapReader *reader, uint64_t offset, size_t size) {
reader->address += offset;
return sqsh__reader_advance(&reader->reader, offset, size);
}
Expand Down
2 changes: 1 addition & 1 deletion libsqsh/src/metablock/metablock_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ sqsh__metablock_iterator_next(

int
sqsh__metablock_iterator_skip(
struct SqshMetablockIterator *iterator, sqsh_index_t *offset) {
struct SqshMetablockIterator *iterator, uint64_t *offset) {
int rv = 0;

size_t current_size = sqsh__metablock_iterator_size(iterator);
Expand Down
5 changes: 2 additions & 3 deletions libsqsh/src/metablock/metablock_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ metablock_iterator_next(void *iterator, size_t desired_size, int *err) {
return sqsh__metablock_iterator_next(iterator, err);
}
static int
metablock_iterator_skip(
void *iterator, sqsh_index_t *offset, size_t desired_size) {
metablock_iterator_skip(void *iterator, uint64_t *offset, size_t desired_size) {
(void)desired_size;
return sqsh__metablock_iterator_skip(iterator, offset);
}
Expand Down Expand Up @@ -85,7 +84,7 @@ sqsh__metablock_reader_init(

int
sqsh__metablock_reader_advance(
struct SqshMetablockReader *reader, sqsh_index_t offset, size_t size) {
struct SqshMetablockReader *reader, uint64_t offset, size_t size) {
return sqsh__reader_advance(&reader->reader, offset, size);
}

Expand Down
2 changes: 1 addition & 1 deletion test/libsqsh/reader/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test_iter_next(void *data, size_t desired_size, int *err) {
}

static int
test_iter_skip(void *iterator, sqsh_index_t *offset, size_t desired_size) {
test_iter_skip(void *iterator, uint64_t *offset, size_t desired_size) {
int rv = 0;

size_t current_size = test_iter_size(iterator);
Expand Down
2 changes: 1 addition & 1 deletion tools/src/fs-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fs_common_read(
return 0;
}

rv = sqsh_file_reader_advance(*reader, offset, size);
rv = sqsh_file_reader_advance2(*reader, offset, size);
if (rv < 0) {
goto out;
}
Expand Down

0 comments on commit 17099d7

Please sign in to comment.