Skip to content

Commit

Permalink
Don’t convert time stamps from DOS to Unix and back.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Apr 22, 2024
1 parent f23d1cc commit ac29119
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 92 deletions.
29 changes: 16 additions & 13 deletions lib/zip_close.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,7 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de, zip_uint32_t changed) {

/* PKWare encryption uses last_mod, make sure it gets the right value. */
if (de->changed & ZIP_DIRENT_LAST_MOD) {
zip_stat_t st_mtime;
zip_stat_init(&st_mtime);
st_mtime.valid = ZIP_STAT_MTIME;
st_mtime.mtime = _zip_d2u_time(&de->last_mod);
if ((src_tmp = _zip_source_window_new(src_final, 0, -1, &st_mtime, 0, NULL, NULL, 0, true, &za->error)) == NULL) {
if ((src_tmp = _zip_source_window_new(src_final, 0, -1, NULL, 0, NULL, &de->last_mod, NULL, 0, true, &za->error)) == NULL) {
zip_source_free(src_final);
return -1;
}
Expand Down Expand Up @@ -529,16 +525,23 @@ add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de, zip_uint32_t changed) {
}

if ((de->changed & ZIP_DIRENT_LAST_MOD) == 0) {
time_t mtime;
if (st.valid & ZIP_STAT_MTIME) {
mtime = st.mtime;
}
else {
time(&mtime);
}
if (_zip_u2d_time(st.mtime, &de->last_mod.time, &de->last_mod.date, &za->error) < 0) {
int ret2 = zip_source_get_dos_time(src, &de->last_mod);
if (ret2 < 0) {
zip_error_set_from_source(&za->error, src);
return -1;
}
if (ret2 == 0) {
time_t mtime;
if (st.valid & ZIP_STAT_MTIME) {
mtime = st.mtime;
}
else {
time(&mtime);
}
if (_zip_u2d_time(st.mtime, &de->last_mod, &za->error) < 0) {
return -1;
}
}
}
de->comp_method = st.comp_method;
de->crc = st.crc;
Expand Down
10 changes: 5 additions & 5 deletions lib/zip_dirent.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,14 +1111,14 @@ _zip_get_dirent(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *err


int
_zip_u2d_time(time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate, zip_error_t *ze) {
_zip_u2d_time(time_t intime, zip_dostime_t *dtime, zip_error_t *ze) {
struct tm *tpm;
struct tm tm;
tpm = zip_localtime(&intime, &tm);
if (tpm == NULL) {
/* if localtime fails, return an arbitrary date (1980-01-01 00:00:00) */
*ddate = (1 << 5) + 1;
*dtime = 0;
dtime->date = (1 << 5) + 1;
dtime->time = 0;
if (ze) {
zip_error_set(ze, ZIP_ER_INVAL, errno);
}
Expand All @@ -1128,8 +1128,8 @@ _zip_u2d_time(time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate, zip_error
tpm->tm_year = 80;
}

*ddate = (zip_uint16_t)(((tpm->tm_year + 1900 - 1980) << 9) + ((tpm->tm_mon + 1) << 5) + tpm->tm_mday);
*dtime = (zip_uint16_t)(((tpm->tm_hour) << 11) + ((tpm->tm_min) << 5) + ((tpm->tm_sec) >> 1));
dtime->date = (zip_uint16_t)(((tpm->tm_year + 1900 - 1980) << 9) + ((tpm->tm_mon + 1) << 5) + tpm->tm_mday);
dtime->time = (zip_uint16_t)(((tpm->tm_hour) << 11) + ((tpm->tm_min) << 5) + ((tpm->tm_sec) >> 1));

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/zip_file_set_mtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ zip_file_set_dostime(zip_t *za, zip_uint64_t idx, zip_uint16_t dtime, zip_uint16

ZIP_EXTERN int
zip_file_set_mtime(zip_t *za, zip_uint64_t idx, time_t mtime, zip_flags_t flags) {
zip_uint16_t ddate, dtime;
zip_dostime_t dostime;

if (_zip_u2d_time(mtime, &dtime, &ddate, &za->error) < 0) {
if (_zip_u2d_time(mtime, &dostime, &za->error) < 0) {
return -1;
}

return zip_file_set_dostime(za, idx, dtime, ddate, flags);
return zip_file_set_dostime(za, idx, dostime.time, dostime.date, flags);
}
2 changes: 1 addition & 1 deletion lib/zip_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ zip_check_torrentzip(zip_t *za, const zip_cdir_t *cdir) {
st.valid |= ZIP_STAT_SIZE | ZIP_STAT_CRC;
st.size = cdir->size;
st.crc = crc_should;
if ((src_window = _zip_source_window_new(za->src, cdir->offset, cdir->size, &st, 0, NULL, NULL, 0, false, NULL)) == NULL) {
if ((src_window = _zip_source_window_new(za->src, cdir->offset, cdir->size, &st, 0, NULL, NULL, NULL, 0, false, NULL)) == NULL) {
return;
}
if ((src_crc = zip_source_crc_create(src_window, 1, NULL)) == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion lib/zip_source_pass_to_lower_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ zip_int64_t zip_source_pass_to_lower_layer(zip_source_t *src, void *data, zip_ui

case ZIP_SOURCE_ACCEPT_EMPTY:
case ZIP_SOURCE_ERROR:
case ZIP_SOURCE_GET_DOS_TIME:
case ZIP_SOURCE_READ:
case ZIP_SOURCE_SEEK:
case ZIP_SOURCE_TELL:
return _zip_source_call(src, data, length, command);


case ZIP_SOURCE_BEGIN_WRITE:
case ZIP_SOURCE_BEGIN_WRITE_CLONING:
case ZIP_SOURCE_COMMIT_WRITE:
Expand Down
41 changes: 19 additions & 22 deletions lib/zip_source_pkware_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ zip_source_pkware_decode(zip_t *za, zip_source_t *src, zip_uint16_t em, int flag
static int
decrypt_header(zip_source_t *src, struct trad_pkware *ctx) {
zip_uint8_t header[ZIP_CRYPTO_PKWARE_HEADERLEN];
struct zip_stat st;
zip_stat_t st;
zip_dostime_t dostime;
zip_int64_t n;
bool ok = false;

if ((n = zip_source_read(src, header, ZIP_CRYPTO_PKWARE_HEADERLEN)) < 0) {
zip_error_set_from_source(&ctx->error, src);
Expand All @@ -96,38 +96,35 @@ decrypt_header(zip_source_t *src, struct trad_pkware *ctx) {

_zip_pkware_decrypt(&ctx->keys, header, header, ZIP_CRYPTO_PKWARE_HEADERLEN);

if (zip_source_stat(src, &st)) {
/* stat failed, skip password validation */
if (zip_source_stat(src, &st) < 0 || (st.valid & ZIP_STAT_CRC) == 0) {
/* skip password validation */
return 0;
}

/* password verification - two ways:
* mtime - InfoZIP way, to avoid computing complete CRC before encrypting data
* CRC - old PKWare way
*/
if (zip_source_get_dos_time(src, &dostime) <= 0) {
if ((st.valid & ZIP_STAT_MTIME) == 0) {
/* no date available, skip password validation */
return 0;
}

if (st.valid & ZIP_STAT_MTIME) {
unsigned short dostime, dosdate;
if (_zip_u2d_time(st.mtime, &dostime, &dosdate, &ctx->error) < 0) {
if (_zip_u2d_time(st.mtime, &dostime, &ctx->error) < 0) {
return -1;
}
if (header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == dostime >> 8) {
ok = true;
}
}

if (st.valid & ZIP_STAT_CRC) {
if (header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == st.crc >> 24) {
ok = true;
}
/*
password verification - two ways:
- mtime - InfoZIP way, to avoid computing complete CRC before encrypting data
- CRC - old PKWare way
*/
if (header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == dostime.time >> 8
|| header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == st.crc >> 24) {
return 0;
}

if (!ok && ((st.valid & (ZIP_STAT_MTIME | ZIP_STAT_CRC)) != 0)) {
else {
zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0);
return -1;
}

return 0;
}


Expand Down
63 changes: 26 additions & 37 deletions lib/zip_source_pkware_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ struct trad_pkware {
zip_pkware_keys_t keys;
zip_buffer_t *buffer;
bool eof;
bool mtime_set;
time_t mtime;
zip_dostime_t dostime;
zip_error_t error;
};

Expand All @@ -52,7 +51,6 @@ static int encrypt_header(zip_source_t *, struct trad_pkware *);
static zip_int64_t pkware_encrypt(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
static void trad_pkware_free(struct trad_pkware *);
static struct trad_pkware *trad_pkware_new(const char *password, zip_error_t *error);
static void set_mtime(struct trad_pkware* ctx, zip_stat_t* st);

zip_source_t *
zip_source_pkware_encode(zip_t *za, zip_source_t *src, zip_uint16_t em, int flags, const char *password) {
Expand All @@ -69,9 +67,24 @@ zip_source_pkware_encode(zip_t *za, zip_source_t *src, zip_uint16_t em, int flag
}

if ((ctx = trad_pkware_new(password, &za->error)) == NULL) {
zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}

if (zip_source_get_dos_time(src, &ctx->dostime) <= 0) {
zip_stat_t st;

if (zip_source_stat(src, &st) < 0) {
zip_error_set_from_source(&za->error, src);
trad_pkware_free(ctx);
return NULL;
}
if (_zip_u2d_time((st.valid & ZIP_STAT_MTIME) ? st.mtime : time(NULL), &ctx->dostime, &za->error) < 0) {
trad_pkware_free(ctx);
return NULL;
}
}

if ((s2 = zip_source_layered(za, src, pkware_encrypt, ctx)) == NULL) {
trad_pkware_free(ctx);
return NULL;
Expand All @@ -83,22 +96,8 @@ zip_source_pkware_encode(zip_t *za, zip_source_t *src, zip_uint16_t em, int flag

static int
encrypt_header(zip_source_t *src, struct trad_pkware *ctx) {
unsigned short dostime, dosdate;
zip_uint8_t *header;

if (!ctx->mtime_set) {
struct zip_stat st;
if (zip_source_stat(src, &st) != 0) {
zip_error_set_from_source(&ctx->error, src);
return -1;
}
set_mtime(ctx, &st);
}

if (_zip_u2d_time(ctx->mtime, &dostime, &dosdate, &ctx->error) < 0) {
return -1;
}

if ((ctx->buffer = _zip_buffer_new(NULL, ZIP_CRYPTO_PKWARE_HEADERLEN)) == NULL) {
zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
return -1;
Expand All @@ -114,7 +113,7 @@ encrypt_header(zip_source_t *src, struct trad_pkware *ctx) {
ctx->buffer = NULL;
return -1;
}
header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] = (zip_uint8_t)((dostime >> 8) & 0xff);
header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] = (zip_uint8_t)((ctx->dostime.time >> 8) & 0xff);

_zip_pkware_encrypt(&ctx->keys, header, header, ZIP_CRYPTO_PKWARE_HEADERLEN);

Expand Down Expand Up @@ -189,9 +188,6 @@ pkware_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, zip
if (st->valid & ZIP_STAT_COMP_SIZE) {
st->comp_size += ZIP_CRYPTO_PKWARE_HEADERLEN;
}
set_mtime(ctx, st);
st->mtime = ctx->mtime;
st->valid |= ZIP_STAT_MTIME;

return 0;
}
Expand All @@ -208,8 +204,16 @@ pkware_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, zip
return 0;
}

case ZIP_SOURCE_GET_DOS_TIME:
if (length < sizeof(ctx->dostime)) {
zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
return -1;
}
(void)memcpy_s(data, sizeof(ctx->dostime), &ctx->dostime, sizeof(ctx->dostime));
return sizeof(ctx->dostime);

case ZIP_SOURCE_SUPPORTS:
return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_GET_FILE_ATTRIBUTES, -1);
return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_GET_FILE_ATTRIBUTES, ZIP_SOURCE_GET_DOS_TIME, -1);

case ZIP_SOURCE_ERROR:
return zip_error_to_data(&ctx->error, data, length);
Expand Down Expand Up @@ -239,8 +243,6 @@ trad_pkware_new(const char *password, zip_error_t *error) {
return NULL;
}
ctx->buffer = NULL;
ctx->mtime_set = false;
ctx->mtime = 0;
zip_error_init(&ctx->error);

return ctx;
Expand All @@ -258,16 +260,3 @@ trad_pkware_free(struct trad_pkware *ctx) {
zip_error_fini(&ctx->error);
free(ctx);
}


static void set_mtime(struct trad_pkware* ctx, zip_stat_t* st) {
if (!ctx->mtime_set) {
if (st->valid & ZIP_STAT_MTIME) {
ctx->mtime = st->mtime;
}
else {
time(&ctx->mtime);
}
ctx->mtime_set = true;
}
}
28 changes: 25 additions & 3 deletions lib/zip_source_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct window {
zip_stat_t stat;
zip_uint64_t stat_invalid;
zip_file_attributes_t attributes;
zip_dostime_t dostime;
bool dostime_valid;
zip_error_t error;
zip_int64_t supports;
bool needs_seek;
Expand All @@ -61,12 +63,12 @@ static zip_int64_t window_read(zip_source_t *, void *, void *, zip_uint64_t, zip

ZIP_EXTERN zip_source_t *
zip_source_window_create(zip_source_t *src, zip_uint64_t start, zip_int64_t len, zip_error_t *error) {
return _zip_source_window_new(src, start, len, NULL, 0, NULL, NULL, 0, false, error);
return _zip_source_window_new(src, start, len, NULL, 0, NULL, NULL, NULL, 0, false, error);
}


zip_source_t *
_zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_int64_t length, zip_stat_t *st, zip_uint64_t st_invalid, zip_file_attributes_t *attributes, zip_t *source_archive, zip_uint64_t source_index, bool take_ownership, zip_error_t *error) {
_zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_int64_t length, zip_stat_t *st, zip_uint64_t st_invalid, zip_file_attributes_t *attributes, zip_dostime_t *dostime, zip_t *source_archive, zip_uint64_t source_index, bool take_ownership, zip_error_t *error) {
zip_source_t* window_source;
struct window *ctx;

Expand Down Expand Up @@ -103,10 +105,17 @@ _zip_source_window_new(zip_source_t *src, zip_uint64_t start, zip_int64_t length
else {
zip_file_attributes_init(&ctx->attributes);
}
if (dostime != NULL) {
ctx->dostime = *dostime;
ctx->dostime_valid = true;
}
else {
ctx->dostime_valid = false;
}
ctx->source_archive = source_archive;
ctx->source_index = source_index;
zip_error_init(&ctx->error);
ctx->supports = (zip_source_supports(src) & (ZIP_SOURCE_SUPPORTS_SEEKABLE | ZIP_SOURCE_SUPPORTS_REOPEN)) | (zip_source_make_command_bitmap(ZIP_SOURCE_GET_FILE_ATTRIBUTES, ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, ZIP_SOURCE_FREE, -1));
ctx->supports = (zip_source_supports(src) & (ZIP_SOURCE_SUPPORTS_SEEKABLE | ZIP_SOURCE_SUPPORTS_REOPEN)) | (zip_source_make_command_bitmap(ZIP_SOURCE_GET_FILE_ATTRIBUTES, ZIP_SOURCE_GET_DOS_TIME, ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, ZIP_SOURCE_FREE, -1));
ctx->needs_seek = (ctx->supports & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_SEEK)) ? true : false;

if (st) {
Expand Down Expand Up @@ -309,6 +318,19 @@ window_read(zip_source_t *src, void *_ctx, void *data, zip_uint64_t len, zip_sou
(void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes));
return sizeof(ctx->attributes);

case ZIP_SOURCE_GET_DOS_TIME:
if (len < sizeof(ctx->dostime)) {
zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
return -1;
}
if (ctx->dostime_valid) {
(void)memcpy_s(data, sizeof(ctx->dostime), &ctx->dostime, sizeof(ctx->dostime));
return sizeof(ctx->dostime);
}
else {
return 0;
}

case ZIP_SOURCE_SUPPORTS:
return ctx->supports;

Expand Down
Loading

0 comments on commit ac29119

Please sign in to comment.