From 730df610f8af0de1d11761646e5fda486864b03d Mon Sep 17 00:00:00 2001 From: Echo J Date: Sat, 14 Dec 2024 13:12:46 +0200 Subject: [PATCH 1/7] lib: Re-add DOS date members in FD struct These were originally present in the early unshield code but for some reason they were removed later (this change is based on the i5comp and i6comp struct layout) Those struct members have a correct date with the Baldur's Gate test (likely expanded_size = READ_UINT32(p); p += 4; fd->compressed_size = READ_UINT32(p); p += 4; - p += 0x14; + + p += 4; + fd->dos_date = (uint16_t)READ_UINT32(p); p += 4; + fd->dos_time = (uint16_t)READ_UINT32(p); p += 4; + p += 8; + fd->data_offset = READ_UINT32(p); p += 4; #if VERBOSE >= 2 @@ -74,6 +79,8 @@ static FileDescriptor* unshield_read_file_descriptor(Unshield* unshield, int ind unshield_trace("Flags: %04x", fd->flags); unshield_trace("Expanded size: %08x", fd->expanded_size); unshield_trace("Compressed size: %08x", fd->compressed_size); + unshield_trace("DOS date: %04x", fd->dos_date); + unshield_trace("DOS time: %04x", fd->dos_time); unshield_trace("Data offset: %08x", fd->data_offset); #endif @@ -114,8 +121,12 @@ static FileDescriptor* unshield_read_file_descriptor(Unshield* unshield, int ind assert(fd->directory_index < unshield_directory_count(unshield)); assert((p - saved_p) == 0x40); - - p += 0xc; + + p += 4; + fd->dos_date = READ_UINT16(p); p += 2; + fd->dos_time = READ_UINT16(p); p += 2; + + p += 4; fd->link_previous = READ_UINT32(p); p += 4; fd->link_next = READ_UINT32(p); p += 4; fd->link_flags = *p; p ++; From c55396147ede0c57c97374006bde91a565c62bcf Mon Sep 17 00:00:00 2001 From: Echo J Date: Sat, 14 Dec 2024 13:42:48 +0200 Subject: [PATCH 2/7] Print modification date in file listings This also adds a helper function to load the DOS date/time into a tm structure and an library function to get the ISO 8601 date Co-authored-by: GitHub Copilot --- lib/file.c | 24 ++++++++++++++++++++++++ lib/helper.c | 24 ++++++++++++++++++++++++ lib/internal.h | 2 ++ lib/libunshield.h | 2 ++ src/unshield.c | 10 +++++++--- 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/lib/file.c b/lib/file.c index 7af86725..8c700c69 100644 --- a/lib/file.c +++ b/lib/file.c @@ -967,6 +967,30 @@ size_t unshield_file_size(Unshield* unshield, int index)/*{{{*/ return 0; }/*}}}*/ +/* This uses the ISO 8601 date format (except for milliseconds) */ +bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t size)/*{{{*/ +{ + FileDescriptor* fd = unshield_get_file_descriptor(unshield, index); + struct tm date; + + if (size < 32) + { + unshield_error("Buffer size %" SIZE_FORMAT " is too small", size); + return false; + } + + if (fd) + unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date); + else + { + unshield_warning("Failed to get file descriptor %i", index); + memset(&date, 0, sizeof(date)); + } + + (void)strftime(buf, size, "%Y-%m-%d %H:%M:%S", &date); + return fd ? true : false; +}/*}}}*/ + bool unshield_file_save_raw(Unshield* unshield, int index, const char* filename) { /* XXX: Thou Shalt Not Cut & Paste... */ diff --git a/lib/helper.c b/lib/helper.c index 95327c54..f0524f45 100644 --- a/lib/helper.c +++ b/lib/helper.c @@ -268,4 +268,28 @@ const char* unshield_header_get_string(Header* header, uint32_t offset) return unshield_get_utf8_string(header, unshield_header_get_buffer(header, offset)); } +/** + Based on MIT Wine code and DosDateTimeToFileTime() MSDN documentation + */ +void unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm) +{ + memset(tm, 0, sizeof(*tm)); + + tm->tm_isdst = -1; + + if (dos_date) + { + tm->tm_year = (dos_date >> 9) + 80; + tm->tm_mon = ((dos_date >> 5) & 0x0f) - 1; + tm->tm_mday = dos_date & 0x1f; + } + + if (dos_time) + { + tm->tm_hour = dos_time >> 11; + tm->tm_min = (dos_time >> 5) & 0x3f; + tm->tm_sec = (dos_time & 0x1f) * 2; + } +} + diff --git a/lib/internal.h b/lib/internal.h index f9d091d4..3051bca1 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -87,6 +87,8 @@ const char* unshield_header_get_string(Header* header, uint32_t offset); uint8_t* unshield_header_get_buffer(Header* header, uint32_t offset); char *unshield_get_last_path_separator(char *path); +void unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm); + static inline void* unshield_fopen(Unshield* unshield, const char *filename, const char *modes) { return unshield->io_callbacks->fopen(filename, modes, unshield->io_userdata); diff --git a/lib/libunshield.h b/lib/libunshield.h index f937f715..5b0dd7c3 100644 --- a/lib/libunshield.h +++ b/lib/libunshield.h @@ -3,6 +3,7 @@ #include #include +#include #define UNSHIELD_LOG_LEVEL_LOWEST 0 @@ -108,6 +109,7 @@ UNSHIELD_API bool unshield_file_is_valid (Unshield* unshield, int in UNSHIELD_API bool unshield_file_save (Unshield* unshield, int index, const char* filename); UNSHIELD_API int unshield_file_directory (Unshield* unshield, int index); UNSHIELD_API size_t unshield_file_size (Unshield* unshield, int index); +UNSHIELD_API bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t size); /** For investigation of compressed data */ UNSHIELD_API bool unshield_file_save_raw(Unshield* unshield, int index, const char* filename); diff --git a/src/unshield.c b/src/unshield.c index 857c5105..45ecbd94 100644 --- a/src/unshield.c +++ b/src/unshield.c @@ -683,11 +683,14 @@ 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++; + (void)unshield_file_iso_date(unshield, i, date, sizeof(date)); + if (prefix && prefix[0]) { strcpy(dirname, prefix); @@ -708,10 +711,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)); } } @@ -748,7 +752,7 @@ static bool do_action(Unshield* unshield, ActionHelper helper) } } - printf(" -------- -------\n %i files\n", count); + printf(" -------- ------------------- -------\n %i files\n", count); return true; } From a83e97767090596d84abcb2a350e6f037f4c2ed2 Mon Sep 17 00:00:00 2001 From: Echo J Date: Sat, 14 Dec 2024 19:41:40 +0200 Subject: [PATCH 3/7] Add option to set the modification date/time This also adds an API function for getting an Unix timestamp (and updates the man page) Co-authored-by: GitHub Copilot --- lib/file.c | 15 ++++++++++ lib/libunshield.h | 1 + man/unshield.1 | 7 +++-- src/unshield.c | 70 ++++++++++++++++++++++++++++++++++++----------- 4 files changed, 75 insertions(+), 18 deletions(-) diff --git a/lib/file.c b/lib/file.c index 8c700c69..a3daa445 100644 --- a/lib/file.c +++ b/lib/file.c @@ -991,6 +991,21 @@ bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t si return fd ? true : false; }/*}}}*/ +time_t unshield_file_timestamp (Unshield* unshield, int index)/*{{{*/ +{ + FileDescriptor* fd = unshield_get_file_descriptor(unshield, index); + struct tm date; + + if (fd) + { + unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date); + return mktime(&date); + } + + unshield_warning("Failed to get file descriptor %i", index); + return 0; +}/*}}}*/ + bool unshield_file_save_raw(Unshield* unshield, int index, const char* filename) { /* XXX: Thou Shalt Not Cut & Paste... */ diff --git a/lib/libunshield.h b/lib/libunshield.h index 5b0dd7c3..dbcac075 100644 --- a/lib/libunshield.h +++ b/lib/libunshield.h @@ -110,6 +110,7 @@ UNSHIELD_API bool unshield_file_save (Unshield* unshield, int in UNSHIELD_API int unshield_file_directory (Unshield* unshield, int index); UNSHIELD_API size_t unshield_file_size (Unshield* unshield, int index); UNSHIELD_API bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t size); +UNSHIELD_API time_t unshield_file_timestamp (Unshield* unshield, int index); /** For investigation of compressed data */ UNSHIELD_API bool unshield_file_save_raw(Unshield* unshield, int index, const char* filename); diff --git a/man/unshield.1 b/man/unshield.1 index 284dc67d..6476fda6 100644 --- a/man/unshield.1 +++ b/man/unshield.1 @@ -1,8 +1,8 @@ -.TH UNSHIELD "1" "November 2023" "The Unshield project" "https://github.com/twogood/unshield" +.TH UNSHIELD "1" "January 2026" "The Unshield project" "https://github.com/twogood/unshield" .SH NAME unshield \- extract CAB files from an InstallShield installer archive .SH SYNOPSIS -unshield [\-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...] +unshield [\-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...] .SH DESCRIPTION Unshield extracts CAB files from InstallShield installers, used to install software on Microsoft Windows based machines. @@ -50,6 +50,9 @@ Save raw data (do not decompress) \fB\-R\fR Don't do any conversion to file and directory names when extracting .TP +\fB\-t\fR +Set the modification date/time from the metadata when extracting +.TP \fB\-V\fR, \fB\-\-version\fR Print version information .SS "Commands:" diff --git a/src/unshield.c b/src/unshield.c index 45ecbd94..22fd7e29 100644 --- a/src/unshield.c +++ b/src/unshield.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #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); @@ -367,6 +374,7 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) bool success; char* dirname; char* filename; + char* full_filename; char* p; int directory = unshield_file_directory(unshield, index); long int path_max; @@ -384,7 +392,7 @@ 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); + full_filename = malloc(path_max); if (real_output_directory == NULL || real_filename == NULL) { fprintf(stderr,"Unable to allocate memory."); @@ -486,10 +494,11 @@ 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)); + filename = 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 +510,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; @@ -512,47 +521,76 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) #ifdef __GLIBC__ /* 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); 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) + { + 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); + } + + if (ret < 0 || ret2 < 0) + fprintf(stderr, "Failed to set timestamp for file '%s'.", filename); + } + } + 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); + unlink(full_filename); exit_status = 1; } free(real_filename); free(real_output_directory); free(dirname); - free(filename); + free(full_filename); return success; } From 72be695401cb1eefbfd25cbb9650f0b830bffa2a Mon Sep 17 00:00:00 2001 From: Echo J Date: Sat, 14 Dec 2024 13:12:46 +0200 Subject: [PATCH 4/7] lib: Re-add DOS date members in FD struct These were originally present in the early unshield code but for some reason they were removed later (this change is based on the i5comp and i6comp struct layout) Those struct members have a correct date with the Baldur's Gate test (likely expanded_size = READ_UINT32(p); p += 4; fd->compressed_size = READ_UINT32(p); p += 4; - p += 0x14; + + p += 4; + fd->dos_date = (uint16_t)READ_UINT32(p); p += 4; + fd->dos_time = (uint16_t)READ_UINT32(p); p += 4; + p += 8; + fd->data_offset = READ_UINT32(p); p += 4; #if VERBOSE >= 2 @@ -73,6 +78,8 @@ static FileDescriptor* unshield_read_file_descriptor(Unshield* unshield, int ind unshield_trace("Flags: %04x", fd->flags); unshield_trace("Expanded size: %08x", fd->expanded_size); unshield_trace("Compressed size: %08x", fd->compressed_size); + unshield_trace("DOS date: %04x", fd->dos_date); + unshield_trace("DOS time: %04x", fd->dos_time); unshield_trace("Data offset: %08x", fd->data_offset); #endif @@ -113,8 +120,12 @@ static FileDescriptor* unshield_read_file_descriptor(Unshield* unshield, int ind UNSHIELD_ASSERT(fd->directory_index < unshield_directory_count(unshield)); UNSHIELD_ASSERT((p - saved_p) == 0x40); - - p += 0xc; + + p += 4; + fd->dos_date = READ_UINT16(p); p += 2; + fd->dos_time = READ_UINT16(p); p += 2; + + p += 4; fd->link_previous = READ_UINT32(p); p += 4; fd->link_next = READ_UINT32(p); p += 4; fd->link_flags = *p; p ++; From feea68c51f6e3838c8a0747426d4c5efd5007e61 Mon Sep 17 00:00:00 2001 From: Echo J Date: Sat, 14 Dec 2024 13:42:48 +0200 Subject: [PATCH 5/7] Print modification date in file listings This also adds a helper function to load the DOS date/time into a tm structure and an library function to get the ISO 8601 date Co-authored-by: GitHub Copilot --- lib/file.c | 24 ++++++++++++++++++++++++ lib/helper.c | 24 ++++++++++++++++++++++++ lib/internal.h | 2 ++ lib/libunshield.h | 2 ++ src/unshield.c | 10 +++++++--- 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/lib/file.c b/lib/file.c index d9fb5eff..9c9c511c 100644 --- a/lib/file.c +++ b/lib/file.c @@ -966,6 +966,30 @@ size_t unshield_file_size(Unshield* unshield, int index)/*{{{*/ return 0; }/*}}}*/ +/* This uses the ISO 8601 date format (except for milliseconds) */ +bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t size)/*{{{*/ +{ + FileDescriptor* fd = unshield_get_file_descriptor(unshield, index); + struct tm date; + + if (size < 32) + { + unshield_error("Buffer size %" SIZE_FORMAT " is too small", size); + return false; + } + + if (fd) + unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date); + else + { + unshield_warning("Failed to get file descriptor %i", index); + memset(&date, 0, sizeof(date)); + } + + (void)strftime(buf, size, "%Y-%m-%d %H:%M:%S", &date); + return fd ? true : false; +}/*}}}*/ + bool unshield_file_save_raw(Unshield* unshield, int index, const char* filename) { /* XXX: Thou Shalt Not Cut & Paste... */ diff --git a/lib/helper.c b/lib/helper.c index 9b816aaa..5734f74d 100644 --- a/lib/helper.c +++ b/lib/helper.c @@ -266,4 +266,28 @@ const char* unshield_header_get_string(Header* header, uint32_t offset) return unshield_get_utf8_string(header, unshield_header_get_buffer(header, offset)); } +/** + Based on MIT Wine code and DosDateTimeToFileTime() MSDN documentation + */ +void unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm) +{ + memset(tm, 0, sizeof(*tm)); + + tm->tm_isdst = -1; + + if (dos_date) + { + tm->tm_year = (dos_date >> 9) + 80; + tm->tm_mon = ((dos_date >> 5) & 0x0f) - 1; + tm->tm_mday = dos_date & 0x1f; + } + + if (dos_time) + { + tm->tm_hour = dos_time >> 11; + tm->tm_min = (dos_time >> 5) & 0x3f; + tm->tm_sec = (dos_time & 0x1f) * 2; + } +} + diff --git a/lib/internal.h b/lib/internal.h index f9d091d4..3051bca1 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -87,6 +87,8 @@ const char* unshield_header_get_string(Header* header, uint32_t offset); uint8_t* unshield_header_get_buffer(Header* header, uint32_t offset); char *unshield_get_last_path_separator(char *path); +void unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm); + static inline void* unshield_fopen(Unshield* unshield, const char *filename, const char *modes) { return unshield->io_callbacks->fopen(filename, modes, unshield->io_userdata); diff --git a/lib/libunshield.h b/lib/libunshield.h index 7808ae48..c6a37d7d 100644 --- a/lib/libunshield.h +++ b/lib/libunshield.h @@ -4,6 +4,7 @@ #include #include #include +#include #define UNSHIELD_LOG_LEVEL_LOWEST 0 @@ -127,6 +128,7 @@ UNSHIELD_API bool unshield_file_is_valid (Unshield* unshield, int in UNSHIELD_API bool unshield_file_save (Unshield* unshield, int index, const char* filename); UNSHIELD_API int unshield_file_directory (Unshield* unshield, int index); UNSHIELD_API size_t unshield_file_size (Unshield* unshield, int index); +UNSHIELD_API bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t size); /** For investigation of compressed data */ UNSHIELD_API bool unshield_file_save_raw(Unshield* unshield, int index, const char* filename); diff --git a/src/unshield.c b/src/unshield.c index 6f2a3dbd..61d2c8a4 100644 --- a/src/unshield.c +++ b/src/unshield.c @@ -686,11 +686,14 @@ 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++; + (void)unshield_file_iso_date(unshield, i, date, sizeof(date)); + if (prefix && prefix[0]) { strcpy(dirname, prefix); @@ -711,10 +714,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 +755,7 @@ static bool do_action(Unshield* unshield, ActionHelper helper) } } - printf(" -------- -------\n %i files\n", count); + printf(" -------- ------------------- -------\n %i files\n", count); return true; } From ce79d724edce4a33635d144f30e381ea73847e72 Mon Sep 17 00:00:00 2001 From: Echo J Date: Sat, 14 Dec 2024 19:41:40 +0200 Subject: [PATCH 6/7] Add option to set the modification date/time This also adds an API function for getting an Unix timestamp (and updates the man page) Co-authored-by: GitHub Copilot --- lib/file.c | 15 ++++++++++ lib/libunshield.h | 1 + man/unshield.1 | 7 +++-- src/unshield.c | 70 ++++++++++++++++++++++++++++++++++++----------- 4 files changed, 75 insertions(+), 18 deletions(-) diff --git a/lib/file.c b/lib/file.c index 9c9c511c..ea9c43a8 100644 --- a/lib/file.c +++ b/lib/file.c @@ -990,6 +990,21 @@ bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t si return fd ? true : false; }/*}}}*/ +time_t unshield_file_timestamp (Unshield* unshield, int index)/*{{{*/ +{ + FileDescriptor* fd = unshield_get_file_descriptor(unshield, index); + struct tm date; + + if (fd) + { + unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date); + return mktime(&date); + } + + unshield_warning("Failed to get file descriptor %i", index); + return 0; +}/*}}}*/ + bool unshield_file_save_raw(Unshield* unshield, int index, const char* filename) { /* XXX: Thou Shalt Not Cut & Paste... */ diff --git a/lib/libunshield.h b/lib/libunshield.h index c6a37d7d..2b3ec2b9 100644 --- a/lib/libunshield.h +++ b/lib/libunshield.h @@ -129,6 +129,7 @@ UNSHIELD_API bool unshield_file_save (Unshield* unshield, int in UNSHIELD_API int unshield_file_directory (Unshield* unshield, int index); UNSHIELD_API size_t unshield_file_size (Unshield* unshield, int index); UNSHIELD_API bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t size); +UNSHIELD_API time_t unshield_file_timestamp (Unshield* unshield, int index); /** For investigation of compressed data */ UNSHIELD_API bool unshield_file_save_raw(Unshield* unshield, int index, const char* filename); diff --git a/man/unshield.1 b/man/unshield.1 index 284dc67d..6476fda6 100644 --- a/man/unshield.1 +++ b/man/unshield.1 @@ -1,8 +1,8 @@ -.TH UNSHIELD "1" "November 2023" "The Unshield project" "https://github.com/twogood/unshield" +.TH UNSHIELD "1" "January 2026" "The Unshield project" "https://github.com/twogood/unshield" .SH NAME unshield \- extract CAB files from an InstallShield installer archive .SH SYNOPSIS -unshield [\-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...] +unshield [\-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...] .SH DESCRIPTION Unshield extracts CAB files from InstallShield installers, used to install software on Microsoft Windows based machines. @@ -50,6 +50,9 @@ Save raw data (do not decompress) \fB\-R\fR Don't do any conversion to file and directory names when extracting .TP +\fB\-t\fR +Set the modification date/time from the metadata when extracting +.TP \fB\-V\fR, \fB\-\-version\fR Print version information .SS "Commands:" diff --git a/src/unshield.c b/src/unshield.c index 61d2c8a4..b99514b6 100644 --- a/src/unshield.c +++ b/src/unshield.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #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); @@ -367,6 +374,7 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) bool success = false; char* dirname; char* filename; + char* full_filename; char* p; int directory = unshield_file_directory(unshield, index); long int path_max; @@ -384,7 +392,7 @@ 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); + full_filename = malloc(path_max); if (real_output_directory == NULL || real_filename == NULL) { fprintf(stderr,"Unable to allocate memory."); @@ -486,10 +494,11 @@ 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)); + filename = 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 +510,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,7 +523,7 @@ 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, @@ -522,40 +531,69 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) { 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) + { + 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); + } + + if (ret < 0 || ret2 < 0) + fprintf(stderr, "Failed to set timestamp for file '%s'.", filename); + } + } + 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); + unlink(full_filename); exit_status = 1; } free(real_filename); free(real_output_directory); free(dirname); - free(filename); + free(full_filename); return success; } From 8049058a48063de526e91a78d1eeb5a24a964a82 Mon Sep 17 00:00:00 2001 From: David Eriksson Date: Sun, 18 Oct 2026 20:19:29 +0200 Subject: [PATCH 7/7] Fix timestamp validation and extraction error paths --- lib/file.c | 21 ++++----- lib/helper.c | 35 +++++++------- lib/internal.h | 2 +- lib/libunshield.h | 1 + src/unshield.c | 17 +++---- test/CMakeLists.txt | 10 ++++ test/timestamps.c | 107 ++++++++++++++++++++++++++++++++++++++++++ test/timestamps.cmake | 73 ++++++++++++++++++++++++++++ 8 files changed, 228 insertions(+), 38 deletions(-) create mode 100644 test/timestamps.c create mode 100644 test/timestamps.cmake diff --git a/lib/file.c b/lib/file.c index ea9c43a8..3e58efb8 100644 --- a/lib/file.c +++ b/lib/file.c @@ -978,16 +978,11 @@ bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t si return false; } - if (fd) - unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date); - else - { - unshield_warning("Failed to get file descriptor %i", index); - memset(&date, 0, sizeof(date)); - } + buf[0] = '\0'; + if (!fd || !unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date)) + return false; - (void)strftime(buf, size, "%Y-%m-%d %H:%M:%S", &date); - return fd ? true : false; + return strftime(buf, size, "%Y-%m-%d %H:%M:%S", &date) != 0; }/*}}}*/ time_t unshield_file_timestamp (Unshield* unshield, int index)/*{{{*/ @@ -995,13 +990,13 @@ time_t unshield_file_timestamp (Unshield* unshield, int index)/*{{{*/ FileDescriptor* fd = unshield_get_file_descriptor(unshield, index); struct tm date; - if (fd) + if (fd && unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date)) { - unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date); - return mktime(&date); + time_t timestamp = mktime(&date); + if (timestamp != (time_t)-1) + return timestamp; } - unshield_warning("Failed to get file descriptor %i", index); return 0; }/*}}}*/ diff --git a/lib/helper.c b/lib/helper.c index 5734f74d..45d5dfba 100644 --- a/lib/helper.c +++ b/lib/helper.c @@ -269,25 +269,28 @@ const char* unshield_header_get_string(Header* header, uint32_t offset) /** Based on MIT Wine code and DosDateTimeToFileTime() MSDN documentation */ -void unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm) +bool unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm) { - memset(tm, 0, sizeof(*tm)); + static const int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int year, days; + memset(tm, 0, sizeof(*tm)); tm->tm_isdst = -1; + tm->tm_year = (dos_date >> 9) + 80; + tm->tm_mon = ((dos_date >> 5) & 0x0f) - 1; + tm->tm_mday = dos_date & 0x1f; + tm->tm_hour = dos_time >> 11; + tm->tm_min = (dos_time >> 5) & 0x3f; + tm->tm_sec = (dos_time & 0x1f) * 2; + + if (tm->tm_mon < 0 || tm->tm_mon > 11 || + tm->tm_hour > 23 || tm->tm_min > 59 || tm->tm_sec > 59) + return false; - if (dos_date) - { - tm->tm_year = (dos_date >> 9) + 80; - tm->tm_mon = ((dos_date >> 5) & 0x0f) - 1; - tm->tm_mday = dos_date & 0x1f; - } - - if (dos_time) - { - tm->tm_hour = dos_time >> 11; - tm->tm_min = (dos_time >> 5) & 0x3f; - tm->tm_sec = (dos_time & 0x1f) * 2; - } + year = tm->tm_year + 1900; + days = month_days[tm->tm_mon]; + if (tm->tm_mon == 1 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) + days++; + return tm->tm_mday >= 1 && tm->tm_mday <= days; } - diff --git a/lib/internal.h b/lib/internal.h index 3051bca1..14b70bb7 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -87,7 +87,7 @@ const char* unshield_header_get_string(Header* header, uint32_t offset); uint8_t* unshield_header_get_buffer(Header* header, uint32_t offset); char *unshield_get_last_path_separator(char *path); -void unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm); +bool unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* tm); static inline void* unshield_fopen(Unshield* unshield, const char *filename, const char *modes) { diff --git a/lib/libunshield.h b/lib/libunshield.h index 2b3ec2b9..4fa81b44 100644 --- a/lib/libunshield.h +++ b/lib/libunshield.h @@ -129,6 +129,7 @@ UNSHIELD_API bool unshield_file_save (Unshield* unshield, int in UNSHIELD_API int unshield_file_directory (Unshield* unshield, int index); UNSHIELD_API size_t unshield_file_size (Unshield* unshield, int index); UNSHIELD_API bool unshield_file_iso_date (Unshield* unshield, int index, char* buf, size_t size); +/* Returns 0 if the date is missing, invalid, or cannot be converted to time_t. */ UNSHIELD_API time_t unshield_file_timestamp (Unshield* unshield, int index); /** For investigation of compressed data */ diff --git a/src/unshield.c b/src/unshield.c index b99514b6..e5062822 100644 --- a/src/unshield.c +++ b/src/unshield.c @@ -373,7 +373,7 @@ 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); @@ -392,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); - full_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; @@ -494,8 +495,6 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) make_sure_directory_exists(dirname); - filename = unshield_file_name(unshield, index); - snprintf(full_filename, path_max, "%s%s", dirname, filename); for (p = full_filename + strlen(dirname); *p != '\0'; p++) @@ -559,7 +558,7 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) struct utimbuf utim; struct stat info; - if (!timestamp) + if (!timestamp || timestamp == (time_t)-1) { fprintf(stderr, "Failed to get timestamp for file '%s'.", filename); } @@ -587,7 +586,8 @@ static bool extract_file(Unshield* unshield, const char* prefix, int index) fprintf(stderr, "Failed to extract file '%s'.%s\n", filename, (log_level < 3) ? "Run unshield again with -D 3 for more information." : ""); - unlink(full_filename); + if (full_filename) + unlink(full_filename); exit_status = 1; } free(real_filename); @@ -730,7 +730,8 @@ static int list_files_helper(Unshield* unshield, const char* prefix, int first, { valid_count++; - (void)unshield_file_iso_date(unshield, i, date, sizeof(date)); + if (!unshield_file_iso_date(unshield, i, date, sizeof(date))) + strcpy(date, "-------------------"); if (prefix && prefix[0]) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a075212b..d6ad7c76 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,3 +12,13 @@ else() add_subdirectory(v5/CVE-2015-1386) endif() #add_subdirectory(v5/stack_memory_file) + +add_executable(test_timestamps timestamps.c) +target_link_libraries(test_timestamps libunshield) +add_test(NAME timestamps + COMMAND ${CMAKE_COMMAND} + -DUNSHIELD=$ + -DPROBE=$ + -DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/timestamps + -P ${CMAKE_CURRENT_SOURCE_DIR}/timestamps.cmake) +set_tests_properties(timestamps PROPERTIES ENVIRONMENT "TZ=UTC0") diff --git a/test/timestamps.c b/test/timestamps.c new file mode 100644 index 00000000..70dc8dcc --- /dev/null +++ b/test/timestamps.c @@ -0,0 +1,107 @@ +#ifdef NDEBUG +#undef NDEBUG +#endif +#include +#include +#include +#include +#include +#include +#include +#include "libunshield.h" + +static void put32(uint8_t* p, uint32_t value) +{ + for (int i = 0; i < 4; i++) + p[i] = (uint8_t)(value >> (8 * i)); +} + +static void write_file(const char* name, const uint8_t* data, size_t size) +{ + FILE* file = fopen(name, "wb"); + assert(file); + assert(fwrite(data, 1, size, file) == size); + assert(fclose(file) == 0); +} + +int main(int argc, char** argv) +{ + assert(argc >= 3); + if (strcmp(argv[1], "prepare") == 0) + { + /* Minimal IS5 archive: one uncompressed file in one file group. */ + uint8_t header[0x400] = {0}; + uint8_t cabinet[0x45] = {0}; + uint8_t* descriptor = header + 0x2a8; + Unshield* archive; + char date[32]; + assert(argc == 6); + put32(header, 0x28635349); + put32(header + 4, 0x01005000); + put32(header + 12, 0x10); + put32(header + 16, sizeof(header) - 0x10); + put32(header + 0x1c, 0x290); /* file table, relative to cabinet descriptor */ + put32(header + 0x24, 8); + put32(header + 0x28, 8); + put32(header + 0x2c, 1); /* directory count */ + put32(header + 0x38, 1); /* file count */ + put32(header + 0x4e, 0x350); /* file group offset list */ + put32(header + 0x2a0, 0x60); /* directory name */ + put32(header + 0x2a4, 8); /* file descriptor */ + put32(descriptor, 0x50); /* file name */ + put32(descriptor + 10, 5); + put32(descriptor + 14, 5); + put32(descriptor + 22, (uint32_t)strtoul(argv[2], NULL, 0)); + put32(descriptor + 26, (uint32_t)strtoul(argv[3], NULL, 0)); + put32(descriptor + 38, 0x40); /* data offset */ + memcpy(header + 0x2f0, "test.txt", 9); + memcpy(header + 0x300, "files", 6); + put32(header + 0x310, 0x370); /* group name */ + put32(header + 0x360, 0x370); + put32(header + 0x364, 0x300); /* group descriptor */ + memcpy(header + 0x380, "group", 6); + memcpy(cabinet, header, 20); + put32(cabinet + 36, 0x40); + put32(cabinet + 40, 5); + put32(cabinet + 44, 5); + put32(cabinet + 48, 0x40); + put32(cabinet + 52, 5); + put32(cabinet + 56, 5); + memcpy(cabinet + 0x40, "TEST", 5); + write_file("data1.hdr", header, sizeof(header)); + write_file("data1.cab", cabinet, sizeof(cabinet)); + archive = unshield_open("data1.cab"); + assert(archive); + if (strcmp(argv[4], "missing") == 0) + { + assert(!unshield_file_iso_date(archive, 0, date, sizeof(date))); + assert(date[0] == '\0'); + assert(unshield_file_timestamp(archive, 0) == 0); + } + else + { + assert(unshield_file_iso_date(archive, 0, date, sizeof(date))); + assert(strcmp(date, argv[4]) == 0); + assert(unshield_file_timestamp(archive, 0) == (time_t)strtoll(argv[5], NULL, 10)); + } + unshield_close(archive); + } + else + { + struct stat info; + char content[5]; + FILE* file = fopen(argv[2], "rb"); + assert(argc == 4); + assert(file); + assert(fread(content, 1, sizeof(content), file) == sizeof(content)); + assert(memcmp(content, "TEST", sizeof(content)) == 0); + assert(fclose(file) == 0); + assert(stat(argv[2], &info) == 0); + if (strcmp(argv[3], "current") != 0) + assert(info.st_mtime == (time_t)strtoll(argv[3], NULL, 10)); + else + assert(difftime(time(NULL), info.st_mtime) >= -2 && + difftime(time(NULL), info.st_mtime) < 60); + } + return 0; +} diff --git a/test/timestamps.cmake b/test/timestamps.cmake new file mode 100644 index 00000000..53f4a312 --- /dev/null +++ b/test/timestamps.cmake @@ -0,0 +1,73 @@ +file(MAKE_DIRECTORY "${TEST_DIR}") + +function(run) + execute_process(COMMAND ${ARGV} WORKING_DIRECTORY "${TEST_DIR}" + RESULT_VARIABLE result OUTPUT_VARIABLE output ERROR_VARIABLE error) + if(NOT result STREQUAL "0") + message(FATAL_ERROR "${ARGV}: ${result}\n${output}\n${error}") + endif() + set(output "${output}" PARENT_SCOPE) +endfunction() + +# DOS date/time: 2024-06-15 12:34:56, zero date, invalid calendar/time fields. +foreach(case valid valid_midnight valid_leap missing missing_zero month day february leap_century hour minute second) + set(date 0x58cf) + set(time 0x645c) + set(expected missing) + set(timestamp 0) + if(case STREQUAL "valid") + set(expected "2024-06-15 12:34:56") + set(timestamp 1718454896) + elseif(case STREQUAL "valid_midnight") + set(time 0) + set(expected "2024-06-15 00:00:00") + set(timestamp 1718409600) + elseif(case STREQUAL "valid_leap") + set(date 0x585d) + set(expected "2024-02-29 12:34:56") + set(timestamp 1709210096) + elseif(case STREQUAL "missing") + set(date 0) + elseif(case STREQUAL "month") + set(date 0x59af) + elseif(case STREQUAL "day") + set(date 0x58c0) + elseif(case STREQUAL "february") + set(date 0x585e) + elseif(case STREQUAL "leap_century") + set(date 0xf05d) # 2100-02-29 is not a leap day. + elseif(case STREQUAL "hour") + set(time 0xc45c) + elseif(case STREQUAL "minute") + set(time 0x679c) + elseif(case STREQUAL "second") + set(time 0x645e) + else() + set(date 0) + set(time 0) + endif() + run("${PROBE}" prepare "${date}" "${time}" "${expected}" "${timestamp}") + run("${UNSHIELD}" -t -d "${case}" x data1.cab) + if(NOT expected STREQUAL "missing") + run("${PROBE}" check "${case}/group/files/test.txt" "${timestamp}") + run("${UNSHIELD}" -d default x data1.cab) + run("${PROBE}" check default/group/files/test.txt current) + else() + run("${PROBE}" check "${case}/group/files/test.txt" current) + endif() +endforeach() + +# Listing must not invent a date for invalid metadata. +run("${UNSHIELD}" l data1.cab) +if(NOT output MATCHES "-------------------") + message(FATAL_ERROR "Missing date not marked in listing: ${output}") +endif() + +# Fail before constructing the file path; diagnostics still need the file name. +string(REPEAT x 8192 long_directory) +execute_process(COMMAND "${UNSHIELD}" -d "${long_directory}" x data1.cab + WORKING_DIRECTORY "${TEST_DIR}" RESULT_VARIABLE result + OUTPUT_VARIABLE output ERROR_VARIABLE error) +if(NOT result STREQUAL "1" OR NOT error MATCHES "Failed to extract file 'test.txt'") + message(FATAL_ERROR "Early extraction failure: ${result}\n${error}") +endif()