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 /* ------------------------------------------------------------------------ */
16 #define DUMP_HEADER 1 /* for debugging */
18 #if !STRCHR_8BIT_CLEAN
19 /* should use 8 bit clean version */
22 #define strchr xstrchr
23 #define strrchr xstrrchr
26 /* ------------------------------------------------------------------------ */
28 #define GET_BYTE() (*get_ptr++ & 0xff)
31 static char *start_ptr;
32 #define setup_get(PTR) (start_ptr = get_ptr = (PTR))
33 #define get_byte() dump_get_byte()
34 #define skip_bytes(len) dump_skip_bytes(len)
36 #define setup_get(PTR) (get_ptr = (PTR))
37 #define get_byte() GET_BYTE()
38 #define skip_bytes(len) (get_ptr += (len))
40 #define put_ptr get_ptr
41 #define setup_put(PTR) (put_ptr = (PTR))
42 #define put_byte(c) (*put_ptr++ = (char)(c))
44 int optional_archive_kanji_code = NONE;
45 int optional_system_kanji_code = NONE;
46 char *optional_archive_delim = NULL;
47 char *optional_system_delim = NULL;
48 int optional_filename_case = NONE;
50 #ifdef MULTIBYTE_FILENAME
51 int default_system_kanji_code = MULTIBYTE_FILENAME;
53 int default_system_kanji_code = NONE;
56 /* ------------------------------------------------------------------------ */
64 for (sum = 0; len; len--)
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");
104 /* ------------------------------------------------------------------------ */
112 if (verbose_listing && verbose > 1)
113 printf("%02d %2d: ", get_ptr - start_ptr, 2);
119 if (verbose_listing && verbose > 1)
120 printf("%d(0x%04x)\n", w, w);
125 /* ------------------------------------------------------------------------ */
134 /* ------------------------------------------------------------------------ */
142 if (verbose_listing && verbose > 1)
143 printf("%02d %2d: ", get_ptr - start_ptr, 4);
149 l = (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
151 if (verbose_listing && verbose > 1)
152 printf("%ld(0x%08lx)\n", l, l);
157 /* ------------------------------------------------------------------------ */
169 get_bytes(buf, len, size)
176 if (verbose_listing && verbose > 1)
177 printf("%02d %2d: \"", get_ptr - start_ptr, len);
179 for (i = 0; i < len; i++) {
180 if (i < size) buf[i] = get_ptr[i];
182 if (verbose_listing && verbose > 1) {
184 printf("%c", buf[i]);
186 printf("\\x%02x", (unsigned char)buf[i]);
190 if (verbose_listing && verbose > 1)
193 for (i = 0; i < len && i < size; i++)
207 for (i = 0; i < len; i++)
211 /* added by Koji Arai */
213 convert_filename(name, len, size,
215 from_delim, to_delim,
220 int from_code, to_code, case_to;
221 char *from_delim, *to_delim;
225 #ifdef MULTIBYTE_FILENAME
226 char tmp[FILENAME_LENGTH];
228 if (from_code == CODE_SJIS && to_code == CODE_UTF8) {
229 for (i = 0; i < len; i++)
230 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
231 if ((unsigned char)name[i] == LHA_PATHSEP) name[i] = '/';
232 sjis_to_utf8(tmp, name, sizeof(tmp));
233 strncpy(name, tmp, size);
236 for (i = 0; i < len; i++)
237 if (name[i] == '/') name[i] = LHA_PATHSEP;
238 from_code = CODE_UTF8;
240 else if (from_code == CODE_UTF8 && to_code == CODE_SJIS) {
241 for (i = 0; i < len; i++)
242 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
243 if ((unsigned char)name[i] == LHA_PATHSEP) name[i] = '/';
244 utf8_to_sjis(tmp, name, sizeof(tmp));
245 strncpy(name, tmp, size);
248 for (i = 0; i < len; i++)
249 if (name[i] == '/') name[i] = LHA_PATHSEP;
250 from_code = CODE_SJIS;
254 /* special case: if `name' has small lettter, not convert case. */
255 if (from_code == CODE_SJIS && case_to == TO_LOWER) {
256 for (i = 0; i < len; i++) {
257 #ifdef MULTIBYTE_FILENAME
258 if (SJIS_FIRST_P(name[i]) && SJIS_SECOND_P(name[i+1]))
262 if (islower(name[i])) {
269 for (i = 0; i < len; i ++) {
270 #ifdef MULTIBYTE_FILENAME
271 if (from_code == CODE_EUC &&
272 (unsigned char)name[i] == 0x8e) {
273 if (to_code != CODE_SJIS) {
279 memmove(name + i, name + i + 1, len - i);
283 if (from_code == CODE_SJIS && X0201_KANA_P(name[i])) {
284 if (to_code != CODE_EUC) {
288 if (len == size - 1) /* check overflow */
290 memmove(name+i+1, name+i, len-i);
296 if (from_code == CODE_EUC && (name[i] & 0x80) && (name[i+1] & 0x80)) {
298 if (to_code != CODE_SJIS) {
303 c1 = (unsigned char)name[i];
304 c2 = (unsigned char)name[i+1];
311 if (from_code == CODE_SJIS &&
312 SJIS_FIRST_P(name[i]) &&
313 SJIS_SECOND_P(name[i+1])) {
316 if (to_code != CODE_EUC) {
321 c1 = (unsigned char)name[i];
322 c2 = (unsigned char)name[i+1];
329 #endif /* MULTIBYTE_FILENAME */
333 /* transpose from_delim to to_delim */
335 if ((ptr = strchr(from_delim, name[i])) != NULL) {
336 name[i] = to_delim[ptr - from_delim];
341 if (case_to == TO_UPPER && islower(name[i])) {
342 name[i] = toupper(name[i]);
345 if (case_to == TO_LOWER && isupper(name[i])) {
346 name[i] = tolower(name[i]);
352 /* ------------------------------------------------------------------------ */
354 /* Generic stamp format: */
356 /* 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 */
357 /* |<------- year ----->|<- month ->|<--- day ---->| */
359 /* 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 */
360 /* |<--- hour --->|<---- minute --->|<- second*2 ->| */
362 /* ------------------------------------------------------------------------ */
365 * NOTE : If you don't have `gettimeofday(2)', or your gettimeofday(2)
366 * returns bogus timezone information, try FTIME, MKTIME, TIMELOCAL or TZSET.
370 #include <sys/timeb.h>
373 #if !defined(HAVE_MKTIME) && !defined(HAVE_TIMELOCAL)
379 extern long timezone; /* not defined in time.h */
388 return buf.timezone * 60L;
389 #elif HAVE_STRUCT_TM_TM_GMTOFF
393 return -localtime(&tt)->tm_gmtoff;
394 #elif GETTIMEOFDAY_HAS_2ND_ARG
397 gettimeofday(&tp, &tzp);/* specific to 4.3BSD */
399 * return (tzp.tz_minuteswest * 60L + (tzp.tz_dsttime != 0 ? 60L *
402 return (tzp.tz_minuteswest * 60L);
404 /* Compile error will be caused */
405 CANNOT GET TIMEZONE INFORMATION ON YOUR SYSTEM.
406 TAKE THE ANOTHER WAY.
411 /* ------------------------------------------------------------------------ */
413 generic_to_unix_stamp(t)
416 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL)
420 * special case: if MSDOS format date and time were zero, then we
421 * set time to be zero here too.
426 dostm.tm_sec = (t & 0x1f) * 2;
427 dostm.tm_min = t >> 5 & 0x3f;
428 dostm.tm_hour = t >> 11 & 0x1f;
429 dostm.tm_mday = t >> 16 & 0x1f;
430 dostm.tm_mon = (t >> (16+5) & 0x0f) - 1; /* 0..11 */
431 dostm.tm_year = (t >> (16+9) & 0x7f) + 80;
435 return (time_t) mktime(&dostm);
436 #else /* HAVE_TIMELOCAL is defined */
437 return (time_t) timelocal(&dostm);
440 #else /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
441 int year, month, day, hour, min, sec;
443 static unsigned int dsboy[12] = {0, 31, 59, 90, 120, 151,
444 181, 212, 243, 273, 304, 334};
448 * special case: if MSDOS format date and time were zero, then we
449 * set time to be zero here too.
454 year = ((int) (t >> 16 + 9) & 0x7f) + 1980;
455 month = (int) (t >> 16 + 5) & 0x0f; /* 1..12 means Jan..Dec */
456 day = (int) (t >> 16) & 0x1f; /* 1..31 means 1st,...31st */
458 hour = ((int) t >> 11) & 0x1f;
459 min = ((int) t >> 5) & 0x3f;
460 sec = ((int) t & 0x1f) * 2;
462 /* Calculate days since 1970.01.01 */
463 days = (365 * (year - 1970) + /* days due to whole years */
464 (year - 1970 + 1) / 4 + /* days due to leap years */
465 dsboy[month - 1] + /* days since beginning of this year */
466 day - 1); /* days since beginning of month */
468 if ((year % 4 == 0) &&
469 (year % 100 != 0 || year % 400 == 0) && /* 1999.5.24 t.oka */
470 (month >= 3)) /* if this is a leap year and month */
471 days++; /* is March or later, add a day */
473 /* Knowing the days, we can find seconds */
474 longtime = (((days * 24) + hour) * 60 + min) * 60 + sec;
475 longtime += gettz(); /* adjust for timezone */
477 /* LONGTIME is now the time in seconds, since 1970/01/01 00:00:00. */
478 return (time_t) longtime;
479 #endif /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
482 /* ------------------------------------------------------------------------ */
484 unix_to_generic_stamp(t)
487 struct tm *tm = localtime(&t);
489 return ((((long) (tm->tm_year - 80)) << 25) +
490 (((long) (tm->tm_mon + 1)) << 21) +
491 (((long) tm->tm_mday) << 16) +
492 (long) ((tm->tm_hour << 11) +
498 wintime_to_unix_stamp()
502 uint64_t epoch = 0x019db1ded53e8000; /* 1970-01-01 00:00:00 (UTC) */
505 t += (uint64_t)get_longword() << 32;
506 t = (t - epoch) / 10000000;
510 unsigned long t, q, x;
511 unsigned long wintime[8];
512 unsigned long epoch[8] = {0x01,0x9d,0xb1,0xde, 0xd5,0x3e,0x80,0x00};
513 /* 1970-01-01 00:00:00 (UTC) */
514 /* wintime -= epoch */
516 for (i = 7; i >= 0; i--) {
517 wintime[i] = (unsigned)get_byte() - epoch[i] - borrow;
518 borrow = (wintime[i] > 0xff) ? 1 : 0;
522 /* q = wintime / 10000000 */
524 x = 10000000; /* x: 24bit */
525 for (i = 0; i < 8; i++) {
526 t = (t << 8) + wintime[i]; /* 24bit + 8bit. t must be 32bit variable */
527 q <<= 8; /* q must be 32bit (time_t) */
535 /* ------------------------------------------------------------------------ */
536 /* build header functions */
537 /* ------------------------------------------------------------------------ */
543 * --------------------------------
545 * 2 or 4 next-header size [*1]
546 * --------------------------------------
547 * ext header: 1 ext-type ^
548 * ? contents | [*1] next-header size
549 * 2 or 4 next-header size v
550 * --------------------------------------
552 * on level 1, 2 header:
553 * size field is 2 bytes
555 * size field is 4 bytes
559 get_extended_header(fp, hdr, header_size, hcrc)
565 char data[LZHEADER_STORAGE];
567 char dirname[FILENAME_LENGTH];
570 long whole_size = header_size;
572 int n = 1 + hdr->size_field_length; /* `ext-type' + `next-header size' */
574 if (hdr->header_level == 0)
577 name_length = strlen(hdr->name);
579 while (header_size) {
581 if (sizeof(data) < header_size) {
582 error("header size (%ld) too large.", header_size);
586 if (fread(data, header_size, 1, fp) == 0) {
587 error("Invalid header (LHa file ?)");
591 ext_type = get_byte();
594 /* header crc (CRC-16) */
595 hdr->header_crc = get_word();
596 /* clear buffer for CRC calculation. */
597 data[1] = data[2] = 0;
598 skip_bytes(header_size - n - 2);
603 get_bytes(hdr->name, header_size-n, sizeof(hdr->name)-1);
604 hdr->name[name_length] = 0;
608 dir_length = get_bytes(dirname, header_size-n, sizeof(dirname)-1);
609 dirname[dir_length] = 0;
612 /* MS-DOS attribute */
613 hdr->attribute = get_word();
616 /* Windows time stamp (FILETIME structure) */
617 /* it is time in 100 nano seconds since 1601-01-01 00:00:00 */
619 skip_bytes(8); /* create time is ignored */
621 /* set last modified time */
622 if (hdr->header_level >= 2)
623 skip_bytes(8); /* time_t has been already set */
625 hdr->unix_last_modified_stamp = wintime_to_unix_stamp();
627 skip_bytes(8); /* last access time is ignored */
631 /* UNIX permission */
632 hdr->unix_mode = get_word();
635 /* UNIX gid and uid */
636 hdr->unix_gid = get_word();
637 hdr->unix_uid = get_word();
640 /* UNIX group name */
641 i = get_bytes(hdr->group, header_size-n, sizeof(hdr->group)-1);
642 hdr->group[i] = '\0';
646 i = get_bytes(hdr->user, header_size-n, sizeof(hdr->user)-1);
650 /* UNIX last modified time */
651 hdr->unix_last_modified_stamp = (time_t) get_longword();
655 /* 0x39: multi-disk header
656 0x3f: uncompressed comment
657 0x42: 64bit large file size
658 0x48-0x4f(?): reserved for authenticity verification
660 0x7e: extended attribute - platform information
661 0x7f: extended attribute - permission, owner-id and timestamp
663 0xc4: compressed comment (dict size: 4096)
664 0xc5: compressed comment (dict size: 8192)
665 0xc6: compressed comment (dict size: 16384)
666 0xc7: compressed comment (dict size: 32768)
667 0xc8: compressed comment (dict size: 65536)
668 0xd0-0xdf(?): operating systemm specific information
669 0xfc: encapsulation (another opinion)
670 0xfe: extended attribute - platform information(another opinion)
671 0xff: extended attribute - permission, owner-id and timestamp
672 (level 3 on UNLHA32) */
674 warning("unknown extended header 0x%02x", ext_type);
675 skip_bytes(header_size - n);
680 *hcrc = calccrc(*hcrc, data, header_size);
682 if (hdr->size_field_length == 2)
683 whole_size += header_size = get_word();
685 whole_size += header_size = get_longword();
688 /* concatenate dirname and filename */
690 if (name_length + dir_length >= sizeof(hdr->name)) {
691 warning("the length of pathname \"%s%s\" is too long.",
693 name_length = sizeof(hdr->name) - dir_length - 1;
694 hdr->name[name_length] = 0;
696 strcat(dirname, hdr->name);
697 strcpy(hdr->name, dirname);
698 name_length += dir_length;
704 #define I_HEADER_SIZE 0 /* level 0,1,2 */
705 #define I_HEADER_CHECKSUM 1 /* level 0,1 */
706 #define I_METHOD 2 /* level 0,1,2,3 */
707 #define I_PACKED_SIZE 7 /* level 0,1,2,3 */
708 #define I_ATTRIBUTE 19 /* level 0,1,2,3 */
709 #define I_HEADER_LEVEL 20 /* level 0,1,2,3 */
711 #define COMMON_HEADER_SIZE 21 /* size of common part */
713 #define I_GENERIC_HEADER_SIZE 24 /* + name_length */
714 #define I_LEVEL0_HEADER_SIZE 36 /* + name_length (unix extended) */
715 #define I_LEVEL1_HEADER_SIZE 27 /* + name_length */
716 #define I_LEVEL2_HEADER_SIZE 26 /* + padding */
717 #define I_LEVEL3_HEADER_SIZE 32
723 * offset size field name
724 * ----------------------------------
725 * 0 1 header size [*1]
727 * ---------------------------------------
729 * 7 4 packed size [*2] |
730 * 11 4 original size |
733 * 19 1 attribute | [*1] header size (X+Y+22)
734 * 20 1 level (0x00 fixed) |
737 * X +22 2 file crc (CRC-16) |
738 * X +24 Y ext-header(old style) v
739 * -------------------------------------------------
741 * : | [*2] packed size
743 * -------------------------------------------------
745 * ext-header(old style)
759 * bit6 archive bit (need to backup)
763 get_header_level0(fp, hdr, data)
774 hdr->size_field_length = 2; /* in bytes */
775 hdr->header_size = header_size = get_byte();
776 checksum = get_byte();
778 if (fread(data + COMMON_HEADER_SIZE,
779 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
780 error("Invalid header (LHarc file ?)");
781 return FALSE; /* finish */
784 if (calc_sum(data + I_METHOD, header_size) != checksum) {
785 error("Checksum error (LHarc file?)");
789 get_bytes(hdr->method, 5, sizeof(hdr->method));
790 hdr->packed_size = get_longword();
791 hdr->original_size = get_longword();
792 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
793 hdr->attribute = get_byte(); /* MS-DOS attribute */
794 hdr->header_level = get_byte();
795 name_length = get_byte();
796 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
799 /* defaults for other type */
800 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
804 extend_size = header_size+2 - name_length - 24;
806 if (extend_size < 0) {
807 if (extend_size == -2) {
808 /* CRC field is not given */
809 hdr->extend_type = EXTEND_GENERIC;
810 hdr->has_crc = FALSE;
815 error("Unkonwn header (lha file?)");
820 hdr->crc = get_word();
822 if (extend_size == 0)
825 hdr->extend_type = get_byte();
828 if (hdr->extend_type == EXTEND_UNIX) {
829 if (extend_size >= 11) {
830 hdr->minor_version = get_byte();
831 hdr->unix_last_modified_stamp = (time_t) get_longword();
832 hdr->unix_mode = get_word();
833 hdr->unix_uid = get_word();
834 hdr->unix_gid = get_word();
837 hdr->extend_type = EXTEND_GENERIC;
841 skip_bytes(extend_size);
843 hdr->header_size += 2;
851 * offset size field name
852 * -----------------------------------
853 * 0 1 header size [*1]
855 * -------------------------------------
857 * 7 4 skip size [*2] |
858 * 11 4 original size |
861 * 19 1 attribute (0x20 fixed) | [*1] header size (X+Y+25)
862 * 20 1 level (0x01 fixed) |
865 * X+ 22 2 file crc (CRC-16) |
868 * X+Y+25 2 next-header size v
869 * -------------------------------------------------
870 * X+Y+27 Z ext-header ^
872 * ----------------------------------- | [*2] skip size
875 * -------------------------------------------------
879 get_header_level1(fp, hdr, data)
884 int header_size, extend_size;
889 hdr->size_field_length = 2; /* in bytes */
890 hdr->header_size = header_size = get_byte();
891 checksum = get_byte();
893 if (fread(data + COMMON_HEADER_SIZE,
894 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
895 error("Invalid header (LHarc file ?)");
896 return FALSE; /* finish */
899 if (calc_sum(data + I_METHOD, header_size) != checksum) {
900 error("Checksum error (LHarc file?)");
904 get_bytes(hdr->method, 5, sizeof(hdr->method));
905 hdr->packed_size = get_longword(); /* skip size */
906 hdr->original_size = get_longword();
907 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
908 hdr->attribute = get_byte(); /* 0x20 fixed */
909 hdr->header_level = get_byte();
911 name_length = get_byte();
912 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
915 /* defaults for other type */
916 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
921 hdr->crc = get_word();
922 hdr->extend_type = get_byte();
924 dummy = header_size+2 - name_length - I_LEVEL1_HEADER_SIZE;
926 skip_bytes(dummy); /* skip old style extend header */
928 extend_size = get_word();
929 extend_size = get_extended_header(fp, hdr, extend_size, 0);
930 if (extend_size == -1)
933 /* On level 1 header, size fields should be adjusted. */
934 /* the `packed_size' field contains the extended header size. */
935 /* the `header_size' field does not. */
936 hdr->packed_size -= extend_size;
937 hdr->header_size += extend_size + 2;
946 * offset size field name
947 * --------------------------------------------------
948 * 0 2 total header size [*1] ^
949 * ----------------------- |
951 * 7 4 packed size [*2] |
952 * 11 4 original size |
954 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
955 * 20 1 level (0x02 fixed) | (X+26+(1))
956 * 21 2 file crc (CRC-16) |
958 * 24 2 next-header size |
959 * ----------------------------------- |
962 * ----------------------------------- |
963 * X +26 (1) padding v
964 * -------------------------------------------------
966 * : | [*2] packed size
968 * -------------------------------------------------
972 get_header_level2(fp, hdr, data)
977 int header_size, extend_size;
981 hdr->size_field_length = 2; /* in bytes */
982 hdr->header_size = header_size = get_word();
984 if (fread(data + COMMON_HEADER_SIZE,
985 I_LEVEL2_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
986 error("Invalid header (LHarc file ?)");
987 return FALSE; /* finish */
990 get_bytes(hdr->method, 5, sizeof(hdr->method));
991 hdr->packed_size = get_longword();
992 hdr->original_size = get_longword();
993 hdr->unix_last_modified_stamp = get_longword();
994 hdr->attribute = get_byte(); /* reserved */
995 hdr->header_level = get_byte();
997 /* defaults for other type */
998 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1002 hdr->has_crc = TRUE;
1003 hdr->crc = get_word();
1004 hdr->extend_type = get_byte();
1005 extend_size = get_word();
1007 INITIALIZE_CRC(hcrc);
1008 hcrc = calccrc(hcrc, data, get_ptr - data);
1010 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1011 if (extend_size == -1)
1014 padding = header_size - I_LEVEL2_HEADER_SIZE - extend_size;
1015 while (padding--) /* padding should be 0 or 1 */
1016 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1018 if (hdr->header_crc != hcrc)
1019 error("header CRC error");
1028 * offset size field name
1029 * --------------------------------------------------
1030 * 0 2 size field length (4 fixed) ^
1032 * 7 4 packed size [*2] |
1033 * 11 4 original size |
1035 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
1036 * 20 1 level (0x03 fixed) | (X+32)
1037 * 21 2 file crc (CRC-16) |
1039 * 24 4 total header size [*1] |
1040 * 28 4 next-header size |
1041 * ----------------------------------- |
1044 * -------------------------------------------------
1046 * : | [*2] packed size
1048 * -------------------------------------------------
1052 get_header_level3(fp, hdr, data)
1057 long header_size, extend_size;
1061 hdr->size_field_length = get_word();
1063 if (fread(data + COMMON_HEADER_SIZE,
1064 I_LEVEL3_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
1065 error("Invalid header (LHarc file ?)");
1066 return FALSE; /* finish */
1069 get_bytes(hdr->method, 5, sizeof(hdr->method));
1070 hdr->packed_size = get_longword();
1071 hdr->original_size = get_longword();
1072 hdr->unix_last_modified_stamp = get_longword();
1073 hdr->attribute = get_byte(); /* reserved */
1074 hdr->header_level = get_byte();
1076 /* defaults for other type */
1077 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1081 hdr->has_crc = TRUE;
1082 hdr->crc = get_word();
1083 hdr->extend_type = get_byte();
1084 hdr->header_size = header_size = get_longword();
1085 extend_size = get_longword();
1087 INITIALIZE_CRC(hcrc);
1088 hcrc = calccrc(hcrc, data, get_ptr - data);
1090 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1091 if (extend_size == -1)
1094 padding = header_size - I_LEVEL3_HEADER_SIZE - extend_size;
1095 while (padding--) /* padding should be 0 */
1096 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1098 if (hdr->header_crc != hcrc)
1099 error("header CRC error");
1109 char data[LZHEADER_STORAGE];
1111 int archive_kanji_code = CODE_SJIS;
1112 int system_kanji_code = default_system_kanji_code;
1113 char *archive_delim = "\377\\"; /* `\' is for level 0 header and
1115 char *system_delim = "//";
1116 int filename_case = NONE;
1119 memset(hdr, 0, sizeof(LzHeader));
1123 if ((end_mark = getc(fp)) == EOF || end_mark == 0) {
1124 return FALSE; /* finish */
1128 if (fread(data + 1, COMMON_HEADER_SIZE - 1, 1, fp) == 0) {
1129 error("Invalid header (LHarc file ?)");
1130 return FALSE; /* finish */
1133 switch (data[I_HEADER_LEVEL]) {
1135 if (get_header_level0(fp, hdr, data) == FALSE)
1139 if (get_header_level1(fp, hdr, data) == FALSE)
1143 if (get_header_level2(fp, hdr, data) == FALSE)
1147 if (get_header_level3(fp, hdr, data) == FALSE)
1151 error("Unknown level header (level %d)", data[I_HEADER_LEVEL]);
1155 /* filename conversion */
1156 switch (hdr->extend_type) {
1158 filename_case = noconvertcase ? NONE : TO_LOWER;
1165 filename_case = NONE;
1169 archive_delim = "\377/:\\";
1170 /* `\' is for level 0 header and broken archive. */
1171 system_delim = "/://";
1172 filename_case = NONE;
1176 filename_case = noconvertcase ? NONE : TO_LOWER;
1180 if (optional_archive_kanji_code)
1181 archive_kanji_code = optional_archive_kanji_code;
1182 if (optional_system_kanji_code)
1183 system_kanji_code = optional_system_kanji_code;
1184 if (optional_archive_delim)
1185 archive_delim = optional_archive_delim;
1186 if (optional_system_delim)
1187 system_delim = optional_system_delim;
1188 if (optional_filename_case)
1189 filename_case = optional_filename_case;
1191 /* kanji code and delimiter conversion */
1192 convert_filename(hdr->name, strlen(hdr->name), sizeof(hdr->name),
1195 archive_delim, system_delim, filename_case);
1197 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1199 /* split symbolic link */
1200 p = strchr(hdr->name, '|');
1202 /* hdr->name is symbolic link name */
1203 /* hdr->realname is real name */
1205 strncpy(hdr->realname, p+1, sizeof(hdr->realname));
1208 error("unknown symlink name \"%s\"", hdr->name);
1214 /* skip SFX header */
1216 skip_msdos_sfx1_code(fp)
1219 unsigned char buffer[64 * 1024]; /* SFX header size */
1223 n = fread(buffer, 1, sizeof(buffer), fp);
1225 for (p = buffer; p < buffer + n; p++) {
1226 if (! (p[I_METHOD]=='-' && p[I_METHOD+1]=='l' && p[I_METHOD+4]=='-'))
1228 /* found "-l??-" keyword (as METHOD type string) */
1230 /* size and checksum validate check */
1232 /* level 0 header */
1233 if (p[I_HEADER_LEVEL] == 0
1234 && p[I_HEADER_SIZE] > 20
1235 && p[I_HEADER_CHECKSUM] == calc_sum(p+2, p[I_HEADER_SIZE])) {
1236 fseek(fp, (p - buffer) - n, SEEK_CUR);
1240 /* level 2 header */
1241 if (p[I_HEADER_LEVEL] == 2
1242 && p[I_HEADER_SIZE] >= 24
1243 && p[I_ATTRIBUTE] == 0x20) {
1244 fseek(fp, (p - buffer) - n, SEEK_CUR);
1249 fseek(fp, -n, SEEK_CUR);
1253 /* ------------------------------------------------------------------------ */
1255 init_header(name, v_stat, hdr)
1257 struct stat *v_stat;
1262 memset(hdr, 0, sizeof(LzHeader));
1264 /* the `method' member is rewrote by the encoding function.
1265 but need set for empty files */
1266 memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STORAGE);
1268 hdr->packed_size = 0;
1269 hdr->original_size = v_stat->st_size;
1270 hdr->attribute = GENERIC_ATTRIBUTE;
1271 hdr->header_level = header_level;
1272 strcpy(hdr->name, name);
1275 hdr->extend_type = EXTEND_UNIX;
1276 hdr->unix_last_modified_stamp = v_stat->st_mtime;
1277 /* since 00:00:00 JAN.1.1970 */
1278 #ifdef NOT_COMPATIBLE_MODE
1279 /* Please need your modification in this space. */
1281 hdr->unix_mode = v_stat->st_mode;
1284 hdr->unix_uid = v_stat->st_uid;
1285 hdr->unix_gid = v_stat->st_gid;
1287 #if INCLUDE_OWNER_NAME_IN_HEADER
1290 struct passwd *ent = getpwuid(hdr->unix_uid);
1293 strncpy(hdr->user, ent->pw_name, sizeof(hdr->user));
1294 if (hdr->user[sizeof(hdr->user)-1])
1295 hdr->user[sizeof(hdr->user)-1] = 0;
1301 struct group *ent = getgrgid(hdr->unix_gid);
1304 strncpy(hdr->group, ent->gr_name, sizeof(hdr->group));
1305 if (hdr->group[sizeof(hdr->group)-1])
1306 hdr->group[sizeof(hdr->group)-1] = 0;
1310 #endif /* INCLUDE_OWNER_NAME_IN_HEADER */
1311 if (is_directory(v_stat)) {
1312 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1313 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1314 hdr->original_size = 0;
1315 if (len > 0 && hdr->name[len - 1] != '/')
1316 strcpy(&hdr->name[len++], "/");
1320 if (is_symlink(v_stat)) {
1321 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1322 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1323 hdr->original_size = 0;
1324 readlink(name, hdr->realname, sizeof(hdr->realname));
1330 write_unix_info(hdr)
1333 /* UNIX specific informations */
1335 put_word(5); /* size */
1336 put_byte(0x50); /* permission */
1337 put_word(hdr->unix_mode);
1339 put_word(7); /* size */
1340 put_byte(0x51); /* gid and uid */
1341 put_word(hdr->unix_gid);
1342 put_word(hdr->unix_uid);
1344 if (hdr->group[0]) {
1345 int len = strlen(hdr->group);
1346 put_word(len + 3); /* size */
1347 put_byte(0x52); /* group name */
1348 put_bytes(hdr->group, len);
1352 int len = strlen(hdr->user);
1353 put_word(len + 3); /* size */
1354 put_byte(0x53); /* user name */
1355 put_bytes(hdr->user, len);
1358 if (hdr->header_level == 1) {
1359 put_word(7); /* size */
1360 put_byte(0x54); /* time stamp */
1361 put_longword(hdr->unix_last_modified_stamp);
1365 /* ------------------------------------------------------------------------ */
1366 /* Write unix extended header or generic header. */
1369 write_header_level0(data, hdr, pathname)
1371 char *data, *pathname;
1378 memset(data, 0, LZHEADER_STORAGE);
1380 put_byte(0x00); /* header size */
1381 put_byte(0x00); /* check sum */
1382 put_bytes(hdr->method, 5);
1383 put_longword(hdr->packed_size);
1384 put_longword(hdr->original_size);
1385 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1386 put_byte(hdr->attribute);
1387 put_byte(hdr->header_level); /* level 0 */
1389 /* write pathname (level 0 header contains the directory part) */
1390 name_length = strlen(pathname);
1392 limit = 255 - I_GENERIC_HEADER_SIZE + 2;
1394 limit = 255 - I_LEVEL0_HEADER_SIZE + 2;
1396 if (name_length > limit) {
1397 warning("the length of pathname \"%s\" is too long.", pathname);
1398 name_length = limit;
1400 put_byte(name_length);
1401 put_bytes(pathname, name_length);
1404 if (generic_format) {
1405 header_size = I_GENERIC_HEADER_SIZE + name_length - 2;
1406 data[I_HEADER_SIZE] = header_size;
1407 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1409 /* write old-style extend header */
1410 put_byte(EXTEND_UNIX);
1411 put_byte(CURRENT_UNIX_MINOR_VERSION);
1412 put_longword(hdr->unix_last_modified_stamp);
1413 put_word(hdr->unix_mode);
1414 put_word(hdr->unix_uid);
1415 put_word(hdr->unix_gid);
1417 /* size of extended header is 12 */
1418 header_size = I_LEVEL0_HEADER_SIZE + name_length - 2;
1419 data[I_HEADER_SIZE] = header_size;
1420 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1423 return header_size + 2;
1427 write_header_level1(data, hdr, pathname)
1429 char *data, *pathname;
1431 int name_length, dir_length, limit;
1432 char *basename, *dirname;
1434 char *extend_header_top;
1435 int extend_header_size;
1437 basename = strrchr(pathname, LHA_PATHSEP);
1440 name_length = strlen(basename);
1442 dir_length = basename - dirname;
1445 basename = pathname;
1446 name_length = strlen(basename);
1452 memset(data, 0, LZHEADER_STORAGE);
1454 put_byte(0x00); /* header size */
1455 put_byte(0x00); /* check sum */
1456 put_bytes(hdr->method, 5);
1457 put_longword(hdr->packed_size);
1458 put_longword(hdr->original_size);
1459 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1461 put_byte(hdr->header_level); /* level 1 */
1463 /* level 1 header: write filename (basename only) */
1464 limit = 255 - I_LEVEL1_HEADER_SIZE + 2;
1465 if (name_length > limit) {
1466 put_byte(0); /* name length */
1469 put_byte(name_length);
1470 put_bytes(basename, name_length);
1478 put_byte(EXTEND_UNIX);
1480 /* write extend header from here. */
1482 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1483 header_size = extend_header_top - data - 2;
1485 /* write filename and dirname */
1487 if (name_length > limit) {
1488 put_word(name_length + 3); /* size */
1489 put_byte(0x01); /* filename */
1490 put_bytes(basename, name_length);
1493 if (dir_length > 0) {
1494 put_word(dir_length + 3); /* size */
1495 put_byte(0x02); /* dirname */
1496 put_bytes(dirname, dir_length);
1499 if (!generic_format)
1500 write_unix_info(hdr);
1502 put_word(0x0000); /* next header size */
1504 extend_header_size = put_ptr - extend_header_top;
1505 /* On level 1 header, the packed size field is contains the ext-header */
1506 hdr->packed_size += put_ptr - extend_header_top;
1508 /* put `skip size' */
1509 setup_put(data + I_PACKED_SIZE);
1510 put_longword(hdr->packed_size);
1512 data[I_HEADER_SIZE] = header_size;
1513 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1515 return header_size + extend_header_size + 2;
1519 write_header_level2(data, hdr, pathname)
1521 char *data, *pathname;
1523 int name_length, dir_length;
1524 char *basename, *dirname;
1526 char *extend_header_top;
1527 char *headercrc_ptr;
1530 basename = strrchr(pathname, LHA_PATHSEP);
1533 name_length = strlen(basename);
1535 dir_length = basename - dirname;
1538 basename = pathname;
1539 name_length = strlen(basename);
1545 memset(data, 0, LZHEADER_STORAGE);
1547 put_word(0x0000); /* header size */
1548 put_bytes(hdr->method, 5);
1549 put_longword(hdr->packed_size);
1550 put_longword(hdr->original_size);
1551 put_longword(hdr->unix_last_modified_stamp);
1553 put_byte(hdr->header_level); /* level 2 */
1560 put_byte(EXTEND_UNIX);
1562 /* write extend header from here. */
1564 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1566 /* write common header */
1569 headercrc_ptr = put_ptr;
1570 put_word(0x0000); /* header CRC */
1572 /* write filename and dirname */
1573 /* must have this header, even if the name_length is 0. */
1574 put_word(name_length + 3); /* size */
1575 put_byte(0x01); /* filename */
1576 put_bytes(basename, name_length);
1578 if (dir_length > 0) {
1579 put_word(dir_length + 3); /* size */
1580 put_byte(0x02); /* dirname */
1581 put_bytes(dirname, dir_length);
1584 if (!generic_format)
1585 write_unix_info(hdr);
1587 put_word(0x0000); /* next header size */
1589 header_size = put_ptr - data;
1590 if ((header_size & 0xff) == 0) {
1591 /* cannot put zero at the first byte on level 2 header. */
1592 /* adjust header size. */
1593 put_byte(0); /* padding */
1597 /* put hader size */
1598 setup_put(data + I_HEADER_SIZE);
1599 put_word(header_size);
1601 /* put hader CRC in extended header */
1602 INITIALIZE_CRC(hcrc);
1603 hcrc = calccrc(hcrc, data, (unsigned int) header_size);
1604 setup_put(headercrc_ptr);
1611 write_header(fp, hdr)
1616 char data[LZHEADER_STORAGE];
1618 int archive_kanji_code = CODE_SJIS;
1619 int system_kanji_code = default_system_kanji_code;
1620 char *archive_delim = "\377";
1621 char *system_delim = "/";
1622 int filename_case = NONE;
1623 char pathname[FILENAME_LENGTH];
1625 if (optional_archive_kanji_code)
1626 archive_kanji_code = optional_archive_kanji_code;
1627 if (optional_system_kanji_code)
1628 system_kanji_code = optional_system_kanji_code;
1631 filename_case = TO_UPPER;
1633 if (hdr->header_level == HEADER_LEVEL0) {
1634 archive_delim = "\\";
1637 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1639 p = strchr(hdr->name, '|');
1641 error("symlink name \"%s\" contains '|' char. change it into '_'",
1645 if (xsnprintf(pathname, sizeof(pathname),
1646 "%s|%s", hdr->name, hdr->realname) == -1)
1647 error("file name is too long (%s -> %s)", hdr->name, hdr->realname);
1650 strncpy(pathname, hdr->name, sizeof(pathname));
1651 pathname[sizeof(pathname)-1] = 0;
1654 convert_filename(pathname, strlen(pathname), sizeof(pathname),
1657 system_delim, archive_delim, filename_case);
1659 switch (hdr->header_level) {
1661 header_size = write_header_level0(data, hdr, pathname);
1664 header_size = write_header_level1(data, hdr, pathname);
1667 header_size = write_header_level2(data, hdr, pathname);
1670 error("Unknown level header (level %d)", hdr->header_level);
1674 if (fwrite(data, header_size, 1, fp) == 0)
1675 fatal_error("Cannot write to temporary file");
1678 #if MULTIBYTE_FILENAME
1680 #if defined(__APPLE__)
1682 #include <CoreFoundation/CFString.h>
1683 #include <CoreFoundation/CFStringEncodingExt.h>
1685 /* this is not need for Mac OS X v 10.2 later */
1687 kCFStringEncodingAllowLossyConversion = 1,
1688 kCFStringEncodingBasicDirectionLeftToRight = (1 << 1),
1689 kCFStringEncodingBasicDirectionRightToLeft = (1 << 2),
1690 kCFStringEncodingSubstituteCombinings = (1 << 3),
1691 kCFStringEncodingComposeCombinings = (1 << 4),
1692 kCFStringEncodingIgnoreCombinings = (1 << 5),
1693 kCFStringEncodingUseCanonical = (1 << 6),
1694 kCFStringEncodingUseHFSPlusCanonical = (1 << 7),
1695 kCFStringEncodingPrependBOM = (1 << 8),
1696 kCFStringEncodingDisableCorporateArea = (1 << 9),
1697 kCFStringEncodingASCIICompatibleConversion = (1 << 10),
1701 ConvertEncodingToUTF8(const char* inCStr,
1702 char* outUTF8Buffer,
1703 int outUTF8BufferLength,
1704 unsigned long scriptEncoding,
1705 unsigned long flags)
1707 unsigned long unicodeChars;
1708 unsigned long srcCharsUsed;
1709 unsigned long usedByteLen = 0;
1710 UniChar uniStr[512];
1711 unsigned long cfResult;
1713 cfResult = CFStringEncodingBytesToUnicode(scriptEncoding,
1721 if (cfResult == 0) {
1722 cfResult = CFStringEncodingUnicodeToBytes(kCFStringEncodingUTF8,
1727 (char*)outUTF8Buffer,
1728 outUTF8BufferLength - 1,
1730 outUTF8Buffer[usedByteLen] = '\0';
1737 ConvertUTF8ToEncoding(const char* inUTF8Buf,
1738 int inUTF8BufLength,
1739 char* outCStrBuffer,
1740 int outCStrBufferLength,
1741 unsigned long scriptEncoding,
1742 unsigned long flags)
1744 unsigned long unicodeChars;
1745 unsigned long srcCharsUsed;
1746 unsigned long usedByteLen = 0;
1747 UniChar uniStr[256];
1748 unsigned long cfResult;
1750 cfResult = CFStringEncodingBytesToUnicode(kCFStringEncodingUTF8,
1758 if (cfResult == 0) {
1759 cfResult = CFStringEncodingUnicodeToBytes(scriptEncoding,
1764 (char*)outCStrBuffer,
1765 outCStrBufferLength - 1,
1767 outCStrBuffer[usedByteLen] = '\0';
1777 ConvertEncodingByIconv(const char *src, char *dst, int dstsize,
1778 const char *srcEnc, const char *dstEnc)
1781 static char szTmpBuf[2048];
1787 dst_p = &szTmpBuf[0];
1788 iLen = (size_t)sizeof(szTmpBuf)-1;
1789 src_p = (char *)src;
1790 sLen = (size_t)strlen(src);
1791 memset(szTmpBuf, 0, sizeof(szTmpBuf));
1792 memset(dst, 0, dstsize);
1794 ic = iconv_open(dstEnc, srcEnc);
1795 if (ic == (iconv_t)-1) {
1796 error("iconv_open() failure: %s", strerror(errno));
1800 if (iconv(ic, &src_p, &sLen, &dst_p, &iLen) == (size_t)-1) {
1801 error("iconv() failure: %s", strerror(errno));
1806 strncpy(dst, szTmpBuf, dstsize);
1812 #endif /* defined(__APPLE__) */
1815 sjis_to_utf8(char *dst, const char *src, size_t dstsize)
1817 #if defined(__APPLE__)
1819 if (ConvertEncodingToUTF8(src, dst, dstsize,
1820 kCFStringEncodingDOSJapanese,
1821 kCFStringEncodingUseHFSPlusCanonical) == 0)
1824 if (ConvertEncodingByIconv(src, dst, dstsize, "SJIS", "UTF-8") != -1)
1827 error("not support utf-8 conversion");
1830 if (dstsize < 1) return dst;
1832 return strncpy(dst, src, dstsize-1);
1836 utf8_to_sjis(char *dst, const char *src, size_t dstsize)
1838 #if defined(__APPLE__)
1842 srclen = strlen(src);
1843 if (ConvertUTF8ToEncoding(src, srclen, dst, dstsize,
1844 kCFStringEncodingDOSJapanese,
1845 kCFStringEncodingUseHFSPlusCanonical) == 0)
1848 if (ConvertEncodingByIconv(src, dst, dstsize, "UTF-8", "SJIS") != -1)
1851 error("not support utf-8 conversion");
1854 if (dstsize < 1) return dst;
1856 return strncpy(dst, src, dstsize-1);
1860 * SJIS <-> EUC ÊÑ´¹´Ø¿ô
1861 * ¡ÖÆüËܸì¾ðÊó½èÍý¡× ¥½¥Õ¥È¥Ð¥ó¥¯(³ô)
1862 * ¤è¤êÈ´¿è(by Koji Arai)
1865 euc2sjis(int *p1, int *p2)
1867 unsigned char c1 = *p1 & 0x7f;
1868 unsigned char c2 = *p2 & 0x7f;
1869 int rowoff = c1 < 0x5f ? 0x70 : 0xb0;
1870 int celoff = c1 % 2 ? (c2 > 0x5f ? 0x20 : 0x1f) : 0x7e;
1871 *p1 = ((c1 + 1) >> 1) + rowoff;
1872 *p2 += celoff - 0x80;
1876 sjis2euc(int *p1, int *p2)
1878 unsigned char c1 = *p1;
1879 unsigned char c2 = *p2;
1880 int adjust = c2 < 0x9f;
1881 int rowoff = c1 < 0xa0 ? 0x70 : 0xb0;
1882 int celoff = adjust ? (c2 > 0x7f ? 0x20 : 0x1f) : 0x7e;
1883 *p1 = ((c1 - rowoff) << 1) - adjust;
1889 #endif /* MULTIBYTE_FILENAME */