1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <fnmatch.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <libgen.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include <set>
32 #include <string>
33
34 #include <android-base/file.h>
35 #include <android-base/strings.h>
36 #include <ziparchive/zip_archive.h>
37
38 using android::base::EndsWith;
39 using android::base::StartsWith;
40
41 enum OverwriteMode {
42 kAlways,
43 kNever,
44 kPrompt,
45 };
46
47 enum Role {
48 kUnzip,
49 kZipinfo,
50 };
51
52 static Role role;
53 static OverwriteMode overwrite_mode = kPrompt;
54 static bool flag_1 = false;
55 static std::string flag_d;
56 static bool flag_j = false;
57 static bool flag_l = false;
58 static bool flag_p = false;
59 static bool flag_q = false;
60 static bool flag_v = false;
61 static bool flag_x = false;
62 static const char* archive_name = nullptr;
63 static std::set<std::string> includes;
64 static std::set<std::string> excludes;
65 static uint64_t total_uncompressed_length = 0;
66 static uint64_t total_compressed_length = 0;
67 static size_t file_count = 0;
68
69 static const char* g_progname;
70
die(int error,const char * fmt,...)71 static void die(int error, const char* fmt, ...) {
72 va_list ap;
73
74 va_start(ap, fmt);
75 fprintf(stderr, "%s: ", g_progname);
76 vfprintf(stderr, fmt, ap);
77 if (error != 0) fprintf(stderr, ": %s", strerror(error));
78 fprintf(stderr, "\n");
79 va_end(ap);
80 exit(1);
81 }
82
ShouldInclude(const std::string & name)83 static bool ShouldInclude(const std::string& name) {
84 // Explicitly excluded?
85 if (!excludes.empty()) {
86 for (const auto& exclude : excludes) {
87 if (!fnmatch(exclude.c_str(), name.c_str(), 0)) return false;
88 }
89 }
90
91 // Implicitly included?
92 if (includes.empty()) return true;
93
94 // Explicitly included?
95 for (const auto& include : includes) {
96 if (!fnmatch(include.c_str(), name.c_str(), 0)) return true;
97 }
98 return false;
99 }
100
MakeDirectoryHierarchy(const std::string & path)101 static bool MakeDirectoryHierarchy(const std::string& path) {
102 // stat rather than lstat because a symbolic link to a directory is fine too.
103 struct stat sb;
104 if (stat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return true;
105
106 // Ensure the parent directories exist first.
107 if (!MakeDirectoryHierarchy(android::base::Dirname(path))) return false;
108
109 // Then try to create this directory.
110 return (mkdir(path.c_str(), 0777) != -1);
111 }
112
CompressionRatio(int64_t uncompressed,int64_t compressed)113 static float CompressionRatio(int64_t uncompressed, int64_t compressed) {
114 if (uncompressed == 0) return 0;
115 return static_cast<float>(100LL * (uncompressed - compressed)) /
116 static_cast<float>(uncompressed);
117 }
118
MaybeShowHeader(ZipArchiveHandle zah)119 static void MaybeShowHeader(ZipArchiveHandle zah) {
120 if (role == kUnzip) {
121 // unzip has three formats.
122 if (!flag_q) printf("Archive: %s\n", archive_name);
123 if (flag_v) {
124 printf(
125 " Length Method Size Cmpr Date Time CRC-32 Name\n"
126 "-------- ------ ------- ---- ---------- ----- -------- ----\n");
127 } else if (flag_l) {
128 printf(
129 " Length Date Time Name\n"
130 "--------- ---------- ----- ----\n");
131 }
132 } else {
133 // zipinfo.
134 if (!flag_1 && includes.empty() && excludes.empty()) {
135 ZipArchiveInfo info{GetArchiveInfo(zah)};
136 printf("Archive: %s\n", archive_name);
137 printf("Zip file size: %" PRId64 " bytes, number of entries: %" PRIu64 "\n",
138 info.archive_size, info.entry_count);
139 }
140 }
141 }
142
MaybeShowFooter()143 static void MaybeShowFooter() {
144 if (role == kUnzip) {
145 if (flag_v) {
146 printf(
147 "-------- ------- --- -------\n"
148 "%8" PRId64 " %8" PRId64 " %3.0f%% %zu file%s\n",
149 total_uncompressed_length, total_compressed_length,
150 CompressionRatio(total_uncompressed_length, total_compressed_length), file_count,
151 (file_count == 1) ? "" : "s");
152 } else if (flag_l) {
153 printf(
154 "--------- -------\n"
155 "%9" PRId64 " %zu file%s\n",
156 total_uncompressed_length, file_count, (file_count == 1) ? "" : "s");
157 }
158 } else {
159 if (!flag_1 && includes.empty() && excludes.empty()) {
160 printf("%zu files, %" PRId64 " bytes uncompressed, %" PRId64 " bytes compressed: %.1f%%\n",
161 file_count, total_uncompressed_length, total_compressed_length,
162 CompressionRatio(total_uncompressed_length, total_compressed_length));
163 }
164 }
165 }
166
PromptOverwrite(const std::string & dst)167 static bool PromptOverwrite(const std::string& dst) {
168 // TODO: [r]ename not implemented because it doesn't seem useful.
169 printf("replace %s? [y]es, [n]o, [A]ll, [N]one: ", dst.c_str());
170 fflush(stdout);
171 while (true) {
172 char* line = nullptr;
173 size_t n;
174 if (getline(&line, &n, stdin) == -1) {
175 die(0, "(EOF/read error; assuming [N]one...)");
176 overwrite_mode = kNever;
177 return false;
178 }
179 if (n == 0) continue;
180 char cmd = line[0];
181 free(line);
182 switch (cmd) {
183 case 'y':
184 return true;
185 case 'n':
186 return false;
187 case 'A':
188 overwrite_mode = kAlways;
189 return true;
190 case 'N':
191 overwrite_mode = kNever;
192 return false;
193 }
194 }
195 }
196
ExtractToPipe(ZipArchiveHandle zah,const ZipEntry64 & entry,const std::string & name)197 static void ExtractToPipe(ZipArchiveHandle zah, const ZipEntry64& entry, const std::string& name) {
198 // We need to extract to memory because ExtractEntryToFile insists on
199 // being able to seek and truncate, and you can't do that with stdout.
200 if (entry.uncompressed_length > SIZE_MAX) {
201 die(0, "entry size %" PRIu64 " is too large to extract.", entry.uncompressed_length);
202 }
203 auto uncompressed_length = static_cast<size_t>(entry.uncompressed_length);
204 uint8_t* buffer = new uint8_t[uncompressed_length];
205 int err = ExtractToMemory(zah, &entry, buffer, uncompressed_length);
206 if (err < 0) {
207 die(0, "failed to extract %s: %s", name.c_str(), ErrorCodeString(err));
208 }
209 if (!android::base::WriteFully(1, buffer, uncompressed_length)) {
210 die(errno, "failed to write %s to stdout", name.c_str());
211 }
212 delete[] buffer;
213 }
214
ExtractOne(ZipArchiveHandle zah,const ZipEntry64 & entry,std::string name)215 static void ExtractOne(ZipArchiveHandle zah, const ZipEntry64& entry, std::string name) {
216 // Bad filename?
217 if (StartsWith(name, "/") || StartsWith(name, "../") || name.find("/../") != std::string::npos) {
218 die(0, "bad filename %s", name.c_str());
219 }
220
221 // Junk the path if we were asked to.
222 if (flag_j) name = android::base::Basename(name);
223
224 // Where are we actually extracting to (for human-readable output)?
225 // flag_d is the empty string if -d wasn't used, or has a trailing '/'
226 // otherwise.
227 std::string dst = flag_d + name;
228
229 // Ensure the directory hierarchy exists.
230 if (!MakeDirectoryHierarchy(android::base::Dirname(name))) {
231 die(errno, "couldn't create directory hierarchy for %s", dst.c_str());
232 }
233
234 // An entry in a zip file can just be a directory itself.
235 if (EndsWith(name, "/")) {
236 if (mkdir(name.c_str(), entry.unix_mode) == -1) {
237 // If the directory already exists, that's fine.
238 if (errno == EEXIST) {
239 struct stat sb;
240 if (stat(name.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) return;
241 }
242 die(errno, "couldn't extract directory %s", dst.c_str());
243 }
244 return;
245 }
246
247 // Create the file.
248 int fd = open(name.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC | O_EXCL, entry.unix_mode);
249 if (fd == -1 && errno == EEXIST) {
250 if (overwrite_mode == kNever) return;
251 if (overwrite_mode == kPrompt && !PromptOverwrite(dst)) return;
252 // Either overwrite_mode is kAlways or the user consented to this specific case.
253 fd = open(name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, entry.unix_mode);
254 }
255 if (fd == -1) die(errno, "couldn't create file %s", dst.c_str());
256
257 // Actually extract into the file.
258 if (!flag_q) printf(" inflating: %s\n", dst.c_str());
259 int err = ExtractEntryToFile(zah, &entry, fd);
260 if (err < 0) die(0, "failed to extract %s: %s", dst.c_str(), ErrorCodeString(err));
261 close(fd);
262 }
263
ListOne(const ZipEntry64 & entry,const std::string & name)264 static void ListOne(const ZipEntry64& entry, const std::string& name) {
265 tm t = entry.GetModificationTime();
266 char time[32];
267 snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1,
268 t.tm_mday, t.tm_hour, t.tm_min);
269 if (flag_v) {
270 printf("%8" PRIu64 " %s %8" PRIu64 " %3.0f%% %s %08x %s\n", entry.uncompressed_length,
271 (entry.method == kCompressStored) ? "Stored" : "Defl:N", entry.compressed_length,
272 CompressionRatio(entry.uncompressed_length, entry.compressed_length), time, entry.crc32,
273 name.c_str());
274 } else {
275 printf("%9" PRIu64 " %s %s\n", entry.uncompressed_length, time, name.c_str());
276 }
277 }
278
InfoOne(const ZipEntry64 & entry,const std::string & name)279 static void InfoOne(const ZipEntry64& entry, const std::string& name) {
280 if (flag_1) {
281 // "android-ndk-r19b/sources/android/NOTICE"
282 printf("%s\n", name.c_str());
283 return;
284 }
285
286 int version = entry.version_made_by & 0xff;
287 int os = (entry.version_made_by >> 8) & 0xff;
288
289 // TODO: Support suid/sgid? Non-Unix/non-FAT host file system attributes?
290 const char* src_fs = "???";
291 char mode[] = "??? ";
292 if (os == 0) {
293 src_fs = "fat";
294 // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
295 int attrs = entry.external_file_attributes & 0xff;
296 mode[0] = (attrs & 0x10) ? 'd' : '-';
297 mode[1] = 'r';
298 mode[2] = (attrs & 0x01) ? '-' : 'w';
299 // The man page also mentions ".btm", but that seems to be obsolete?
300 mode[3] = EndsWith(name, ".exe") || EndsWith(name, ".com") || EndsWith(name, ".bat") ||
301 EndsWith(name, ".cmd")
302 ? 'x'
303 : '-';
304 mode[4] = (attrs & 0x20) ? 'a' : '-';
305 mode[5] = (attrs & 0x02) ? 'h' : '-';
306 mode[6] = (attrs & 0x04) ? 's' : '-';
307 } else if (os == 3) {
308 src_fs = "unx";
309 mode[0] = S_ISDIR(entry.unix_mode) ? 'd' : (S_ISREG(entry.unix_mode) ? '-' : '?');
310 mode[1] = entry.unix_mode & S_IRUSR ? 'r' : '-';
311 mode[2] = entry.unix_mode & S_IWUSR ? 'w' : '-';
312 mode[3] = entry.unix_mode & S_IXUSR ? 'x' : '-';
313 mode[4] = entry.unix_mode & S_IRGRP ? 'r' : '-';
314 mode[5] = entry.unix_mode & S_IWGRP ? 'w' : '-';
315 mode[6] = entry.unix_mode & S_IXGRP ? 'x' : '-';
316 mode[7] = entry.unix_mode & S_IROTH ? 'r' : '-';
317 mode[8] = entry.unix_mode & S_IWOTH ? 'w' : '-';
318 mode[9] = entry.unix_mode & S_IXOTH ? 'x' : '-';
319 }
320
321 char method[5] = "stor";
322 if (entry.method == kCompressDeflated) {
323 snprintf(method, sizeof(method), "def%c", "NXFS"[(entry.gpbf >> 1) & 0x3]);
324 }
325
326 // TODO: zipinfo (unlike unzip) sometimes uses time zone?
327 // TODO: this uses 4-digit years because we're not barbarians unless interoperability forces it.
328 tm t = entry.GetModificationTime();
329 char time[32];
330 snprintf(time, sizeof(time), "%04d-%02d-%02d %02d:%02d", t.tm_year + 1900, t.tm_mon + 1,
331 t.tm_mday, t.tm_hour, t.tm_min);
332
333 // "-rw-r--r-- 3.0 unx 577 t- defX 19-Feb-12 16:09 android-ndk-r19b/sources/android/NOTICE"
334 printf("%s %2d.%d %s %8" PRIu64 " %c%c %s %s %s\n", mode, version / 10, version % 10, src_fs,
335 entry.uncompressed_length, entry.is_text ? 't' : 'b',
336 entry.has_data_descriptor ? 'X' : 'x', method, time, name.c_str());
337 }
338
ProcessOne(ZipArchiveHandle zah,const ZipEntry64 & entry,const std::string & name)339 static void ProcessOne(ZipArchiveHandle zah, const ZipEntry64& entry, const std::string& name) {
340 if (role == kUnzip) {
341 if (flag_l || flag_v) {
342 // -l or -lv or -lq or -v.
343 ListOne(entry, name);
344 } else {
345 // Actually extract.
346 if (flag_p) {
347 ExtractToPipe(zah, entry, name);
348 } else {
349 ExtractOne(zah, entry, name);
350 }
351 }
352 } else {
353 // zipinfo or zipinfo -1.
354 InfoOne(entry, name);
355 }
356 total_uncompressed_length += entry.uncompressed_length;
357 total_compressed_length += entry.compressed_length;
358 ++file_count;
359 }
360
ProcessAll(ZipArchiveHandle zah)361 static void ProcessAll(ZipArchiveHandle zah) {
362 MaybeShowHeader(zah);
363
364 // libziparchive iteration order doesn't match the central directory.
365 // We could sort, but that would cost extra and wouldn't match either.
366 void* cookie;
367 int err = StartIteration(zah, &cookie);
368 if (err != 0) {
369 die(0, "couldn't iterate %s: %s", archive_name, ErrorCodeString(err));
370 }
371
372 ZipEntry64 entry;
373 std::string name;
374 while ((err = Next(cookie, &entry, &name)) >= 0) {
375 if (ShouldInclude(name)) ProcessOne(zah, entry, name);
376 }
377
378 if (err < -1) die(0, "failed iterating %s: %s", archive_name, ErrorCodeString(err));
379 EndIteration(cookie);
380
381 MaybeShowFooter();
382 }
383
ShowHelp(bool full)384 static void ShowHelp(bool full) {
385 if (role == kUnzip) {
386 fprintf(full ? stdout : stderr, "usage: unzip [-d DIR] [-lnopqv] ZIP [FILE...] [-x FILE...]\n");
387 if (!full) exit(EXIT_FAILURE);
388
389 printf(
390 "\n"
391 "Extract FILEs from ZIP archive. Default is all files. Both the include and\n"
392 "exclude (-x) lists use shell glob patterns.\n"
393 "\n"
394 "-d DIR Extract into DIR\n"
395 "-j Junk (ignore) file paths\n"
396 "-l List contents (-lq excludes archive name, -lv is verbose)\n"
397 "-n Never overwrite files (default: prompt)\n"
398 "-o Always overwrite files\n"
399 "-p Pipe to stdout\n"
400 "-q Quiet\n"
401 "-v List contents verbosely\n"
402 "-x FILE Exclude files\n");
403 } else {
404 fprintf(full ? stdout : stderr, "usage: zipinfo [-1] ZIP [FILE...] [-x FILE...]\n");
405 if (!full) exit(EXIT_FAILURE);
406
407 printf(
408 "\n"
409 "Show information about FILEs from ZIP archive. Default is all files.\n"
410 "Both the include and exclude (-x) lists use shell glob patterns.\n"
411 "\n"
412 "-1 Show filenames only, one per line\n"
413 "-x FILE Exclude files\n");
414 }
415 exit(EXIT_SUCCESS);
416 }
417
HandleCommonOption(int opt)418 static void HandleCommonOption(int opt) {
419 switch (opt) {
420 case 'h':
421 ShowHelp(true);
422 break;
423 case 'x':
424 flag_x = true;
425 break;
426 case 1:
427 // -x swallows all following arguments, so we use '-' in the getopt
428 // string and collect files here.
429 if (!archive_name) {
430 archive_name = optarg;
431 } else if (flag_x) {
432 excludes.insert(optarg);
433 } else {
434 includes.insert(optarg);
435 }
436 break;
437 default:
438 ShowHelp(false);
439 break;
440 }
441 }
442
main(int argc,char * argv[])443 int main(int argc, char* argv[]) {
444 // Who am I, and what am I doing?
445 g_progname = basename(argv[0]);
446 if (!strcmp(g_progname, "ziptool") && argc > 1) return main(argc - 1, argv + 1);
447 if (!strcmp(g_progname, "unzip")) {
448 role = kUnzip;
449 } else if (!strcmp(g_progname, "zipinfo")) {
450 role = kZipinfo;
451 } else {
452 die(0, "run as ziptool with unzip or zipinfo as the first argument, or symlink");
453 }
454
455 static const struct option opts[] = {
456 {"help", no_argument, 0, 'h'},
457 {},
458 };
459
460 if (role == kUnzip) {
461 // `unzip -Z` is "zipinfo mode", so in that case just restart...
462 if (argc > 1 && !strcmp(argv[1], "-Z")) {
463 argv[1] = const_cast<char*>("zipinfo");
464 return main(argc - 1, argv + 1);
465 }
466
467 int opt;
468 while ((opt = getopt_long(argc, argv, "-d:hjlnopqvx", opts, nullptr)) != -1) {
469 switch (opt) {
470 case 'd':
471 flag_d = optarg;
472 if (!EndsWith(flag_d, "/")) flag_d += '/';
473 break;
474 case 'j':
475 flag_j = true;
476 break;
477 case 'l':
478 flag_l = true;
479 break;
480 case 'n':
481 overwrite_mode = kNever;
482 break;
483 case 'o':
484 overwrite_mode = kAlways;
485 break;
486 case 'p':
487 flag_p = flag_q = true;
488 break;
489 case 'q':
490 flag_q = true;
491 break;
492 case 'v':
493 flag_v = true;
494 break;
495 default:
496 HandleCommonOption(opt);
497 break;
498 }
499 }
500 } else {
501 int opt;
502 while ((opt = getopt_long(argc, argv, "-1hx", opts, nullptr)) != -1) {
503 switch (opt) {
504 case '1':
505 flag_1 = true;
506 break;
507 default:
508 HandleCommonOption(opt);
509 break;
510 }
511 }
512 }
513
514 if (!archive_name) die(0, "missing archive filename");
515
516 // We can't support "-" to unzip from stdin because libziparchive relies on mmap.
517 ZipArchiveHandle zah;
518 int32_t err;
519 if ((err = OpenArchive(archive_name, &zah)) != 0) {
520 die(0, "couldn't open %s: %s", archive_name, ErrorCodeString(err));
521 }
522
523 // Implement -d by changing into that directory.
524 // We'll create implicit directories based on paths in the zip file, and we'll create
525 // the -d directory itself, but we require that *parents* of the -d directory already exists.
526 // This is pretty arbitrary, but it's the behavior of the original unzip.
527 if (!flag_d.empty()) {
528 if (mkdir(flag_d.c_str(), 0777) == -1 && errno != EEXIST) {
529 die(errno, "couldn't created %s", flag_d.c_str());
530 }
531 if (chdir(flag_d.c_str()) == -1) {
532 die(errno, "couldn't chdir to %s", flag_d.c_str());
533 }
534 }
535
536 ProcessAll(zah);
537
538 CloseArchive(zah);
539 return 0;
540 }
541