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());
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]))
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 #if defined(HAVE_MKTIME)
371 #ifdef HAVE_TIMELOCAL
372 #undef HAVE_TIMELOCAL
374 #endif /* defined(HAVE_MKTIME) */
376 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL)
380 #endif /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
382 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) || defined(HAVE_TZSET)
388 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) || defined(HAVE_TZSET) || defined(HAVE_FTIME)
389 #ifdef HAVE_GETTIMEOFDAY
390 #undef HAVE_GETTIMEOFDAY
393 #ifndef HAVE_GETTIMEOFDAY
394 #define HAVE_GETTIMEOFDAY /* use gettimeofday() */
399 #include <sys/timeb.h>
403 * You may define as : #define TIMEZONE_HOOK \ extern long
404 * timezone ; \ extern void tzset();
408 /* Which do you like better, `TIMEZONE_HOOK' or `TIMEZONE_HOOK;' ? */
411 #if defined(HAVE_TZSET) && defined(_MINIX)
412 extern long timezone; /* not defined in time.h */
415 /* ------------------------------------------------------------------------ */
416 #if defined(HAVE_FTIME) || defined(HAVE_GETTIMEOFDAY) || defined(HAVE_TZSET)
426 /* ------------------------------------------------------------------------ */
427 #if !defined(HAVE_TZSET) && defined(HAVE_FTIME)
432 return buf.timezone * 60L;
436 /* ------------------------------------------------------------------------ */
437 #if !defined(HAVE_TZSET) && !defined(HAVE_FTIME) /* maybe defined(HAVE_GETTIMEOFDAY) */
439 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
443 return -localtime(&tt)->tm_gmtoff;
444 #else /* HAVE_STRUCT_TM_TM_GMTOFF */
445 #if GETTIMEOFDAY_HAS_2ND_ARG
448 gettimeofday(&tp, &tzp);/* specific to 4.3BSD */
450 * return (tzp.tz_minuteswest * 60L + (tzp.tz_dsttime != 0 ? 60L *
453 return (tzp.tz_minuteswest * 60L);
455 /* Compile error will be caused */
456 CANNOT GET TIMEZONE INFORMATION ON YOUR SYSTEM.
457 TAKE THE ANOTHER WAY.
459 #endif /* HAVE_STRUCT_TM_TM_GMTOFF */
462 #endif /* defined(HAVE_FTIME) || defined(HAVE_GETTIMEOFDAY) ||
463 * defined(HAVE_TZSET) */
465 /* ------------------------------------------------------------------------ */
467 generic_to_unix_stamp(t)
469 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL)
474 * special case: if MSDOS format date and time were zero, then we
475 * set time to be zero here too.
480 dostm.tm_sec = (t & 0x1f) * 2;
481 dostm.tm_min = t >> 5 & 0x3f;
482 dostm.tm_hour = t >> 11 & 0x1f;
483 dostm.tm_mday = t >> 16 & 0x1f;
484 dostm.tm_mon = (t >> (16+5) & 0x0f) - 1; /* 0..11 */
485 dostm.tm_year = (t >> (16+9) & 0x7f) + 80;
487 dostm.tm_isdst = 0; /* correct? */
489 dostm.tm_isdst = -1; /* correct? */
491 return (time_t) mktime(&dostm);
492 #else /* maybe defined(HAVE_TIMELOCAL) */
493 return (time_t) timelocal(&dostm);
497 #else /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
499 int year, month, day, hour, min, sec;
501 static unsigned int dsboy[12] = {0, 31, 59, 90, 120, 151,
502 181, 212, 243, 273, 304, 334};
506 * special case: if MSDOS format date and time were zero, then we
507 * set time to be zero here too.
512 year = ((int) (t >> 16 + 9) & 0x7f) + 1980;
513 month = (int) (t >> 16 + 5) & 0x0f; /* 1..12 means Jan..Dec */
514 day = (int) (t >> 16) & 0x1f; /* 1..31 means 1st,...31st */
516 hour = ((int) t >> 11) & 0x1f;
517 min = ((int) t >> 5) & 0x3f;
518 sec = ((int) t & 0x1f) * 2;
520 /* Calculate days since 1970.01.01 */
521 days = (365 * (year - 1970) + /* days due to whole years */
522 (year - 1970 + 1) / 4 + /* days due to leap years */
523 dsboy[month - 1] + /* days since beginning of this year */
524 day - 1); /* days since beginning of month */
526 if ((year % 4 == 0) &&
527 (year % 100 != 0 || year % 400 == 0) && /* 1999.5.24 t.oka */
528 (month >= 3)) /* if this is a leap year and month */
529 days++; /* is March or later, add a day */
531 /* Knowing the days, we can find seconds */
532 longtime = (((days * 24) + hour) * 60 + min) * 60 + sec;
533 longtime += gettz(); /* adjust for timezone */
535 /* LONGTIME is now the time in seconds, since 1970/01/01 00:00:00. */
536 return (time_t) longtime;
538 #endif /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
540 /* ------------------------------------------------------------------------ */
542 unix_to_generic_stamp(t)
545 struct tm *tm = localtime(&t);
547 return ((((long) (tm->tm_year - 80)) << 25) +
548 (((long) (tm->tm_mon + 1)) << 21) +
549 (((long) tm->tm_mday) << 16) +
550 (long) ((tm->tm_hour << 11) +
555 /* ------------------------------------------------------------------------ */
556 /* build header functions */
557 /* ------------------------------------------------------------------------ */
563 * --------------------------------
565 * 2 or 4 next-header size [*1]
566 * --------------------------------------
567 * ext header: 1 ext-type ^
568 * ? contents | [*1] next-header size
569 * 2 or 4 next-header size v
570 * --------------------------------------
572 * on level 1, 2 header:
573 * size field is 2 bytes
575 * size field is 4 bytes
578 get_extended_header(fp, hdr, header_size, hcrc)
584 char data[LZHEADER_STORAGE];
586 char dirname[FILENAME_LENGTH];
589 long whole_size = header_size;
591 int n = 1 + hdr->size_field_length; /* `ext-type' + `next-header size' */
593 if (hdr->header_level == 0)
596 name_length = strlen(hdr->name);
598 while (header_size) {
600 if (sizeof(data) < header_size) {
601 error("header size (%ld) too large.", header_size);
605 if (fread(data, header_size, 1, fp) == 0) {
606 error("Invalid header (LHa file ?)");
610 ext_type = get_byte();
613 /* header crc (CRC-16) */
614 hdr->header_crc = get_word();
615 /* clear buffer for CRC calculation. */
616 data[1] = data[2] = 0;
617 skip_bytes(header_size - n - 2);
622 get_bytes(hdr->name, header_size-n, sizeof(hdr->name)-1);
623 hdr->name[name_length] = 0;
627 dir_length = get_bytes(dirname, header_size-n, sizeof(dirname)-1);
628 dirname[dir_length] = 0;
631 /* MS-DOS attribute */
632 hdr->attribute = get_word();
635 /* Windows time stamp (FILETIME structure) */
636 skip_bytes(header_size - n); /* ignored */
639 /* UNIX permission */
640 hdr->unix_mode = get_word();
643 /* UNIX gid and uid */
644 hdr->unix_gid = get_word();
645 hdr->unix_uid = get_word();
648 /* UNIX group name */
649 i = get_bytes(hdr->group, header_size-n, sizeof(hdr->group)-1);
650 hdr->group[i] = '\0';
654 i = get_bytes(hdr->user, header_size-n, sizeof(hdr->user)-1);
658 /* UNIX last modified time */
659 hdr->unix_last_modified_stamp = (time_t) get_longword();
663 /* 0x39: multi-disk header
664 0x3f: uncompressed comment
665 0x42: 64bit large file size
666 0x48-0x4f(?): reserved for authenticity verification
668 0x7e: extended attribute - platform information
669 0x7f: extended attribute - permission, owner-id and timestamp
671 0xc4: compressed comment (dict size: 4096)
672 0xc5: compressed comment (dict size: 8192)
673 0xc6: compressed comment (dict size: 16384)
674 0xc7: compressed comment (dict size: 32768)
675 0xc8: compressed comment (dict size: 65536)
676 0xd0-0xdf(?): operating systemm specific information
677 0xfc: encapsulation (another opinion)
678 0xfe: extended attribute - platform information(another opinion)
679 0xff: extended attribute - permission, owner-id and timestamp
680 (level 3 on UNLHA32) */
682 warning("unknown extended header 0x%02x", ext_type);
683 skip_bytes(header_size - n);
688 *hcrc = calccrc(*hcrc, data, header_size);
690 if (hdr->size_field_length == 2)
691 whole_size += header_size = get_word();
693 whole_size += header_size = get_longword();
696 /* concatenate dirname and filename */
698 if (name_length + dir_length >= sizeof(hdr->name)) {
699 warning("the length of pathname \"%s%s\" is too long.",
701 name_length = sizeof(hdr->name) - dir_length - 1;
702 hdr->name[name_length] = 0;
704 strcat(dirname, hdr->name);
705 strcpy(hdr->name, dirname);
706 name_length += dir_length;
716 * offset size field name
717 * ----------------------------------
718 * 0 1 header size [*1]
720 * ---------------------------------------
722 * 7 4 packed size [*2] |
723 * 11 4 original size |
726 * 19 1 attribute | [*1] header size (X+Y+22)
727 * 20 1 level (0x00 fixed) |
730 * X +22 2 file crc (CRC-16) |
731 * X +24 Y ext-header(old style) v
732 * -------------------------------------------------
734 * : | [*2] packed size
736 * -------------------------------------------------
738 * ext-header(old style)
752 * bit6 archive bit (need to backup)
756 get_header_level0(fp, hdr, data)
767 hdr->size_field_length = 2; /* in bytes */
768 hdr->header_size = header_size = get_byte();
769 checksum = get_byte();
771 if (fread(data+I_NAME_LENGTH, header_size+2-I_NAME_LENGTH, 1, fp) == 0) {
772 error("Invalid header (LHarc file ?)");
773 return FALSE; /* finish */
776 if (calc_sum(data + I_METHOD, header_size) != checksum) {
777 error("Checksum error (LHarc file?)");
781 get_bytes(hdr->method, 5, sizeof(hdr->method));
782 hdr->packed_size = get_longword();
783 hdr->original_size = get_longword();
784 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
785 hdr->attribute = get_byte(); /* MS-DOS attribute */
786 hdr->header_level = get_byte();
787 name_length = get_byte();
788 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
791 /* defaults for other type */
792 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
796 extend_size = header_size+2 - name_length - 24;
798 if (extend_size < 0) {
799 if (extend_size == -2) {
800 /* CRC field is not given */
801 hdr->extend_type = EXTEND_GENERIC;
802 hdr->has_crc = FALSE;
807 error("Unkonwn header (lha file?)");
812 hdr->crc = get_word();
814 if (extend_size == 0)
817 hdr->extend_type = get_byte();
820 if (hdr->extend_type == EXTEND_UNIX) {
821 if (extend_size >= 11) {
822 hdr->minor_version = get_byte();
823 hdr->unix_last_modified_stamp = (time_t) get_longword();
824 hdr->unix_mode = get_word();
825 hdr->unix_uid = get_word();
826 hdr->unix_gid = get_word();
829 hdr->extend_type = EXTEND_GENERIC;
833 skip_bytes(extend_size);
835 hdr->header_size += 2;
843 * offset size field name
844 * -----------------------------------
845 * 0 1 header size [*1]
847 * -------------------------------------
849 * 7 4 skip size [*2] |
850 * 11 4 original size |
853 * 19 1 attribute (0x20 fixed) | [*1] header size (X+Y+25)
854 * 20 1 level (0x01 fixed) |
857 * X+ 22 2 file crc (CRC-16) |
860 * X+Y+25 2 next-header size v
861 * -------------------------------------------------
862 * X+Y+27 Z ext-header ^
864 * ----------------------------------- | [*2] skip size
867 * -------------------------------------------------
871 get_header_level1(fp, hdr, data)
876 int header_size, extend_size;
881 hdr->size_field_length = 2; /* in bytes */
882 hdr->header_size = header_size = get_byte();
883 checksum = get_byte();
885 if (fread(data+I_NAME_LENGTH, header_size+2-I_NAME_LENGTH, 1, fp) == 0) {
886 error("Invalid header (LHarc file ?)");
887 return FALSE; /* finish */
890 if (calc_sum(data + I_METHOD, header_size) != checksum) {
891 error("Checksum error (LHarc file?)");
895 get_bytes(hdr->method, 5, sizeof(hdr->method));
896 hdr->packed_size = get_longword(); /* skip size */
897 hdr->original_size = get_longword();
898 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
899 hdr->attribute = get_byte(); /* 0x20 fixed */
900 hdr->header_level = get_byte();
902 name_length = get_byte();
903 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
906 /* defaults for other type */
907 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
912 hdr->crc = get_word();
913 hdr->extend_type = get_byte();
915 dummy = header_size+2 - name_length - 27;
917 skip_bytes(dummy); /* skip old style extend header */
919 extend_size = get_word();
920 extend_size = get_extended_header(fp, hdr, extend_size, 0);
921 if (extend_size == -1)
924 /* On level 1 header, size fields should be adjusted. */
925 /* the `packed_size' field contains the extended header size. */
926 /* the `header_size' field does not. */
927 hdr->packed_size -= extend_size;
928 hdr->header_size += extend_size + 2;
937 * offset size field name
938 * --------------------------------------------------
939 * 0 2 total header size [*1] ^
940 * ----------------------- |
942 * 7 4 packed size [*2] |
943 * 11 4 original size |
945 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
946 * 20 1 level (0x02 fixed) | (X+26+(1))
947 * 21 2 file crc (CRC-16) |
949 * 24 2 next-header size |
950 * ----------------------------------- |
953 * ----------------------------------- |
954 * X +26 (1) padding v
955 * -------------------------------------------------
957 * : | [*2] packed size
959 * -------------------------------------------------
963 get_header_level2(fp, hdr, data)
968 int header_size, extend_size;
972 hdr->size_field_length = 2; /* in bytes */
973 hdr->header_size = header_size = get_word();
975 if (fread(data + I_NAME_LENGTH, 26 - I_NAME_LENGTH, 1, fp) == 0) {
976 error("Invalid header (LHarc file ?)");
977 return FALSE; /* finish */
980 get_bytes(hdr->method, 5, sizeof(hdr->method));
981 hdr->packed_size = get_longword();
982 hdr->original_size = get_longword();
983 hdr->unix_last_modified_stamp = get_longword();
984 hdr->attribute = get_byte(); /* reserved */
985 hdr->header_level = get_byte();
987 /* defaults for other type */
988 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
993 hdr->crc = get_word();
994 hdr->extend_type = get_byte();
995 extend_size = get_word();
997 INITIALIZE_CRC(hcrc);
998 hcrc = calccrc(hcrc, data, get_ptr - data);
1000 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1001 if (extend_size == -1)
1004 padding = header_size - 26 - extend_size;
1005 while (padding--) /* padding should be 0 or 1 */
1006 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1008 if (hdr->header_crc != hcrc)
1009 error("header CRC error");
1018 * offset size field name
1019 * --------------------------------------------------
1020 * 0 2 size field length (4 fixed) ^
1022 * 7 4 packed size [*2] |
1023 * 11 4 original size |
1025 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
1026 * 20 1 level (0x03 fixed) | (X+32)
1027 * 21 2 file crc (CRC-16) |
1029 * 24 4 total header size [*1] |
1030 * 28 4 next-header size |
1031 * ----------------------------------- |
1034 * -------------------------------------------------
1036 * : | [*2] packed size
1038 * -------------------------------------------------
1042 get_header_level3(fp, hdr, data)
1047 long header_size, extend_size;
1051 hdr->size_field_length = get_word();
1053 if (fread(data + I_NAME_LENGTH, 32 - I_NAME_LENGTH, 1, fp) == 0) {
1054 error("Invalid header (LHarc file ?)");
1055 return FALSE; /* finish */
1058 get_bytes(hdr->method, 5, sizeof(hdr->method));
1059 hdr->packed_size = get_longword();
1060 hdr->original_size = get_longword();
1061 hdr->unix_last_modified_stamp = get_longword();
1062 hdr->attribute = get_byte(); /* reserved */
1063 hdr->header_level = get_byte();
1065 /* defaults for other type */
1066 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1070 hdr->has_crc = TRUE;
1071 hdr->crc = get_word();
1072 hdr->extend_type = get_byte();
1073 hdr->header_size = header_size = get_longword();
1074 extend_size = get_longword();
1076 INITIALIZE_CRC(hcrc);
1077 hcrc = calccrc(hcrc, data, get_ptr - data);
1079 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1080 if (extend_size == -1)
1083 padding = header_size - 32 - extend_size;
1084 while (padding--) /* padding should be 0 */
1085 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1087 if (hdr->header_crc != hcrc)
1088 error("header CRC error");
1098 char data[LZHEADER_STORAGE];
1100 int archive_kanji_code = CODE_SJIS;
1101 int system_kanji_code = default_system_kanji_code;
1102 char *archive_delim = "\377\\"; /* `\' is for level 0 header and
1104 char *system_delim = "//";
1105 int filename_case = NONE;
1108 memset(hdr, 0, sizeof(LzHeader));
1112 if ((end_mark = getc(fp)) == EOF || end_mark == 0) {
1113 return FALSE; /* finish */
1117 if (fread(data + 1, I_NAME_LENGTH - 1, 1, fp) == 0) {
1118 error("Invalid header (LHarc file ?)");
1119 return FALSE; /* finish */
1122 switch (data[I_HEADER_LEVEL]) {
1124 if (get_header_level0(fp, hdr, data) == FALSE)
1128 if (get_header_level1(fp, hdr, data) == FALSE)
1132 if (get_header_level2(fp, hdr, data) == FALSE)
1136 if (get_header_level3(fp, hdr, data) == FALSE)
1140 error("Unknown level header (level %d)", data[I_HEADER_LEVEL]);
1144 /* filename conversion */
1145 switch (hdr->extend_type) {
1147 filename_case = noconvertcase ? NONE : TO_LOWER;
1154 filename_case = NONE;
1158 archive_delim = "\377/:\\";
1159 /* `\' is for level 0 header and broken archive. */
1160 system_delim = "/://";
1161 filename_case = NONE;
1165 filename_case = noconvertcase ? NONE : TO_LOWER;
1169 if (optional_archive_kanji_code)
1170 archive_kanji_code = optional_archive_kanji_code;
1171 if (optional_system_kanji_code)
1172 system_kanji_code = optional_system_kanji_code;
1173 if (optional_archive_delim)
1174 archive_delim = optional_archive_delim;
1175 if (optional_system_delim)
1176 system_delim = optional_system_delim;
1177 if (optional_filename_case)
1178 filename_case = optional_filename_case;
1180 /* kanji code and delimiter conversion */
1181 convert_filename(hdr->name, strlen(hdr->name), sizeof(hdr->name),
1184 archive_delim, system_delim, filename_case);
1189 /* ------------------------------------------------------------------------ */
1191 init_header(name, v_stat, hdr)
1193 struct stat *v_stat;
1198 memset(hdr, 0, sizeof(LzHeader));
1200 /* the `method' member is rewrote by the encoding function.
1201 but need set for empty files */
1202 memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STORAGE);
1204 hdr->packed_size = 0;
1205 hdr->original_size = v_stat->st_size;
1206 hdr->attribute = GENERIC_ATTRIBUTE;
1207 hdr->header_level = header_level;
1208 strcpy(hdr->name, name);
1211 hdr->extend_type = EXTEND_UNIX;
1212 hdr->unix_last_modified_stamp = v_stat->st_mtime;
1213 /* since 00:00:00 JAN.1.1970 */
1214 #ifdef NOT_COMPATIBLE_MODE
1215 /* Please need your modification in this space. */
1217 hdr->unix_mode = v_stat->st_mode;
1220 hdr->unix_uid = v_stat->st_uid;
1221 hdr->unix_gid = v_stat->st_gid;
1223 #if INCLUDE_OWNER_NAME_IN_HEADER
1226 struct passwd *ent = getpwuid(hdr->unix_uid);
1229 strncpy(hdr->user, ent->pw_name, sizeof(hdr->user));
1230 if (hdr->user[sizeof(hdr->user)-1])
1231 hdr->user[sizeof(hdr->user)-1] = 0;
1237 struct group *ent = getgrgid(hdr->unix_gid);
1240 strncpy(hdr->group, ent->gr_name, sizeof(hdr->group));
1241 if (hdr->group[sizeof(hdr->group)-1])
1242 hdr->group[sizeof(hdr->group)-1] = 0;
1246 #endif /* INCLUDE_OWNER_NAME_IN_HEADER */
1247 if (is_directory(v_stat)) {
1248 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1249 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1250 hdr->original_size = 0;
1251 if (len > 0 && hdr->name[len - 1] != '/')
1252 strcpy(&hdr->name[len++], "/");
1256 if (is_symlink(v_stat)) {
1257 char lkname[FILENAME_LENGTH];
1259 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1260 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1261 hdr->original_size = 0;
1262 len = readlink(name, lkname, sizeof(lkname));
1263 if (xsnprintf(hdr->name, sizeof(hdr->name),
1264 "%s|%.*s", hdr->name, len, lkname) == -1)
1265 error("file name is too long (%s -> %.*s)", hdr->name, len, lkname);
1271 write_unix_info(hdr)
1274 /* UNIX specific informations */
1276 put_word(5); /* size */
1277 put_byte(0x50); /* permission */
1278 put_word(hdr->unix_mode);
1280 put_word(7); /* size */
1281 put_byte(0x51); /* gid and uid */
1282 put_word(hdr->unix_gid);
1283 put_word(hdr->unix_uid);
1285 if (hdr->group[0]) {
1286 int len = strlen(hdr->group);
1287 put_word(len + 3); /* size */
1288 put_byte(0x52); /* group name */
1289 put_bytes(hdr->group, len);
1293 int len = strlen(hdr->user);
1294 put_word(len + 3); /* size */
1295 put_byte(0x53); /* user name */
1296 put_bytes(hdr->user, len);
1299 if (hdr->header_level == 1) {
1300 put_word(7); /* size */
1301 put_byte(0x54); /* time stamp */
1302 put_longword(hdr->unix_last_modified_stamp);
1306 /* ------------------------------------------------------------------------ */
1307 /* Write unix extended header or generic header. */
1310 write_header_level0(data, hdr, pathname)
1312 char *data, *pathname;
1319 memset(data, 0, LZHEADER_STORAGE);
1321 put_byte(0x00); /* header size */
1322 put_byte(0x00); /* check sum */
1323 put_bytes(hdr->method, 5);
1324 put_longword(hdr->packed_size);
1325 put_longword(hdr->original_size);
1326 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1327 put_byte(hdr->attribute);
1328 put_byte(hdr->header_level); /* level 0 */
1330 /* write pathname (level 0 header contains the directory part) */
1331 name_length = strlen(pathname);
1333 limit = 255 - I_GENERIC_HEADER_BOTTOM + 2;
1335 limit = 255 - I_UNIX_EXTEND_BOTTOM + 2;
1337 if (name_length > limit) {
1338 warning("the length of pathname \"%s\" is too long.", pathname);
1339 name_length = limit;
1341 put_byte(name_length);
1342 put_bytes(pathname, name_length);
1345 if (generic_format) {
1346 header_size = I_GENERIC_HEADER_BOTTOM + name_length - 2;
1347 data[I_HEADER_SIZE] = header_size;
1348 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1350 /* write old-style extend header */
1351 put_byte(EXTEND_UNIX);
1352 put_byte(CURRENT_UNIX_MINOR_VERSION);
1353 put_longword(hdr->unix_last_modified_stamp);
1354 put_word(hdr->unix_mode);
1355 put_word(hdr->unix_uid);
1356 put_word(hdr->unix_gid);
1358 /* size of extended header is 12 */
1359 header_size = I_UNIX_EXTEND_BOTTOM + name_length - 2;
1360 data[I_HEADER_SIZE] = header_size;
1361 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1364 return header_size + 2;
1368 write_header_level1(data, hdr, pathname)
1370 char *data, *pathname;
1372 int name_length, dir_length, limit;
1373 char *basename, *dirname;
1375 char *extend_header_top;
1376 int extend_header_size;
1378 basename = strrchr(pathname, LHA_PATHSEP);
1381 name_length = strlen(basename);
1383 dir_length = basename - dirname;
1386 basename = pathname;
1387 name_length = strlen(basename);
1393 memset(data, 0, LZHEADER_STORAGE);
1395 put_byte(0x00); /* header size */
1396 put_byte(0x00); /* check sum */
1397 put_bytes(hdr->method, 5);
1398 put_longword(hdr->packed_size);
1399 put_longword(hdr->original_size);
1400 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1402 put_byte(hdr->header_level); /* level 1 */
1404 /* level 1 header: write filename (basename only) */
1405 limit = 255 - 27 + 2;
1406 if (name_length > limit) {
1407 put_byte(0); /* name length */
1410 put_byte(name_length);
1411 put_bytes(basename, name_length);
1419 put_byte(EXTEND_UNIX);
1421 /* write extend header from here. */
1423 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1424 header_size = extend_header_top - data - 2;
1426 /* write filename and dirname */
1428 if (name_length > limit) {
1429 put_word(name_length + 3); /* size */
1430 put_byte(0x01); /* filename */
1431 put_bytes(basename, name_length);
1434 if (dir_length > 0) {
1435 put_word(dir_length + 3); /* size */
1436 put_byte(0x02); /* dirname */
1437 put_bytes(dirname, dir_length);
1440 if (!generic_format)
1441 write_unix_info(hdr);
1443 put_word(0x0000); /* next header size */
1445 extend_header_size = put_ptr - extend_header_top;
1446 /* On level 1 header, the packed size field is contains the ext-header */
1447 hdr->packed_size += put_ptr - extend_header_top;
1449 /* put `skip size' */
1450 setup_put(data + I_PACKED_SIZE);
1451 put_longword(hdr->packed_size);
1453 data[I_HEADER_SIZE] = header_size;
1454 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1456 return header_size + extend_header_size + 2;
1460 write_header_level2(data, hdr, pathname)
1462 char *data, *pathname;
1464 int name_length, dir_length;
1465 char *basename, *dirname;
1467 char *extend_header_top;
1468 char *headercrc_ptr;
1471 basename = strrchr(pathname, LHA_PATHSEP);
1474 name_length = strlen(basename);
1476 dir_length = basename - dirname;
1479 basename = pathname;
1480 name_length = strlen(basename);
1486 memset(data, 0, LZHEADER_STORAGE);
1488 put_word(0x0000); /* header size */
1489 put_bytes(hdr->method, 5);
1490 put_longword(hdr->packed_size);
1491 put_longword(hdr->original_size);
1492 put_longword(hdr->unix_last_modified_stamp);
1494 put_byte(hdr->header_level); /* level 2 */
1501 put_byte(EXTEND_UNIX);
1503 /* write extend header from here. */
1505 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1507 /* write common header */
1510 headercrc_ptr = put_ptr;
1511 put_word(0x0000); /* header CRC */
1513 /* write filename and dirname */
1514 /* must have this header, even if the name_length is 0. */
1515 put_word(name_length + 3); /* size */
1516 put_byte(0x01); /* filename */
1517 put_bytes(basename, name_length);
1519 if (dir_length > 0) {
1520 put_word(dir_length + 3); /* size */
1521 put_byte(0x02); /* dirname */
1522 put_bytes(dirname, dir_length);
1525 if (!generic_format)
1526 write_unix_info(hdr);
1528 put_word(0x0000); /* next header size */
1530 header_size = put_ptr - data;
1531 if ((header_size & 0xff) == 0) {
1532 /* cannot put zero at the first byte on level 2 header. */
1533 /* adjust header size. */
1534 put_byte(0); /* padding */
1538 /* put hader size */
1539 setup_put(data + I_HEADER_SIZE);
1540 put_word(header_size);
1542 /* put hader CRC in extended header */
1543 INITIALIZE_CRC(hcrc);
1544 hcrc = calccrc(hcrc, data, (unsigned int) header_size);
1545 setup_put(headercrc_ptr);
1552 write_header(fp, hdr)
1557 char data[LZHEADER_STORAGE];
1559 int archive_kanji_code = CODE_SJIS;
1560 int system_kanji_code = default_system_kanji_code;
1561 char *archive_delim = "\377";
1562 char *system_delim = "/";
1563 int filename_case = NONE;
1564 char pathname[FILENAME_LENGTH];
1566 if (optional_archive_kanji_code)
1567 archive_kanji_code = optional_archive_kanji_code;
1568 if (optional_system_kanji_code)
1569 system_kanji_code = optional_system_kanji_code;
1572 filename_case = TO_UPPER;
1574 if (hdr->header_level == HEADER_LEVEL0) {
1575 archive_delim = "\\";
1578 strncpy(pathname, hdr->name, sizeof(pathname));
1579 pathname[sizeof(pathname)-1] = 0;
1580 convert_filename(pathname, strlen(pathname), sizeof(pathname),
1583 system_delim, archive_delim, filename_case);
1585 switch (hdr->header_level) {
1587 header_size = write_header_level0(data, hdr, pathname);
1590 header_size = write_header_level1(data, hdr, pathname);
1593 header_size = write_header_level2(data, hdr, pathname);
1596 error("Unknown level header (level %d)", hdr->header_level);
1600 if (fwrite(data, header_size, 1, fp) == 0)
1601 fatal_error("Cannot write to temporary file");
1604 #if MULTIBYTE_FILENAME
1606 #if defined(__APPLE__)
1608 #include <CoreFoundation/CFString.h>
1609 #include <CoreFoundation/CFStringEncodingExt.h>
1611 /* this is not need for Mac OS X v 10.2 later */
1613 kCFStringEncodingAllowLossyConversion = 1,
1614 kCFStringEncodingBasicDirectionLeftToRight = (1 << 1),
1615 kCFStringEncodingBasicDirectionRightToLeft = (1 << 2),
1616 kCFStringEncodingSubstituteCombinings = (1 << 3),
1617 kCFStringEncodingComposeCombinings = (1 << 4),
1618 kCFStringEncodingIgnoreCombinings = (1 << 5),
1619 kCFStringEncodingUseCanonical = (1 << 6),
1620 kCFStringEncodingUseHFSPlusCanonical = (1 << 7),
1621 kCFStringEncodingPrependBOM = (1 << 8),
1622 kCFStringEncodingDisableCorporateArea = (1 << 9),
1623 kCFStringEncodingASCIICompatibleConversion = (1 << 10),
1627 ConvertEncodingToUTF8(const char* inCStr,
1628 char* outUTF8Buffer,
1629 int outUTF8BufferLength,
1630 unsigned long scriptEncoding,
1631 unsigned long flags)
1633 unsigned long unicodeChars;
1634 unsigned long srcCharsUsed;
1635 unsigned long usedByteLen = 0;
1636 UniChar uniStr[512];
1637 unsigned long cfResult;
1639 cfResult = CFStringEncodingBytesToUnicode(scriptEncoding,
1647 if (cfResult == 0) {
1648 cfResult = CFStringEncodingUnicodeToBytes(kCFStringEncodingUTF8,
1653 (char*)outUTF8Buffer,
1654 outUTF8BufferLength - 1,
1656 outUTF8Buffer[usedByteLen] = '\0';
1663 ConvertUTF8ToEncoding(const char* inUTF8Buf,
1664 int inUTF8BufLength,
1665 char* outCStrBuffer,
1666 int outCStrBufferLength,
1667 unsigned long scriptEncoding,
1668 unsigned long flags)
1670 unsigned long unicodeChars;
1671 unsigned long srcCharsUsed;
1672 unsigned long usedByteLen = 0;
1673 UniChar uniStr[256];
1674 unsigned long cfResult;
1676 cfResult = CFStringEncodingBytesToUnicode(kCFStringEncodingUTF8,
1684 if (cfResult == 0) {
1685 cfResult = CFStringEncodingUnicodeToBytes(scriptEncoding,
1690 (char*)outCStrBuffer,
1691 outCStrBufferLength - 1,
1693 outCStrBuffer[usedByteLen] = '\0';
1703 ConvertEncodingByIconv(const char *src, char *dst, int dstsize,
1704 const char *srcEnc, const char *dstEnc)
1707 static char szTmpBuf[2048];
1713 dst_p = &szTmpBuf[0];
1714 iLen = (size_t)sizeof(szTmpBuf)-1;
1715 src_p = (char *)src;
1716 sLen = (size_t)strlen(src);
1717 memset(szTmpBuf, 0, sizeof(szTmpBuf));
1718 memset(dst, 0, dstsize);
1720 ic = iconv_open(dstEnc, srcEnc);
1721 if (ic == (iconv_t)-1) {
1722 error("iconv_open() failure: %s", strerror(errno));
1726 if (iconv(ic, &src_p, &sLen, &dst_p, &iLen) == (size_t)-1) {
1727 error("iconv() failure: %s", strerror(errno));
1732 strncpy(dst, szTmpBuf, dstsize);
1738 #endif /* defined(__APPLE__) */
1741 sjis_to_utf8(char *dst, const char *src, size_t dstsize)
1743 #if defined(__APPLE__)
1745 if (ConvertEncodingToUTF8(src, dst, dstsize,
1746 kCFStringEncodingDOSJapanese,
1747 kCFStringEncodingUseHFSPlusCanonical) == 0)
1750 if (ConvertEncodingByIconv(src, dst, dstsize, "SJIS", "UTF-8") != -1)
1753 error("not support utf-8 conversion");
1756 if (dstsize < 1) return dst;
1758 return strncpy(dst, src, dstsize-1);
1762 utf8_to_sjis(char *dst, const char *src, size_t dstsize)
1764 #if defined(__APPLE__)
1768 srclen = strlen(src);
1769 if (ConvertUTF8ToEncoding(src, srclen, dst, dstsize,
1770 kCFStringEncodingDOSJapanese,
1771 kCFStringEncodingUseHFSPlusCanonical) == 0)
1774 if (ConvertEncodingByIconv(src, dst, dstsize, "UTF-8", "SJIS") != -1)
1777 error("not support utf-8 conversion");
1780 if (dstsize < 1) return dst;
1782 return strncpy(dst, src, dstsize-1);
1786 * SJIS <-> EUC ÊÑ´¹´Ø¿ô
1787 * ¡ÖÆüËܸì¾ðÊó½èÍý¡× ¥½¥Õ¥È¥Ð¥ó¥¯(³ô)
1788 * ¤è¤êÈ´¿è(by Koji Arai)
1791 euc2sjis(int *p1, int *p2)
1793 unsigned char c1 = *p1 & 0x7f;
1794 unsigned char c2 = *p2 & 0x7f;
1795 int rowoff = c1 < 0x5f ? 0x70 : 0xb0;
1796 int celoff = c1 % 2 ? (c2 > 0x5f ? 0x20 : 0x1f) : 0x7e;
1797 *p1 = ((c1 + 1) >> 1) + rowoff;
1798 *p2 += celoff - 0x80;
1802 sjis2euc(int *p1, int *p2)
1804 unsigned char c1 = *p1;
1805 unsigned char c2 = *p2;
1806 int adjust = c2 < 0x9f;
1807 int rowoff = c1 < 0xa0 ? 0x70 : 0xb0;
1808 int celoff = adjust ? (c2 > 0x7f ? 0x20 : 0x1f) : 0x7e;
1809 *p1 = ((c1 - rowoff) << 1) - adjust;
1815 #endif /* MULTIBYTE_FILENAME */
1817 /* Local Variables: */
1820 /* compile-command:"gcc -c header.c" */
1822 /* vi: set tabstop=4: */