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 /* ------------------------------------------------------------------------ */
17 static int ConvertEncodingToUTF8(const char* inCStr, char* outUTF8Buffer, int outUTF8BufferLength, unsigned long scriptEncoding, unsigned long flags);
18 static int ConvertUTF8ToEncoding(const char* inUTF8Buf, int inUTF8BufLength, char* outCStrBuffer, int outCStrBufferLength, unsigned long scriptEncoding, unsigned long flags);
19 #endif /* __APPLE__ */
21 /* ------------------------------------------------------------------------ */
24 int optional_archive_kanji_code = NONE;
25 int optional_system_kanji_code = NONE;
26 char *optional_archive_delim = NULL;
27 char *optional_system_delim = NULL;
28 int optional_filename_case = NONE;
30 #ifdef MULTIBYTE_FILENAME
31 int default_system_kanji_code = MULTIBYTE_FILENAME;
33 int default_system_kanji_code = NONE;
36 /* ------------------------------------------------------------------------ */
44 for (sum = 0; len; len--)
50 /* ------------------------------------------------------------------------ */
58 return (b1 << 8) + b0;
61 /* ------------------------------------------------------------------------ */
70 /* ------------------------------------------------------------------------ */
80 return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
83 /* ------------------------------------------------------------------------ */
94 /* ------------------------------------------------------------------------ */
96 msdos_to_unix_filename(name, len)
102 #ifdef MULTIBYTE_FILENAME
103 for (i = 0; i < len; i++) {
104 if (MULTIBYTE_FIRST_P(name[i]) &&
105 MULTIBYTE_SECOND_P(name[i + 1]))
107 else if (name[i] == '\\')
109 else if (!noconvertcase && isupper(name[i]))
110 name[i] = tolower(name[i]);
113 for (i = 0; i < len; i++) {
116 else if (!noconvertcase && isupper(name[i]))
117 name[i] = tolower(name[i]);
122 /* ------------------------------------------------------------------------ */
124 generic_to_unix_filename(name, len)
129 boolean lower_case_used = FALSE;
131 #ifdef MULTIBYTE_FILENAME
132 for (i = 0; i < len; i++) {
133 if (MULTIBYTE_FIRST_P(name[i]) &&
134 MULTIBYTE_SECOND_P(name[i + 1]))
136 else if (islower(name[i])) {
137 lower_case_used = TRUE;
141 for (i = 0; i < len; i++) {
142 if (MULTIBYTE_FIRST_P(name[i]) &&
143 MULTIBYTE_SECOND_P(name[i + 1]))
145 else if (name[i] == '\\')
147 else if (!noconvertcase && !lower_case_used && isupper(name[i]))
148 name[i] = tolower(name[i]);
151 for (i = 0; i < len; i++)
152 if (islower(name[i])) {
153 lower_case_used = TRUE;
156 for (i = 0; i < len; i++) {
159 else if (!noconvertcase && !lower_case_used && isupper(name[i]))
160 name[i] = tolower(name[i]);
165 /* ------------------------------------------------------------------------ */
167 macos_to_unix_filename(name, len)
173 for (i = 0; i < len; i++) {
176 else if (name[i] == '/')
181 /* ------------------------------------------------------------------------ */
183 unix_to_generic_filename(name, len)
189 for (i = 0; i < len; i++) {
192 else if (islower(name[i]))
193 name[i] = toupper(name[i]);
197 /* added by Koji Arai */
199 filename_conv(name, len, size,
201 from_delim, to_delim,
206 int from_code, to_code, case_to;
207 char *from_delim, *to_delim;
212 for (i = 0; i < len; i ++) {
213 #ifdef MULTIBYTE_FILENAME
214 if (from_code == CODE_EUC &&
215 (unsigned char)name[i] == 0x8e) {
216 if (to_code != CODE_SJIS) {
222 memmove(name + i, name + i + 1, len - i);
226 if (from_code == CODE_SJIS && X0201_KANA_P(name[i])) {
227 if (to_code != CODE_EUC) {
231 if (len == size - 1) /* check overflow */
233 memmove(name+i+1, name+i, len-i);
239 if (from_code == CODE_EUC && (name[i] & 0x80) && (name[i+1] & 0x80)) {
241 if (to_code != CODE_SJIS) {
246 c1 = (unsigned char)name[i];
247 c2 = (unsigned char)name[i+1];
254 if (from_code == CODE_SJIS &&
255 SJC_FIRST_P(name[i]) &&
256 SJC_SECOND_P(name[i+1])) {
259 if (to_code != CODE_EUC) {
264 c1 = (unsigned char)name[i];
265 c2 = (unsigned char)name[i+1];
272 #endif /* MULTIBYTE_FILENAME */
276 /* transpose from_delim to to_delim */
278 if ((ptr = strchr(from_delim, name[i])) != NULL) {
279 name[i] = to_delim[ptr - from_delim];
284 if (case_to == TO_UPPER && islower(name[i])) {
285 name[i] = toupper(name[i]);
288 if (case_to == TO_LOWER && isupper(name[i])) {
289 name[i] = tolower(name[i]);
295 /* ------------------------------------------------------------------------ */
297 /* Generic stamp format: */
299 /* 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 */
300 /* |<-------- year ------->|<- month ->|<-- day -->| */
302 /* 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 */
303 /* |<--- hour --->|<---- minute --->|<- second*2 ->| */
305 /* ------------------------------------------------------------------------ */
308 * NOTE : If you don't have `gettimeofday(2)', or your gettimeofday(2)
309 * returns bogus timezone information, try FTIME, MKTIME, TIMELOCAL or TZSET.
313 #if defined(HAVE_MKTIME)
314 #ifdef HAVE_TIMELOCAL
315 #undef HAVE_TIMELOCAL
317 #endif /* defined(HAVE_MKTIME) */
319 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL)
323 #endif /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
325 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) || defined(HAVE_TZSET)
331 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) || defined(HAVE_TZSET) || defined(HAVE_FTIME)
332 #ifdef HAVE_GETTIMEOFDAY
333 #undef HAVE_GETTIMEOFDAY
336 #ifndef HAVE_GETTIMEOFDAY
337 #define HAVE_GETTIMEOFDAY /* use gettimeofday() */
342 #include <sys/timeb.h>
346 * You may define as : #define TIMEZONE_HOOK \ extern long
347 * timezone ; \ extern void tzset();
351 /* Which do you like better, `TIMEZONE_HOOK' or `TIMEZONE_HOOK;' ? */
354 #if defined(HAVE_TZSET) && defined(_MINIX)
355 extern long timezone; /* not defined in time.h */
358 /* ------------------------------------------------------------------------ */
359 #if defined(HAVE_FTIME) || defined(HAVE_GETTIMEOFDAY) || defined(HAVE_TZSET)
369 /* ------------------------------------------------------------------------ */
370 #if !defined(HAVE_TZSET) && defined(HAVE_FTIME)
375 return buf.timezone * 60L;
379 /* ------------------------------------------------------------------------ */
380 #if !defined(HAVE_TZSET) && !defined(HAVE_FTIME) /* maybe defined(HAVE_GETTIMEOFDAY) */
382 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
386 return -localtime(&tt)->tm_gmtoff;
387 #else /* HAVE_STRUCT_TM_TM_GMTOFF */
390 gettimeofday(&tp, &tzp);/* specific to 4.3BSD */
392 * return (tzp.tz_minuteswest * 60L + (tzp.tz_dsttime != 0 ? 60L *
395 return (tzp.tz_minuteswest * 60L);
396 #endif /* HAVE_STRUCT_TM_TM_GMTOFF */
399 #endif /* defined(HAVE_FTIME) || defined(HAVE_GETTIMEOFDAY) ||
400 * defined(HAVE_TZSET) */
402 /* ------------------------------------------------------------------------ */
405 msdos_to_unix_stamp_tm(a)
410 t.tm_sec = (a & 0x1f) * 2;
411 t.tm_min = (a >> 5) & 0x3f;
412 t.tm_hour = (a >> 11) & 0x1f;
413 t.tm_mday = (a >> 16) & 0x1f;
414 t.tm_mon = ((a >> 16 + 5) & 0x0f) - 1;
415 t.tm_year = ((a >> 16 + 9) & 0x7f) + 80;
420 /* ------------------------------------------------------------------------ */
422 generic_to_unix_stamp(t)
424 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL)
429 * special case: if MSDOS format date and time were zero, then we
430 * set time to be zero here too.
435 dostm.tm_sec = (t & 0x1f) * 2;
436 dostm.tm_min = t >> 5 & 0x3f;
437 dostm.tm_hour = t >> 11 & 0x1f;
438 dostm.tm_mday = t >> 16 & 0x1f;
439 dostm.tm_mon = (t >> 16 + 5 & 0x0f) - 1; /* 0..11 */
440 dostm.tm_year = (t >> 16 + 9 & 0x7f) + 80;
442 dostm.tm_isdst = 0; /* correct? */
444 dostm.tm_isdst = -1; /* correct? */
446 return (time_t) mktime(&dostm);
447 #else /* maybe defined(HAVE_TIMELOCAL) */
448 return (time_t) timelocal(&dostm);
452 #else /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
454 int year, month, day, hour, min, sec;
456 static unsigned int dsboy[12] = {0, 31, 59, 90, 120, 151,
457 181, 212, 243, 273, 304, 334};
461 * special case: if MSDOS format date and time were zero, then we
462 * set time to be zero here too.
467 year = ((int) (t >> 16 + 9) & 0x7f) + 1980;
468 month = (int) (t >> 16 + 5) & 0x0f; /* 1..12 means Jan..Dec */
469 day = (int) (t >> 16) & 0x1f; /* 1..31 means 1st,...31st */
471 hour = ((int) t >> 11) & 0x1f;
472 min = ((int) t >> 5) & 0x3f;
473 sec = ((int) t & 0x1f) * 2;
475 /* Calculate days since 1970.01.01 */
476 days = (365 * (year - 1970) + /* days due to whole years */
477 (year - 1970 + 1) / 4 + /* days due to leap years */
478 dsboy[month - 1] + /* days since beginning of this year */
479 day - 1); /* days since beginning of month */
481 if ((year % 4 == 0) &&
482 (year % 100 != 0 || year % 400 == 0) && /* 1999.5.24 t.oka */
483 (month >= 3)) /* if this is a leap year and month */
484 days++; /* is March or later, add a day */
486 /* Knowing the days, we can find seconds */
487 longtime = (((days * 24) + hour) * 60 + min) * 60 + sec;
488 longtime += gettz(); /* adjust for timezone */
490 /* LONGTIME is now the time in seconds, since 1970/01/01 00:00:00. */
491 return (time_t) longtime;
493 #endif /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
495 /* ------------------------------------------------------------------------ */
497 unix_to_generic_stamp(t)
500 struct tm *tm = localtime(&t);
502 return ((((long) (tm->tm_year - 80)) << 25) +
503 (((long) (tm->tm_mon + 1)) << 21) +
504 (((long) tm->tm_mday) << 16) +
505 (long) ((tm->tm_hour << 11) +
510 /* ------------------------------------------------------------------------ */
511 /* build header functions */
512 /* ------------------------------------------------------------------------ */
516 register LzHeader *hdr;
520 char data[LZHEADER_STRAGE];
521 char dirname[FILENAME_LENGTH];
529 int archive_kanji_code = CODE_SJIS;
530 int system_kanji_code = default_system_kanji_code;
531 char *archive_delim = "";
532 char *system_delim = "";
533 int filename_case = NONE;
535 memset(hdr, 0, sizeof(LzHeader));
537 if (((header_size = getc(fp)) == EOF) || (header_size == 0)) {
538 return FALSE; /* finish */
541 if (fread(data + I_HEADER_CHECKSUM,
542 sizeof(char), header_size - 1, fp) < header_size - 1) {
543 fatal_error("Invalid header (LHarc file ?)");
544 return FALSE; /* finish */
546 setup_get(data + I_HEADER_LEVEL);
547 hdr->header_level = get_byte();
548 if (hdr->header_level != 2 &&
549 fread(data + header_size, sizeof(char), 2, fp) < 2) {
550 fatal_error("Invalid header (LHarc file ?)");
551 return FALSE; /* finish */
554 if (hdr->header_level >= 3) {
555 fatal_error("Unknown level header (level %d)", hdr->header_level);
559 setup_get(data + I_HEADER_CHECKSUM);
560 checksum = get_byte();
562 if (hdr->header_level == 2) {
563 hdr->header_size = header_size + checksum*256;
565 hdr->header_size = header_size;
567 memcpy(hdr->method, data + I_METHOD, METHOD_TYPE_STRAGE);
568 setup_get(data + I_PACKED_SIZE);
569 hdr->packed_size = get_longword();
570 hdr->original_size = get_longword();
571 hdr->last_modified_stamp = get_longword();
572 hdr->attribute = get_byte();
574 if ((hdr->header_level = get_byte()) != 2) {
575 if (calc_sum(data + I_METHOD, header_size) != checksum)
576 warning("Checksum error (LHarc file?)");
577 name_length = get_byte();
578 for (i = 0; i < name_length; i++)
579 hdr->name[i] = (char) get_byte();
580 hdr->name[name_length] = '\0';
583 hdr->unix_last_modified_stamp = hdr->last_modified_stamp;
587 /* defaults for other type */
588 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
592 if (hdr->header_level == 0) {
593 extend_size = header_size - name_length -22;
594 if (extend_size < 0) {
595 if (extend_size == -2) {
596 hdr->extend_type = EXTEND_GENERIC;
597 hdr->has_crc = FALSE;
599 error("Unkonwn header (lha file?)");
604 hdr->crc = get_word();
607 if (extend_size >= 1) {
608 hdr->extend_type = get_byte();
611 if (hdr->extend_type == EXTEND_UNIX) {
612 if (extend_size >= 11) {
613 hdr->minor_version = get_byte();
614 hdr->unix_last_modified_stamp = (time_t) get_longword();
615 hdr->unix_mode = get_word();
616 hdr->unix_uid = get_word();
617 hdr->unix_gid = get_word();
620 hdr->extend_type = EXTEND_GENERIC;
623 while (extend_size-- > 0)
625 if (hdr->extend_type == EXTEND_UNIX)
627 } else if (hdr->header_level == 1) {
629 extend_size = header_size - name_length-25;
630 hdr->crc = get_word();
631 hdr->extend_type = get_byte();
632 while (extend_size-- > 0)
634 } else { /* level 2 */
636 hdr->crc = get_word();
637 hdr->extend_type = get_byte();
640 if (hdr->header_level > 0) {
642 if (hdr->header_level != 2)
643 setup_get(data + hdr->header_size);
645 while ((header_size = get_word()) != 0) {
646 if (hdr->header_level != 2 &&
647 ((data + LZHEADER_STRAGE - get_ptr < header_size) ||
648 fread(get_ptr, sizeof(char), header_size, fp) < header_size)) {
649 fatal_error("Invalid header (LHa file ?)");
652 switch (get_byte()) {
657 setup_get(get_ptr + header_size - 3);
663 for (i = 0; i < header_size - 3; i++)
664 hdr->name[i] = (char) get_byte();
665 hdr->name[header_size - 3] = '\0';
666 name_length = header_size - 3;
672 for (i = 0; i < header_size - 3; i++)
673 dirname[i] = (char) get_byte();
674 dirname[header_size - 3] = '\0';
675 dir_length = header_size - 3;
681 if (hdr->extend_type == EXTEND_MSDOS ||
682 hdr->extend_type == EXTEND_HUMAN ||
683 hdr->extend_type == EXTEND_GENERIC)
684 hdr->attribute = get_word();
690 if (hdr->extend_type == EXTEND_UNIX)
691 hdr->unix_mode = get_word();
697 if (hdr->extend_type == EXTEND_UNIX) {
698 hdr->unix_gid = get_word();
699 hdr->unix_uid = get_word();
706 for (i = 0; i < header_size - 3; i++)
707 hdr->group[i] = get_byte();
708 hdr->group[i] = '\0';
714 for (i = 0; i < header_size - 3; i++)
715 hdr->user[i] = get_byte();
720 * UNIX last modified time
722 if (hdr->extend_type == EXTEND_UNIX)
723 hdr->unix_last_modified_stamp = (time_t) get_longword();
729 setup_get(get_ptr + header_size - 3);
733 if (hdr->header_level != 2 && get_ptr - ptr != 2) {
734 hdr->packed_size -= get_ptr - ptr - 2;
735 hdr->header_size += get_ptr - ptr - 2;
739 switch (hdr->extend_type) {
741 archive_delim = "\xff\\";
743 filename_case = noconvertcase ? NONE : TO_LOWER;
747 if (hdr->header_level == 2)
748 hdr->unix_last_modified_stamp = hdr->last_modified_stamp;
750 hdr->unix_last_modified_stamp =
751 generic_to_unix_stamp(hdr->last_modified_stamp);
759 archive_delim = "\xff";
761 filename_case = NONE;
766 archive_delim = "\xff/:";
767 system_delim = "/:/";
768 filename_case = NONE;
770 hdr->unix_last_modified_stamp =
771 generic_to_unix_stamp(hdr->last_modified_stamp, sizeof(hdr->name));
775 archive_delim = "\xff\\";
777 filename_case = noconvertcase ? NONE : TO_LOWER;
778 /* FIXME: if small letter is included in filename,
779 the generic_to_unix_filename() do not case conversion,
780 but this code does not consider it. */
782 if (hdr->header_level == 2)
783 hdr->unix_last_modified_stamp = hdr->last_modified_stamp;
785 hdr->unix_last_modified_stamp =
786 generic_to_unix_stamp(hdr->last_modified_stamp);
789 /* filename kanji code and delimiter conversion */
790 if (optional_archive_kanji_code)
791 archive_kanji_code = optional_archive_kanji_code;
792 if (optional_system_kanji_code)
793 system_kanji_code = optional_system_kanji_code;
794 if (optional_archive_delim)
795 archive_delim = optional_archive_delim;
796 if (optional_system_delim)
797 system_delim = optional_system_delim;
798 if (optional_filename_case)
799 filename_case = optional_filename_case;
802 strcat(dirname, hdr->name);
803 strcpy(hdr->name, dirname);
804 name_length += dir_length;
807 filename_conv(hdr->name, name_length, sizeof(hdr->name),
810 archive_delim, system_delim, filename_case);
814 char *p = sjis_to_utf8_static(hdr->name);
815 strncpy(hdr->name, p, sizeof(hdr->name));
822 /* ------------------------------------------------------------------------ */
824 init_header(name, v_stat, hdr)
831 int system_kanji_code = default_system_kanji_code;
832 char *archive_delim = "";
833 char *system_delim = "";
834 int filename_case = NONE;
836 memset(hdr, 0, sizeof(LzHeader));
838 if (optional_system_kanji_code)
839 system_kanji_code = optional_system_kanji_code;
841 if (compress_method == LZHUFF5_METHOD_NUM) /* Changed N.Watazaki */
842 memcpy(hdr->method, LZHUFF5_METHOD, METHOD_TYPE_STRAGE);
843 else if (compress_method)
844 memcpy(hdr->method, LZHUFF1_METHOD, METHOD_TYPE_STRAGE);
846 memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STRAGE);
848 hdr->packed_size = 0;
849 hdr->original_size = v_stat->st_size;
850 hdr->last_modified_stamp = unix_to_generic_stamp(v_stat->st_mtime);
851 hdr->attribute = GENERIC_ATTRIBUTE;
852 hdr->header_level = header_level;
853 strcpy(hdr->name, name);
856 hdr->extend_type = EXTEND_UNIX;
857 hdr->unix_last_modified_stamp = v_stat->st_mtime;
858 /* since 00:00:00 JAN.1.1970 */
859 #ifdef NOT_COMPATIBLE_MODE
860 /* Please need your modification in this space. */
862 hdr->unix_mode = v_stat->st_mode;
865 hdr->unix_uid = v_stat->st_uid;
866 hdr->unix_gid = v_stat->st_gid;
868 #if INCLUDE_OWNER_NAME_IN_HEADER
871 struct passwd *ent = getpwuid(hdr->unix_uid);
874 strncpy(hdr->user, ent->pw_name, sizeof(hdr->user));
875 if (hdr->user[sizeof(hdr->user)-1])
876 hdr->user[sizeof(hdr->user)-1] = 0;
882 struct group *ent = getgrgid(hdr->unix_gid);
885 strncpy(hdr->group, ent->gr_name, sizeof(hdr->group));
886 if (hdr->group[sizeof(hdr->group)-1])
887 hdr->group[sizeof(hdr->group)-1] = 0;
891 #endif /* INCLUDE_OWNER_NAME_IN_HEADER */
892 if (is_directory(v_stat)) {
893 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STRAGE);
894 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
895 hdr->original_size = 0;
896 if (len > 0 && hdr->name[len - 1] != '/')
897 strcpy(&hdr->name[len++], "/");
901 if (is_symlink(v_stat)) {
902 char lkname[256]; /* FIXME: no enough space */
904 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STRAGE);
905 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
906 hdr->original_size = 0;
907 len = readlink(name, lkname, sizeof(lkname));
908 if (xsnprintf(hdr->name, sizeof(hdr->name),
909 "%s|%.*s", hdr->name, len, lkname) == -1)
910 error("file name is too long (%s -> %.*s)", hdr->name, len, lkname);
914 if (generic_format) {
915 filename_case = TO_UPPER;
916 archive_delim = "\\";
919 filename_conv(hdr->name, len, sizeof(hdr->name),
921 system_kanji_code, /* no change code */
922 system_delim, archive_delim, filename_case);
925 char *p = sjis_to_utf8_static(hdr->name);
926 strncpy(hdr->name, p, sizeof(hdr->name));
931 /* ------------------------------------------------------------------------ */
932 /* Write unix extended header or generic header. */
934 write_header(nafp, hdr)
940 char data[LZHEADER_STRAGE];
943 int archive_kanji_code = CODE_SJIS;
944 int system_kanji_code = default_system_kanji_code;
947 if (optional_archive_kanji_code)
948 archive_kanji_code = optional_archive_kanji_code;
949 if (optional_system_kanji_code)
950 system_kanji_code = optional_system_kanji_code;
952 memset(data, 0, LZHEADER_STRAGE);
953 memcpy(data + I_METHOD, hdr->method, METHOD_TYPE_STRAGE);
954 setup_put(data + I_PACKED_SIZE);
955 put_longword(hdr->packed_size);
956 put_longword(hdr->original_size);
958 if (hdr->header_level == HEADER_LEVEL2)
959 put_longword((long) hdr->unix_last_modified_stamp);
961 put_longword(hdr->last_modified_stamp);
963 switch (hdr->header_level) {
965 put_byte(hdr->attribute);
973 put_byte(hdr->header_level);
977 char *p = utf8_to_sjis_static(hdr->name);
981 strcpy(lzname, hdr->name);
983 filename_conv(lzname, strlen(lzname), sizeof(lzname),
986 "\xff\\/", "\xff\xff\xff", NONE);
988 if (hdr->header_level != HEADER_LEVEL2) {
989 if (p = (char *) strrchr(lzname, DELIM2))
990 name_length = strlen(++p);
992 name_length = strlen(lzname);
993 put_byte(name_length);
994 memcpy(data + I_NAME, p ? p : lzname, name_length);
995 setup_put(data + I_NAME + name_length);
999 if (header_level == HEADER_LEVEL0) {
1000 if (generic_format) {
1001 header_size = I_GENERIC_HEADER_BOTTOM - 2 + name_length;
1002 data[I_HEADER_SIZE] = header_size;
1003 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1005 /* write old-style extend header */
1006 put_byte(EXTEND_UNIX);
1007 put_byte(CURRENT_UNIX_MINOR_VERSION);
1008 put_longword((long) hdr->unix_last_modified_stamp);
1009 put_word(hdr->unix_mode);
1010 put_word(hdr->unix_uid);
1011 put_word(hdr->unix_gid);
1012 header_size = I_UNIX_EXTEND_BOTTOM - 2 + name_length;
1013 data[I_HEADER_SIZE] = header_size;
1014 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1017 /* write extend header. */
1023 put_byte(EXTEND_UNIX);
1026 if (hdr->header_level == HEADER_LEVEL2) {
1027 /* write common header */
1030 headercrc_ptr = put_ptr;
1034 if (generic_format) {
1035 header_size = put_ptr - data; /* +2 for last 0x0000 */
1038 if (hdr->header_level == HEADER_LEVEL1)
1039 header_size = put_ptr - data - 2;
1040 put_byte(0x50); /* permission */
1041 put_word(hdr->unix_mode);
1043 put_byte(0x51); /* gid and uid */
1044 put_word(hdr->unix_gid);
1045 put_word(hdr->unix_uid);
1048 int i, len = strlen(hdr->group);
1050 put_byte(0x52); /* group name */
1051 for (i = 0; i < len; i++)
1052 put_byte(hdr->group[i]);
1054 len = strlen(hdr->user);
1056 put_byte(0x53); /* user name */
1057 for (i = 0; i < len; i++)
1058 put_byte(hdr->user[i]);
1061 if (p = (char *) strrchr(lzname, DELIM2)) {
1064 name_length = p - lzname + 1;
1065 put_word(name_length + 3);
1066 put_byte(2); /* dirname */
1067 for (i = 0; i < name_length; i++)
1068 put_byte(lzname[i]);
1070 } /* if generic .. */
1072 if (header_level != HEADER_LEVEL2) {
1073 if (!generic_format) {
1075 put_byte(0x54); /* time stamp */
1076 put_longword(hdr->unix_last_modified_stamp);
1078 hdr->packed_size += put_ptr - ptr;
1080 setup_put(data + I_PACKED_SIZE);
1081 put_longword(hdr->packed_size);
1083 data[I_HEADER_SIZE] = header_size;
1084 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1085 } else { /* header level 2 */
1087 if (p = (char *) strrchr(lzname, DELIM2))
1088 name_length = strlen(++p);
1091 name_length = strlen(lzname);
1093 put_word(name_length + 3);
1094 put_byte(1); /* filename */
1095 for (i = 0; i < name_length; i++)
1097 } /* if he.. != HEAD_LV2 */
1098 header_size = put_ptr - data;
1101 if (header_level == HEADER_LEVEL2) {
1102 unsigned short hcrc;
1103 setup_put(data + I_HEADER_SIZE);
1104 put_word(header_size + 2);
1106 hcrc = calc_header_crc(data, (unsigned int) header_size + 2);
1107 setup_put(headercrc_ptr);
1111 if (fwrite(data, header_size + 2, 1, nafp) == 0)
1112 fatal_error("Cannot write to temporary file");
1116 /* this is not need for Mac OS X v 10.2 later */
1118 kCFStringEncodingAllowLossyConversion = 1,
1119 kCFStringEncodingBasicDirectionLeftToRight = (1 << 1),
1120 kCFStringEncodingBasicDirectionRightToLeft = (1 << 2),
1121 kCFStringEncodingSubstituteCombinings = (1 << 3),
1122 kCFStringEncodingComposeCombinings = (1 << 4),
1123 kCFStringEncodingIgnoreCombinings = (1 << 5),
1124 kCFStringEncodingUseCanonical = (1 << 6),
1125 kCFStringEncodingUseHFSPlusCanonical = (1 << 7),
1126 kCFStringEncodingPrependBOM = (1 << 8),
1127 kCFStringEncodingDisableCorporateArea = (1 << 9),
1128 kCFStringEncodingASCIICompatibleConversion = (1 << 10),
1131 char *sjis_to_utf8_static(const char *from) {
1132 static char cvtbuf[2048];
1136 src = (unsigned char *)from;
1137 dst = (unsigned char *)cvtbuf;
1140 ConvertEncodingToUTF8((const char*) src, dst, 2048, kCFStringEncodingDOSJapanese, kCFStringEncodingUseHFSPlusCanonical);
1145 char *utf8_to_sjis_static(const char *from) {
1146 static char cvtbuf[2048];
1151 src = (unsigned char *)from;
1152 dst = (unsigned char *)cvtbuf;
1155 srclen = strlen(src);
1156 ConvertUTF8ToEncoding((const char*) src, srclen, dst, 2048, kCFStringEncodingDOSJapanese, kCFStringEncodingUseHFSPlusCanonical);
1161 static int ConvertEncodingToUTF8(const char* inCStr, char* outUTF8Buffer, int outUTF8BufferLength, unsigned long scriptEncoding, unsigned long flags) {
1162 unsigned long unicodeChars;
1163 unsigned long srcCharsUsed;
1164 unsigned long usedByteLen = 0;
1165 UniChar uniStr[512];
1166 unsigned long cfResult;
1168 cfResult = CFStringEncodingBytesToUnicode(scriptEncoding, flags, (char *)inCStr, strlen(inCStr), &srcCharsUsed, uniStr, 512, &unicodeChars);
1169 if (cfResult == 0) {
1170 cfResult = CFStringEncodingUnicodeToBytes(kCFStringEncodingUTF8, flags, uniStr, unicodeChars, &srcCharsUsed, (char*)outUTF8Buffer, outUTF8BufferLength - 1, &usedByteLen);
1171 outUTF8Buffer[usedByteLen] = '\0';
1177 static int ConvertUTF8ToEncoding(const char* inUTF8Buf, int inUTF8BufLength, char* outCStrBuffer, int outCStrBufferLength, unsigned long scriptEncoding, unsigned long flags) {
1178 unsigned long unicodeChars;
1179 unsigned long srcCharsUsed;
1180 unsigned long usedByteLen = 0;
1181 UniChar uniStr[256];
1182 unsigned long cfResult;
1184 cfResult = CFStringEncodingBytesToUnicode(kCFStringEncodingUTF8, flags, (char*)inUTF8Buf, inUTF8BufLength, &srcCharsUsed, uniStr, 255, &unicodeChars);
1185 if (cfResult == 0) {
1186 cfResult = CFStringEncodingUnicodeToBytes(scriptEncoding, flags, uniStr, unicodeChars, &srcCharsUsed, (char*)outCStrBuffer, outCStrBufferLength - 1, &usedByteLen);
1187 outCStrBuffer[usedByteLen] = '\0';
1192 #endif /* __APPLE__ */
1195 * SJIS <-> EUC ÊÑ´¹´Ø¿ô
1196 * ¡ÖÆüËܸì¾ðÊó½èÍý¡× ¥½¥Õ¥È¥Ð¥ó¥¯(³ô)
1197 * ¤è¤êÈ´¿è(by Koji Arai)
1200 euc2sjis(int *p1, int *p2)
1202 unsigned char c1 = *p1 & 0x7f;
1203 unsigned char c2 = *p2 & 0x7f;
1204 int rowoff = c1 < 0x5f ? 0x70 : 0xb0;
1205 int celoff = c1 % 2 ? (c2 > 0x5f ? 0x20 : 0x1f) : 0x7e;
1206 *p1 = ((c1 + 1) >> 1) + rowoff;
1207 *p2 += celoff - 0x80;
1211 sjis2euc(int *p1, int *p2)
1213 unsigned char c1 = *p1;
1214 unsigned char c2 = *p2;
1215 int adjust = c2 < 0x9f;
1216 int rowoff = c1 < 0xa0 ? 0x70 : 0xb0;
1217 int celoff = adjust ? (c2 > 0x7f ? 0x20 : 0x1f) : 0x7e;
1218 *p1 = ((c1 - rowoff) << 1) - adjust;
1225 /* Local Variables: */
1228 /* compile-command:"gcc -c header.c" */
1230 /* vi: set tabstop=4: */