Skip to content

Prevent zlib from being dynamically loaded #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion lib/pkg_editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ if(ZLIB_FOUND)
target_compile_definitions(pkg_editor PRIVATE WINDOWS_ZLIB_PATH="${WINDOWS_ZLIB_DLL_PATH}")
endif()
target_link_libraries(pkg_editor PRIVATE ZLIB::ZLIB)
target_sources(pkg_editor PRIVATE src/zlib.c)
endif()

install(TARGETS pkg_editor
Expand Down
59 changes: 20 additions & 39 deletions lib/pkg_editor/src/pkg_editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
#endif

#include "pkg_editor/pkg_editor.h"
#if USE_ZLIB
#include "zlib_interface.h"
#endif
#include "zlib.h"

typedef struct acl_pkg_file {
const char *fname;
Expand Down Expand Up @@ -1194,7 +1192,7 @@ static int append_data(const void *data, size_t size, ZInfo *z_info, FILE *of,
int ret;
z_info->strm.avail_out = sizeof(z_info->buffer);
z_info->strm.next_out = z_info->buffer;
ret = zlib_deflate(&z_info->strm, Z_FINISH);
ret = deflate(&z_info->strm, Z_FINISH);
assert(ret != Z_STREAM_ERROR);
output_size = sizeof(z_info->buffer) - z_info->strm.avail_out;
if (output_size > 0) {
Expand All @@ -1206,7 +1204,7 @@ static int append_data(const void *data, size_t size, ZInfo *z_info, FILE *of,
} else {
// Only dump the output buffer when it is full.
do {
int ret = zlib_deflate(&z_info->strm, Z_NO_FLUSH);
int ret = deflate(&z_info->strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR);
if (z_info->strm.avail_out == 0) {
if (fwrite(z_info->buffer, sizeof(z_info->buffer), 1, of) != 1) {
Expand Down Expand Up @@ -1476,7 +1474,8 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
z_info.strm.opaque = Z_NULL;
z_info.strm.avail_out = sizeof(z_info.buffer);
z_info.strm.next_out = z_info.buffer;
ret = zlib_deflateInit(&z_info.strm, Z_BEST_COMPRESSION);
ret = deflateInit_(&z_info.strm, Z_BEST_COMPRESSION, ZLIB_VERSION,
(int)sizeof(z_stream));
if (ret != Z_OK) {
fprintf(stderr, "acl_pkg_pack: Unable to initialize zlib for writing %s\n",
out_file);
Expand All @@ -1491,7 +1490,7 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
if (result == PACK_END) {
// We had a failure; stop here.
fclose(of);
zlib_deflateEnd(&z_info.strm);
deflateEnd(&z_info.strm);
return 0;
}
input_files_dirs++;
Expand All @@ -1505,10 +1504,10 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
if (fclose(of) != 0) {
fprintf(stderr, "acl_pkg_pack: Write of %s failed: %s\n", out_file,
strerror(errno));
zlib_deflateEnd(&z_info.strm);
deflateEnd(&z_info.strm);
return 0;
}
zlib_deflateEnd(&z_info.strm);
deflateEnd(&z_info.strm);
return 1 /* success */;
}

Expand All @@ -1534,7 +1533,7 @@ static int read_data(void *data, size_t size, ZInfo *z_info, FILE *in_fd) {
z_info->strm.next_in = z_info->buffer;
}
// Grab the next chunk of data from the input buffer.
ret = zlib_inflate(&z_info->strm, Z_NO_FLUSH);
ret = inflate(&z_info->strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR);
if (ret == Z_STREAM_END) {
// Last bit of data.
Expand Down Expand Up @@ -1590,7 +1589,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
z_info.strm.avail_in = 0;
z_info.strm.next_in = NULL;
}
ret = zlib_inflateInit(&z_info.strm);
ret = inflateInit_(&z_info.strm, ZLIB_VERSION, (int)sizeof(z_stream));
if (ret != Z_OK) {
fprintf(stderr, "%s: Unable to initialize zlib for reading from buffer\n",
routine_name);
Expand All @@ -1611,13 +1610,13 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
acl_pkg_pack_info info;
if (!read_data(&info, sizeof(info), &z_info, input)) {
fprintf(stderr, "%s: Error reading from buffer\n", routine_name);
zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return 0;
}
if (info.magic != PACK_MAGIC) {
fprintf(stderr, "%s: Incorrect magic number read from buffer\n",
routine_name);
zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return 0;
}

Expand All @@ -1630,7 +1629,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
if (!read_data(name, info.name_length, &z_info, input)) {
fprintf(stderr, "%s: Error reading file name from buffer\n",
routine_name);
zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return 0;
}

Expand All @@ -1656,7 +1655,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
if (out_file == NULL) {
fprintf(stderr, "%s: Unable to open %s for writing: %s\n", routine_name,
full_name, strerror(errno));
zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return 0;
}
if (info.file_length > 0) {
Expand All @@ -1666,14 +1665,14 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
fprintf(stderr, "%s: Error reading file data for %s from buffer\n",
routine_name, full_name);
fclose(out_file);
zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return 0;
}
if (fwrite(buf, info.file_length, 1, out_file) != 1) {
fprintf(stderr, "%s: Failed to write to %s: %s\n", routine_name,
full_name, strerror(errno));
fclose(out_file);
zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return 0;
}
} else {
Expand All @@ -1683,23 +1682,23 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
routine_name, full_name, strerror(errno));
fclose(out_file);
free(buf2);
zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return PACK_END;
}
if (!read_data(buf2, info.file_length, &z_info, input)) {
fprintf(stderr, "%s: Error reading file data for %s from buffer\n",
routine_name, full_name);
fclose(out_file);
free(buf2);
zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return 0;
}
if (fwrite(buf2, info.file_length, 1, out_file) != 1) {
fprintf(stderr, "%s: Failed to write to %s: %s\n", routine_name,
full_name, strerror(errno));
fclose(out_file);
free(buf2);
zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return 0;
}
free(buf2);
Expand All @@ -1709,7 +1708,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
}
}

zlib_inflateEnd(&z_info.strm);
inflateEnd(&z_info.strm);
return 1;
}

Expand Down Expand Up @@ -1744,22 +1743,4 @@ int acl_pkg_unpack(const char *in_file, const char *out_dir) {
return ret;
}

#else // USE_ZLIB

int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
// Not implemented if no ZLIB
return 0;
}

int acl_pkg_unpack(const char *in_file, const char *out_dir) {
// Not implemented if no ZLIB
return 0;
}

int acl_pkg_unpack_buffer(const char *buffer, size_t buffer_size,
const char *out_dir) {
// Not implemented if no ZLIB
return 0;
}

#endif // USE_ZLIB
150 changes: 0 additions & 150 deletions lib/pkg_editor/src/zlib.c

This file was deleted.

30 changes: 0 additions & 30 deletions lib/pkg_editor/src/zlib_interface.h

This file was deleted.