Skip to content
Open
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
2 changes: 2 additions & 0 deletions lib/cabfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ typedef struct
uint64_t data_offset;
uint8_t md5[16];
uint16_t volume;
uint16_t dos_date;
uint16_t dos_time;
uint32_t link_previous;
uint32_t link_next;
uint8_t link_flags;
Expand Down
51 changes: 48 additions & 3 deletions lib/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ static FileDescriptor* unshield_read_file_descriptor(Unshield* unshield, int ind

fd->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;
Comment on lines +69 to +71

Copilot AI Jan 21, 2026

Copy link

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.

Copilot uses AI. Check for mistakes.

fd->data_offset = READ_UINT32(p); p += 4;

#if VERBOSE >= 2
Expand All @@ -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

Expand Down Expand Up @@ -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 ++;
Expand Down Expand Up @@ -955,6 +966,40 @@ 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;
}

buf[0] = '\0';
if (!fd || !unshield_dos_to_tm(fd->dos_date, fd->dos_time, &date))
return false;

return strftime(buf, size, "%Y-%m-%d %H:%M:%S", &date) != 0;
}/*}}}*/

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))
{
time_t timestamp = mktime(&date);
if (timestamp != (time_t)-1)
return timestamp;
}

return 0;
}/*}}}*/

uint64_t unshield_file_size_compressed(Unshield* unshield, int index)/*{{{*/
{
FileDescriptor* fd = unshield_get_file_descriptor(unshield, index);
Expand Down
27 changes: 27 additions & 0 deletions lib/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,31 @@ 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
*/
bool unshield_dos_to_tm(uint16_t dos_date, uint16_t dos_time, struct tm* 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;

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;
}

2 changes: 2 additions & 0 deletions lib/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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)
{
return unshield->io_callbacks->fopen(filename, modes, unshield->io_userdata);
Expand Down
4 changes: 4 additions & 0 deletions lib/libunshield.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <time.h>
#include <stdint.h>

#define UNSHIELD_LOG_LEVEL_LOWEST 0
Expand Down Expand Up @@ -134,6 +135,9 @@ 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);
/* 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);
UNSHIELD_API uint64_t unshield_file_size_compressed(Unshield* unshield, int index);
/** Raw file flags (UNSHIELD_FILE_* bits), or 0 if the index is invalid. */
UNSHIELD_API uint16_t unshield_file_flags_raw (Unshield* unshield, int index);
Expand Down
7 changes: 5 additions & 2 deletions man/unshield.1
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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:"
Expand Down
85 changes: 64 additions & 21 deletions src/unshield.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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

Copilot AI Jan 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code uses POSIX utime() and struct utimbuf without Windows-specific handling. On Windows, these may not be available or may need to use _utime() and struct _utimbuf instead. Consider adding platform-specific code or using a more portable approach like utimensat/futimens on systems that support them, or adding Windows-specific equivalents with conditional compilation.

Copilot uses AI. Check for mistakes.
}

if (ret < 0 || ret2 < 0)
fprintf(stderr, "Failed to set timestamp for file '%s'.", filename);
Comment on lines +563 to +579

Copilot AI Jan 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error messages on lines 561 and 577 are missing a newline character at the end, which is inconsistent with other error messages in this file and can make the output harder to read. Add '\n' at the end of both error messages.

Copilot uses AI. Check for mistakes.
}
}
Comment on lines +554 to +581

Copilot AI Jan 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new timestamp setting functionality (-t flag) lacks test coverage. Consider adding tests that verify: 1) timestamps are correctly set when the -t flag is used, 2) extraction still works when timestamps cannot be set, 3) the timestamp values match expected DOS date/time conversions. This would help ensure the feature works correctly across different platforms and edge cases.

Copilot uses AI. Check for mistakes.

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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
Loading
Loading