Skip to content

Commit

Permalink
Add 'extract_as' command to ziptool_regress
Browse files Browse the repository at this point in the history
  • Loading branch information
0-wiz-0 committed Jun 29, 2023
1 parent eb4d8e3 commit 7f53f28
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
22 changes: 22 additions & 0 deletions regress/ziptool_regress.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ unsigned int z_files_count;

static int add_nul(char *argv[]);
static int cancel(char *argv[]);
static int extract_as(char *argv[]);
static int regress_fopen(char *argv[]);
static int regress_fread(char *argv[]);
static int regress_fseek(char *argv[]);
Expand Down Expand Up @@ -46,6 +47,7 @@ static int zin_close(char *argv[]);
#define DISPATCH_REGRESS \
{"add_nul", 2, "name length", "add NUL bytes", add_nul}, \
{"cancel", 1, "limit", "cancel writing archive when limit% have been written (calls print_progress)", cancel}, \
{"extract_as", 2, "index name", "extract file data to given file name", extract_as}, \
{"fopen", 1, "name", "open archive entry", regress_fopen}, \
{"fread", 2, "file_index length", "read from fopened file and print", regress_fread}, \
{"fseek", 3, "file_index offset whence", "seek in fopened file", regress_fseek}, \
Expand Down Expand Up @@ -125,6 +127,26 @@ cancel(char *argv[]) {
return 0;
}

static int
extract_as(char *argv[]) {
zip_uint64_t idx;
FILE *fp;
int ret;

idx = strtoull(argv[0], NULL, 10);
if ((fp=fopen(argv[1], "wb")) == NULL) {
fprintf(stderr, "can't open output file '%s': %s", argv[1], strerror(errno));
return -1;
}
ret = cat_impl_backend(idx, 0, -1, fp);
if (fclose(fp) != 0) {
fprintf(stderr, "can't close output file '%s': %s", argv[1], strerror(errno));
ret = -1;
}
return ret;
}


static int
is_seekable(char *argv[]) {
zip_uint64_t idx;
Expand Down
21 changes: 13 additions & 8 deletions src/ziptool.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,12 @@ zip_flags_t stat_flags;
int hex_encoded_filenames = 0; // Can only be set in ziptool_regress.

static int
cat_impl(zip_uint64_t idx, zip_uint64_t start, zip_uint64_t len) {
cat_impl_backend(zip_uint64_t idx, zip_uint64_t start, zip_uint64_t len, FILE *out) {
zip_error_t error;
zip_source_t *src;
zip_int64_t n;
char buf[8192];

#ifdef _WIN32
/* Need to set stdout to binary mode for Windows */
setmode(fileno(stdout), _O_BINARY);
#endif
zip_error_init(&error);
/* we can't pass 0 as a len to zip_source_zip_create because it
will try to give us compressed data */
Expand Down Expand Up @@ -125,8 +121,8 @@ cat_impl(zip_uint64_t idx, zip_uint64_t start, zip_uint64_t len) {
return -1;
}
while ((n = zip_source_read(src, buf, sizeof(buf))) > 0) {
if (fwrite(buf, (size_t)n, 1, stdout) != 1) {
fprintf(stderr, "can't write file contents to stdout: %s\n", strerror(errno));
if (fwrite(buf, (size_t)n, 1, out) != 1) {
fprintf(stderr, "can't write file contents: %s\n", strerror(errno));
zip_source_free(src);
return -1;
}
Expand All @@ -146,6 +142,15 @@ cat_impl(zip_uint64_t idx, zip_uint64_t start, zip_uint64_t len) {
return 0;
}

static int
cat_impl(zip_uint64_t idx, zip_uint64_t start, zip_uint64_t len) {
#ifdef _WIN32
/* Need to set stdout to binary mode for Windows */
setmode(fileno(stdout), _O_BINARY);
#endif
return cat_impl_backend(idx, start, len, stdout);
}

static int
add(char *argv[]) {
zip_source_t *zs;
Expand Down Expand Up @@ -1149,4 +1154,4 @@ static const char* decode_filename(const char* name) {
*t = '\0';

return filename_buffer;
}
}

0 comments on commit 7f53f28

Please sign in to comment.