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 /* ------------------------------------------------------------------------ */
19 int specific_archive_kanji_code = NONE;
20 int specific_system_kanji_code = NONE;
21 char *specific_archive_delim = NULL;
22 char *specific_system_delim = NULL;
23 int specific_filename_case = NONE;
25 int default_system_kanji_code = CODE_EUC;
26 /* ------------------------------------------------------------------------ */
34 for (sum = 0; len; len--)
40 /* ------------------------------------------------------------------------ */
48 return (b1 << 8) + b0;
51 /* ------------------------------------------------------------------------ */
60 /* ------------------------------------------------------------------------ */
70 return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
73 /* ------------------------------------------------------------------------ */
84 /* ------------------------------------------------------------------------ */
86 msdos_to_unix_filename(name, len)
93 for (i = 0; i < len; i++) {
94 if (MULTIBYTE_FIRST_P(name[i]) &&
95 MULTIBYTE_SECOND_P(name[i + 1]))
97 else if (name[i] == '\\')
99 else if (!noconvertcase && isupper(name[i]))
100 name[i] = tolower(name[i]);
103 for (i = 0; i < len; i++) {
106 else if (!noconvertcase && isupper(name[i]))
107 name[i] = tolower(name[i]);
112 /* ------------------------------------------------------------------------ */
114 generic_to_unix_filename(name, len)
119 boolean lower_case_used = FALSE;
121 #ifdef MULTIBYTE_CHAR
122 for (i = 0; i < len; i++) {
123 if (MULTIBYTE_FIRST_P(name[i]) &&
124 MULTIBYTE_SECOND_P(name[i + 1]))
126 else if (islower(name[i])) {
127 lower_case_used = TRUE;
131 for (i = 0; i < len; i++) {
132 if (MULTIBYTE_FIRST_P(name[i]) &&
133 MULTIBYTE_SECOND_P(name[i + 1]))
135 else if (name[i] == '\\')
137 else if (!noconvertcase && !lower_case_used && isupper(name[i]))
138 name[i] = tolower(name[i]);
141 for (i = 0; i < len; i++)
142 if (islower(name[i])) {
143 lower_case_used = TRUE;
146 for (i = 0; i < len; i++) {
149 else if (!noconvertcase && !lower_case_used && isupper(name[i]))
150 name[i] = tolower(name[i]);
155 /* ------------------------------------------------------------------------ */
157 macos_to_unix_filename(name, len)
163 for (i = 0; i < len; i++) {
166 else if (name[i] == '/')
171 /* ------------------------------------------------------------------------ */
173 unix_to_generic_filename(name, len)
179 for (i = 0; i < len; i++) {
182 else if (islower(name[i]))
183 name[i] = toupper(name[i]);
187 /* added by Koji Arai */
189 filename_conv(name, len, size,
191 from_delim, to_delim,
196 int from_code, to_code, case_to;
197 char *from_delim, *to_delim;
202 for (i = 0; i < len; i ++) {
203 #ifdef MULTIBYTE_CHAR
204 if (from_code == CODE_EUC &&
205 (unsigned char)name[i] == 0x8e) {
206 if (to_code != CODE_SJIS) {
212 memmove(name + i, name + i + 1, len - i);
216 if (from_code == CODE_SJIS && X0201_KANA_P(name[i])) {
217 if (to_code != CODE_EUC) {
221 if (len == size - 1) /* check overflow */
223 memmove(name + i + 1, name, len - i);
229 if (from_code == CODE_EUC && (name[i] & 0x80) && (name[i+1] & 0x80)) {
231 if (to_code != CODE_SJIS) {
236 c1 = (unsigned char)name[i];
237 c2 = (unsigned char)name[i+1];
244 if (from_code == CODE_SJIS &&
245 SJC_FIRST_P(name[i]) &&
246 SJC_SECOND_P(name[i+1])) {
249 if (to_code != CODE_EUC) {
254 c1 = (unsigned char)name[i];
255 c2 = (unsigned char)name[i+1];
262 #endif /* MULTIBYTE_CHAR */
266 /* transpose from_delim to to_delim */
268 if ((ptr = strchr(from_delim, name[i])) != NULL) {
269 name[i] = to_delim[ptr - from_delim];
274 if (case_to == TO_UPPER && islower(name[i])) {
275 name[i] = toupper(name[i]);
278 if (case_to == TO_LOWER && islower(name[i])) {
279 name[i] = toupper(name[i]);
285 /* ------------------------------------------------------------------------ */
287 /* Generic stamp format: */
289 /* 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 */
290 /* |<-------- year ------->|<- month ->|<-- day -->| */
292 /* 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 */
293 /* |<--- hour --->|<---- minute --->|<- second*2 ->| */
295 /* ------------------------------------------------------------------------ */
298 * NOTE : If you don't have `gettimeofday(2)', or your gettimeofday(2)
299 * returns bogus timezone information, try FTIME, MKTIME, TIMELOCAL or TZSET.
303 #if defined(HAVE_MKTIME)
304 #ifdef HAVE_TIMELOCAL
305 #undef HAVE_TIMELOCAL
307 #endif /* defined(HAVE_MKTIME) */
309 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL)
313 #endif /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
315 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) || defined(HAVE_TZSET)
321 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) || defined(HAVE_TZSET) || defined(HAVE_FTIME)
322 #ifdef HAVE_GETTIMEOFDAY
323 #undef HAVE_GETTIMEOFDAY
326 #ifndef HAVE_GETTIMEOFDAY
327 #define HAVE_GETTIMEOFDAY /* use gettimeofday() */
332 #include <sys/timeb.h>
336 * You may define as : #define TIMEZONE_HOOK \ extern long
337 * timezone ; \ extern void tzset();
341 /* Which do you like better, `TIMEZONE_HOOK' or `TIMEZONE_HOOK;' ? */
344 #if defined(HAVE_TZSET) && defined(_MINIX)
345 extern long timezone; /* not defined in time.h */
348 /* ------------------------------------------------------------------------ */
349 #if defined(HAVE_FTIME) || defined(HAVE_GETTIMEOFDAY) || defined(HAVE_TZSET)
359 /* ------------------------------------------------------------------------ */
360 #if !defined(HAVE_TZSET) && defined(HAVE_FTIME)
365 return buf.timezone * 60L;
369 /* ------------------------------------------------------------------------ */
370 #if !defined(HAVE_TZSET) && !defined(HAVE_FTIME) /* maybe defined(HAVE_GETTIMEOFDAY) */
372 #ifdef HAVE_TM_GMTOFF
376 return -localtime(&tt)->tm_gmtoff;
377 #else /* HAVE_TM_GMTOFF */
380 gettimeofday(&tp, &tzp);/* specific to 4.3BSD */
382 * return (tzp.tz_minuteswest * 60L + (tzp.tz_dsttime != 0 ? 60L *
385 return (tzp.tz_minuteswest * 60L);
386 #endif /* HAVE_TM_GMTOFF */
389 #endif /* defined(HAVE_FTIME) || defined(HAVE_GETTIMEOFDAY) ||
390 * defined(HAVE_TZSET) */
392 /* ------------------------------------------------------------------------ */
395 msdos_to_unix_stamp_tm(a)
400 t.tm_sec = (a & 0x1f) * 2;
401 t.tm_min = (a >> 5) & 0x3f;
402 t.tm_hour = (a >> 11) & 0x1f;
403 t.tm_mday = (a >> 16) & 0x1f;
404 t.tm_mon = ((a >> 16 + 5) & 0x0f) - 1;
405 t.tm_year = ((a >> 16 + 9) & 0x7f) + 80;
410 /* ------------------------------------------------------------------------ */
412 generic_to_unix_stamp(t)
414 #if defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL)
419 * special case: if MSDOS format date and time were zero, then we
420 * set time to be zero here too.
425 dostm.tm_sec = (t & 0x1f) * 2;
426 dostm.tm_min = t >> 5 & 0x3f;
427 dostm.tm_hour = t >> 11 & 0x1f;
428 dostm.tm_mday = t >> 16 & 0x1f;
429 dostm.tm_mon = (t >> 16 + 5 & 0x0f) - 1; /* 0..11 */
430 dostm.tm_year = (t >> 16 + 9 & 0x7f) + 80;
432 dostm.tm_isdst = 0; /* correct? */
434 dostm.tm_isdst = -1; /* correct? */
436 return (time_t) mktime(&dostm);
437 #else /* maybe defined(HAVE_TIMELOCAL) */
438 return (time_t) timelocal(&dostm);
442 #else /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
444 int year, month, day, hour, min, sec;
446 static unsigned int dsboy[12] = {0, 31, 59, 90, 120, 151,
447 181, 212, 243, 273, 304, 334};
451 * special case: if MSDOS format date and time were zero, then we
452 * set time to be zero here too.
457 year = ((int) (t >> 16 + 9) & 0x7f) + 1980;
458 month = (int) (t >> 16 + 5) & 0x0f; /* 1..12 means Jan..Dec */
459 day = (int) (t >> 16) & 0x1f; /* 1..31 means 1st,...31st */
461 hour = ((int) t >> 11) & 0x1f;
462 min = ((int) t >> 5) & 0x3f;
463 sec = ((int) t & 0x1f) * 2;
465 /* Calculate days since 1970.01.01 */
466 days = (365 * (year - 1970) + /* days due to whole years */
467 (year - 1970 + 1) / 4 + /* days due to leap years */
468 dsboy[month - 1] + /* days since beginning of this year */
469 day - 1); /* days since beginning of month */
471 if ((year % 4 == 0) &&
472 (year % 100 != 0 || year % 400 == 0) && /* 1999.5.24 t.oka */
473 (month >= 3)) /* if this is a leap year and month */
474 days++; /* is March or later, add a day */
476 /* Knowing the days, we can find seconds */
477 longtime = (((days * 24) + hour) * 60 + min) * 60 + sec;
478 longtime += gettz(); /* adjust for timezone */
480 /* LONGTIME is now the time in seconds, since 1970/01/01 00:00:00. */
481 return (time_t) longtime;
483 #endif /* defined(HAVE_MKTIME) || defined(HAVE_TIMELOCAL) */
485 /* ------------------------------------------------------------------------ */
487 unix_to_generic_stamp(t)
490 struct tm *tm = localtime(&t);
492 return ((((long) (tm->tm_year - 80)) << 25) +
493 (((long) (tm->tm_mon + 1)) << 21) +
494 (((long) tm->tm_mday) << 16) +
495 (long) ((tm->tm_hour << 11) +
500 /* ------------------------------------------------------------------------ */
501 /* build header functions */
502 /* ------------------------------------------------------------------------ */
506 register LzHeader *hdr;
510 char data[LZHEADER_STRAGE];
511 char dirname[FILENAME_LENGTH];
519 int archive_kanji_code = NONE;
520 int system_kanji_code = NONE;
521 char *archive_delim = "";
522 char *system_delim = "";
523 int filename_case = NONE;
525 bzero(hdr, sizeof(LzHeader));
527 if (((header_size = getc(fp)) == EOF) || (header_size == 0)) {
528 return FALSE; /* finish */
531 if (fread(data + I_HEADER_CHECKSUM,
532 sizeof(char), header_size - 1, fp) < header_size - 1) {
533 fatal_error("Invalid header (LHarc file ?)");
534 return FALSE; /* finish */
536 setup_get(data + I_HEADER_LEVEL);
537 hdr->header_level = get_byte();
538 if (hdr->header_level != 2 &&
539 fread(data + header_size, sizeof(char), 2, fp) < 2) {
540 fatal_error("Invalid header (LHarc file ?)");
541 return FALSE; /* finish */
544 if (hdr->header_level >= 3) {
545 fatal_error("Unknown level header");
549 setup_get(data + I_HEADER_CHECKSUM);
550 checksum = get_byte();
552 if (hdr->header_level == 2) {
553 hdr->header_size = header_size + checksum*256;
555 hdr->header_size = header_size;
557 bcopy(data + I_METHOD, hdr->method, METHOD_TYPE_STRAGE);
558 setup_get(data + I_PACKED_SIZE);
559 hdr->packed_size = get_longword();
560 hdr->original_size = get_longword();
561 hdr->last_modified_stamp = get_longword();
562 hdr->attribute = get_byte();
564 if ((hdr->header_level = get_byte()) != 2) {
565 if (calc_sum(data + I_METHOD, header_size) != checksum)
566 warning("Checksum error (LHarc file?)", "");
567 name_length = get_byte();
568 for (i = 0; i < name_length; i++)
569 hdr->name[i] = (char) get_byte();
570 hdr->name[name_length] = '\0';
573 hdr->unix_last_modified_stamp = hdr->last_modified_stamp;
577 /* defaults for other type */
578 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
582 if (hdr->header_level == 0) {
583 extend_size = header_size - name_length -22;
584 if (extend_size < 0) {
585 if (extend_size == -2) {
586 hdr->extend_type = EXTEND_GENERIC;
587 hdr->has_crc = FALSE;
589 fatal_error("Unkonwn header (lha file?)");
594 hdr->crc = get_word();
597 if (extend_size >= 1) {
598 hdr->extend_type = get_byte();
601 if (hdr->extend_type == EXTEND_UNIX) {
602 if (extend_size >= 11) {
603 hdr->minor_version = get_byte();
604 hdr->unix_last_modified_stamp = (time_t) get_longword();
605 hdr->unix_mode = get_word();
606 hdr->unix_uid = get_word();
607 hdr->unix_gid = get_word();
610 hdr->extend_type = EXTEND_GENERIC;
613 while (extend_size-- > 0)
615 if (hdr->extend_type == EXTEND_UNIX)
617 } else if (hdr->header_level == 1) {
619 extend_size = header_size - name_length-25;
620 hdr->crc = get_word();
621 hdr->extend_type = get_byte();
622 while (extend_size-- > 0)
624 } else { /* level 2 */
626 hdr->crc = get_word();
627 hdr->extend_type = get_byte();
630 if (hdr->header_level > 0) {
632 if (hdr->header_level != 2)
633 setup_get(data + hdr->header_size);
635 while ((header_size = get_word()) != 0) {
636 if (hdr->header_level != 2 &&
637 ((data + LZHEADER_STRAGE - get_ptr < header_size) ||
638 fread(get_ptr, sizeof(char), header_size, fp) < header_size)) {
639 fatal_error("Invalid header (LHa file ?)");
642 switch (get_byte()) {
647 setup_get(get_ptr + header_size - 3);
653 for (i = 0; i < header_size - 3; i++)
654 hdr->name[i] = (char) get_byte();
655 hdr->name[header_size - 3] = '\0';
656 name_length = header_size - 3; /* modified by Koji Arai */
662 for (i = 0; i < header_size - 3; i++)
663 dirname[i] = (char) get_byte();
664 dirname[header_size - 3] = '\0';
665 dir_length = header_size - 3;
667 filename_conv(dirname, dir_length, sizeof(dirname),
668 NONE, NONE, "\xff", "/", NONE);
675 if (hdr->extend_type == EXTEND_MSDOS ||
676 hdr->extend_type == EXTEND_HUMAN ||
677 hdr->extend_type == EXTEND_GENERIC)
678 hdr->attribute = get_word();
684 if (hdr->extend_type == EXTEND_UNIX)
685 hdr->unix_mode = get_word();
691 if (hdr->extend_type == EXTEND_UNIX) {
692 hdr->unix_gid = get_word();
693 hdr->unix_uid = get_word();
700 setup_get(get_ptr + header_size - 3);
706 setup_get(get_ptr + header_size - 3);
710 * UNIX last modified time
712 if (hdr->extend_type == EXTEND_UNIX)
713 hdr->unix_last_modified_stamp = (time_t) get_longword();
719 setup_get(get_ptr + header_size - 3);
723 if (hdr->header_level != 2 && get_ptr - ptr != 2) {
724 hdr->packed_size -= get_ptr - ptr - 2;
725 hdr->header_size += get_ptr - ptr - 2;
729 switch (hdr->extend_type) {
731 archive_kanji_code = CODE_SJIS;
732 system_kanji_code = default_system_kanji_code;
733 archive_delim = "\\";
735 filename_case = noconvertcase ? NONE : TO_LOWER;
739 if (hdr->header_level == 2)
740 hdr->unix_last_modified_stamp = hdr->last_modified_stamp;
742 hdr->unix_last_modified_stamp =
743 generic_to_unix_stamp(hdr->last_modified_stamp);
751 archive_kanji_code = CODE_EUC;
752 /* Cygwin, HP-UX and other UNIX are able to use SJIS as native code. */
753 system_kanji_code = default_system_kanji_code;
756 filename_case = NONE;
761 archive_kanji_code = CODE_SJIS;
762 system_kanji_code = default_system_kanji_code;
763 archive_delim = "/:";
765 filename_case = NONE;
767 hdr->unix_last_modified_stamp =
768 generic_to_unix_stamp(hdr->last_modified_stamp, sizeof(hdr->name));
772 archive_kanji_code = NONE;
773 system_kanji_code = NONE;
774 archive_delim = "\\";
776 filename_case = noconvertcase ? NONE : TO_LOWER;
777 /* pending: if small letter is included in filename,
778 the generic_to_unix_filename() do not case conversion,
779 but this code does not consider it. */
781 if (hdr->header_level == 2)
782 hdr->unix_last_modified_stamp = hdr->last_modified_stamp;
784 hdr->unix_last_modified_stamp =
785 generic_to_unix_stamp(hdr->last_modified_stamp);
788 /* filename kanji code and delimiter conversion */
789 if (specific_archive_kanji_code)
790 archive_kanji_code = specific_archive_kanji_code;
791 if (specific_system_kanji_code)
792 system_kanji_code = specific_system_kanji_code;
793 if (specific_archive_delim)
794 archive_delim = specific_archive_delim;
795 if (specific_system_delim)
796 system_delim = specific_system_delim;
797 if (specific_filename_case)
798 filename_case = specific_filename_case;
800 filename_conv(hdr->name, name_length, sizeof(hdr->name),
803 archive_delim, system_delim, filename_case);
805 if (dir_length && hdr->extend_type != EXTEND_MACOS) {
806 filename_conv(dirname, dir_length, sizeof(dirname),
809 archive_delim, system_delim, filename_case);
813 strcat(dirname, hdr->name);
814 strcpy(hdr->name, dirname);
815 name_length += dir_length;
821 /* ------------------------------------------------------------------------ */
823 init_header(name, v_stat, hdr)
830 if (compress_method == LZHUFF5_METHOD_NUM) /* Changed N.Watazaki */
831 bcopy(LZHUFF5_METHOD, hdr->method, METHOD_TYPE_STRAGE);
832 else if (compress_method)
833 bcopy(LZHUFF1_METHOD, hdr->method, METHOD_TYPE_STRAGE);
835 bcopy(LZHUFF0_METHOD, hdr->method, METHOD_TYPE_STRAGE);
837 hdr->packed_size = 0;
838 hdr->original_size = v_stat->st_size;
839 hdr->last_modified_stamp = unix_to_generic_stamp(v_stat->st_mtime);
840 hdr->attribute = GENERIC_ATTRIBUTE;
841 hdr->header_level = header_level;
842 strcpy(hdr->name, name);
845 hdr->extend_type = EXTEND_UNIX;
846 hdr->unix_last_modified_stamp = v_stat->st_mtime;
847 /* since 00:00:00 JAN.1.1970 */
848 #ifdef NOT_COMPATIBLE_MODE
849 /* Please need your modification in this space. */
851 hdr->unix_mode = v_stat->st_mode;
854 hdr->unix_uid = v_stat->st_uid;
855 hdr->unix_gid = v_stat->st_gid;
857 if (is_directory(v_stat)) {
858 bcopy(LZHDIRS_METHOD, hdr->method, METHOD_TYPE_STRAGE);
859 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
860 hdr->original_size = 0;
861 if (len > 0 && hdr->name[len - 1] != '/')
862 strcpy(&hdr->name[len++], "/");
866 if (is_symlink(v_stat)) {
869 bcopy(LZHDIRS_METHOD, hdr->method, METHOD_TYPE_STRAGE);
870 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
871 hdr->original_size = 0;
872 len = readlink(name, lkname, 256);
873 lkname[len] = (char)'\0';
874 sprintf(hdr->name, "%s|%s", hdr->name, lkname);
877 if (generic_format) {
878 filename_conv(hdr->name, len, sizeof(hdr->name),
879 default_system_kanji_code,
880 default_system_kanji_code,
881 "/", "\\", TO_UPPER);
885 /* ------------------------------------------------------------------------ */
886 /* Write unix extended header or generic header. */
888 write_header(nafp, hdr)
894 char data[LZHEADER_STRAGE];
898 bzero(data, LZHEADER_STRAGE);
899 bcopy(hdr->method, data + I_METHOD, METHOD_TYPE_STRAGE);
900 setup_put(data + I_PACKED_SIZE);
901 put_longword(hdr->packed_size);
902 put_longword(hdr->original_size);
904 if (hdr->header_level == HEADER_LEVEL2)
905 put_longword((long) hdr->unix_last_modified_stamp);
907 put_longword(hdr->last_modified_stamp);
909 switch (hdr->header_level) {
911 put_byte(hdr->attribute);
919 put_byte(hdr->header_level);
921 filename_conv(hdr->name, strlen(hdr->name), sizeof(hdr->name),
922 default_system_kanji_code,
923 default_system_kanji_code, /* no change code */
924 "\xff\\/", "\xff\xff\xff", NONE);
926 if (hdr->header_level != HEADER_LEVEL2) {
927 if (p = (char *) strrchr(hdr->name, DELIM2))
928 name_length = strlen(++p);
930 name_length = strlen(hdr->name);
931 put_byte(name_length);
932 bcopy(p ? p : hdr->name, data + I_NAME, name_length);
933 setup_put(data + I_NAME + name_length);
937 if (header_level == HEADER_LEVEL0) {
938 if (generic_format) {
939 header_size = I_GENERIC_HEADER_BOTTOM - 2 + name_length;
940 data[I_HEADER_SIZE] = header_size;
941 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
943 /* write old-style extend header */
944 put_byte(EXTEND_UNIX);
945 put_byte(CURRENT_UNIX_MINOR_VERSION);
946 put_longword((long) hdr->unix_last_modified_stamp);
947 put_word(hdr->unix_mode);
948 put_word(hdr->unix_uid);
949 put_word(hdr->unix_gid);
950 header_size = I_UNIX_EXTEND_BOTTOM - 2 + name_length;
951 data[I_HEADER_SIZE] = header_size;
952 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
955 /* write extend header. */
961 put_byte(EXTEND_UNIX);
964 if (hdr->header_level == HEADER_LEVEL2) {
965 /* write common header */
968 headercrc_ptr = put_ptr;
972 if (generic_format) {
973 header_size = put_ptr - data; /* +2 for last 0x0000 */
976 if (hdr->header_level == HEADER_LEVEL1)
977 header_size = put_ptr - data - 2;
978 put_byte(0x50); /* permission */
979 put_word(hdr->unix_mode);
981 put_byte(0x51); /* gid and uid */
982 put_word(hdr->unix_gid);
983 put_word(hdr->unix_uid);
985 if (p = (char *) strrchr(hdr->name, DELIM2)) {
988 name_length = p - hdr->name + 1;
989 put_word(name_length + 3);
990 put_byte(2); /* dirname */
991 for (i = 0; i < name_length; i++)
992 put_byte(hdr->name[i]);
994 } /* if generic .. */
996 if (header_level != HEADER_LEVEL2) {
997 if (!generic_format) {
999 put_byte(0x54); /* time stamp */
1000 put_longword(hdr->unix_last_modified_stamp);
1002 hdr->packed_size += put_ptr - ptr;
1004 setup_put(data + I_PACKED_SIZE);
1005 put_longword(hdr->packed_size);
1007 data[I_HEADER_SIZE] = header_size;
1008 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1009 } else { /* header level 2 */
1011 if (p = (char *) strrchr(hdr->name, DELIM2))
1012 name_length = strlen(++p);
1015 name_length = strlen(hdr->name);
1017 put_word(name_length + 3);
1018 put_byte(1); /* filename */
1019 for (i = 0; i < name_length; i++)
1021 } /* if he.. != HEAD_LV2 */
1022 header_size = put_ptr - data;
1025 if (header_level == HEADER_LEVEL2) {
1026 unsigned short hcrc;
1027 setup_put(data + I_HEADER_SIZE);
1028 put_word(header_size + 2);
1030 hcrc = calc_header_crc(data, (unsigned int) header_size + 2);
1031 setup_put(headercrc_ptr);
1035 if (fwrite(data, sizeof(char), header_size + 2, nafp) == 0)
1036 fatal_error("Cannot write to temporary file");
1038 filename_conv(hdr->name, strlen(hdr->name), sizeof(hdr->name),
1039 default_system_kanji_code,
1040 default_system_kanji_code, /* no change code */
1041 "\xff\\/", "///", NONE);
1045 * SJIS <-> EUC ÊÑ´¹´Ø¿ô
1046 * ¡ÖÆüËܸì¾ðÊó½èÍý¡× ¥½¥Õ¥È¥Ð¥ó¥¯(³ô)
1047 * ¤è¤êÈ´¿è(by Koji Arai)
1050 euc2sjis(int *p1, int *p2)
1052 unsigned char c1 = *p1 & 0x7f;
1053 unsigned char c2 = *p2 & 0x7f;
1054 int rowoff = c1 < 0x5f ? 0x70 : 0xb0;
1055 int celoff = c1 % 2 ? (c2 > 0x5f ? 0x20 : 0x1f) : 0x7e;
1056 *p1 = ((c1 + 1) >> 1) + rowoff;
1057 *p2 += celoff - 0x80;
1061 sjis2euc(int *p1, int *p2)
1063 unsigned char c1 = *p1;
1064 unsigned char c2 = *p2;
1065 int adjust = c2 < 0x9f;
1066 int rowoff = c1 < 0xa0 ? 0x70 : 0xb0;
1067 int celoff = adjust ? (c2 > 0x7f ? 0x20 : 0x1f) : 0x7e;
1068 *p1 = ((c1 - rowoff) << 1) - adjust;
1075 /* Local Variables: */
1078 /* compile-command:"gcc -c header.c" */