summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Mark Punzalan <markpun@google.com> 2023-09-06 21:32:07 +0000
committer Mark Punzalan <markpun@google.com> 2023-09-06 21:44:40 +0000
commit0d7190b41336d81d73a5e88047dd56d7cffef7bd (patch)
treea192048e6ecf35f20aa0729eac09164fa34378a1
parent2462269f1ffd2f0a2c8da4932309b21bc2baabb3 (diff)
[zip] Set all entry times before 1980 to 1980-01-01
Before this change, entry times before 1980 had the year changed to 1980 but the month/day/hour/minutes/seconds were all kept as-is. Bug: 277978832 Test: atest zipalign_tests Change-Id: I9f87e98a6b985002578490b87c654fee86c65d62
-rw-r--r--tools/zipalign/ZipEntry.cpp23
1 files changed, 16 insertions, 7 deletions
diff --git a/tools/zipalign/ZipEntry.cpp b/tools/zipalign/ZipEntry.cpp
index dd2eac60a8..0355e56238 100644
--- a/tools/zipalign/ZipEntry.cpp
+++ b/tools/zipalign/ZipEntry.cpp
@@ -363,13 +363,22 @@ void ZipEntry::setModWhen(time_t when)
struct tm tmResult;
struct tm* ptm = localtime_r(&even, &tmResult);
- int year;
- year = ptm->tm_year;
- if (year < 80)
- year = 80;
-
- uint16_t zdate = (year - 80) << 9 | (ptm->tm_mon+1) << 5 | ptm->tm_mday;
- uint16_t ztime = ptm->tm_hour << 11 | ptm->tm_min << 5 | ptm->tm_sec >> 1;
+ // The earliest valid time for ZIP file entries is 1980-01-01. See:
+ // https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip.html.
+ // Set any time before 1980 to 1980-01-01.
+ if (ptm->tm_year < 80) {
+ ptm->tm_year = 80;
+ ptm->tm_mon = 0;
+ ptm->tm_mday = 1;
+ ptm->tm_hour = 0;
+ ptm->tm_min = 0;
+ ptm->tm_sec = 0;
+ }
+
+ uint16_t zdate = static_cast<uint16_t>(
+ (ptm->tm_year - 80) << 9 | (ptm->tm_mon + 1) << 5 | ptm->tm_mday);
+ uint16_t ztime = static_cast<uint16_t>(
+ ptm->tm_hour << 11 | ptm->tm_min << 5 | ptm->tm_sec >> 1);
mCDE.mLastModFileTime = mLFH.mLastModFileTime = ztime;
mCDE.mLastModFileDate = mLFH.mLastModFileDate = zdate;