1 /* ------------------------------------------------------------------------ */
3 /* lhext.c -- LHarc extract */
5 /* Copyright (C) MCMLXXXIX Yooichi.Tagawa */
6 /* Modified Nobutaka Watazaki */
8 /* Ver. 0.00 Original 1988.05.23 Y.Tagawa */
9 /* Ver. 1.00 Fixed 1989.09.22 Y.Tagawa */
10 /* Ver. 0.03 LHa for UNIX 1991.12.17 M.Oki */
11 /* Ver. 1.12 LHa for UNIX 1993.10.01 N.Watazaki */
12 /* Ver. 1.13b Symbolic Link Update Bug Fix 1994.06.21 N.Watazaki */
13 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
14 /* Ver. 1.14e bugfix 1999.04.30 T.Okamoto */
15 /* ------------------------------------------------------------------------ */
17 /* ------------------------------------------------------------------------ */
18 static int skip_flg = FALSE; /* FALSE..No Skip , TRUE..Skip */
19 static char *methods[] =
21 LZHUFF0_METHOD, LZHUFF1_METHOD, LZHUFF2_METHOD, LZHUFF3_METHOD,
22 LZHUFF4_METHOD, LZHUFF5_METHOD, LZHUFF6_METHOD, LZHUFF7_METHOD,
23 LARC_METHOD, LARC5_METHOD, LARC4_METHOD,
25 PMARC0_METHOD, PMARC2_METHOD,
29 static void add_dirinfo(char* name, LzHeader* hdr);
30 static void adjust_dirinfo();
32 #ifdef HAVE_LIBAPPLEFILE
33 static boolean decode_macbinary(FILE *ofp, size_t size, const char *outPath);
36 /* ------------------------------------------------------------------------ */
44 if (stat(name, &stbuf) >= 0) {
45 if (!is_regularfile(&stbuf)) {
46 error("\"%s\" already exists (not a file)", name);
51 printf("EXTRACT %s but file is exist.\n", name);
56 warning("skip to extract %s.", name);
60 switch (inquire("OverWrite ?(Yes/[No]/All/Skip)", name, "YyNnAaSs\n")) {
81 printf("EXTRACT %s\n", name);
87 make_name_with_pathcheck(char *name, size_t namesz, const char *dir, const char *q)
94 if (extract_directory) {
95 sz = xsnprintf(name, namesz, "%s/", extract_directory);
102 while ((p = strchr(q, '/')) != NULL) {
103 if (namesz - offset < (p - q) + 2) {
106 memcpy(name + offset, q, (p - q));
107 name[offset + (p - q)] = 0;
112 if (lstat(name, &stbuf) < 0) {
113 name[offset++] = '/';
116 if (is_symlink(&stbuf)) {
119 name[offset++] = '/';
122 str_safe_copy(name + offset, q, namesz - offset);
127 /* ------------------------------------------------------------------------ */
129 make_parent_path(name)
132 char path[FILENAME_LENGTH];
136 /* make parent directory name into PATH for recursive call */
137 str_safe_copy(path, name, sizeof(path));
138 for (p = path + strlen(path); p > path; p--)
145 message("invalid path name \"%s\"", name);
146 return FALSE; /* no more parent. */
149 if (GETSTAT(path, &stbuf) >= 0) {
150 if (is_directory(&stbuf))
155 message("Making directory \"%s\".", path);
157 #if defined __MINGW32__
158 if (mkdir(path) >= 0)
161 if (mkdir(path, 0777) >= 0) /* try */
162 return TRUE; /* successful done. */
165 if (!make_parent_path(path))
168 #if defined __MINGW32__
169 if (mkdir(path) < 0) { /* try again */
170 error("Cannot make directory \"%s\"", path);
174 if (mkdir(path, 0777) < 0) { /* try again */
175 error("Cannot make directory \"%s\"", path);
183 /* ------------------------------------------------------------------------ */
185 open_with_make_path(name)
190 if ((fp = fopen(name, WRITE_BINARY)) == NULL) {
191 if (!make_parent_path(name) ||
192 (fp = fopen(name, WRITE_BINARY)) == NULL)
193 error("Cannot extract a file \"%s\"", name);
198 /* ------------------------------------------------------------------------ */
200 adjust_info(name, hdr)
204 struct utimbuf utimebuf;
206 /* adjust file stamp */
207 utimebuf.actime = utimebuf.modtime = hdr->unix_last_modified_stamp;
209 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
210 utime(name, &utimebuf);
212 if (hdr->extend_type == EXTEND_UNIX
213 || hdr->extend_type == EXTEND_OS68K
214 || hdr->extend_type == EXTEND_XOSK) {
216 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK) {
217 chmod(name, hdr->unix_mode);
221 uid_t uid = hdr->unix_uid;
222 gid_t gid = hdr->unix_gid;
224 #if HAVE_GETPWNAM && HAVE_GETGRNAM
226 struct passwd *ent = getpwnam(hdr->user);
227 if (ent) uid = ent->pw_uid;
230 struct group *ent = getgrnam(hdr->group);
231 if (ent) gid = ent->gr_gid;
236 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
237 lchown(name, uid, gid);
239 #endif /* HAVE_LCHWON */
240 chown(name, uid, gid);
245 /* On Cygwin, execute permission should be set for .exe or .dll. */
248 umask(m = umask(0)); /* get current umask */
249 chmod(name, 0777 & ~m);
254 /* ------------------------------------------------------------------------ */
256 extract_one(afp, hdr)
257 FILE *afp; /* archive file */
260 FILE *fp; /* output file */
261 #if HAVE_LIBAPPLEFILE
262 FILE *tfp; /* temporary output file */
265 char name[FILENAME_LENGTH];
268 boolean save_quiet, save_verbose, up_flag;
269 char *q = hdr->name, c;
272 if (ignore_directory && strrchr(hdr->name, '/')) {
273 q = (char *) strrchr(hdr->name, '/') + 1;
276 if (is_directory_traversal(q)) {
277 fatal_error("Possible directory traversal hack attempt in %s\n", q);
281 while (*q == '/') { q++; }
284 * if OSK then strip device name
286 if (hdr->extend_type == EXTEND_OS68K
287 || hdr->extend_type == EXTEND_XOSK) {
290 while (c && c != '/');
292 q = "."; /* if device name only */
297 if (!make_name_with_pathcheck(name, sizeof(name), extract_directory, q)) {
298 fatal_error("Possible symlink traversal hack attempt in %s\n", q);
301 /* LZHDIRS_METHODを持つヘッダをチェックする */
302 /* 1999.4.30 t.okamoto */
303 for (method = 0;; method++) {
304 if (methods[method] == NULL) {
305 error("Unknown method \"%.*s\"; \"%s\" will be skipped ...",
306 5, hdr->method, name);
309 if (memcmp(hdr->method, methods[method], 5) == 0)
313 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
314 && method != LZHDIRS_METHOD_NUM) {
317 for (method = 0;; method++) {
318 if (methods[method] == NULL) {
319 error("Unknown method \"%.*s\"; \"%s\" will be skipped ...",
320 5, hdr->method, name);
323 if (memcmp(hdr->method, methods[method], 5) == 0)
328 reading_filename = archive_name;
329 writing_filename = name;
330 if (output_to_stdout || verify_mode) {
331 /* "Icon\r" should be a resource fork file encoded in MacBinary
332 format, so that it should be skipped. */
333 if (hdr->extend_type == EXTEND_MACOS
334 && strcmp(basename(name), "Icon\r") == 0
335 && decode_macbinary_contents) {
340 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
345 save_verbose = verbose;
346 if (!quiet && output_to_stdout) {
347 printf("::::::::\n%s\n::::::::\n", name);
351 else if (verify_mode) {
356 #if defined(__MINGW32__) || defined(__DJGPP__)
360 old_mode = setmode(fileno(stdout), O_BINARY);
363 #if HAVE_LIBAPPLEFILE
364 /* On default, MacLHA encodes into MacBinary. */
365 if (hdr->extend_type == EXTEND_MACOS && !verify_mode && decode_macbinary_contents) {
366 /* build temporary file */
367 tfp = NULL; /* avoid compiler warnings `uninitialized' */
368 tfp = build_temporary_file();
370 crc = decode_lzhuf(afp, tfp,
371 hdr->original_size, hdr->packed_size,
372 name, method, &read_size);
374 decode_macbinary(stdout, hdr->original_size, name);
375 unlink(temporary_name);
377 crc = decode_lzhuf(afp, stdout,
378 hdr->original_size, hdr->packed_size,
379 name, method, &read_size);
382 crc = decode_lzhuf(afp, stdout,
383 hdr->original_size, hdr->packed_size,
384 name, method, &read_size);
385 #endif /* HAVE_LIBAPPLEFILE */
386 #if defined(__MINGW32__) || defined(__DJGPP__)
388 setmode(fileno(stdout), old_mode);
392 verbose = save_verbose;
396 /* "Icon\r" should be a resource fork of parent folder's icon,
397 so that it can be skipped when system is not Mac OS X. */
398 if (hdr->extend_type == EXTEND_MACOS
399 && strcmp(basename(name), "Icon\r") == 0
400 && decode_macbinary_contents) {
401 make_parent_path(name); /* create directory only */
404 #endif /* __APPLE__ */
405 if (skip_flg == FALSE) {
406 up_flag = inquire_extract(name);
407 if (up_flag == FALSE && force == FALSE) {
412 if (skip_flg == TRUE) { /* if skip_flg */
413 if (stat(name, &stbuf) == 0 && force != TRUE) {
414 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
416 printf("%s : Skipped...\n", name);
425 signal(SIGINT, interrupt);
427 signal(SIGHUP, interrupt);
431 remove_extracting_file_when_interrupt = TRUE;
433 if ((fp = open_with_make_path(name)) != NULL) {
434 #if HAVE_LIBAPPLEFILE
435 if (hdr->extend_type == EXTEND_MACOS && !verify_mode && decode_macbinary_contents) {
436 /* build temporary file */
437 tfp = NULL; /* avoid compiler warnings `uninitialized' */
438 tfp = build_temporary_file();
440 crc = decode_lzhuf(afp, tfp,
441 hdr->original_size, hdr->packed_size,
442 name, method, &read_size);
444 decode_macbinary(fp, hdr->original_size, name);
446 /* TODO: set resource fork */
447 /* after processing, "Icon\r" is not needed. */
448 if (strcmp(basename(name), "Icon\r") == 0) {
451 #endif /* __APPLE__ */
452 unlink(temporary_name);
454 crc = decode_lzhuf(afp, fp,
455 hdr->original_size, hdr->packed_size,
456 name, method, &read_size);
458 #else /* HAVE_LIBAPPLEFILE */
459 crc = decode_lzhuf(afp, fp,
460 hdr->original_size, hdr->packed_size,
461 name, method, &read_size);
462 #endif /* HAVE_LIBAPPLEFILE */
465 remove_extracting_file_when_interrupt = FALSE;
466 signal(SIGINT, SIG_DFL);
468 signal(SIGHUP, SIG_DFL);
474 if (hdr->has_crc && crc != hdr->crc)
475 error("CRC error: \"%s\"", name);
477 else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
478 || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
479 || method == LZHDIRS_METHOD_NUM) {
480 /* ↑これで、Symbolic Link は、大丈夫か? */
481 if (!ignore_directory && !verify_mode) {
484 printf("EXTRACT %s (directory)\n", name);
487 /* NAME has trailing SLASH '/', (^_^) */
488 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
492 if (skip_flg == FALSE) {
493 up_flag = inquire_extract(name);
494 if (up_flag == FALSE && force == FALSE) {
498 if (GETSTAT(name, &stbuf) == 0 && force != TRUE) {
499 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
501 printf("%s : Skipped...\n", name);
508 make_parent_path(name);
509 l_code = symlink(hdr->realname, name);
512 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
513 name, hdr->realname);
516 message("Symbolic Link %s -> %s",
517 name, hdr->realname);
520 warning("Can't make Symbolic Link %s -> %s",
521 name, hdr->realname);
525 else { /* make directory */
526 if (!output_to_stdout && !make_parent_path(name))
528 /* save directory information */
529 add_dirinfo(name, hdr);
534 if (force) /* force extract */
535 goto extract_regular;
537 error("Unknown file type: \"%s\". use `f' option to force extract.", name);
540 if (!output_to_stdout && !verify_mode) {
541 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_DIRECTORY)
542 adjust_info(name, hdr);
548 /* ------------------------------------------------------------------------ */
549 /* EXTRACT COMMAND MAIN */
550 /* ------------------------------------------------------------------------ */
559 /* open archive file */
560 if ((afp = open_old_archive()) == NULL)
561 fatal_error("Cannot open archive file \"%s\"", archive_name);
563 if (archive_is_msdos_sfx1(archive_name))
564 seek_lha_header(afp);
566 /* extract each files */
567 while (get_header(afp, &hdr)) {
568 if (need_file(hdr.name)) {
570 read_size = extract_one(afp, &hdr);
571 if (read_size != hdr.packed_size) {
572 /* when error occurred in extract_one(), should adjust
573 point of file stream */
574 if (pos != -1 && afp != stdin)
575 fseeko(afp, pos + hdr.packed_size, SEEK_SET);
577 off_t i = hdr.packed_size - read_size;
578 while (i--) fgetc(afp);
583 fseeko(afp, hdr.packed_size, SEEK_CUR);
585 off_t i = hdr.packed_size;
586 while (i--) fgetc(afp);
591 /* close archive file */
594 /* adjust directory information */
601 is_directory_traversal(char *path)
605 for (; *path; path++) {
608 if (*path == '.') state = 1;
612 if (*path == '.') state = 2;
613 else if (*path == '/') state = 0;
617 if (*path == '/') return 1;
621 if (*path == '/') state = 0;
630 * restore directory information (timestamp, permission and uid/gid).
631 * added by A.Iriyama 2003.12.12
634 typedef struct LzHeaderList_t {
635 struct LzHeaderList_t *next;
639 static LzHeaderList *dirinfo;
641 static void add_dirinfo(char *name, LzHeader *hdr)
643 LzHeaderList *p, *tmp, top;
645 if (memcmp(hdr->method, LZHDIRS_METHOD, 5) != 0)
648 p = xmalloc(sizeof(LzHeaderList));
650 memcpy(&p->hdr, hdr, sizeof(LzHeader));
651 strncpy(p->hdr.name, name, sizeof(p->hdr.name));
652 p->hdr.name[sizeof(p->hdr.name)-1] = 0;
664 reverse sorted by pathname order
668 dirinfo->hdr.name = "a/b/d"
669 dirinfo->next->hdr.name = "a/b/c"
670 dirinfo->next->next->hdr.name = "a/b"
674 dirinfo->hdr.name = "a/b/d"
675 dirinfo->next->hdr.name = "a/b/c"
676 dirinfo->next->next->hdr.name = "a/b"
677 dirinfo->next->next->next->hdr.name = "a"
682 for (tmp = ⊤ tmp->next; tmp = tmp->next) {
683 if (strcmp(p->hdr.name, tmp->next->hdr.name) > 0) {
689 if (tmp->next == NULL) {
698 static void adjust_dirinfo()
701 /* message("adjusting [%s]", dirinfo->hdr.name); */
702 adjust_info(dirinfo->hdr.name, &dirinfo->hdr);
705 LzHeaderList *tmp = dirinfo;
706 dirinfo = dirinfo->next;
712 #if HAVE_LIBAPPLEFILE
714 decode_macbinary(ofp, size, outPath)
719 af_file_t *afp = NULL;
721 unsigned char *datap;
724 if ((afp = af_open(temporary_name)) != NULL) {
726 datap = af_data(afp, &dlen);
727 fwrite(datap, sizeof(unsigned char), dlen, ofp);
730 } else { /* it may be not encoded in MacBinary */
732 if ((ifp = fopen(temporary_name, READ_BINARY)) == NULL) {
733 error("Cannot open a temporary file \"%s\"", temporary_name);
736 copyfile(ifp, ofp, size, 0, 0);
743 #endif /* HAVE_LIBAPPLEFILE */