Skip to content

Commit

Permalink
Merge pull request #281 from Gottox/fix/32bit-build
Browse files Browse the repository at this point in the history
chores: fix 32bit build with -Wconversion
  • Loading branch information
Gottox authored Aug 12, 2024
2 parents 2566f85 + 1f59646 commit a1d880f
Show file tree
Hide file tree
Showing 42 changed files with 373 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ jobs:
run: |
echo "$CROSS_FILE" > cross-file.txt
meson setup --cross-file cross-file.txt /tmp/build \
-Dwerror=false \
-Dwerror=true \
-Dexamples=true \
-Dtest=false \
-Ddoc=false
Expand Down
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
4 changes: 2 additions & 2 deletions doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct SqshArchive *archive = sqsh_archive_open("archive.squashfs", NULL, NULL);
assert(archive != NULL);

uint8_t *content = sqsh_easy_file_content(archive, "/path/to/file.txt", NULL);
size_t size = sqsh_easy_file_size(archive, "/path/to/file.txt", NULL);
uint64_t size = sqsh_easy_file_size2(archive, "/path/to/file.txt", NULL);
fwrite(content, 1, size, stdout);
free(content);

Expand Down 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 examples/read_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main(int argc, char *argv[]) {

uint8_t *content = sqsh_easy_file_content(archive, argv[2], NULL);
assert(content != NULL);
size_t size = sqsh_easy_file_size(archive, argv[2], NULL);
size_t size = sqsh_easy_file_size2(archive, argv[2], NULL);

fwrite(content, size, 1, stdout);

Expand Down
2 changes: 1 addition & 1 deletion examples/readme_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ main(int argc, char *argv[]) {

uint8_t *contents = sqsh_easy_file_content(archive, "/path/to/file", NULL);
assert(contents != NULL);
const size_t size = sqsh_easy_file_size(archive, "/path/to/file", NULL);
const size_t size = sqsh_easy_file_size2(archive, "/path/to/file", NULL);
fwrite(contents, 1, size, stdout);
free(contents);

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
28 changes: 28 additions & 0 deletions include/sqsh_easy.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ sqsh_easy_file_content(struct SqshArchive *archive, const char *path, int *err);
*
* @return The size of the file on success, 0 on error.
*/
uint64_t
sqsh_easy_file_size2(struct SqshArchive *archive, const char *path, int *err);

/**
* @deprecated Since 1.5.0. Use sqsh_easy_file_size2() instead.
* @brief retrieves the size of a file.
*
* @param[in] archive The sqsh archive context.
* @param[in] path The path the file or directory.
* @param[out] err Pointer to an int where the error code will be stored.
*
* @return The size of the file on success, 0 on error.
*/
__attribute__((deprecated("Since 1.5.0. Use sqsh_easy_file_size2() instead.")))
size_t
sqsh_easy_file_size(struct SqshArchive *archive, const char *path, int *err);

Expand All @@ -111,6 +125,20 @@ mode_t sqsh_easy_file_permission(
*
* @return The modification time of the file on success, 0 on error.
*/
uint32_t
sqsh_easy_file_mtime2(struct SqshArchive *archive, const char *path, int *err);

/**
* @deprecated Since 1.5.0. Use sqsh_easy_file_mtime2() instead.
* @brief retrieves the modification time of a file.
*
* @param[in] archive The sqsh archive context.
* @param[in] path The path the file or directory.
* @param[out] err Pointer to an int where the error code will be stored.
*
* @return The modification time of the file on success, 0 on error.
*/
__attribute__((deprecated("Since 1.5.0. Use sqsh_easy_file_mtime2() instead.")))
time_t
sqsh_easy_file_mtime(struct SqshArchive *archive, const char *path, int *err);

Expand Down
79 changes: 76 additions & 3 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 Expand Up @@ -366,6 +424,21 @@ uint32_t sqsh_file_modified_time(const struct SqshFile *context);
*/
uint64_t sqsh_file_blocks_start(const struct SqshFile *context);

/**
* @deprecated Since 1.5.0. Use sqsh_file_blockcount2() instead.
* @memberof SqshFile
* @brief Getter for the amount of blocks of the file content. This is only
* internally used and will be used while retrieving the file content.
*
* @param[in] context The file context.
*
* @return the amount of blocks of the file content. If the file is not of
* of type SQSH_FILE_TYPE_FILE, UINT32_MAX will be returned.
*/
__attribute__((deprecated("Since 1.5.0. Use sqsh_file_blockcount2() instead.")))
uint32_t
sqsh_file_block_count(const struct SqshFile *context);

/**
* @memberof SqshFile
* @brief Getter for the amount of blocks of the file content. This is only
Expand All @@ -376,7 +449,7 @@ uint64_t sqsh_file_blocks_start(const struct SqshFile *context);
* @return the amount of blocks of the file content. If the file is not of
* of type SQSH_FILE_TYPE_FILE, UINT32_MAX will be returned.
*/
uint32_t sqsh_file_block_count(const struct SqshFile *context);
uint64_t sqsh_file_block_count2(const struct SqshFile *context);

/**
* @memberof SqshFile
Expand Down
36 changes: 35 additions & 1 deletion include/sqsh_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ struct SqshMemoryMapperImpl {
*/
size_t block_size_hint;
/**
* @deprecated Since 1.5.0. Use SqshMemoryMapperImpl::init2() instead.
* @brief The initialization function for the mapper. Use
* sqsh_mapper_set_user_data() to set custom user data.
*/
#ifndef SQSH__NO_DEPRECATED_FIELD
__attribute__((deprecated("Since 1.5.0. Use init2() instead.")))
#endif
int (*init)(struct SqshMapper *mapper, const void *input, size_t *size);
/**
* @deprecated Since 1.5.0. Use SqshMemoryMapperImpl::map2() instead.
* @brief The function that maps a block of data into memory.
*/
#ifndef SQSH__NO_DEPRECATED_FIELD
__attribute__((deprecated("Since 1.5.0. Use init2() instead.")))
#endif
int (*map)(
const struct SqshMapper *mapper, sqsh_index_t offset, size_t size,
uint8_t **data);
Expand All @@ -75,6 +83,18 @@ struct SqshMemoryMapperImpl {
* @brief The cleanup function for the mapper.
*/
int (*cleanup)(struct SqshMapper *mapper);
/**
* @brief The initialization function for the mapper. Use
* sqsh_mapper_set_user_data() to set custom user data.
*/

int (*init2)(struct SqshMapper *mapper, const void *input, uint64_t *size);
/**
* @brief The function that maps a block of data into memory.
*/
int (*map2)(
const struct SqshMapper *mapper, uint64_t offset, size_t size,
uint8_t **data);
};

/**
Expand All @@ -96,6 +116,20 @@ void sqsh_mapper_set_user_data(struct SqshMapper *mapper, void *user_data);
*/
void *sqsh_mapper_user_data(const struct SqshMapper *mapper);

/**
* @deprecated Since 1.5.0. Use sqsh_mapper_size2() instead.
* @internal
* @memberof SqshMapper
* @brief Retrieves the size of the input data in a mapper.
*
* @param[in] mapper The mapper to retrieve the size from.
*
* @return The size of the input data in the mapper.
*/
__attribute__((deprecated("Since 1.5.0. Use sqsh_mapper_size2() instead.")))
size_t
sqsh_mapper_size(const struct SqshMapper *mapper);

/**
* @internal
* @memberof SqshMapper
Expand All @@ -105,7 +139,7 @@ void *sqsh_mapper_user_data(const struct SqshMapper *mapper);
*
* @return The size of the input data in the mapper.
*/
size_t sqsh_mapper_size(const struct SqshMapper *mapper);
uint64_t sqsh_mapper_size2(const struct SqshMapper *mapper);

/**
* @internal
Expand Down
19 changes: 18 additions & 1 deletion include/sqsh_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,24 @@ struct SqshExportTable;
*
* @return 0 on success, a negative value on error.
*/
int sqsh_export_table_resolve_inode(
int sqsh_export_table_resolve_inode2(
const struct SqshExportTable *table, uint32_t inode,
uint64_t *inode_ref);

/**
* @deprecated Since 1.5.0. Use sqsh_export_table_resolve_inode2 instead.
* @memberof SqshTable
* @brief Retrieves an element from the table.
*
* @param[in] table The table to retrieve the element from.
* @param[in] inode The index of the element to retrieve.
* @param[out] inode_ref A pointer to a uint64_t to store the inode reference
*
* @return 0 on success, a negative value on error.
*/
__attribute__((deprecated(
"Since 1.5.0. Use sqsh_export_table_resolve_inode2 instead."))) int
sqsh_export_table_resolve_inode(
const struct SqshExportTable *table, uint64_t inode,
uint64_t *inode_ref);

Expand Down
2 changes: 1 addition & 1 deletion libsqsh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ assert(archive != NULL);

uint8_t *contents = sqsh_easy_file_content(archive, "/path/to/file", NULL);
assert(contents != NULL);
const size_t size = sqsh_easy_file_size(archive, "/path/to/file", NULL);
const uint64_t size = sqsh_easy_file_size2(archive, "/path/to/file", NULL);
fwrite(contents, 1, size, stdout);
free(contents);

Expand Down
Loading

0 comments on commit a1d880f

Please sign in to comment.