Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,17 @@ char* write_to_temp_file(uint8_t* data, size_t size) {
free(temp_file_name);
return NULL;
}
if (fclose(temp_file) != 0) {
perror("Failed to close temporary file");
unlink(temp_file_name);
free(temp_file_name);
return NULL;
}

fclose(temp_file);
return temp_file_name;
}


EMSCRIPTEN_KEEPALIVE
ExtractedArchive* decompression(uint8_t* inputData, size_t inputSize) {
struct archive* archive;
Expand All @@ -152,14 +158,15 @@ ExtractedArchive* decompression(uint8_t* inputData, size_t inputSize) {
char buff[buffsize];
size_t total_size = 0;
const char *error_message;
size_t files_struct_length = 1;

FileData* files = malloc(sizeof(FileData) * (files_count + 1));

FileData* files = malloc(sizeof(FileData) * files_struct_length);
if (!files) {
printf("Failed to allocate memory for files array\n");
return NULL;
}


ExtractedArchive* result = (ExtractedArchive*)malloc(sizeof(ExtractedArchive));
if (!result) {
free(files);
Expand All @@ -177,19 +184,29 @@ ExtractedArchive* decompression(uint8_t* inputData, size_t inputSize) {
error_message = "Failed to create temporary file";
return error_handler(result, error_message, archive);
}

archive = archive_read_new();
archive_read_support_filter_all(archive);
archive_read_support_format_raw(archive);

if (archive_read_open_filename(archive, temp_file_name, inputSize) != ARCHIVE_OK) {
if (archive_read_open_filename(archive, temp_file_name, 1024 * 1024) != ARCHIVE_OK) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to commit this hardcoded value 1024 * 1024 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martinRenou well, it does not help in any case. This issue will be fixed in scope of #45

unlink(temp_file_name);
free(temp_file_name);
free(files);
return error_handler(result, archive_error_string(archive), archive);
}

while (archive_read_next_header(archive, &entry) == ARCHIVE_OK) {

if (files_count == files_struct_length) {
files_struct_length *= 2;
FileData* temp = realloc(files, sizeof(FileData) * files_struct_length);
if (!temp) {
free(files);
error_message = "Failed to reallocate memory for files";
return error_handler(result, error_message, archive);
}
files = temp;
}

const char* filename = archive_entry_pathname(entry);
if (!filename) filename = "decompression";

Expand Down
Loading