1 /* ------------------------------------------------------------------------ */
3 /* header.c -- header manipulate functions */
5 /* Modified Nobutaka Watazaki */
7 /* Original Y.Tagawa */
8 /* modified 1991.12.16 M.Oki */
9 /* Ver. 1.10 Symbolic Link added 1993.10.01 N.Watazaki */
10 /* Ver. 1.13b Symbolic Link Bug Fix 1994.08.22 N.Watazaki */
11 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
12 /* Ver. 1.14i bug fixed 2000.10.06 t.okamoto */
13 /* Ver. 1.14i Contributed UTF-8 convertion for Mac OS X */
14 /* 2002.06.29 Hiroto Sakai */
15 /* Ver. 1.14i autoconfiscated & rewritten 2003.02.23 Koji Arai */
16 /* ------------------------------------------------------------------------ */
19 #define DUMP_HEADER 1 /* for debugging */
21 #if !STRCHR_8BIT_CLEAN
22 /* should use 8 bit clean version */
25 #define strchr xstrchr
26 #define strrchr xstrrchr
30 #define GET_BYTE() (*get_ptr++ & 0xff)
33 static char *start_ptr;
34 #define setup_get(PTR) (start_ptr = get_ptr = (PTR))
35 #define get_byte() dump_get_byte()
36 #define skip_bytes(len) dump_skip_bytes(len)
38 #define setup_get(PTR) (get_ptr = (PTR))
39 #define get_byte() GET_BYTE()
40 #define skip_bytes(len) (get_ptr += (len))
42 #define put_ptr get_ptr
43 #define setup_put(PTR) (put_ptr = (PTR))
44 #define put_byte(c) (*put_ptr++ = (char)(c))
46 int optional_archive_kanji_code = NONE;
47 int optional_system_kanji_code = NONE;
48 char *optional_archive_delim = NULL;
49 char *optional_system_delim = NULL;
50 int optional_filename_case = NONE;
52 #ifdef MULTIBYTE_FILENAME
53 int default_system_kanji_code = MULTIBYTE_FILENAME;
55 int default_system_kanji_code = NONE;
65 while (len--) sum += *p++;
76 if (verbose_listing && verbose > 1)
77 printf("%02d %2d: ", get_ptr - start_ptr, 1);
79 if (verbose_listing && verbose > 1) {
81 printf("%d(0x%02x) '%c'\n", c, c, c);
83 printf("%d(0x%02x)\n", c, c);
93 if (verbose_listing && verbose > 1) {
94 printf("%02d %2d: ", get_ptr - start_ptr, len);
96 printf("0x%02x ", GET_BYTE());
97 printf("... ignored\n");
111 if (verbose_listing && verbose > 1)
112 printf("%02d %2d: ", get_ptr - start_ptr, 2);
118 if (verbose_listing && verbose > 1)
119 printf("%d(0x%04x)\n", w, w);
139 if (verbose_listing && verbose > 1)
140 printf("%02d %2d: ", get_ptr - start_ptr, 4);
146 l = (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
148 if (verbose_listing && verbose > 1)
149 printf("%ld(0x%08lx)\n", l, l);
165 get_bytes(buf, len, size)
172 if (verbose_listing && verbose > 1)
173 printf("%02d %2d: \"", get_ptr - start_ptr, len);
175 for (i = 0; i < len; i++) {
176 if (i < size) buf[i] = get_ptr[i];
178 if (verbose_listing && verbose > 1) {
180 printf("%c", buf[i]);
182 printf("\\x%02x", (unsigned char)buf[i]);
186 if (verbose_listing && verbose > 1)
189 for (i = 0; i < len && i < size; i++)
203 for (i = 0; i < len; i++)
207 /* added by Koji Arai */
209 convert_filename(name, len, size,
211 from_delim, to_delim,
214 int len; /* length of name */
215 int size; /* size of name buffer */
216 int from_code, to_code, case_to;
217 char *from_delim, *to_delim;
221 #ifdef MULTIBYTE_FILENAME
222 char tmp[FILENAME_LENGTH];
223 int to_code_save = NONE;
225 if (from_code == CODE_CAP) {
226 len = cap_to_sjis(tmp, name, sizeof(tmp));
227 strncpy(name, tmp, size);
230 from_code = CODE_SJIS;
233 if (to_code == CODE_CAP) {
234 to_code_save = CODE_CAP;
238 if (from_code == CODE_SJIS && to_code == CODE_UTF8) {
239 for (i = 0; i < len; i++)
240 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
241 if ((unsigned char)name[i] == LHA_PATHSEP) name[i] = '/';
242 sjis_to_utf8(tmp, name, sizeof(tmp));
243 strncpy(name, tmp, size);
246 for (i = 0; i < len; i++)
247 if (name[i] == '/') name[i] = LHA_PATHSEP;
248 from_code = CODE_UTF8;
250 else if (from_code == CODE_UTF8 && to_code == CODE_SJIS) {
251 for (i = 0; i < len; i++)
252 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
253 if ((unsigned char)name[i] == LHA_PATHSEP) name[i] = '/';
254 utf8_to_sjis(tmp, name, sizeof(tmp));
255 strncpy(name, tmp, size);
258 for (i = 0; i < len; i++)
259 if (name[i] == '/') name[i] = LHA_PATHSEP;
260 from_code = CODE_SJIS;
264 /* special case: if `name' has small lettter, not convert case. */
265 if (from_code == CODE_SJIS && case_to == TO_LOWER) {
266 for (i = 0; i < len; i++) {
267 #ifdef MULTIBYTE_FILENAME
268 if (SJIS_FIRST_P(name[i]) && SJIS_SECOND_P(name[i+1]))
272 if (islower(name[i])) {
279 for (i = 0; i < len; i ++) {
280 #ifdef MULTIBYTE_FILENAME
281 if (from_code == CODE_EUC &&
282 (unsigned char)name[i] == 0x8e) {
283 if (to_code != CODE_SJIS) {
289 memmove(name + i, name + i + 1, len - i);
293 if (from_code == CODE_SJIS && X0201_KANA_P(name[i])) {
294 if (to_code != CODE_EUC) {
298 if (len == size - 1) /* check overflow */
300 memmove(name+i+1, name+i, len-i);
306 if (from_code == CODE_EUC && (name[i] & 0x80) && (name[i+1] & 0x80)) {
308 if (to_code != CODE_SJIS) {
313 c1 = (unsigned char)name[i];
314 c2 = (unsigned char)name[i+1];
321 if (from_code == CODE_SJIS &&
322 SJIS_FIRST_P(name[i]) &&
323 SJIS_SECOND_P(name[i+1])) {
326 if (to_code != CODE_EUC) {
331 c1 = (unsigned char)name[i];
332 c2 = (unsigned char)name[i+1];
339 #endif /* MULTIBYTE_FILENAME */
343 /* transpose from_delim to to_delim */
345 if ((ptr = strchr(from_delim, name[i])) != NULL) {
346 name[i] = to_delim[ptr - from_delim];
351 if (case_to == TO_UPPER && islower(name[i])) {
352 name[i] = toupper(name[i]);
355 if (case_to == TO_LOWER && isupper(name[i])) {
356 name[i] = tolower(name[i]);
361 #ifdef MULTIBYTE_FILENAME
362 if (to_code_save == CODE_CAP) {
363 len = sjis_to_cap(tmp, name, sizeof(tmp));
364 strncpy(name, tmp, size);
368 #endif /* MULTIBYTE_FILENAME */
372 * Generic (MS-DOS style) time stamp format (localtime):
374 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
375 * |<---- year-1980 --->|<- month ->|<--- day ---->|
377 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
378 * |<--- hour --->|<---- minute --->|<- second/2 ->|
383 generic_to_unix_stamp(t)
388 #define subbits(n, off, len) (((n) >> (off)) & ((1 << (len))-1))
390 tm.tm_sec = subbits(t, 0, 5) * 2;
391 tm.tm_min = subbits(t, 5, 6);
392 tm.tm_hour = subbits(t, 11, 5);
393 tm.tm_mday = subbits(t, 16, 5);
394 tm.tm_mon = subbits(t, 21, 4) - 1;
395 tm.tm_year = subbits(t, 25, 7) + 80;
401 return timelocal(&tm);
406 unix_to_generic_stamp(t)
409 struct tm *tm = localtime(&t);
414 return ((long)(tm->tm_year << 25) +
416 (tm->tm_mday << 16) +
417 (tm->tm_hour << 11) +
423 wintime_to_unix_stamp()
427 uint64_t epoch = 0x019db1ded53e8000; /* 1970-01-01 00:00:00 (UTC) */
429 t = (unsigned long)get_longword();
430 t |= (uint64_t)(unsigned long)get_longword() << 32;
431 t = (t - epoch) / 10000000;
435 unsigned long t, q, x;
436 unsigned long wintime[8];
437 unsigned long epoch[8] = {0x01,0x9d,0xb1,0xde, 0xd5,0x3e,0x80,0x00};
438 /* 1970-01-01 00:00:00 (UTC) */
439 /* wintime -= epoch */
441 for (i = 7; i >= 0; i--) {
442 wintime[i] = (unsigned)get_byte() - epoch[i] - borrow;
443 borrow = (wintime[i] > 0xff) ? 1 : 0;
447 /* q = wintime / 10000000 */
449 x = 10000000; /* x: 24bit */
450 for (i = 0; i < 8; i++) {
451 t = (t << 8) + wintime[i]; /* 24bit + 8bit. t must be 32bit variable */
452 q <<= 8; /* q must be 32bit (time_t) */
464 * --------------------------------
466 * 2 or 4 next-header size [*1]
467 * --------------------------------------
468 * ext header: 1 ext-type ^
469 * ? contents | [*1] next-header size
470 * 2 or 4 next-header size v
471 * --------------------------------------
473 * on level 1, 2 header:
474 * size field is 2 bytes
476 * size field is 4 bytes
480 get_extended_header(fp, hdr, header_size, hcrc)
486 char data[LZHEADER_STORAGE];
488 char dirname[FILENAME_LENGTH];
491 ssize_t whole_size = header_size;
493 int n = 1 + hdr->size_field_length; /* `ext-type' + `next-header size' */
495 if (hdr->header_level == 0)
498 name_length = strlen(hdr->name);
500 while (header_size) {
502 if (sizeof(data) < header_size) {
503 error("header size (%ld) too large.", header_size);
507 if (fread(data, header_size, 1, fp) == 0) {
508 error("Invalid header (LHa file ?)");
512 ext_type = get_byte();
515 /* header crc (CRC-16) */
516 hdr->header_crc = get_word();
517 /* clear buffer for CRC calculation. */
518 data[1] = data[2] = 0;
519 skip_bytes(header_size - n - 2);
524 get_bytes(hdr->name, header_size-n, sizeof(hdr->name)-1);
525 hdr->name[name_length] = 0;
529 dir_length = get_bytes(dirname, header_size-n, sizeof(dirname)-1);
530 dirname[dir_length] = 0;
533 /* MS-DOS attribute */
534 hdr->attribute = get_word();
537 /* Windows time stamp (FILETIME structure) */
538 /* it is time in 100 nano seconds since 1601-01-01 00:00:00 */
540 skip_bytes(8); /* create time is ignored */
542 /* set last modified time */
543 if (hdr->header_level >= 2)
544 skip_bytes(8); /* time_t has been already set */
546 hdr->unix_last_modified_stamp = wintime_to_unix_stamp();
548 skip_bytes(8); /* last access time is ignored */
552 /* UNIX permission */
553 hdr->unix_mode = get_word();
556 /* UNIX gid and uid */
557 hdr->unix_gid = get_word();
558 hdr->unix_uid = get_word();
561 /* UNIX group name */
562 i = get_bytes(hdr->group, header_size-n, sizeof(hdr->group)-1);
563 hdr->group[i] = '\0';
567 i = get_bytes(hdr->user, header_size-n, sizeof(hdr->user)-1);
571 /* UNIX last modified time */
572 hdr->unix_last_modified_stamp = (time_t) get_longword();
576 /* 0x39: multi-disk header
577 0x3f: uncompressed comment
578 0x42: 64bit large file size
579 0x48-0x4f(?): reserved for authenticity verification
581 0x7e: extended attribute - platform information
582 0x7f: extended attribute - permission, owner-id and timestamp
584 0xc4: compressed comment (dict size: 4096)
585 0xc5: compressed comment (dict size: 8192)
586 0xc6: compressed comment (dict size: 16384)
587 0xc7: compressed comment (dict size: 32768)
588 0xc8: compressed comment (dict size: 65536)
589 0xd0-0xdf(?): operating systemm specific information
590 0xfc: encapsulation (another opinion)
591 0xfe: extended attribute - platform information(another opinion)
592 0xff: extended attribute - permission, owner-id and timestamp
593 (level 3 on UNLHA32) */
595 warning("unknown extended header 0x%02x", ext_type);
596 skip_bytes(header_size - n);
601 *hcrc = calccrc(*hcrc, data, header_size);
603 if (hdr->size_field_length == 2)
604 whole_size += header_size = get_word();
606 whole_size += header_size = get_longword();
609 /* concatenate dirname and filename */
611 if (name_length + dir_length >= sizeof(hdr->name)) {
612 warning("the length of pathname \"%s%s\" is too long.",
614 name_length = sizeof(hdr->name) - dir_length - 1;
615 hdr->name[name_length] = 0;
617 strcat(dirname, hdr->name);
618 strcpy(hdr->name, dirname);
619 name_length += dir_length;
625 #define I_HEADER_SIZE 0 /* level 0,1,2 */
626 #define I_HEADER_CHECKSUM 1 /* level 0,1 */
627 #define I_METHOD 2 /* level 0,1,2,3 */
628 #define I_PACKED_SIZE 7 /* level 0,1,2,3 */
629 #define I_ATTRIBUTE 19 /* level 0,1,2,3 */
630 #define I_HEADER_LEVEL 20 /* level 0,1,2,3 */
632 #define COMMON_HEADER_SIZE 21 /* size of common part */
634 #define I_GENERIC_HEADER_SIZE 24 /* + name_length */
635 #define I_LEVEL0_HEADER_SIZE 36 /* + name_length (unix extended) */
636 #define I_LEVEL1_HEADER_SIZE 27 /* + name_length */
637 #define I_LEVEL2_HEADER_SIZE 26 /* + padding */
638 #define I_LEVEL3_HEADER_SIZE 32
644 * offset size field name
645 * ----------------------------------
646 * 0 1 header size [*1]
648 * ---------------------------------------
650 * 7 4 packed size [*2] |
651 * 11 4 original size |
654 * 19 1 attribute | [*1] header size (X+Y+22)
655 * 20 1 level (0x00 fixed) |
658 * X +22 2 file crc (CRC-16) |
659 * X +24 Y ext-header(old style) v
660 * -------------------------------------------------
662 * : | [*2] packed size
664 * -------------------------------------------------
666 * ext-header(old style)
680 * bit6 archive bit (need to backup)
684 get_header_level0(fp, hdr, data)
695 hdr->size_field_length = 2; /* in bytes */
696 hdr->header_size = header_size = get_byte();
697 checksum = get_byte();
699 if (fread(data + COMMON_HEADER_SIZE,
700 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
701 error("Invalid header (LHarc file ?)");
702 return FALSE; /* finish */
705 if (calc_sum(data + I_METHOD, header_size) != checksum) {
706 error("Checksum error (LHarc file?)");
710 get_bytes(hdr->method, 5, sizeof(hdr->method));
711 hdr->packed_size = get_longword();
712 hdr->original_size = get_longword();
713 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
714 hdr->attribute = get_byte(); /* MS-DOS attribute */
715 hdr->header_level = get_byte();
716 name_length = get_byte();
717 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
720 /* defaults for other type */
721 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
725 extend_size = header_size+2 - name_length - 24;
727 if (extend_size < 0) {
728 if (extend_size == -2) {
729 /* CRC field is not given */
730 hdr->extend_type = EXTEND_GENERIC;
731 hdr->has_crc = FALSE;
736 error("Unkonwn header (lha file?)");
741 hdr->crc = get_word();
743 if (extend_size == 0)
746 hdr->extend_type = get_byte();
749 if (hdr->extend_type == EXTEND_UNIX) {
750 if (extend_size >= 11) {
751 hdr->minor_version = get_byte();
752 hdr->unix_last_modified_stamp = (time_t) get_longword();
753 hdr->unix_mode = get_word();
754 hdr->unix_uid = get_word();
755 hdr->unix_gid = get_word();
758 hdr->extend_type = EXTEND_GENERIC;
762 skip_bytes(extend_size);
764 hdr->header_size += 2;
772 * offset size field name
773 * -----------------------------------
774 * 0 1 header size [*1]
776 * -------------------------------------
778 * 7 4 skip size [*2] |
779 * 11 4 original size |
782 * 19 1 attribute (0x20 fixed) | [*1] header size (X+Y+25)
783 * 20 1 level (0x01 fixed) |
786 * X+ 22 2 file crc (CRC-16) |
789 * X+Y+25 2 next-header size v
790 * -------------------------------------------------
791 * X+Y+27 Z ext-header ^
793 * ----------------------------------- | [*2] skip size
796 * -------------------------------------------------
800 get_header_level1(fp, hdr, data)
811 hdr->size_field_length = 2; /* in bytes */
812 hdr->header_size = header_size = get_byte();
813 checksum = get_byte();
815 if (fread(data + COMMON_HEADER_SIZE,
816 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
817 error("Invalid header (LHarc file ?)");
818 return FALSE; /* finish */
821 if (calc_sum(data + I_METHOD, header_size) != checksum) {
822 error("Checksum error (LHarc file?)");
826 get_bytes(hdr->method, 5, sizeof(hdr->method));
827 hdr->packed_size = get_longword(); /* skip size */
828 hdr->original_size = get_longword();
829 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
830 hdr->attribute = get_byte(); /* 0x20 fixed */
831 hdr->header_level = get_byte();
833 name_length = get_byte();
834 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
837 /* defaults for other type */
838 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
843 hdr->crc = get_word();
844 hdr->extend_type = get_byte();
846 dummy = header_size+2 - name_length - I_LEVEL1_HEADER_SIZE;
848 skip_bytes(dummy); /* skip old style extend header */
850 extend_size = get_word();
851 extend_size = get_extended_header(fp, hdr, extend_size, 0);
852 if (extend_size == -1)
855 /* On level 1 header, size fields should be adjusted. */
856 /* the `packed_size' field contains the extended header size. */
857 /* the `header_size' field does not. */
858 hdr->packed_size -= extend_size;
859 hdr->header_size += extend_size + 2;
868 * offset size field name
869 * --------------------------------------------------
870 * 0 2 total header size [*1] ^
871 * ----------------------- |
873 * 7 4 packed size [*2] |
874 * 11 4 original size |
876 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
877 * 20 1 level (0x02 fixed) | (X+26+(1))
878 * 21 2 file crc (CRC-16) |
880 * 24 2 next-header size |
881 * ----------------------------------- |
884 * ----------------------------------- |
885 * X +26 (1) padding v
886 * -------------------------------------------------
888 * : | [*2] packed size
890 * -------------------------------------------------
894 get_header_level2(fp, hdr, data)
904 hdr->size_field_length = 2; /* in bytes */
905 hdr->header_size = header_size = get_word();
907 if (fread(data + COMMON_HEADER_SIZE,
908 I_LEVEL2_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
909 error("Invalid header (LHarc file ?)");
910 return FALSE; /* finish */
913 get_bytes(hdr->method, 5, sizeof(hdr->method));
914 hdr->packed_size = get_longword();
915 hdr->original_size = get_longword();
916 hdr->unix_last_modified_stamp = get_longword();
917 hdr->attribute = get_byte(); /* reserved */
918 hdr->header_level = get_byte();
920 /* defaults for other type */
921 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
926 hdr->crc = get_word();
927 hdr->extend_type = get_byte();
928 extend_size = get_word();
930 INITIALIZE_CRC(hcrc);
931 hcrc = calccrc(hcrc, data, get_ptr - data);
933 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
934 if (extend_size == -1)
937 padding = header_size - I_LEVEL2_HEADER_SIZE - extend_size;
938 while (padding--) /* padding should be 0 or 1 */
939 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
941 if (hdr->header_crc != hcrc)
942 error("header CRC error");
951 * offset size field name
952 * --------------------------------------------------
953 * 0 2 size field length (4 fixed) ^
955 * 7 4 packed size [*2] |
956 * 11 4 original size |
958 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
959 * 20 1 level (0x03 fixed) | (X+32)
960 * 21 2 file crc (CRC-16) |
962 * 24 4 total header size [*1] |
963 * 28 4 next-header size |
964 * ----------------------------------- |
967 * -------------------------------------------------
969 * : | [*2] packed size
971 * -------------------------------------------------
975 get_header_level3(fp, hdr, data)
985 hdr->size_field_length = get_word();
987 if (fread(data + COMMON_HEADER_SIZE,
988 I_LEVEL3_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
989 error("Invalid header (LHarc file ?)");
990 return FALSE; /* finish */
993 get_bytes(hdr->method, 5, sizeof(hdr->method));
994 hdr->packed_size = get_longword();
995 hdr->original_size = get_longword();
996 hdr->unix_last_modified_stamp = get_longword();
997 hdr->attribute = get_byte(); /* reserved */
998 hdr->header_level = get_byte();
1000 /* defaults for other type */
1001 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1005 hdr->has_crc = TRUE;
1006 hdr->crc = get_word();
1007 hdr->extend_type = get_byte();
1008 hdr->header_size = header_size = get_longword();
1009 extend_size = get_longword();
1011 INITIALIZE_CRC(hcrc);
1012 hcrc = calccrc(hcrc, data, get_ptr - data);
1014 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1015 if (extend_size == -1)
1018 padding = header_size - I_LEVEL3_HEADER_SIZE - extend_size;
1019 while (padding--) /* padding should be 0 */
1020 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1022 if (hdr->header_crc != hcrc)
1023 error("header CRC error");
1033 char data[LZHEADER_STORAGE];
1035 int archive_kanji_code = CODE_SJIS;
1036 int system_kanji_code = default_system_kanji_code;
1037 char *archive_delim = "\377\\"; /* `\' is for level 0 header and
1039 char *system_delim = "//";
1040 int filename_case = NONE;
1043 memset(hdr, 0, sizeof(LzHeader));
1047 if ((end_mark = getc(fp)) == EOF || end_mark == 0) {
1048 return FALSE; /* finish */
1052 if (fread(data + 1, COMMON_HEADER_SIZE - 1, 1, fp) == 0) {
1053 error("Invalid header (LHarc file ?)");
1054 return FALSE; /* finish */
1057 switch (data[I_HEADER_LEVEL]) {
1059 if (get_header_level0(fp, hdr, data) == FALSE)
1063 if (get_header_level1(fp, hdr, data) == FALSE)
1067 if (get_header_level2(fp, hdr, data) == FALSE)
1071 if (get_header_level3(fp, hdr, data) == FALSE)
1075 error("Unknown level header (level %d)", data[I_HEADER_LEVEL]);
1079 /* filename conversion */
1080 switch (hdr->extend_type) {
1082 filename_case = noconvertcase ? NONE : TO_LOWER;
1089 filename_case = NONE;
1093 archive_delim = "\377/:\\";
1094 /* `\' is for level 0 header and broken archive. */
1095 system_delim = "/://";
1096 filename_case = NONE;
1100 filename_case = noconvertcase ? NONE : TO_LOWER;
1104 if (optional_archive_kanji_code)
1105 archive_kanji_code = optional_archive_kanji_code;
1106 if (optional_system_kanji_code)
1107 system_kanji_code = optional_system_kanji_code;
1108 if (optional_archive_delim)
1109 archive_delim = optional_archive_delim;
1110 if (optional_system_delim)
1111 system_delim = optional_system_delim;
1112 if (optional_filename_case)
1113 filename_case = optional_filename_case;
1115 /* kanji code and delimiter conversion */
1116 convert_filename(hdr->name, strlen(hdr->name), sizeof(hdr->name),
1119 archive_delim, system_delim, filename_case);
1121 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1123 /* split symbolic link */
1124 p = strchr(hdr->name, '|');
1126 /* hdr->name is symbolic link name */
1127 /* hdr->realname is real name */
1129 strncpy(hdr->realname, p+1, sizeof(hdr->realname));
1132 error("unknown symlink name \"%s\"", hdr->name);
1138 /* skip SFX header */
1143 unsigned char buffer[64 * 1024]; /* max seek size */
1147 n = fread(buffer, 1, sizeof(buffer), fp);
1149 for (p = buffer; p < buffer + n; p++) {
1150 if (! (p[I_METHOD]=='-' && p[I_METHOD+1]=='l' && p[I_METHOD+4]=='-'))
1152 /* found "-l??-" keyword (as METHOD type string) */
1154 /* level 0 or 1 header */
1155 if ((p[I_HEADER_LEVEL] == 0 || p[I_HEADER_LEVEL] == 1)
1156 && p[I_HEADER_SIZE] > 20
1157 && p[I_HEADER_CHECKSUM] == calc_sum(p+2, p[I_HEADER_SIZE])) {
1158 if (fseeko(fp, (p - buffer) - n, SEEK_CUR) == -1)
1159 fatal_error("cannot seek header");
1163 /* level 2 header */
1164 if (p[I_HEADER_LEVEL] == 2
1165 && p[I_HEADER_SIZE] >= 24
1166 && p[I_ATTRIBUTE] == 0x20) {
1167 if (fseeko(fp, (p - buffer) - n, SEEK_CUR) == -1)
1168 fatal_error("cannot seek header");
1173 if (fseeko(fp, -n, SEEK_CUR) == -1)
1174 fatal_error("cannot seek header");
1179 init_header(name, v_stat, hdr)
1181 struct stat *v_stat;
1186 memset(hdr, 0, sizeof(LzHeader));
1188 /* the `method' member is rewrote by the encoding function.
1189 but need set for empty files */
1190 memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STORAGE);
1192 hdr->packed_size = 0;
1193 hdr->original_size = v_stat->st_size;
1194 hdr->attribute = GENERIC_ATTRIBUTE;
1195 hdr->header_level = header_level;
1196 strcpy(hdr->name, name);
1199 hdr->extend_type = EXTEND_UNIX;
1200 hdr->unix_last_modified_stamp = v_stat->st_mtime;
1201 /* since 00:00:00 JAN.1.1970 */
1202 #ifdef NOT_COMPATIBLE_MODE
1203 /* Please need your modification in this space. */
1205 hdr->unix_mode = v_stat->st_mode;
1208 hdr->unix_uid = v_stat->st_uid;
1209 hdr->unix_gid = v_stat->st_gid;
1211 #if INCLUDE_OWNER_NAME_IN_HEADER
1214 struct passwd *ent = getpwuid(hdr->unix_uid);
1217 strncpy(hdr->user, ent->pw_name, sizeof(hdr->user));
1218 if (hdr->user[sizeof(hdr->user)-1])
1219 hdr->user[sizeof(hdr->user)-1] = 0;
1225 struct group *ent = getgrgid(hdr->unix_gid);
1228 strncpy(hdr->group, ent->gr_name, sizeof(hdr->group));
1229 if (hdr->group[sizeof(hdr->group)-1])
1230 hdr->group[sizeof(hdr->group)-1] = 0;
1234 #endif /* INCLUDE_OWNER_NAME_IN_HEADER */
1235 if (is_directory(v_stat)) {
1236 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1237 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1238 hdr->original_size = 0;
1239 if (len > 0 && hdr->name[len - 1] != '/')
1240 strcpy(&hdr->name[len++], "/");
1244 if (is_symlink(v_stat)) {
1245 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1246 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1247 hdr->original_size = 0;
1248 readlink(name, hdr->realname, sizeof(hdr->realname));
1254 write_unix_info(hdr)
1257 /* UNIX specific informations */
1259 put_word(5); /* size */
1260 put_byte(0x50); /* permission */
1261 put_word(hdr->unix_mode);
1263 put_word(7); /* size */
1264 put_byte(0x51); /* gid and uid */
1265 put_word(hdr->unix_gid);
1266 put_word(hdr->unix_uid);
1268 if (hdr->group[0]) {
1269 int len = strlen(hdr->group);
1270 put_word(len + 3); /* size */
1271 put_byte(0x52); /* group name */
1272 put_bytes(hdr->group, len);
1276 int len = strlen(hdr->user);
1277 put_word(len + 3); /* size */
1278 put_byte(0x53); /* user name */
1279 put_bytes(hdr->user, len);
1282 if (hdr->header_level == 1) {
1283 put_word(7); /* size */
1284 put_byte(0x54); /* time stamp */
1285 put_longword(hdr->unix_last_modified_stamp);
1290 write_header_level0(data, hdr, pathname)
1292 char *data, *pathname;
1299 memset(data, 0, LZHEADER_STORAGE);
1301 put_byte(0x00); /* header size */
1302 put_byte(0x00); /* check sum */
1303 put_bytes(hdr->method, 5);
1304 put_longword(hdr->packed_size);
1305 put_longword(hdr->original_size);
1306 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1307 put_byte(hdr->attribute);
1308 put_byte(hdr->header_level); /* level 0 */
1310 /* write pathname (level 0 header contains the directory part) */
1311 name_length = strlen(pathname);
1313 limit = 255 - I_GENERIC_HEADER_SIZE + 2;
1315 limit = 255 - I_LEVEL0_HEADER_SIZE + 2;
1317 if (name_length > limit) {
1318 warning("the length of pathname \"%s\" is too long.", pathname);
1319 name_length = limit;
1321 put_byte(name_length);
1322 put_bytes(pathname, name_length);
1325 if (generic_format) {
1326 header_size = I_GENERIC_HEADER_SIZE + name_length - 2;
1327 data[I_HEADER_SIZE] = header_size;
1328 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1330 /* write old-style extend header */
1331 put_byte(EXTEND_UNIX);
1332 put_byte(CURRENT_UNIX_MINOR_VERSION);
1333 put_longword(hdr->unix_last_modified_stamp);
1334 put_word(hdr->unix_mode);
1335 put_word(hdr->unix_uid);
1336 put_word(hdr->unix_gid);
1338 /* size of extended header is 12 */
1339 header_size = I_LEVEL0_HEADER_SIZE + name_length - 2;
1340 data[I_HEADER_SIZE] = header_size;
1341 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1344 return header_size + 2;
1348 write_header_level1(data, hdr, pathname)
1350 char *data, *pathname;
1352 int name_length, dir_length, limit;
1353 char *basename, *dirname;
1355 char *extend_header_top;
1356 size_t extend_header_size;
1358 basename = strrchr(pathname, LHA_PATHSEP);
1361 name_length = strlen(basename);
1363 dir_length = basename - dirname;
1366 basename = pathname;
1367 name_length = strlen(basename);
1373 memset(data, 0, LZHEADER_STORAGE);
1375 put_byte(0x00); /* header size */
1376 put_byte(0x00); /* check sum */
1377 put_bytes(hdr->method, 5);
1378 put_longword(hdr->packed_size);
1379 put_longword(hdr->original_size);
1380 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1382 put_byte(hdr->header_level); /* level 1 */
1384 /* level 1 header: write filename (basename only) */
1385 limit = 255 - I_LEVEL1_HEADER_SIZE + 2;
1386 if (name_length > limit) {
1387 put_byte(0); /* name length */
1390 put_byte(name_length);
1391 put_bytes(basename, name_length);
1399 put_byte(EXTEND_UNIX);
1401 /* write extend header from here. */
1403 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1404 header_size = extend_header_top - data - 2;
1406 /* write filename and dirname */
1408 if (name_length > limit) {
1409 put_word(name_length + 3); /* size */
1410 put_byte(0x01); /* filename */
1411 put_bytes(basename, name_length);
1414 if (dir_length > 0) {
1415 put_word(dir_length + 3); /* size */
1416 put_byte(0x02); /* dirname */
1417 put_bytes(dirname, dir_length);
1420 if (!generic_format)
1421 write_unix_info(hdr);
1423 put_word(0x0000); /* next header size */
1425 extend_header_size = put_ptr - extend_header_top;
1426 /* On level 1 header, the packed size field is contains the ext-header */
1427 hdr->packed_size += put_ptr - extend_header_top;
1429 /* put `skip size' */
1430 setup_put(data + I_PACKED_SIZE);
1431 put_longword(hdr->packed_size);
1433 data[I_HEADER_SIZE] = header_size;
1434 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1436 return header_size + extend_header_size + 2;
1440 write_header_level2(data, hdr, pathname)
1442 char *data, *pathname;
1444 int name_length, dir_length;
1445 char *basename, *dirname;
1447 char *extend_header_top;
1448 char *headercrc_ptr;
1451 basename = strrchr(pathname, LHA_PATHSEP);
1454 name_length = strlen(basename);
1456 dir_length = basename - dirname;
1459 basename = pathname;
1460 name_length = strlen(basename);
1466 memset(data, 0, LZHEADER_STORAGE);
1468 put_word(0x0000); /* header size */
1469 put_bytes(hdr->method, 5);
1470 put_longword(hdr->packed_size);
1471 put_longword(hdr->original_size);
1472 put_longword(hdr->unix_last_modified_stamp);
1474 put_byte(hdr->header_level); /* level 2 */
1481 put_byte(EXTEND_UNIX);
1483 /* write extend header from here. */
1485 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1487 /* write common header */
1490 headercrc_ptr = put_ptr;
1491 put_word(0x0000); /* header CRC */
1493 /* write filename and dirname */
1494 /* must have this header, even if the name_length is 0. */
1495 put_word(name_length + 3); /* size */
1496 put_byte(0x01); /* filename */
1497 put_bytes(basename, name_length);
1499 if (dir_length > 0) {
1500 put_word(dir_length + 3); /* size */
1501 put_byte(0x02); /* dirname */
1502 put_bytes(dirname, dir_length);
1505 if (!generic_format)
1506 write_unix_info(hdr);
1508 put_word(0x0000); /* next header size */
1510 header_size = put_ptr - data;
1511 if ((header_size & 0xff) == 0) {
1512 /* cannot put zero at the first byte on level 2 header. */
1513 /* adjust header size. */
1514 put_byte(0); /* padding */
1518 /* put hader size */
1519 setup_put(data + I_HEADER_SIZE);
1520 put_word(header_size);
1522 /* put hader CRC in extended header */
1523 INITIALIZE_CRC(hcrc);
1524 hcrc = calccrc(hcrc, data, (unsigned int) header_size);
1525 setup_put(headercrc_ptr);
1532 write_header(fp, hdr)
1537 char data[LZHEADER_STORAGE];
1539 int archive_kanji_code = CODE_SJIS;
1540 int system_kanji_code = default_system_kanji_code;
1541 char *archive_delim = "\377";
1542 char *system_delim = "/";
1543 int filename_case = NONE;
1544 char pathname[FILENAME_LENGTH];
1546 if (optional_archive_kanji_code)
1547 archive_kanji_code = optional_archive_kanji_code;
1548 if (optional_system_kanji_code)
1549 system_kanji_code = optional_system_kanji_code;
1552 filename_case = TO_UPPER;
1554 if (hdr->header_level == 0) {
1555 archive_delim = "\\";
1558 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1560 p = strchr(hdr->name, '|');
1562 error("symlink name \"%s\" contains '|' char. change it into '_'",
1566 if (xsnprintf(pathname, sizeof(pathname),
1567 "%s|%s", hdr->name, hdr->realname) == -1)
1568 error("file name is too long (%s -> %s)", hdr->name, hdr->realname);
1571 strncpy(pathname, hdr->name, sizeof(pathname));
1572 pathname[sizeof(pathname)-1] = 0;
1575 convert_filename(pathname, strlen(pathname), sizeof(pathname),
1578 system_delim, archive_delim, filename_case);
1580 switch (hdr->header_level) {
1582 header_size = write_header_level0(data, hdr, pathname);
1585 header_size = write_header_level1(data, hdr, pathname);
1588 header_size = write_header_level2(data, hdr, pathname);
1591 error("Unknown level header (level %d)", hdr->header_level);
1595 if (fwrite(data, header_size, 1, fp) == 0)
1596 fatal_error("Cannot write to temporary file");
1599 #if MULTIBYTE_FILENAME
1601 #if defined(__APPLE__) /* Added by Hiroto Sakai */
1603 #include <CoreFoundation/CFString.h>
1604 #include <CoreFoundation/CFStringEncodingExt.h>
1606 /* this is not need for Mac OS X v 10.2 later */
1608 kCFStringEncodingAllowLossyConversion = 1,
1609 kCFStringEncodingBasicDirectionLeftToRight = (1 << 1),
1610 kCFStringEncodingBasicDirectionRightToLeft = (1 << 2),
1611 kCFStringEncodingSubstituteCombinings = (1 << 3),
1612 kCFStringEncodingComposeCombinings = (1 << 4),
1613 kCFStringEncodingIgnoreCombinings = (1 << 5),
1614 kCFStringEncodingUseCanonical = (1 << 6),
1615 kCFStringEncodingUseHFSPlusCanonical = (1 << 7),
1616 kCFStringEncodingPrependBOM = (1 << 8),
1617 kCFStringEncodingDisableCorporateArea = (1 << 9),
1618 kCFStringEncodingASCIICompatibleConversion = (1 << 10),
1622 ConvertEncodingToUTF8(const char* inCStr,
1623 char* outUTF8Buffer,
1624 int outUTF8BufferLength,
1625 unsigned long scriptEncoding,
1626 unsigned long flags)
1628 unsigned long unicodeChars;
1629 unsigned long srcCharsUsed;
1630 unsigned long usedByteLen = 0;
1631 UniChar uniStr[512];
1632 unsigned long cfResult;
1634 cfResult = CFStringEncodingBytesToUnicode(scriptEncoding,
1642 if (cfResult == 0) {
1643 cfResult = CFStringEncodingUnicodeToBytes(kCFStringEncodingUTF8,
1648 (char*)outUTF8Buffer,
1649 outUTF8BufferLength - 1,
1651 outUTF8Buffer[usedByteLen] = '\0';
1658 ConvertUTF8ToEncoding(const char* inUTF8Buf,
1659 int inUTF8BufLength,
1660 char* outCStrBuffer,
1661 int outCStrBufferLength,
1662 unsigned long scriptEncoding,
1663 unsigned long flags)
1665 unsigned long unicodeChars;
1666 unsigned long srcCharsUsed;
1667 unsigned long usedByteLen = 0;
1668 UniChar uniStr[256];
1669 unsigned long cfResult;
1671 cfResult = CFStringEncodingBytesToUnicode(kCFStringEncodingUTF8,
1679 if (cfResult == 0) {
1680 cfResult = CFStringEncodingUnicodeToBytes(scriptEncoding,
1685 (char*)outCStrBuffer,
1686 outCStrBufferLength - 1,
1688 outCStrBuffer[usedByteLen] = '\0';
1698 ConvertEncodingByIconv(const char *src, char *dst, int dstsize,
1699 const char *srcEnc, const char *dstEnc)
1702 static char szTmpBuf[2048];
1708 dst_p = &szTmpBuf[0];
1709 iLen = (size_t)sizeof(szTmpBuf)-1;
1710 src_p = (char *)src;
1711 sLen = (size_t)strlen(src);
1712 memset(szTmpBuf, 0, sizeof(szTmpBuf));
1713 memset(dst, 0, dstsize);
1715 ic = iconv_open(dstEnc, srcEnc);
1716 if (ic == (iconv_t)-1) {
1717 error("iconv_open() failure: %s", strerror(errno));
1721 if (iconv(ic, &src_p, &sLen, &dst_p, &iLen) == (size_t)-1) {
1722 error("iconv() failure: %s", strerror(errno));
1727 strncpy(dst, szTmpBuf, dstsize);
1733 #endif /* defined(__APPLE__) */
1736 sjis_to_utf8(char *dst, const char *src, size_t dstsize)
1738 #if defined(__APPLE__)
1740 if (ConvertEncodingToUTF8(src, dst, dstsize,
1741 kCFStringEncodingDOSJapanese,
1742 kCFStringEncodingUseHFSPlusCanonical) == 0)
1745 if (ConvertEncodingByIconv(src, dst, dstsize, "SJIS", "UTF-8") != -1)
1748 error("not support utf-8 conversion");
1751 if (dstsize < 1) return dst;
1753 return strncpy(dst, src, dstsize-1);
1757 utf8_to_sjis(char *dst, const char *src, size_t dstsize)
1759 #if defined(__APPLE__)
1763 srclen = strlen(src);
1764 if (ConvertUTF8ToEncoding(src, srclen, dst, dstsize,
1765 kCFStringEncodingDOSJapanese,
1766 kCFStringEncodingUseHFSPlusCanonical) == 0)
1769 if (ConvertEncodingByIconv(src, dst, dstsize, "UTF-8", "SJIS") != -1)
1772 error("not support utf-8 conversion");
1775 if (dstsize < 1) return dst;
1777 return strncpy(dst, src, dstsize-1);
1781 * SJIS <-> EUC ÊÑ´¹´Ø¿ô
1782 * ¡ÖÆüËܸì¾ðÊó½èÍý¡× ¥½¥Õ¥È¥Ð¥ó¥¯(³ô)
1783 * ¤è¤êÈ´¿è(by Koji Arai)
1786 euc2sjis(int *p1, int *p2)
1788 unsigned char c1 = *p1 & 0x7f;
1789 unsigned char c2 = *p2 & 0x7f;
1790 int rowoff = c1 < 0x5f ? 0x70 : 0xb0;
1791 int celoff = c1 % 2 ? (c2 > 0x5f ? 0x20 : 0x1f) : 0x7e;
1792 *p1 = ((c1 + 1) >> 1) + rowoff;
1793 *p2 += celoff - 0x80;
1797 sjis2euc(int *p1, int *p2)
1799 unsigned char c1 = *p1;
1800 unsigned char c2 = *p2;
1801 int adjust = c2 < 0x9f;
1802 int rowoff = c1 < 0xa0 ? 0x70 : 0xb0;
1803 int celoff = adjust ? (c2 > 0x7f ? 0x20 : 0x1f) : 0x7e;
1804 *p1 = ((c1 - rowoff) << 1) - adjust;
1815 case '0': case '1': case '2': case '3': case '4':
1816 case '5': case '6': case '7': case '8': case '9':
1819 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1820 return c - 'a' + 10;
1822 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1823 return c - 'A' + 10;
1833 case 0: case 1: case 2: case 3: case 4:
1834 case 5: case 6: case 7: case 8: case 9:
1837 case 10: case 11: case 12: case 13: case 14: case 15:
1838 return c + 'a' - 10;
1846 cap_to_sjis(char *dst, const char *src, size_t dstsize)
1849 size_t len = strlen(src);
1852 for (i = j = 0; i < len && i < dstsize; i++) {
1853 if (src[i] != ':') {
1859 a = hex2int((unsigned char)src[i]);
1860 b = hex2int((unsigned char)src[i+1]);
1862 if (a == -1 || b == -1) {
1865 strncpy(dst+j, src+i, dstsize-j);
1872 dst[j++] = a * 16 + b;
1879 sjis_to_cap(char *dst, const char *src, size_t dstsize)
1882 size_t len = strlen(src);
1885 for (i = j = 0; i < len && i < dstsize; i++) {
1886 if (src[i] == ':') {
1887 strncpy(dst+j, ":3a", dstsize-j);
1892 if (isprint(src[i])) {
1897 if (j + 3 >= dstsize) {
1902 a = int2hex((unsigned char)src[i] / 16);
1903 b = int2hex((unsigned char)src[i] % 16);
1912 #endif /* MULTIBYTE_FILENAME */