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 *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 error("Possible directory traversal hack attempt in %s", q);
282 while (*q == '/') { q++; }
285 * if OSK then strip device name
287 if (hdr->extend_type == EXTEND_OS68K
288 || hdr->extend_type == EXTEND_XOSK) {
291 while (c && c != '/');
293 q = "."; /* if device name only */
298 if (!make_name_with_pathcheck(name, sizeof(name), q)) {
299 error("Possible symlink traversal hack attempt in %s", q);
303 /* LZHDIRS_METHODを持つヘッダをチェックする */
304 /* 1999.4.30 t.okamoto */
305 for (method = 0;; method++) {
306 if (methods[method] == NULL) {
307 error("Unknown method \"%.*s\"; \"%s\" will be skipped ...",
308 5, hdr->method, name);
311 if (memcmp(hdr->method, methods[method], 5) == 0)
315 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
316 && method != LZHDIRS_METHOD_NUM) {
319 for (method = 0;; method++) {
320 if (methods[method] == NULL) {
321 error("Unknown method \"%.*s\"; \"%s\" will be skipped ...",
322 5, hdr->method, name);
325 if (memcmp(hdr->method, methods[method], 5) == 0)
330 reading_filename = archive_name;
331 writing_filename = name;
332 if (output_to_stdout || verify_mode) {
333 /* "Icon\r" should be a resource fork file encoded in MacBinary
334 format, so that it should be skipped. */
335 if (hdr->extend_type == EXTEND_MACOS
336 && strcmp(basename(name), "Icon\r") == 0
337 && decode_macbinary_contents) {
342 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
347 save_verbose = verbose;
348 if (!quiet && output_to_stdout) {
349 printf("::::::::\n%s\n::::::::\n", name);
353 else if (verify_mode) {
358 #if defined(__MINGW32__) || defined(__DJGPP__)
362 old_mode = setmode(fileno(stdout), O_BINARY);
365 #if HAVE_LIBAPPLEFILE
366 /* On default, MacLHA encodes into MacBinary. */
367 if (hdr->extend_type == EXTEND_MACOS && !verify_mode && decode_macbinary_contents) {
368 /* build temporary file */
369 tfp = NULL; /* avoid compiler warnings `uninitialized' */
370 tfp = build_temporary_file();
372 crc = decode_lzhuf(afp, tfp,
373 hdr->original_size, hdr->packed_size,
374 name, method, &read_size);
376 decode_macbinary(stdout, hdr->original_size, name);
377 unlink(temporary_name);
379 crc = decode_lzhuf(afp, stdout,
380 hdr->original_size, hdr->packed_size,
381 name, method, &read_size);
384 crc = decode_lzhuf(afp, stdout,
385 hdr->original_size, hdr->packed_size,
386 name, method, &read_size);
387 #endif /* HAVE_LIBAPPLEFILE */
388 #if defined(__MINGW32__) || defined(__DJGPP__)
390 setmode(fileno(stdout), old_mode);
394 verbose = save_verbose;
398 /* "Icon\r" should be a resource fork of parent folder's icon,
399 so that it can be skipped when system is not Mac OS X. */
400 if (hdr->extend_type == EXTEND_MACOS
401 && strcmp(basename(name), "Icon\r") == 0
402 && decode_macbinary_contents) {
403 make_parent_path(name); /* create directory only */
406 #endif /* __APPLE__ */
407 if (skip_flg == FALSE) {
408 up_flag = inquire_extract(name);
409 if (up_flag == FALSE && force == FALSE) {
414 if (skip_flg == TRUE) { /* if skip_flg */
415 if (stat(name, &stbuf) == 0 && force != TRUE) {
416 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
418 printf("%s : Skipped...\n", name);
427 signal(SIGINT, interrupt);
429 signal(SIGHUP, interrupt);
433 remove_extracting_file_when_interrupt = TRUE;
435 if ((fp = open_with_make_path(name)) != NULL) {
436 #if HAVE_LIBAPPLEFILE
437 if (hdr->extend_type == EXTEND_MACOS && !verify_mode && decode_macbinary_contents) {
438 /* build temporary file */
439 tfp = NULL; /* avoid compiler warnings `uninitialized' */
440 tfp = build_temporary_file();
442 crc = decode_lzhuf(afp, tfp,
443 hdr->original_size, hdr->packed_size,
444 name, method, &read_size);
446 decode_macbinary(fp, hdr->original_size, name);
448 /* TODO: set resource fork */
449 /* after processing, "Icon\r" is not needed. */
450 if (strcmp(basename(name), "Icon\r") == 0) {
453 #endif /* __APPLE__ */
454 unlink(temporary_name);
456 crc = decode_lzhuf(afp, fp,
457 hdr->original_size, hdr->packed_size,
458 name, method, &read_size);
460 #else /* HAVE_LIBAPPLEFILE */
461 crc = decode_lzhuf(afp, fp,
462 hdr->original_size, hdr->packed_size,
463 name, method, &read_size);
464 #endif /* HAVE_LIBAPPLEFILE */
467 remove_extracting_file_when_interrupt = FALSE;
468 signal(SIGINT, SIG_DFL);
470 signal(SIGHUP, SIG_DFL);
476 if (hdr->has_crc && crc != hdr->crc)
477 error("CRC error: \"%s\"", name);
479 else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
480 || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
481 || method == LZHDIRS_METHOD_NUM) {
482 /* ↑これで、Symbolic Link は、大丈夫か? */
483 if (!ignore_directory && !verify_mode) {
486 printf("EXTRACT %s (directory)\n", name);
489 /* NAME has trailing SLASH '/', (^_^) */
490 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
494 if (skip_flg == FALSE) {
495 up_flag = inquire_extract(name);
496 if (up_flag == FALSE && force == FALSE) {
500 if (GETSTAT(name, &stbuf) == 0 && force != TRUE) {
501 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
503 printf("%s : Skipped...\n", name);
510 make_parent_path(name);
511 l_code = symlink(hdr->realname, name);
514 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
515 name, hdr->realname);
518 message("Symbolic Link %s -> %s",
519 name, hdr->realname);
522 warning("Can't make Symbolic Link %s -> %s",
523 name, hdr->realname);
527 else { /* make directory */
528 if (!output_to_stdout && !make_parent_path(name))
530 /* save directory information */
531 add_dirinfo(name, hdr);
536 if (force) /* force extract */
537 goto extract_regular;
539 error("Unknown file type: \"%s\". use `f' option to force extract.", name);
542 if (!output_to_stdout && !verify_mode) {
543 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_DIRECTORY)
544 adjust_info(name, hdr);
550 /* ------------------------------------------------------------------------ */
551 /* EXTRACT COMMAND MAIN */
552 /* ------------------------------------------------------------------------ */
561 /* open archive file */
562 if ((afp = open_old_archive()) == NULL)
563 fatal_error("Cannot open archive file \"%s\"", archive_name);
565 if (archive_is_msdos_sfx1(archive_name))
566 seek_lha_header(afp);
568 /* extract each files */
569 while (get_header(afp, &hdr)) {
570 if (need_file(hdr.name)) {
572 read_size = extract_one(afp, &hdr);
573 if (read_size != hdr.packed_size) {
574 /* when error occurred in extract_one(), should adjust
575 point of file stream */
576 if (pos != -1 && afp != stdin)
577 fseeko(afp, pos + hdr.packed_size, SEEK_SET);
579 off_t i = hdr.packed_size - read_size;
580 while (i--) fgetc(afp);
585 fseeko(afp, hdr.packed_size, SEEK_CUR);
587 off_t i = hdr.packed_size;
588 while (i--) fgetc(afp);
593 /* close archive file */
596 /* adjust directory information */
603 is_directory_traversal(char *path)
607 for (; *path; path++) {
610 if (*path == '.') state = 1;
614 if (*path == '.') state = 2;
615 else if (*path == '/') state = 0;
619 if (*path == '/') return 1;
623 if (*path == '/') state = 0;
632 * restore directory information (timestamp, permission and uid/gid).
633 * added by A.Iriyama 2003.12.12
636 typedef struct LzHeaderList_t {
637 struct LzHeaderList_t *next;
641 static LzHeaderList *dirinfo;
643 static void add_dirinfo(char *name, LzHeader *hdr)
645 LzHeaderList *p, *tmp, top;
647 if (memcmp(hdr->method, LZHDIRS_METHOD, 5) != 0)
650 p = xmalloc(sizeof(LzHeaderList));
652 memcpy(&p->hdr, hdr, sizeof(LzHeader));
653 strncpy(p->hdr.name, name, sizeof(p->hdr.name));
654 p->hdr.name[sizeof(p->hdr.name)-1] = 0;
666 reverse sorted by pathname order
670 dirinfo->hdr.name = "a/b/d"
671 dirinfo->next->hdr.name = "a/b/c"
672 dirinfo->next->next->hdr.name = "a/b"
676 dirinfo->hdr.name = "a/b/d"
677 dirinfo->next->hdr.name = "a/b/c"
678 dirinfo->next->next->hdr.name = "a/b"
679 dirinfo->next->next->next->hdr.name = "a"
684 for (tmp = ⊤ tmp->next; tmp = tmp->next) {
685 if (strcmp(p->hdr.name, tmp->next->hdr.name) > 0) {
691 if (tmp->next == NULL) {
700 static void adjust_dirinfo()
703 /* message("adjusting [%s]", dirinfo->hdr.name); */
704 adjust_info(dirinfo->hdr.name, &dirinfo->hdr);
707 LzHeaderList *tmp = dirinfo;
708 dirinfo = dirinfo->next;
714 #if HAVE_LIBAPPLEFILE
716 decode_macbinary(ofp, size, outPath)
721 af_file_t *afp = NULL;
723 unsigned char *datap;
726 if ((afp = af_open(temporary_name)) != NULL) {
728 datap = af_data(afp, &dlen);
729 fwrite(datap, sizeof(unsigned char), dlen, ofp);
732 } else { /* it may be not encoded in MacBinary */
734 if ((ifp = fopen(temporary_name, READ_BINARY)) == NULL) {
735 error("Cannot open a temporary file \"%s\"", temporary_name);
738 copyfile(ifp, ofp, size, 0, 0);
745 #endif /* HAVE_LIBAPPLEFILE */