Skip to content

Commit

Permalink
extractor: make buffer an object variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Jul 22, 2023
1 parent 97ad3a0 commit f9624e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
12 changes: 7 additions & 5 deletions include/sqsh_extract_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct SqshExtractor {
/**
* @privatesection
*/
struct SqshBuffer *buffer;
const struct SqshExtractorImpl *impl;
size_t block_size;
};
Expand All @@ -101,29 +102,30 @@ struct SqshExtractor {
* @brief Initializes a extractor context.
*
* @param[out] extractor The context to initialize.
* @param[out] buffer The buffer to store the decompressed data.
* @param[in] algorithm_id The id of the compression algorithm to use.
* @param[in] block_size The block size to use for the extraction.
*
* @return 0 on success, a negative value on error.
*/
SQSH_NO_UNUSED int sqsh__extractor_init(
struct SqshExtractor *extractor, int algorithm_id, size_t block_size);
struct SqshExtractor *extractor, struct SqshBuffer *buffer,
int algorithm_id, size_t block_size);

/**
* @internal
* @memberof SqshExtractor
* @brief Decompresses data to a buffer.
*
* @param[in] extractor The extractor context to use.
* @param[out] buffer The buffer to store the decompressed data.
* @param[in] extractor The extractor context to use.
* @param[in] compressed The compressed data to decompress.
* @param[in] compressed_size The size of the compressed data.
*
* @return 0 on success, a negative value on error.
*/
SQSH_NO_UNUSED int sqsh__extractor_to_buffer(
const struct SqshExtractor *extractor, struct SqshBuffer *buffer,
const uint8_t *compressed, const size_t compressed_size);
const struct SqshExtractor *extractor, const uint8_t *compressed,
const size_t compressed_size);

/**
* @internal
Expand Down
5 changes: 3 additions & 2 deletions lib/extract/extract_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ sqsh__extract_manager_uncompress(
}
const uint8_t *data = sqsh__map_reader_data(reader);

rv = sqsh__extractor_init(&extractor, compression_id, block_size);
rv = sqsh__extractor_init(
&extractor, &buffer, compression_id, block_size);
if (rv < 0) {
goto out;
}

rv = sqsh__extractor_to_buffer(&extractor, &buffer, data, size);
rv = sqsh__extractor_to_buffer(&extractor, data, size);
if (rv < 0) {
sqsh__buffer_cleanup(&buffer);
goto out;
Expand Down
9 changes: 6 additions & 3 deletions lib/extract/extractor.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,29 @@ extractor_by_id(int id) {

int
sqsh__extractor_init(
struct SqshExtractor *extractor, int algorithm_id, size_t block_size) {
struct SqshExtractor *extractor, struct SqshBuffer *buffer,
int algorithm_id, size_t block_size) {
const struct SqshExtractorImpl *impl = extractor_by_id(algorithm_id);
if (impl == NULL) {
return -SQSH_ERROR_COMPRESSION_UNSUPPORTED;
}
extractor->impl = impl;
extractor->block_size = block_size;
extractor->buffer = buffer;
return 0;
}

int
sqsh__extractor_to_buffer(
const struct SqshExtractor *extractor, struct SqshBuffer *buffer,
const uint8_t *compressed, const size_t compressed_size) {
const struct SqshExtractor *extractor, const uint8_t *compressed,
const size_t compressed_size) {
int rv = 0;
size_t max_size = extractor->block_size;
size_t size = 0;
uint8_t *decompressed = NULL;
const struct SqshExtractorImpl *impl = extractor->impl;
sqsh__extractor_context_t extractor_context = {0};
struct SqshBuffer *buffer = extractor->buffer;

rv = sqsh__buffer_add_capacity(buffer, &decompressed, max_size);
if (rv < 0) {
Expand Down

0 comments on commit f9624e4

Please sign in to comment.