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]) && 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) +
497 /* ------------------------------------------------------------------------ */
498 /* build header functions */
499 /* ------------------------------------------------------------------------ */
505 * --------------------------------
507 * 2 or 4 next-header size [*1]
508 * --------------------------------------
509 * ext header: 1 ext-type ^
510 * ? contents | [*1] next-header size
511 * 2 or 4 next-header size v
512 * --------------------------------------
514 * on level 1, 2 header:
515 * size field is 2 bytes
517 * size field is 4 bytes
520 get_extended_header(fp, hdr, header_size, hcrc)
526 char data[LZHEADER_STORAGE];
528 char dirname[FILENAME_LENGTH];
531 long whole_size = header_size;
533 int n = 1 + hdr->size_field_length; /* `ext-type' + `next-header size' */
535 if (hdr->header_level == 0)
538 name_length = strlen(hdr->name);
540 while (header_size) {
542 if (sizeof(data) < header_size) {
543 error("header size (%ld) too large.", header_size);
547 if (fread(data, header_size, 1, fp) == 0) {
548 error("Invalid header (LHa file ?)");
552 ext_type = get_byte();
555 /* header crc (CRC-16) */
556 hdr->header_crc = get_word();
557 /* clear buffer for CRC calculation. */
558 data[1] = data[2] = 0;
559 skip_bytes(header_size - n - 2);
564 get_bytes(hdr->name, header_size-n, sizeof(hdr->name)-1);
565 hdr->name[name_length] = 0;
569 dir_length = get_bytes(dirname, header_size-n, sizeof(dirname)-1);
570 dirname[dir_length] = 0;
573 /* MS-DOS attribute */
574 hdr->attribute = get_word();
577 /* Windows time stamp (FILETIME structure) */
578 skip_bytes(header_size - n); /* ignored */
581 /* UNIX permission */
582 hdr->unix_mode = get_word();
585 /* UNIX gid and uid */
586 hdr->unix_gid = get_word();
587 hdr->unix_uid = get_word();
590 /* UNIX group name */
591 i = get_bytes(hdr->group, header_size-n, sizeof(hdr->group)-1);
592 hdr->group[i] = '\0';
596 i = get_bytes(hdr->user, header_size-n, sizeof(hdr->user)-1);
600 /* UNIX last modified time */
601 hdr->unix_last_modified_stamp = (time_t) get_longword();
605 /* 0x39: multi-disk header
606 0x3f: uncompressed comment
607 0x42: 64bit large file size
608 0x48-0x4f(?): reserved for authenticity verification
610 0x7e: extended attribute - platform information
611 0x7f: extended attribute - permission, owner-id and timestamp
613 0xc4: compressed comment (dict size: 4096)
614 0xc5: compressed comment (dict size: 8192)
615 0xc6: compressed comment (dict size: 16384)
616 0xc7: compressed comment (dict size: 32768)
617 0xc8: compressed comment (dict size: 65536)
618 0xd0-0xdf(?): operating systemm specific information
619 0xfc: encapsulation (another opinion)
620 0xfe: extended attribute - platform information(another opinion)
621 0xff: extended attribute - permission, owner-id and timestamp
622 (level 3 on UNLHA32) */
624 warning("unknown extended header 0x%02x", ext_type);
625 skip_bytes(header_size - n);
630 *hcrc = calccrc(*hcrc, data, header_size);
632 if (hdr->size_field_length == 2)
633 whole_size += header_size = get_word();
635 whole_size += header_size = get_longword();
638 /* concatenate dirname and filename */
640 if (name_length + dir_length >= sizeof(hdr->name)) {
641 warning("the length of pathname \"%s%s\" is too long.",
643 name_length = sizeof(hdr->name) - dir_length - 1;
644 hdr->name[name_length] = 0;
646 strcat(dirname, hdr->name);
647 strcpy(hdr->name, dirname);
648 name_length += dir_length;
658 * offset size field name
659 * ----------------------------------
660 * 0 1 header size [*1]
662 * ---------------------------------------
664 * 7 4 packed size [*2] |
665 * 11 4 original size |
668 * 19 1 attribute | [*1] header size (X+Y+22)
669 * 20 1 level (0x00 fixed) |
672 * X +22 2 file crc (CRC-16) |
673 * X +24 Y ext-header(old style) v
674 * -------------------------------------------------
676 * : | [*2] packed size
678 * -------------------------------------------------
680 * ext-header(old style)
694 * bit6 archive bit (need to backup)
698 get_header_level0(fp, hdr, data)
709 hdr->size_field_length = 2; /* in bytes */
710 hdr->header_size = header_size = get_byte();
711 checksum = get_byte();
713 if (fread(data+I_NAME_LENGTH, header_size+2-I_NAME_LENGTH, 1, fp) == 0) {
714 error("Invalid header (LHarc file ?)");
715 return FALSE; /* finish */
718 if (calc_sum(data + I_METHOD, header_size) != checksum) {
719 error("Checksum error (LHarc file?)");
723 get_bytes(hdr->method, 5, sizeof(hdr->method));
724 hdr->packed_size = get_longword();
725 hdr->original_size = get_longword();
726 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
727 hdr->attribute = get_byte(); /* MS-DOS attribute */
728 hdr->header_level = get_byte();
729 name_length = get_byte();
730 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
733 /* defaults for other type */
734 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
738 extend_size = header_size+2 - name_length - 24;
740 if (extend_size < 0) {
741 if (extend_size == -2) {
742 /* CRC field is not given */
743 hdr->extend_type = EXTEND_GENERIC;
744 hdr->has_crc = FALSE;
749 error("Unkonwn header (lha file?)");
754 hdr->crc = get_word();
756 if (extend_size == 0)
759 hdr->extend_type = get_byte();
762 if (hdr->extend_type == EXTEND_UNIX) {
763 if (extend_size >= 11) {
764 hdr->minor_version = get_byte();
765 hdr->unix_last_modified_stamp = (time_t) get_longword();
766 hdr->unix_mode = get_word();
767 hdr->unix_uid = get_word();
768 hdr->unix_gid = get_word();
771 hdr->extend_type = EXTEND_GENERIC;
775 skip_bytes(extend_size);
777 hdr->header_size += 2;
785 * offset size field name
786 * -----------------------------------
787 * 0 1 header size [*1]
789 * -------------------------------------
791 * 7 4 skip size [*2] |
792 * 11 4 original size |
795 * 19 1 attribute (0x20 fixed) | [*1] header size (X+Y+25)
796 * 20 1 level (0x01 fixed) |
799 * X+ 22 2 file crc (CRC-16) |
802 * X+Y+25 2 next-header size v
803 * -------------------------------------------------
804 * X+Y+27 Z ext-header ^
806 * ----------------------------------- | [*2] skip size
809 * -------------------------------------------------
813 get_header_level1(fp, hdr, data)
818 int header_size, extend_size;
823 hdr->size_field_length = 2; /* in bytes */
824 hdr->header_size = header_size = get_byte();
825 checksum = get_byte();
827 if (fread(data+I_NAME_LENGTH, header_size+2-I_NAME_LENGTH, 1, fp) == 0) {
828 error("Invalid header (LHarc file ?)");
829 return FALSE; /* finish */
832 if (calc_sum(data + I_METHOD, header_size) != checksum) {
833 error("Checksum error (LHarc file?)");
837 get_bytes(hdr->method, 5, sizeof(hdr->method));
838 hdr->packed_size = get_longword(); /* skip size */
839 hdr->original_size = get_longword();
840 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
841 hdr->attribute = get_byte(); /* 0x20 fixed */
842 hdr->header_level = get_byte();
844 name_length = get_byte();
845 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
848 /* defaults for other type */
849 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
854 hdr->crc = get_word();
855 hdr->extend_type = get_byte();
857 dummy = header_size+2 - name_length - 27;
859 skip_bytes(dummy); /* skip old style extend header */
861 extend_size = get_word();
862 extend_size = get_extended_header(fp, hdr, extend_size, 0);
863 if (extend_size == -1)
866 /* On level 1 header, size fields should be adjusted. */
867 /* the `packed_size' field contains the extended header size. */
868 /* the `header_size' field does not. */
869 hdr->packed_size -= extend_size;
870 hdr->header_size += extend_size + 2;
879 * offset size field name
880 * --------------------------------------------------
881 * 0 2 total header size [*1] ^
882 * ----------------------- |
884 * 7 4 packed size [*2] |
885 * 11 4 original size |
887 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
888 * 20 1 level (0x02 fixed) | (X+26+(1))
889 * 21 2 file crc (CRC-16) |
891 * 24 2 next-header size |
892 * ----------------------------------- |
895 * ----------------------------------- |
896 * X +26 (1) padding v
897 * -------------------------------------------------
899 * : | [*2] packed size
901 * -------------------------------------------------
905 get_header_level2(fp, hdr, data)
910 int header_size, extend_size;
914 hdr->size_field_length = 2; /* in bytes */
915 hdr->header_size = header_size = get_word();
917 if (fread(data + I_NAME_LENGTH, 26 - I_NAME_LENGTH, 1, fp) == 0) {
918 error("Invalid header (LHarc file ?)");
919 return FALSE; /* finish */
922 get_bytes(hdr->method, 5, sizeof(hdr->method));
923 hdr->packed_size = get_longword();
924 hdr->original_size = get_longword();
925 hdr->unix_last_modified_stamp = get_longword();
926 hdr->attribute = get_byte(); /* reserved */
927 hdr->header_level = get_byte();
929 /* defaults for other type */
930 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
935 hdr->crc = get_word();
936 hdr->extend_type = get_byte();
937 extend_size = get_word();
939 INITIALIZE_CRC(hcrc);
940 hcrc = calccrc(hcrc, data, get_ptr - data);
942 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
943 if (extend_size == -1)
946 padding = header_size - 26 - extend_size;
947 while (padding--) /* padding should be 0 or 1 */
948 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
950 if (hdr->header_crc != hcrc)
951 error("header CRC error");
960 * offset size field name
961 * --------------------------------------------------
962 * 0 2 size field length (4 fixed) ^
964 * 7 4 packed size [*2] |
965 * 11 4 original size |
967 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
968 * 20 1 level (0x03 fixed) | (X+32)
969 * 21 2 file crc (CRC-16) |
971 * 24 4 total header size [*1] |
972 * 28 4 next-header size |
973 * ----------------------------------- |
976 * -------------------------------------------------
978 * : | [*2] packed size
980 * -------------------------------------------------
984 get_header_level3(fp, hdr, data)
989 long header_size, extend_size;
993 hdr->size_field_length = get_word();
995 if (fread(data + I_NAME_LENGTH, 32 - I_NAME_LENGTH, 1, fp) == 0) {
996 error("Invalid header (LHarc file ?)");
997 return FALSE; /* finish */
1000 get_bytes(hdr->method, 5, sizeof(hdr->method));
1001 hdr->packed_size = get_longword();
1002 hdr->original_size = get_longword();
1003 hdr->unix_last_modified_stamp = get_longword();
1004 hdr->attribute = get_byte(); /* reserved */
1005 hdr->header_level = get_byte();
1007 /* defaults for other type */
1008 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1012 hdr->has_crc = TRUE;
1013 hdr->crc = get_word();
1014 hdr->extend_type = get_byte();
1015 hdr->header_size = header_size = get_longword();
1016 extend_size = get_longword();
1018 INITIALIZE_CRC(hcrc);
1019 hcrc = calccrc(hcrc, data, get_ptr - data);
1021 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1022 if (extend_size == -1)
1025 padding = header_size - 32 - extend_size;
1026 while (padding--) /* padding should be 0 */
1027 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1029 if (hdr->header_crc != hcrc)
1030 error("header CRC error");
1040 char data[LZHEADER_STORAGE];
1042 int archive_kanji_code = CODE_SJIS;
1043 int system_kanji_code = default_system_kanji_code;
1044 char *archive_delim = "\377\\"; /* `\' is for level 0 header and
1046 char *system_delim = "//";
1047 int filename_case = NONE;
1050 memset(hdr, 0, sizeof(LzHeader));
1054 if ((end_mark = getc(fp)) == EOF || end_mark == 0) {
1055 return FALSE; /* finish */
1059 if (fread(data + 1, I_NAME_LENGTH - 1, 1, fp) == 0) {
1060 error("Invalid header (LHarc file ?)");
1061 return FALSE; /* finish */
1064 switch (data[I_HEADER_LEVEL]) {
1066 if (get_header_level0(fp, hdr, data) == FALSE)
1070 if (get_header_level1(fp, hdr, data) == FALSE)
1074 if (get_header_level2(fp, hdr, data) == FALSE)
1078 if (get_header_level3(fp, hdr, data) == FALSE)
1082 error("Unknown level header (level %d)", data[I_HEADER_LEVEL]);
1086 /* filename conversion */
1087 switch (hdr->extend_type) {
1089 filename_case = noconvertcase ? NONE : TO_LOWER;
1096 filename_case = NONE;
1100 archive_delim = "\377/:\\";
1101 /* `\' is for level 0 header and broken archive. */
1102 system_delim = "/://";
1103 filename_case = NONE;
1107 filename_case = noconvertcase ? NONE : TO_LOWER;
1111 if (optional_archive_kanji_code)
1112 archive_kanji_code = optional_archive_kanji_code;
1113 if (optional_system_kanji_code)
1114 system_kanji_code = optional_system_kanji_code;
1115 if (optional_archive_delim)
1116 archive_delim = optional_archive_delim;
1117 if (optional_system_delim)
1118 system_delim = optional_system_delim;
1119 if (optional_filename_case)
1120 filename_case = optional_filename_case;
1122 /* kanji code and delimiter conversion */
1123 convert_filename(hdr->name, strlen(hdr->name), sizeof(hdr->name),
1126 archive_delim, system_delim, filename_case);
1131 /* ------------------------------------------------------------------------ */
1133 init_header(name, v_stat, hdr)
1135 struct stat *v_stat;
1140 memset(hdr, 0, sizeof(LzHeader));
1142 /* the `method' member is rewrote by the encoding function.
1143 but need set for empty files */
1144 memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STORAGE);
1146 hdr->packed_size = 0;
1147 hdr->original_size = v_stat->st_size;
1148 hdr->attribute = GENERIC_ATTRIBUTE;
1149 hdr->header_level = header_level;
1150 strcpy(hdr->name, name);
1153 hdr->extend_type = EXTEND_UNIX;
1154 hdr->unix_last_modified_stamp = v_stat->st_mtime;
1155 /* since 00:00:00 JAN.1.1970 */
1156 #ifdef NOT_COMPATIBLE_MODE
1157 /* Please need your modification in this space. */
1159 hdr->unix_mode = v_stat->st_mode;
1162 hdr->unix_uid = v_stat->st_uid;
1163 hdr->unix_gid = v_stat->st_gid;
1165 #if INCLUDE_OWNER_NAME_IN_HEADER
1168 struct passwd *ent = getpwuid(hdr->unix_uid);
1171 strncpy(hdr->user, ent->pw_name, sizeof(hdr->user));
1172 if (hdr->user[sizeof(hdr->user)-1])
1173 hdr->user[sizeof(hdr->user)-1] = 0;
1179 struct group *ent = getgrgid(hdr->unix_gid);
1182 strncpy(hdr->group, ent->gr_name, sizeof(hdr->group));
1183 if (hdr->group[sizeof(hdr->group)-1])
1184 hdr->group[sizeof(hdr->group)-1] = 0;
1188 #endif /* INCLUDE_OWNER_NAME_IN_HEADER */
1189 if (is_directory(v_stat)) {
1190 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1191 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1192 hdr->original_size = 0;
1193 if (len > 0 && hdr->name[len - 1] != '/')
1194 strcpy(&hdr->name[len++], "/");
1198 if (is_symlink(v_stat)) {
1199 char lkname[FILENAME_LENGTH];
1201 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1202 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1203 hdr->original_size = 0;
1204 len = readlink(name, lkname, sizeof(lkname));
1205 if (xsnprintf(hdr->name, sizeof(hdr->name),
1206 "%s|%.*s", hdr->name, len, lkname) == -1)
1207 error("file name is too long (%s -> %.*s)", hdr->name, len, lkname);
1213 write_unix_info(hdr)
1216 /* UNIX specific informations */
1218 put_word(5); /* size */
1219 put_byte(0x50); /* permission */
1220 put_word(hdr->unix_mode);
1222 put_word(7); /* size */
1223 put_byte(0x51); /* gid and uid */
1224 put_word(hdr->unix_gid);
1225 put_word(hdr->unix_uid);
1227 if (hdr->group[0]) {
1228 int len = strlen(hdr->group);
1229 put_word(len + 3); /* size */
1230 put_byte(0x52); /* group name */
1231 put_bytes(hdr->group, len);
1235 int len = strlen(hdr->user);
1236 put_word(len + 3); /* size */
1237 put_byte(0x53); /* user name */
1238 put_bytes(hdr->user, len);
1241 if (hdr->header_level == 1) {
1242 put_word(7); /* size */
1243 put_byte(0x54); /* time stamp */
1244 put_longword(hdr->unix_last_modified_stamp);
1248 /* ------------------------------------------------------------------------ */
1249 /* Write unix extended header or generic header. */
1252 write_header_level0(data, hdr, pathname)
1254 char *data, *pathname;
1261 memset(data, 0, LZHEADER_STORAGE);
1263 put_byte(0x00); /* header size */
1264 put_byte(0x00); /* check sum */
1265 put_bytes(hdr->method, 5);
1266 put_longword(hdr->packed_size);
1267 put_longword(hdr->original_size);
1268 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1269 put_byte(hdr->attribute);
1270 put_byte(hdr->header_level); /* level 0 */
1272 /* write pathname (level 0 header contains the directory part) */
1273 name_length = strlen(pathname);
1275 limit = 255 - I_GENERIC_HEADER_BOTTOM + 2;
1277 limit = 255 - I_UNIX_EXTEND_BOTTOM + 2;
1279 if (name_length > limit) {
1280 warning("the length of pathname \"%s\" is too long.", pathname);
1281 name_length = limit;
1283 put_byte(name_length);
1284 put_bytes(pathname, name_length);
1287 if (generic_format) {
1288 header_size = I_GENERIC_HEADER_BOTTOM + name_length - 2;
1289 data[I_HEADER_SIZE] = header_size;
1290 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1292 /* write old-style extend header */
1293 put_byte(EXTEND_UNIX);
1294 put_byte(CURRENT_UNIX_MINOR_VERSION);
1295 put_longword(hdr->unix_last_modified_stamp);
1296 put_word(hdr->unix_mode);
1297 put_word(hdr->unix_uid);
1298 put_word(hdr->unix_gid);
1300 /* size of extended header is 12 */
1301 header_size = I_UNIX_EXTEND_BOTTOM + name_length - 2;
1302 data[I_HEADER_SIZE] = header_size;
1303 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1306 return header_size + 2;
1310 write_header_level1(data, hdr, pathname)
1312 char *data, *pathname;
1314 int name_length, dir_length, limit;
1315 char *basename, *dirname;
1317 char *extend_header_top;
1318 int extend_header_size;
1320 basename = strrchr(pathname, LHA_PATHSEP);
1323 name_length = strlen(basename);
1325 dir_length = basename - dirname;
1328 basename = pathname;
1329 name_length = strlen(basename);
1335 memset(data, 0, LZHEADER_STORAGE);
1337 put_byte(0x00); /* header size */
1338 put_byte(0x00); /* check sum */
1339 put_bytes(hdr->method, 5);
1340 put_longword(hdr->packed_size);
1341 put_longword(hdr->original_size);
1342 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1344 put_byte(hdr->header_level); /* level 1 */
1346 /* level 1 header: write filename (basename only) */
1347 limit = 255 - 27 + 2;
1348 if (name_length > limit) {
1349 put_byte(0); /* name length */
1352 put_byte(name_length);
1353 put_bytes(basename, name_length);
1361 put_byte(EXTEND_UNIX);
1363 /* write extend header from here. */
1365 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1366 header_size = extend_header_top - data - 2;
1368 /* write filename and dirname */
1370 if (name_length > limit) {
1371 put_word(name_length + 3); /* size */
1372 put_byte(0x01); /* filename */
1373 put_bytes(basename, name_length);
1376 if (dir_length > 0) {
1377 put_word(dir_length + 3); /* size */
1378 put_byte(0x02); /* dirname */
1379 put_bytes(dirname, dir_length);
1382 if (!generic_format)
1383 write_unix_info(hdr);
1385 put_word(0x0000); /* next header size */
1387 extend_header_size = put_ptr - extend_header_top;
1388 /* On level 1 header, the packed size field is contains the ext-header */
1389 hdr->packed_size += put_ptr - extend_header_top;
1391 /* put `skip size' */
1392 setup_put(data + I_PACKED_SIZE);
1393 put_longword(hdr->packed_size);
1395 data[I_HEADER_SIZE] = header_size;
1396 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1398 return header_size + extend_header_size + 2;
1402 write_header_level2(data, hdr, pathname)
1404 char *data, *pathname;
1406 int name_length, dir_length;
1407 char *basename, *dirname;
1409 char *extend_header_top;
1410 char *headercrc_ptr;
1413 basename = strrchr(pathname, LHA_PATHSEP);
1416 name_length = strlen(basename);
1418 dir_length = basename - dirname;
1421 basename = pathname;
1422 name_length = strlen(basename);
1428 memset(data, 0, LZHEADER_STORAGE);
1430 put_word(0x0000); /* header size */
1431 put_bytes(hdr->method, 5);
1432 put_longword(hdr->packed_size);
1433 put_longword(hdr->original_size);
1434 put_longword(hdr->unix_last_modified_stamp);
1436 put_byte(hdr->header_level); /* level 2 */
1443 put_byte(EXTEND_UNIX);
1445 /* write extend header from here. */
1447 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1449 /* write common header */
1452 headercrc_ptr = put_ptr;
1453 put_word(0x0000); /* header CRC */
1455 /* write filename and dirname */
1456 /* must have this header, even if the name_length is 0. */
1457 put_word(name_length + 3); /* size */
1458 put_byte(0x01); /* filename */
1459 put_bytes(basename, name_length);
1461 if (dir_length > 0) {
1462 put_word(dir_length + 3); /* size */
1463 put_byte(0x02); /* dirname */
1464 put_bytes(dirname, dir_length);
1467 if (!generic_format)
1468 write_unix_info(hdr);
1470 put_word(0x0000); /* next header size */
1472 header_size = put_ptr - data;
1473 if ((header_size & 0xff) == 0) {
1474 /* cannot put zero at the first byte on level 2 header. */
1475 /* adjust header size. */
1476 put_byte(0); /* padding */
1480 /* put hader size */
1481 setup_put(data + I_HEADER_SIZE);
1482 put_word(header_size);
1484 /* put hader CRC in extended header */
1485 INITIALIZE_CRC(hcrc);
1486 hcrc = calccrc(hcrc, data, (unsigned int) header_size);
1487 setup_put(headercrc_ptr);
1494 write_header(fp, hdr)
1499 char data[LZHEADER_STORAGE];
1501 int archive_kanji_code = CODE_SJIS;
1502 int system_kanji_code = default_system_kanji_code;
1503 char *archive_delim = "\377";
1504 char *system_delim = "/";
1505 int filename_case = NONE;
1506 char pathname[FILENAME_LENGTH];
1508 if (optional_archive_kanji_code)
1509 archive_kanji_code = optional_archive_kanji_code;
1510 if (optional_system_kanji_code)
1511 system_kanji_code = optional_system_kanji_code;
1514 filename_case = TO_UPPER;
1516 if (hdr->header_level == HEADER_LEVEL0) {
1517 archive_delim = "\\";
1520 strncpy(pathname, hdr->name, sizeof(pathname));
1521 pathname[sizeof(pathname)-1] = 0;
1522 convert_filename(pathname, strlen(pathname), sizeof(pathname),
1525 system_delim, archive_delim, filename_case);
1527 switch (hdr->header_level) {
1529 header_size = write_header_level0(data, hdr, pathname);
1532 header_size = write_header_level1(data, hdr, pathname);
1535 header_size = write_header_level2(data, hdr, pathname);
1538 error("Unknown level header (level %d)", hdr->header_level);
1542 if (fwrite(data, header_size, 1, fp) == 0)
1543 fatal_error("Cannot write to temporary file");
1546 #if MULTIBYTE_FILENAME
1548 #if defined(__APPLE__)
1550 #include <CoreFoundation/CFString.h>
1551 #include <CoreFoundation/CFStringEncodingExt.h>
1553 /* this is not need for Mac OS X v 10.2 later */
1555 kCFStringEncodingAllowLossyConversion = 1,
1556 kCFStringEncodingBasicDirectionLeftToRight = (1 << 1),
1557 kCFStringEncodingBasicDirectionRightToLeft = (1 << 2),
1558 kCFStringEncodingSubstituteCombinings = (1 << 3),
1559 kCFStringEncodingComposeCombinings = (1 << 4),
1560 kCFStringEncodingIgnoreCombinings = (1 << 5),
1561 kCFStringEncodingUseCanonical = (1 << 6),
1562 kCFStringEncodingUseHFSPlusCanonical = (1 << 7),
1563 kCFStringEncodingPrependBOM = (1 << 8),
1564 kCFStringEncodingDisableCorporateArea = (1 << 9),
1565 kCFStringEncodingASCIICompatibleConversion = (1 << 10),
1569 ConvertEncodingToUTF8(const char* inCStr,
1570 char* outUTF8Buffer,
1571 int outUTF8BufferLength,
1572 unsigned long scriptEncoding,
1573 unsigned long flags)
1575 unsigned long unicodeChars;
1576 unsigned long srcCharsUsed;
1577 unsigned long usedByteLen = 0;
1578 UniChar uniStr[512];
1579 unsigned long cfResult;
1581 cfResult = CFStringEncodingBytesToUnicode(scriptEncoding,
1589 if (cfResult == 0) {
1590 cfResult = CFStringEncodingUnicodeToBytes(kCFStringEncodingUTF8,
1595 (char*)outUTF8Buffer,
1596 outUTF8BufferLength - 1,
1598 outUTF8Buffer[usedByteLen] = '\0';
1605 ConvertUTF8ToEncoding(const char* inUTF8Buf,
1606 int inUTF8BufLength,
1607 char* outCStrBuffer,
1608 int outCStrBufferLength,
1609 unsigned long scriptEncoding,
1610 unsigned long flags)
1612 unsigned long unicodeChars;
1613 unsigned long srcCharsUsed;
1614 unsigned long usedByteLen = 0;
1615 UniChar uniStr[256];
1616 unsigned long cfResult;
1618 cfResult = CFStringEncodingBytesToUnicode(kCFStringEncodingUTF8,
1626 if (cfResult == 0) {
1627 cfResult = CFStringEncodingUnicodeToBytes(scriptEncoding,
1632 (char*)outCStrBuffer,
1633 outCStrBufferLength - 1,
1635 outCStrBuffer[usedByteLen] = '\0';
1645 ConvertEncodingByIconv(const char *src, char *dst, int dstsize,
1646 const char *srcEnc, const char *dstEnc)
1649 static char szTmpBuf[2048];
1655 dst_p = &szTmpBuf[0];
1656 iLen = (size_t)sizeof(szTmpBuf)-1;
1657 src_p = (char *)src;
1658 sLen = (size_t)strlen(src);
1659 memset(szTmpBuf, 0, sizeof(szTmpBuf));
1660 memset(dst, 0, dstsize);
1662 ic = iconv_open(dstEnc, srcEnc);
1663 if (ic == (iconv_t)-1) {
1664 error("iconv_open() failure: %s", strerror(errno));
1668 if (iconv(ic, &src_p, &sLen, &dst_p, &iLen) == (size_t)-1) {
1669 error("iconv() failure: %s", strerror(errno));
1674 strncpy(dst, szTmpBuf, dstsize);
1680 #endif /* defined(__APPLE__) */
1683 sjis_to_utf8(char *dst, const char *src, size_t dstsize)
1685 #if defined(__APPLE__)
1687 if (ConvertEncodingToUTF8(src, dst, dstsize,
1688 kCFStringEncodingDOSJapanese,
1689 kCFStringEncodingUseHFSPlusCanonical) == 0)
1692 if (ConvertEncodingByIconv(src, dst, dstsize, "SJIS", "UTF-8") != -1)
1695 error("not support utf-8 conversion");
1698 if (dstsize < 1) return dst;
1700 return strncpy(dst, src, dstsize-1);
1704 utf8_to_sjis(char *dst, const char *src, size_t dstsize)
1706 #if defined(__APPLE__)
1710 srclen = strlen(src);
1711 if (ConvertUTF8ToEncoding(src, srclen, dst, dstsize,
1712 kCFStringEncodingDOSJapanese,
1713 kCFStringEncodingUseHFSPlusCanonical) == 0)
1716 if (ConvertEncodingByIconv(src, dst, dstsize, "UTF-8", "SJIS") != -1)
1719 error("not support utf-8 conversion");
1722 if (dstsize < 1) return dst;
1724 return strncpy(dst, src, dstsize-1);
1728 * SJIS <-> EUC ÊÑ´¹´Ø¿ô
1729 * ¡ÖÆüËܸì¾ðÊó½èÍý¡× ¥½¥Õ¥È¥Ð¥ó¥¯(³ô)
1730 * ¤è¤êÈ´¿è(by Koji Arai)
1733 euc2sjis(int *p1, int *p2)
1735 unsigned char c1 = *p1 & 0x7f;
1736 unsigned char c2 = *p2 & 0x7f;
1737 int rowoff = c1 < 0x5f ? 0x70 : 0xb0;
1738 int celoff = c1 % 2 ? (c2 > 0x5f ? 0x20 : 0x1f) : 0x7e;
1739 *p1 = ((c1 + 1) >> 1) + rowoff;
1740 *p2 += celoff - 0x80;
1744 sjis2euc(int *p1, int *p2)
1746 unsigned char c1 = *p1;
1747 unsigned char c2 = *p2;
1748 int adjust = c2 < 0x9f;
1749 int rowoff = c1 < 0xa0 ? 0x70 : 0xb0;
1750 int celoff = adjust ? (c2 > 0x7f ? 0x20 : 0x1f) : 0x7e;
1751 *p1 = ((c1 - rowoff) << 1) - adjust;
1757 #endif /* MULTIBYTE_FILENAME */
1759 /* Local Variables: */
1762 /* compile-command:"gcc -c header.c" */
1764 /* vi: set tabstop=4: */