Repository navigation
Timestamp support #187
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
base: main
Are you sure you want to change the base?
Timestamp support #187
Changes from all commits
730df61
c553961
a83e977
72be695
feea68c
ce79d72
bfcec03
8049058
e37cde0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <unistd.h> | ||
| #include <utime.h> | ||
| #include <getopt.h> | ||
| #include "../lib/libunshield.h" | ||
| #ifdef HAVE_CONFIG_H | ||
|
|
@@ -68,6 +69,7 @@ static const char* component_name = NULL; | |
| static bool junk_paths = false; | ||
| static bool make_lowercase = false; | ||
| static bool raw_filename = false; | ||
| static bool set_timestamp = false; | ||
| static ACTION action = ACTION_EXTRACT; | ||
| static int log_level = UNSHIELD_LOG_LEVEL_LOWEST; | ||
| static int exit_status = 0; | ||
|
|
@@ -192,7 +194,7 @@ static void show_usage(const char* name) | |
| fprintf(stderr, | ||
| "Syntax:\n" | ||
| "\n" | ||
| "\t%s [-c COMPONENT] [-d DIRECTORY] [-D LEVEL] [-g GROUP] [-h] [-i VERSION] [-e ENCODING] [-j] [-L] [-O] [-r] [-R] [-V] c|g|l|t|x CABFILE [FILENAME...]\n" | ||
| "\t%s [-c COMPONENT] [-d DIRECTORY] [-D LEVEL] [-g GROUP] [-h] [-i VERSION] [-e ENCODING] [-j] [-L] [-O] [-r] [-R] [-t] [-V] c|g|l|t|x CABFILE [FILENAME...]\n" | ||
| "\n" | ||
| "Options:\n" | ||
| "\t-c COMPONENT Only list/extract this component\n" | ||
|
|
@@ -211,6 +213,7 @@ static void show_usage(const char* name) | |
| "\t-O Use old compression\n" | ||
| "\t-r Save raw data (do not decompress)\n" | ||
| "\t-R Don't do any conversion to file and directory names when extracting\n" | ||
| "\t-t Set the modification date/time from the metadata when extracting\n" | ||
| "\t-V --version Print copyright and version information\n" | ||
| "\n" | ||
| "Commands:\n" | ||
|
|
@@ -243,7 +246,7 @@ static bool handle_parameters( | |
| { NULL, 0, NULL, 0 } | ||
| }; | ||
|
|
||
| while ((c = getopt_long(argc, argv, "c:d:D:g:hi:e:jLOrRV", long_options, NULL)) != -1) | ||
| while ((c = getopt_long(argc, argv, "c:d:D:g:hi:e:jLOrRtV", long_options, NULL)) != -1) | ||
| { | ||
| switch (c) | ||
| { | ||
|
|
@@ -297,6 +300,10 @@ static bool handle_parameters( | |
| format = FORMAT_RAW; | ||
| break; | ||
|
|
||
| case 't': | ||
| set_timestamp = true; | ||
| break; | ||
|
|
||
| case 'V': | ||
| printf("Unshield version " VERSION ". MIT License. (C) 2003-2023 David Eriksson.\n"); | ||
| exit(0); | ||
|
|
@@ -366,7 +373,8 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) | |
| { | ||
| bool success = false; | ||
| char* dirname; | ||
| char* filename; | ||
| const char* filename = unshield_file_name(unshield, index); | ||
| char* full_filename; | ||
| char* p; | ||
| int directory = unshield_file_directory(unshield, index); | ||
| long int path_max; | ||
|
|
@@ -384,8 +392,9 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) | |
| real_output_directory = malloc(path_max); | ||
| real_filename = malloc(path_max); | ||
| dirname = malloc(path_max); | ||
| filename = malloc(path_max); | ||
| if (real_output_directory == NULL || real_filename == NULL) | ||
| full_filename = calloc(1, path_max); | ||
| if (real_output_directory == NULL || real_filename == NULL || | ||
| dirname == NULL || full_filename == NULL) | ||
| { | ||
| fprintf(stderr,"Unable to allocate memory."); | ||
| success=false; | ||
|
|
@@ -486,10 +495,9 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) | |
|
|
||
| make_sure_directory_exists(dirname); | ||
|
|
||
| snprintf(filename, path_max, "%s%s", | ||
| dirname, unshield_file_name(unshield, index)); | ||
| snprintf(full_filename, path_max, "%s%s", dirname, filename); | ||
|
|
||
| for (p = filename + strlen(dirname); *p != '\0'; p++) | ||
| for (p = full_filename + strlen(dirname); *p != '\0'; p++) | ||
| { | ||
| if (!raw_filename) | ||
| { | ||
|
|
@@ -501,7 +509,7 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) | |
| } | ||
|
|
||
| #ifdef HAVE_ICONV | ||
| if (!convert_encoding(filename + strlen(dirname), | ||
| if (!convert_encoding(full_filename + strlen(dirname), | ||
| path_max - strlen(dirname))) | ||
| { | ||
| success = false; | ||
|
|
@@ -514,48 +522,78 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) | |
| #pragma GCC diagnostic ignored "-Wunused-result" | ||
| /* use GNU extension to return non-existing files to real_output_directory */ | ||
| realpath(output_directory, real_output_directory); | ||
| realpath(filename, real_filename); | ||
| realpath(full_filename, real_filename); | ||
| #pragma GCC diagnostic pop | ||
| if (real_filename == NULL || strncmp(real_filename, | ||
| real_output_directory, | ||
| strlen(real_output_directory)) != 0) | ||
| { | ||
| fprintf(stderr, "\n\nExtraction failed.\n"); | ||
| fprintf(stderr, "Error: %s (%d).\n", strerror(errno), errno); | ||
| fprintf(stderr, "Possible directory traversal attack for: %s\n", filename); | ||
| fprintf(stderr, "Possible directory traversal attack for: %s\n", full_filename); | ||
| fprintf(stderr, "To be placed at: %s\n\n", real_filename); | ||
| success = false; | ||
| goto exit; | ||
| } | ||
| #endif | ||
|
|
||
| printf(" extracting: %s\n", filename); | ||
| printf(" extracting: %s\n", full_filename); | ||
| switch (format) | ||
| { | ||
| case FORMAT_NEW: | ||
| success = unshield_file_save(unshield, index, filename); | ||
| success = unshield_file_save(unshield, index, full_filename); | ||
| break; | ||
| case FORMAT_OLD: | ||
| success = unshield_file_save_old(unshield, index, filename); | ||
| success = unshield_file_save_old(unshield, index, full_filename); | ||
| break; | ||
| case FORMAT_RAW: | ||
| success = unshield_file_save_raw(unshield, index, filename); | ||
| success = unshield_file_save_raw(unshield, index, full_filename); | ||
| break; | ||
| } | ||
|
|
||
| if (success && set_timestamp) | ||
| { | ||
| time_t timestamp = unshield_file_timestamp(unshield, index); | ||
| int ret = 0, ret2 = 0; | ||
| struct utimbuf utim; | ||
| struct stat info; | ||
|
|
||
| if (!timestamp || timestamp == (time_t)-1) | ||
| { | ||
| fprintf(stderr, "Failed to get timestamp for file '%s'.", filename); | ||
| } | ||
| else | ||
| { | ||
| memset(&utim, 0, sizeof(utim)); | ||
| memset(&info, 0, sizeof(info)); | ||
|
|
||
| ret = stat(full_filename, &info); | ||
| if (ret == 0) | ||
| { | ||
| utim.actime = info.st_atime; | ||
| utim.modtime = timestamp; | ||
| ret2 = utime(full_filename, &utim); | ||
|
Comment on lines
+558
to
+575
|
||
| } | ||
|
|
||
| if (ret < 0 || ret2 < 0) | ||
| fprintf(stderr, "Failed to set timestamp for file '%s'.", filename); | ||
|
Comment on lines
+563
to
+579
|
||
| } | ||
| } | ||
|
Comment on lines
+554
to
+581
|
||
|
|
||
| exit: | ||
| if (!success) | ||
| { | ||
| fprintf(stderr, "Failed to extract file '%s'.%s\n", | ||
| unshield_file_name(unshield, index), | ||
| filename, | ||
| (log_level < 3) ? "Run unshield again with -D 3 for more information." : ""); | ||
| unlink(filename); | ||
| if (full_filename) | ||
| unlink(full_filename); | ||
| exit_status = 1; | ||
| } | ||
| free(real_filename); | ||
| free(real_output_directory); | ||
| free(dirname); | ||
| free(filename); | ||
| free(full_filename); | ||
| return success; | ||
| } | ||
|
|
||
|
|
@@ -686,11 +724,15 @@ static int list_files_helper(Unshield* unshield, const char* prefix, int first, | |
| for (i = first; i <= last; i++) | ||
| { | ||
| char dirname[4096]; | ||
| char date[32]; | ||
|
|
||
| if (unshield_file_is_valid(unshield, i) && should_process_file(unshield, i)) | ||
| { | ||
| valid_count++; | ||
|
|
||
| if (!unshield_file_iso_date(unshield, i, date, sizeof(date))) | ||
| strcpy(date, "-------------------"); | ||
|
|
||
| if (prefix && prefix[0]) | ||
| { | ||
| strcpy(dirname, prefix); | ||
|
|
@@ -711,10 +753,11 @@ static int list_files_helper(Unshield* unshield, const char* prefix, int first, | |
| if (dirname[strlen(dirname)-1] != '\\') | ||
| strcat(dirname, "\\"); | ||
|
|
||
| printf(" %8" SIZE_FORMAT " %s%s\n", | ||
| printf(" %8" SIZE_FORMAT " %s %s%s\n", | ||
| unshield_file_size(unshield, i), | ||
| date, | ||
| dirname, | ||
| unshield_file_name(unshield, i)); | ||
| unshield_file_name(unshield, i)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -751,7 +794,7 @@ static bool do_action(Unshield* unshield, ActionHelper helper) | |
| } | ||
| } | ||
|
|
||
| printf(" -------- -------\n %i files\n", count); | ||
| printf(" -------- ------------------- -------\n %i files\n", count); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code reads dos_date and dos_time as 32-bit values using READ_UINT32 and then casts them to 16-bit. This seems inconsistent with the later versions (6+) which read them as 16-bit values directly. If the file format actually stores these as 32-bit values in versions 0 and 5, consider documenting why this is the case. If they are actually 16-bit fields, use READ_UINT16 instead and adjust the pointer increment accordingly. The current approach may work but could be confusing and potentially read incorrect data depending on endianness and padding.