| 1 |
// |
| 2 |
// Copyright(C) 2023 by Pierre Wendling |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA zipfile support using libzip |
| 16 |
// |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <zip.h> |
| 20 |
|
| 21 |
#include "i_system.h" |
| 22 |
#include "lprintf.h" |
| 23 |
#include "m_file.h" |
| 24 |
#include "z_zone.h" |
| 25 |
|
| 26 |
#include "dsda/utility.h" |
| 27 |
|
| 28 |
static char **temp_dirs; |
| 29 |
|
| 30 |
/* Allow a maximum of 1GB to be uncompressed to prevent zip-bombs */ |
| 31 |
#define UNZIPPED_BYTES_LIMIT 1000000000ULL |
| 32 |
|
| 33 |
static zip_uint64_t total_bytes_read; |
| 34 |
|
| 35 |
#define CHUNK_SIZE 4 * 1024U |
| 36 |
|
| 37 |
static void dsda_WriteContentToFile(zip_file_t *input_file, FILE *dest_file, zip_uint64_t data_size) { |
| 38 |
byte buffer[CHUNK_SIZE]; |
| 39 |
|
| 40 |
while (data_size != 0) { |
| 41 |
zip_uint64_t chunk_size; |
| 42 |
zip_int64_t bytes_read; |
| 43 |
|
| 44 |
chunk_size = MIN(data_size, CHUNK_SIZE); |
| 45 |
bytes_read = zip_fread(input_file, buffer, chunk_size); |
| 46 |
if (bytes_read == -1) |
| 47 |
I_Error("dsda_WriteContentToFile: Unable to read data from archive."); |
| 48 |
|
| 49 |
if (fwrite(buffer, sizeof(char), bytes_read, dest_file) != bytes_read) |
| 50 |
I_Error("dsda_WriteContentToFile: Failed to write data to file."); |
| 51 |
|
| 52 |
data_size -= bytes_read; |
| 53 |
total_bytes_read += bytes_read; |
| 54 |
if (total_bytes_read >= UNZIPPED_BYTES_LIMIT) |
| 55 |
I_Error("dsda_WriteContentToFile: Too much data to decompress."); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static void dsda_WriteZippedFilesToDest(zip_t *archive, const char *destination_directory) { |
| 60 |
zip_int64_t i; |
| 61 |
|
| 62 |
for (i = 0; i < zip_get_num_entries(archive, ZIP_FL_UNCHANGED); i++) { |
| 63 |
dsda_string_t full_path; |
| 64 |
zip_file_t *zipped_file; |
| 65 |
zip_stat_t stat; |
| 66 |
FILE *dest_file; |
| 67 |
const char *file_name = dsda_BaseName(zip_get_name(archive, i, ZIP_FL_UNCHANGED)); |
| 68 |
|
| 69 |
/* Intermediate directories have a trailing '/', so their base name is empty */ |
| 70 |
if (*file_name == '\0') { |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
dsda_StringPrintF(&full_path, "%s/%s", destination_directory, file_name); |
| 75 |
|
| 76 |
zip_stat_index(archive, i, ZIP_FL_UNCHANGED, &stat); |
| 77 |
if ((stat.valid & ZIP_STAT_SIZE) == 0) |
| 78 |
I_Error("dsda_WriteZippedFilesToDest: Failed to read size of zipped file %s.", file_name); |
| 79 |
|
| 80 |
zipped_file = zip_fopen_index(archive, i, ZIP_FL_UNCHANGED); |
| 81 |
if (zipped_file == NULL) |
| 82 |
I_Error("dsda_WriteZippedFilesToDest: Failed to open zipped file %s.", file_name); |
| 83 |
|
| 84 |
dest_file = M_OpenFile(full_path.string, "wb"); |
| 85 |
if (dest_file == NULL) |
| 86 |
I_Error("dsda_WriteZippedFilesToDest: Failed to open destination file %s.", full_path.string); |
| 87 |
|
| 88 |
dsda_WriteContentToFile(zipped_file, dest_file, stat.size); |
| 89 |
|
| 90 |
zip_fclose(zipped_file); |
| 91 |
fclose(dest_file); |
| 92 |
dsda_FreeString(&full_path); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
static void dsda_UnzipFileToDestination(const char *zipped_file_name, const char *destination_directory) { |
| 97 |
int error_code; |
| 98 |
zip_t *archive_handle; |
| 99 |
|
| 100 |
total_bytes_read = 0; |
| 101 |
archive_handle = zip_open(zipped_file_name, ZIP_RDONLY, &error_code); |
| 102 |
if (archive_handle == NULL) { |
| 103 |
zip_error_t error; |
| 104 |
zip_error_init_with_code(&error, error_code); |
| 105 |
I_Error("dsda_UnzipFileToDestination: Unable to open %s: %s.\n", zipped_file_name, zip_error_strerror(&error)); |
| 106 |
} |
| 107 |
|
| 108 |
dsda_WriteZippedFilesToDest(archive_handle, destination_directory); |
| 109 |
|
| 110 |
zip_close(archive_handle); |
| 111 |
} |
| 112 |
|
| 113 |
const char* dsda_UnzipFile(const char *zipped_file_name) { |
| 114 |
dsda_string_t temporary_directory; |
| 115 |
static unsigned int file_counter = 0; |
| 116 |
|
| 117 |
dsda_StringPrintF(&temporary_directory, "%s/%u-%s", I_GetTempDir(), file_counter, dsda_BaseName(zipped_file_name)); |
| 118 |
if (M_IsDir(temporary_directory.string)) |
| 119 |
if (!M_RemoveFilesAtPath(temporary_directory.string)) |
| 120 |
I_Error("dsda_UnzipFile: unable to clear tempdir %s\n", temporary_directory.string); |
| 121 |
M_MakeDir(temporary_directory.string, true); |
| 122 |
|
| 123 |
dsda_UnzipFileToDestination(zipped_file_name, temporary_directory.string); |
| 124 |
|
| 125 |
temp_dirs = Z_Realloc(temp_dirs, (file_counter + 2) * sizeof(*temp_dirs)); |
| 126 |
temp_dirs[file_counter] = temporary_directory.string; |
| 127 |
temp_dirs[file_counter + 1] = NULL; |
| 128 |
file_counter++; |
| 129 |
|
| 130 |
return temporary_directory.string; |
| 131 |
} |
| 132 |
|
| 133 |
const char* dsda_ReadUnzippedFile(const char *zipped_file_name) { |
| 134 |
dsda_string_t temporary_directory; |
| 135 |
static unsigned int file_counter = 0; |
| 136 |
|
| 137 |
dsda_StringPrintF(&temporary_directory, "%s/%u-%s", I_GetTempDir(), file_counter, dsda_BaseName(zipped_file_name)); |
| 138 |
|
| 139 |
temp_dirs = Z_Realloc(temp_dirs, (file_counter + 2) * sizeof(*temp_dirs)); |
| 140 |
temp_dirs[file_counter] = temporary_directory.string; |
| 141 |
temp_dirs[file_counter + 1] = NULL; |
| 142 |
file_counter++; |
| 143 |
|
| 144 |
return temporary_directory.string; |
| 145 |
} |
| 146 |
|
| 147 |
void dsda_CleanZipTempDirs(void) { |
| 148 |
int i; |
| 149 |
|
| 150 |
if(temp_dirs == NULL) |
| 151 |
return; |
| 152 |
|
| 153 |
for (i = 0; temp_dirs[i] != NULL; i++) { |
| 154 |
M_RemoveFilesAtPath(temp_dirs[i]); |
| 155 |
M_remove(temp_dirs[i]); |
| 156 |
Z_Free(temp_dirs[i]); |
| 157 |
} |
| 158 |
Z_Free(temp_dirs); |
| 159 |
} |