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 /* Ver. 1.14i Contributed UTF-8 convertion for Mac OS X */
14 /* 2002.06.29 Hiroto Sakai */
15 /* Ver. 1.14i autoconfiscated & rewritten 2003.02.23 Koji Arai */
16 /* ------------------------------------------------------------------------ */
19 #define DUMP_HEADER 1 /* for debugging */
21 #if !STRCHR_8BIT_CLEAN
22 /* should use 8 bit clean version */
25 #define strchr xstrchr
26 #define strrchr xstrrchr
30 #define GET_BYTE() (*get_ptr++ & 0xff)
33 static char *start_ptr;
34 #define setup_get(PTR) (start_ptr = get_ptr = (PTR))
35 #define get_byte() dump_get_byte()
36 #define skip_bytes(len) dump_skip_bytes(len)
38 #define setup_get(PTR) (get_ptr = (PTR))
39 #define get_byte() GET_BYTE()
40 #define skip_bytes(len) (get_ptr += (len))
42 #define put_ptr get_ptr
43 #define setup_put(PTR) (put_ptr = (PTR))
44 #define put_byte(c) (*put_ptr++ = (char)(c))
46 int optional_archive_kanji_code = NONE;
47 int optional_system_kanji_code = NONE;
48 char *optional_archive_delim = NULL;
49 char *optional_system_delim = NULL;
50 int optional_filename_case = NONE;
52 #ifdef MULTIBYTE_FILENAME
53 int default_system_kanji_code = MULTIBYTE_FILENAME;
55 int default_system_kanji_code = NONE;
65 while (len--) sum += *(unsigned char*)p++;
76 if (verbose_listing && verbose > 1)
77 printf("%02d %2d: ", get_ptr - start_ptr, 1);
79 if (verbose_listing && verbose > 1) {
81 printf("%d(0x%02x) '%c'\n", c, c, c);
83 printf("%d(0x%02x)\n", c, c);
93 if (verbose_listing && verbose > 1) {
94 printf("%02d %2d: ", get_ptr - start_ptr, len);
96 printf("0x%02x ", GET_BYTE());
97 printf("... ignored\n");
111 if (verbose_listing && verbose > 1)
112 printf("%02d %2d: ", get_ptr - start_ptr, 2);
118 if (verbose_listing && verbose > 1)
119 printf("%d(0x%04x)\n", w, w);
139 if (verbose_listing && verbose > 1)
140 printf("%02d %2d: ", get_ptr - start_ptr, 4);
146 l = (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
148 if (verbose_listing && verbose > 1)
149 printf("%ld(0x%08lx)\n", l, l);
167 uint64_t b0, b1, b2, b3, b4, b5, b6, b7;
171 if (verbose_listing && verbose > 1)
172 printf("%02d %2d: ", get_ptr - start_ptr, 4);
183 l = (b7 << 24) + (b6 << 16) + (b5 << 8) + b4;
185 l |= (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
187 if (verbose_listing && verbose > 1)
188 printf("%lld(%#016llx)\n", l, l);
194 put_longlongword(uint64_t v)
208 get_bytes(buf, len, size)
215 if (verbose_listing && verbose > 1)
216 printf("%02d %2d: \"", get_ptr - start_ptr, len);
218 for (i = 0; i < len; i++) {
219 if (i < size) buf[i] = get_ptr[i];
221 if (verbose_listing && verbose > 1) {
223 printf("%c", buf[i]);
225 printf("\\x%02x", (unsigned char)buf[i]);
229 if (verbose_listing && verbose > 1)
232 for (i = 0; i < len && i < size; i++)
246 for (i = 0; i < len; i++)
250 /* added by Koji Arai */
252 convert_filename(name, len, size,
254 from_delim, to_delim,
257 int len; /* length of name */
258 int size; /* size of name buffer */
259 int from_code, to_code, case_to;
260 char *from_delim, *to_delim;
264 #ifdef MULTIBYTE_FILENAME
265 char tmp[FILENAME_LENGTH];
266 int to_code_save = NONE;
268 if (from_code == CODE_CAP) {
269 len = cap_to_sjis(tmp, name, sizeof(tmp));
270 strncpy(name, tmp, size);
273 from_code = CODE_SJIS;
276 if (to_code == CODE_CAP) {
277 to_code_save = CODE_CAP;
281 if (from_code == CODE_SJIS && to_code == CODE_UTF8) {
282 for (i = 0; i < len; i++) {
283 if (SJIS_FIRST_P(name[i]) && SJIS_SECOND_P(name[i+1]))
286 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
287 if (strchr(from_delim, name[i]))
291 sjis_to_utf8(tmp, name, sizeof(tmp));
292 strncpy(name, tmp, size);
295 for (i = 0; i < len; i++)
296 if (name[i] == '/') name[i] = LHA_PATHSEP;
297 from_code = CODE_UTF8;
299 else if (from_code == CODE_UTF8 && to_code == CODE_SJIS) {
300 for (i = 0; i < len; i++)
301 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
302 if ((unsigned char)name[i] == LHA_PATHSEP) name[i] = '/';
303 utf8_to_sjis(tmp, name, sizeof(tmp));
304 strncpy(name, tmp, size);
307 for (i = 0; i < len; i++)
308 if (name[i] == '/') name[i] = LHA_PATHSEP;
309 from_code = CODE_SJIS;
313 /* special case: if `name' has small lettter, not convert case. */
314 if (from_code == CODE_SJIS && case_to == TO_LOWER) {
315 for (i = 0; i < len; i++) {
316 #ifdef MULTIBYTE_FILENAME
317 if (SJIS_FIRST_P(name[i]) && SJIS_SECOND_P(name[i+1]))
321 if (islower(name[i])) {
328 for (i = 0; i < len; i ++) {
329 #ifdef MULTIBYTE_FILENAME
330 if (from_code == CODE_EUC &&
331 (unsigned char)name[i] == 0x8e) {
332 if (to_code != CODE_SJIS) {
338 memmove(name + i, name + i + 1, len - i);
342 if (from_code == CODE_SJIS && X0201_KANA_P(name[i])) {
343 if (to_code != CODE_EUC) {
347 if (len == size - 1) /* check overflow */
349 memmove(name+i+1, name+i, len-i);
355 if (from_code == CODE_EUC && (name[i] & 0x80) && (name[i+1] & 0x80)) {
357 if (to_code != CODE_SJIS) {
362 c1 = (unsigned char)name[i];
363 c2 = (unsigned char)name[i+1];
370 if (from_code == CODE_SJIS &&
371 SJIS_FIRST_P(name[i]) &&
372 SJIS_SECOND_P(name[i+1])) {
375 if (to_code != CODE_EUC) {
380 c1 = (unsigned char)name[i];
381 c2 = (unsigned char)name[i+1];
388 #endif /* MULTIBYTE_FILENAME */
392 /* transpose from_delim to to_delim */
394 if ((ptr = strchr(from_delim, name[i])) != NULL) {
395 name[i] = to_delim[ptr - from_delim];
400 if (case_to == TO_UPPER && islower(name[i])) {
401 name[i] = toupper(name[i]);
404 if (case_to == TO_LOWER && isupper(name[i])) {
405 name[i] = tolower(name[i]);
410 #ifdef MULTIBYTE_FILENAME
411 if (to_code_save == CODE_CAP) {
412 len = sjis_to_cap(tmp, name, sizeof(tmp));
413 strncpy(name, tmp, size);
417 #endif /* MULTIBYTE_FILENAME */
421 * Generic (MS-DOS style) time stamp format (localtime):
423 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
424 * |<---- year-1980 --->|<- month ->|<--- day ---->|
426 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
427 * |<--- hour --->|<---- minute --->|<- second/2 ->|
432 generic_to_unix_stamp(t)
437 #define subbits(n, off, len) (((n) >> (off)) & ((1 << (len))-1))
439 tm.tm_sec = subbits(t, 0, 5) * 2;
440 tm.tm_min = subbits(t, 5, 6);
441 tm.tm_hour = subbits(t, 11, 5);
442 tm.tm_mday = subbits(t, 16, 5);
443 tm.tm_mon = subbits(t, 21, 4) - 1;
444 tm.tm_year = subbits(t, 25, 7) + 80;
450 return timelocal(&tm);
455 unix_to_generic_stamp(t)
458 struct tm *tm = localtime(&t);
463 return ((long)(tm->tm_year << 25) +
465 (tm->tm_mday << 16) +
466 (tm->tm_hour << 11) +
472 wintime_to_unix_stamp()
476 uint64_t epoch = ((uint64_t)0x019db1de << 32) + 0xd53e8000;
477 /* 0x019db1ded53e8000ULL: 1970-01-01 00:00:00 (UTC) */
479 t = (unsigned long)get_longword();
480 t |= (uint64_t)(unsigned long)get_longword() << 32;
481 t = (t - epoch) / 10000000;
485 unsigned long t, q, x;
486 unsigned long wintime[8];
487 unsigned long epoch[8] = {0x01,0x9d,0xb1,0xde, 0xd5,0x3e,0x80,0x00};
488 /* 1970-01-01 00:00:00 (UTC) */
489 /* wintime -= epoch */
491 for (i = 7; i >= 0; i--) {
492 wintime[i] = (unsigned)get_byte() - epoch[i] - borrow;
493 borrow = (wintime[i] > 0xff) ? 1 : 0;
497 /* q = wintime / 10000000 */
499 x = 10000000; /* x: 24bit */
500 for (i = 0; i < 8; i++) {
501 t = (t << 8) + wintime[i]; /* 24bit + 8bit. t must be 32bit variable */
502 q <<= 8; /* q must be 32bit (time_t) */
514 * --------------------------------
516 * 2 or 4 next-header size [*1]
517 * --------------------------------------
518 * ext header: 1 ext-type ^
519 * ? contents | [*1] next-header size
520 * 2 or 4 next-header size v
521 * --------------------------------------
523 * on level 1, 2 header:
524 * size field is 2 bytes
526 * size field is 4 bytes
530 get_extended_header(fp, hdr, header_size, hcrc)
536 char data[LZHEADER_STORAGE];
538 char dirname[FILENAME_LENGTH];
541 ssize_t whole_size = header_size;
543 int n = 1 + hdr->size_field_length; /* `ext-type' + `next-header size' */
545 if (hdr->header_level == 0)
548 name_length = strlen(hdr->name);
550 while (header_size) {
552 if (verbose_listing && verbose > 1)
556 if (sizeof(data) < header_size) {
557 error("header size (%ld) too large.", header_size);
561 if (fread(data, header_size, 1, fp) == 0) {
562 error("Invalid header (LHa file ?)");
566 ext_type = get_byte();
570 if (verbose_listing && verbose > 1) printf(" < header crc >\n");
572 /* header crc (CRC-16) */
573 hdr->header_crc = get_word();
574 /* clear buffer for CRC calculation. */
575 data[1] = data[2] = 0;
576 skip_bytes(header_size - n - 2);
580 if (verbose_listing && verbose > 1) printf(" < filename >\n");
584 get_bytes(hdr->name, header_size-n, sizeof(hdr->name)-1);
585 hdr->name[name_length] = 0;
589 if (verbose_listing && verbose > 1) printf(" < directory >\n");
592 dir_length = get_bytes(dirname, header_size-n, sizeof(dirname)-1);
593 dirname[dir_length] = 0;
597 if (verbose_listing && verbose > 1) printf(" < MS-DOS attribute >\n");
599 /* MS-DOS attribute */
600 hdr->attribute = get_word();
604 if (verbose_listing && verbose > 1) printf(" < Windows time stamp (FILETIME) >\n");
606 /* Windows time stamp (FILETIME structure) */
607 /* it is time in 100 nano seconds since 1601-01-01 00:00:00 */
609 skip_bytes(8); /* create time is ignored */
611 /* set last modified time */
612 if (hdr->header_level >= 2)
613 skip_bytes(8); /* time_t has been already set */
615 hdr->unix_last_modified_stamp = wintime_to_unix_stamp();
617 skip_bytes(8); /* last access time is ignored */
622 if (verbose_listing && verbose > 1) printf(" < 64bits file size header >\n");
625 /* 64bits file size header (UNLHA32 extension) */
626 hdr->packed_size = get_longlongword();
627 hdr->original_size = get_longlongword();
636 if (verbose_listing && verbose > 1) printf(" < UNIX permission >\n");
638 /* UNIX permission */
639 hdr->unix_mode = get_word();
643 if (verbose_listing && verbose > 1) printf(" < UNIX gid and uid >\n");
645 /* UNIX gid and uid */
646 hdr->unix_gid = get_word();
647 hdr->unix_uid = get_word();
651 if (verbose_listing && verbose > 1) printf(" < UNIX group name >\n");
653 /* UNIX group name */
654 i = get_bytes(hdr->group, header_size-n, sizeof(hdr->group)-1);
655 hdr->group[i] = '\0';
659 if (verbose_listing && verbose > 1) printf(" < UNIX user name >\n");
662 i = get_bytes(hdr->user, header_size-n, sizeof(hdr->user)-1);
667 if (verbose_listing && verbose > 1) printf(" < UNIX last modifed time (time_t) >\n");
669 /* UNIX last modified time */
670 hdr->unix_last_modified_stamp = (time_t) get_longword();
674 /* 0x39: multi-disk header
675 0x3f: uncompressed comment
676 0x42: 64bit large file size
677 0x48-0x4f(?): reserved for authenticity verification
679 0x7e: extended attribute - platform information
680 0x7f: extended attribute - permission, owner-id and timestamp
682 0xc4: compressed comment (dict size: 4096)
683 0xc5: compressed comment (dict size: 8192)
684 0xc6: compressed comment (dict size: 16384)
685 0xc7: compressed comment (dict size: 32768)
686 0xc8: compressed comment (dict size: 65536)
687 0xd0-0xdf(?): operating systemm specific information
688 0xfc: encapsulation (another opinion)
689 0xfe: extended attribute - platform information(another opinion)
690 0xff: extended attribute - permission, owner-id and timestamp
691 (level 3 on UNLHA32) */
693 warning("unknown extended header 0x%02x", ext_type);
694 skip_bytes(header_size - n);
699 *hcrc = calccrc(*hcrc, data, header_size);
701 if (hdr->size_field_length == 2)
702 whole_size += header_size = get_word();
704 whole_size += header_size = get_longword();
707 /* concatenate dirname and filename */
709 if (name_length + dir_length >= sizeof(hdr->name)) {
710 warning("the length of pathname \"%s%s\" is too long.",
712 name_length = sizeof(hdr->name) - dir_length - 1;
713 hdr->name[name_length] = 0;
715 strcat(dirname, hdr->name); /* ok */
716 strcpy(hdr->name, dirname); /* ok */
717 name_length += dir_length;
723 #define I_HEADER_SIZE 0 /* level 0,1,2 */
724 #define I_HEADER_CHECKSUM 1 /* level 0,1 */
725 #define I_METHOD 2 /* level 0,1,2,3 */
726 #define I_PACKED_SIZE 7 /* level 0,1,2,3 */
727 #define I_ATTRIBUTE 19 /* level 0,1,2,3 */
728 #define I_HEADER_LEVEL 20 /* level 0,1,2,3 */
730 #define COMMON_HEADER_SIZE 21 /* size of common part */
732 #define I_GENERIC_HEADER_SIZE 24 /* + name_length */
733 #define I_LEVEL0_HEADER_SIZE 36 /* + name_length (unix extended) */
734 #define I_LEVEL1_HEADER_SIZE 27 /* + name_length */
735 #define I_LEVEL2_HEADER_SIZE 26 /* + padding */
736 #define I_LEVEL3_HEADER_SIZE 32
742 * offset size field name
743 * ----------------------------------
744 * 0 1 header size [*1]
746 * ---------------------------------------
748 * 7 4 packed size [*2] |
749 * 11 4 original size |
752 * 19 1 attribute | [*1] header size (X+Y+22)
753 * 20 1 level (0x00 fixed) |
756 * X +22 2 file crc (CRC-16) |
757 * X +24 Y ext-header(old style) v
758 * -------------------------------------------------
760 * : | [*2] packed size
762 * -------------------------------------------------
764 * ext-header(old style)
778 * bit6 archive bit (need to backup)
782 get_header_level0(fp, hdr, data)
793 hdr->size_field_length = 2; /* in bytes */
794 hdr->header_size = header_size = get_byte();
795 checksum = get_byte();
797 if (fread(data + COMMON_HEADER_SIZE,
798 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
799 error("Invalid header (LHarc file ?)");
800 return FALSE; /* finish */
803 if (calc_sum(data + I_METHOD, header_size) != checksum) {
804 error("Checksum error (LHarc file?)");
808 get_bytes(hdr->method, 5, sizeof(hdr->method));
809 hdr->packed_size = (unsigned long)get_longword();
810 hdr->original_size = (unsigned long)get_longword();
811 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
812 hdr->attribute = get_byte(); /* MS-DOS attribute */
813 hdr->header_level = get_byte();
814 name_length = get_byte();
815 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
818 /* defaults for other type */
819 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
823 extend_size = header_size+2 - name_length - 24;
825 if (extend_size < 0) {
826 if (extend_size == -2) {
827 /* CRC field is not given */
828 hdr->extend_type = EXTEND_GENERIC;
829 hdr->has_crc = FALSE;
834 error("Unkonwn header (lha file?)");
839 hdr->crc = get_word();
841 if (extend_size == 0)
844 hdr->extend_type = get_byte();
847 if (hdr->extend_type == EXTEND_UNIX) {
848 if (extend_size >= 11) {
849 hdr->minor_version = get_byte();
850 hdr->unix_last_modified_stamp = (time_t) get_longword();
851 hdr->unix_mode = get_word();
852 hdr->unix_uid = get_word();
853 hdr->unix_gid = get_word();
856 hdr->extend_type = EXTEND_GENERIC;
860 skip_bytes(extend_size);
862 hdr->header_size += 2;
870 * offset size field name
871 * -----------------------------------
872 * 0 1 header size [*1]
874 * -------------------------------------
876 * 7 4 skip size [*2] |
877 * 11 4 original size |
880 * 19 1 attribute (0x20 fixed) | [*1] header size (X+Y+25)
881 * 20 1 level (0x01 fixed) |
884 * X+ 22 2 file crc (CRC-16) |
887 * X+Y+25 2 next-header size v
888 * -------------------------------------------------
889 * X+Y+27 Z ext-header ^
891 * ----------------------------------- | [*2] skip size
894 * -------------------------------------------------
898 get_header_level1(fp, hdr, data)
909 hdr->size_field_length = 2; /* in bytes */
910 hdr->header_size = header_size = get_byte();
911 checksum = get_byte();
913 if (fread(data + COMMON_HEADER_SIZE,
914 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
915 error("Invalid header (LHarc file ?)");
916 return FALSE; /* finish */
919 if (calc_sum(data + I_METHOD, header_size) != checksum) {
920 error("Checksum error (LHarc file?)");
924 get_bytes(hdr->method, 5, sizeof(hdr->method));
925 hdr->packed_size = (unsigned long)get_longword(); /* skip size */
926 hdr->original_size = (unsigned long)get_longword();
927 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
928 hdr->attribute = get_byte(); /* 0x20 fixed */
929 hdr->header_level = get_byte();
931 name_length = get_byte();
932 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
935 /* defaults for other type */
936 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
941 hdr->crc = get_word();
942 hdr->extend_type = get_byte();
944 dummy = header_size+2 - name_length - I_LEVEL1_HEADER_SIZE;
946 skip_bytes(dummy); /* skip old style extend header */
948 extend_size = get_word();
949 extend_size = get_extended_header(fp, hdr, extend_size, 0);
950 if (extend_size == -1)
953 /* On level 1 header, size fields should be adjusted. */
954 /* the `packed_size' field contains the extended header size. */
955 /* the `header_size' field does not. */
956 hdr->packed_size -= extend_size;
957 hdr->header_size += extend_size + 2;
966 * offset size field name
967 * --------------------------------------------------
968 * 0 2 total header size [*1] ^
969 * ----------------------- |
971 * 7 4 packed size [*2] |
972 * 11 4 original size |
974 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
975 * 20 1 level (0x02 fixed) | (X+26+(1))
976 * 21 2 file crc (CRC-16) |
978 * 24 2 next-header size |
979 * ----------------------------------- |
982 * ----------------------------------- |
983 * X +26 (1) padding v
984 * -------------------------------------------------
986 * : | [*2] packed size
988 * -------------------------------------------------
992 get_header_level2(fp, hdr, data)
1002 hdr->size_field_length = 2; /* in bytes */
1003 hdr->header_size = header_size = get_word();
1005 if (fread(data + COMMON_HEADER_SIZE,
1006 I_LEVEL2_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
1007 error("Invalid header (LHarc file ?)");
1008 return FALSE; /* finish */
1011 get_bytes(hdr->method, 5, sizeof(hdr->method));
1012 hdr->packed_size = (unsigned long)get_longword();
1013 hdr->original_size = (unsigned long)get_longword();
1014 hdr->unix_last_modified_stamp = get_longword();
1015 hdr->attribute = get_byte(); /* reserved */
1016 hdr->header_level = get_byte();
1018 /* defaults for other type */
1019 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1023 hdr->has_crc = TRUE;
1024 hdr->crc = get_word();
1025 hdr->extend_type = get_byte();
1026 extend_size = get_word();
1028 INITIALIZE_CRC(hcrc);
1029 hcrc = calccrc(hcrc, data, get_ptr - data);
1031 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1032 if (extend_size == -1)
1035 padding = header_size - I_LEVEL2_HEADER_SIZE - extend_size;
1036 while (padding--) /* padding should be 0 or 1 */
1037 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1039 if (hdr->header_crc != hcrc)
1040 error("header CRC error");
1049 * offset size field name
1050 * --------------------------------------------------
1051 * 0 2 size field length (4 fixed) ^
1053 * 7 4 packed size [*2] |
1054 * 11 4 original size |
1056 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
1057 * 20 1 level (0x03 fixed) | (X+32)
1058 * 21 2 file crc (CRC-16) |
1060 * 24 4 total header size [*1] |
1061 * 28 4 next-header size |
1062 * ----------------------------------- |
1065 * -------------------------------------------------
1067 * : | [*2] packed size
1069 * -------------------------------------------------
1073 get_header_level3(fp, hdr, data)
1079 ssize_t extend_size;
1083 hdr->size_field_length = get_word();
1085 if (fread(data + COMMON_HEADER_SIZE,
1086 I_LEVEL3_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
1087 error("Invalid header (LHarc file ?)");
1088 return FALSE; /* finish */
1091 get_bytes(hdr->method, 5, sizeof(hdr->method));
1092 hdr->packed_size = (unsigned long)get_longword();
1093 hdr->original_size = (unsigned long)get_longword();
1094 hdr->unix_last_modified_stamp = get_longword();
1095 hdr->attribute = get_byte(); /* reserved */
1096 hdr->header_level = get_byte();
1098 /* defaults for other type */
1099 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1103 hdr->has_crc = TRUE;
1104 hdr->crc = get_word();
1105 hdr->extend_type = get_byte();
1106 hdr->header_size = header_size = get_longword();
1107 extend_size = get_longword();
1109 INITIALIZE_CRC(hcrc);
1110 hcrc = calccrc(hcrc, data, get_ptr - data);
1112 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1113 if (extend_size == -1)
1116 padding = header_size - I_LEVEL3_HEADER_SIZE - extend_size;
1117 while (padding--) /* padding should be 0 */
1118 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1120 if (hdr->header_crc != hcrc)
1121 error("header CRC error");
1131 char data[LZHEADER_STORAGE];
1133 int archive_kanji_code = CODE_SJIS;
1134 int system_kanji_code = default_system_kanji_code;
1135 char *archive_delim = "\377\\"; /* `\' is for level 0 header and
1137 char *system_delim = "//";
1138 int filename_case = NONE;
1141 memset(hdr, 0, sizeof(LzHeader));
1145 if ((end_mark = getc(fp)) == EOF || end_mark == 0) {
1146 return FALSE; /* finish */
1150 if (fread(data + 1, COMMON_HEADER_SIZE - 1, 1, fp) == 0) {
1151 error("Invalid header (LHarc file ?)");
1152 return FALSE; /* finish */
1155 switch (data[I_HEADER_LEVEL]) {
1157 if (get_header_level0(fp, hdr, data) == FALSE)
1161 if (get_header_level1(fp, hdr, data) == FALSE)
1165 if (get_header_level2(fp, hdr, data) == FALSE)
1169 if (get_header_level3(fp, hdr, data) == FALSE)
1173 error("Unknown level header (level %d)", data[I_HEADER_LEVEL]);
1177 /* filename conversion */
1178 switch (hdr->extend_type) {
1180 filename_case = convertcase ? TO_LOWER : NONE;
1187 filename_case = NONE;
1191 archive_delim = "\377/:\\";
1192 /* `\' is for level 0 header and broken archive. */
1193 system_delim = "/://";
1194 filename_case = NONE;
1198 filename_case = convertcase ? TO_LOWER : NONE;
1202 if (optional_archive_kanji_code)
1203 archive_kanji_code = optional_archive_kanji_code;
1204 if (optional_system_kanji_code)
1205 system_kanji_code = optional_system_kanji_code;
1206 if (optional_archive_delim)
1207 archive_delim = optional_archive_delim;
1208 if (optional_system_delim)
1209 system_delim = optional_system_delim;
1210 if (optional_filename_case)
1211 filename_case = optional_filename_case;
1213 /* kanji code and delimiter conversion */
1214 convert_filename(hdr->name, strlen(hdr->name), sizeof(hdr->name),
1217 archive_delim, system_delim, filename_case);
1219 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1221 /* split symbolic link */
1222 p = strchr(hdr->name, '|');
1224 /* hdr->name is symbolic link name */
1225 /* hdr->realname is real name */
1227 strcpy(hdr->realname, p+1); /* ok */
1230 error("unknown symlink name \"%s\"", hdr->name);
1236 /* skip SFX header */
1241 unsigned char buffer[64 * 1024]; /* max seek size */
1245 n = fread(buffer, 1, sizeof(buffer), fp);
1247 for (p = buffer; p < buffer + n; p++) {
1248 if (! (p[I_METHOD]=='-' && p[I_METHOD+1]=='l' && p[I_METHOD+4]=='-'))
1250 /* found "-l??-" keyword (as METHOD type string) */
1252 /* level 0 or 1 header */
1253 if ((p[I_HEADER_LEVEL] == 0 || p[I_HEADER_LEVEL] == 1)
1254 && p[I_HEADER_SIZE] > 20
1255 && p[I_HEADER_CHECKSUM] == calc_sum(p+2, p[I_HEADER_SIZE])) {
1256 if (fseeko(fp, (p - buffer) - n, SEEK_CUR) == -1)
1257 fatal_error("cannot seek header");
1261 /* level 2 header */
1262 if (p[I_HEADER_LEVEL] == 2
1263 && p[I_HEADER_SIZE] >= 24
1264 && p[I_ATTRIBUTE] == 0x20) {
1265 if (fseeko(fp, (p - buffer) - n, SEEK_CUR) == -1)
1266 fatal_error("cannot seek header");
1271 if (fseeko(fp, -n, SEEK_CUR) == -1)
1272 fatal_error("cannot seek header");
1277 /* remove leading `xxxx/..' */
1279 remove_leading_dots(char *path)
1284 if (strcmp(first, "..") == 0) {
1285 warning("Removing leading `..' from member name.");
1286 return first+1; /* change to "." */
1289 if (strstr(first, "..") == 0)
1292 while (path && *path) {
1294 if (strcmp(path, "..") == 0)
1295 ptr = path = path+2;
1296 else if (strncmp(path, "../", 3) == 0)
1297 ptr = path = path+3;
1299 path = strchr(path, '/');
1301 if (path && *path == '/') {
1307 warning("Removing leading `%.*s' from member name.", ptr-first, first);
1315 copy_path_element(char *dst, const char *src, int size)
1319 if (size < 1) return 0;
1321 for (i = 0; i < size; i++) {
1325 if (dst[i] == '/') {
1339 remove leading "xxx/../"
1340 remove "./", "././", "././ ... ./"
1341 remove duplicated "/"
1344 canon_path(char *newpath, char *path, size_t size)
1348 path = remove_leading_dots(path);
1351 if (path[0] == '.' && path[1] == '/')
1355 len = copy_path_element(newpath, path, size);
1364 /* remove duplicated '/' */
1365 while (*path == '/') path++;
1368 /* When newpath is empty, set "." */
1370 strcpy(newpath, ".");
1374 return newpath - p; /* string length */
1378 init_header(name, v_stat, hdr)
1380 struct stat *v_stat;
1385 memset(hdr, 0, sizeof(LzHeader));
1387 /* the `method' member is rewrote by the encoding function.
1388 but need set for empty files */
1389 memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STORAGE);
1391 hdr->packed_size = 0;
1392 hdr->original_size = v_stat->st_size;
1393 hdr->attribute = GENERIC_ATTRIBUTE;
1394 hdr->header_level = header_level;
1396 len = canon_path(hdr->name, name, sizeof(hdr->name));
1399 hdr->extend_type = EXTEND_UNIX;
1400 hdr->unix_last_modified_stamp = v_stat->st_mtime;
1401 /* since 00:00:00 JAN.1.1970 */
1402 #ifdef NOT_COMPATIBLE_MODE
1403 /* Please need your modification in this space. */
1406 if (S_ISREG(v_stat->st_mode))
1407 hdr->unix_mode = hdr->unix_mode | UNIX_FILE_REGULAR;
1408 if (S_ISDIR(v_stat->st_mode))
1409 hdr->unix_mode = hdr->unix_mode | UNIX_FILE_DIRECTORY;
1410 if (S_ISLNK(v_stat->st_mode))
1411 hdr->unix_mode = hdr->unix_mode | UNIX_FILE_SYMLINK;
1412 if (v_stat->st_mode & S_IRUSR)
1413 hdr->unix_mode = hdr->unix_mode | UNIX_OWNER_READ_PERM;
1414 if (v_stat->st_mode & S_IRGRP)
1415 hdr->unix_mode = hdr->unix_mode | UNIX_GROUP_READ_PERM;
1416 if (v_stat->st_mode & S_IROTH)
1417 hdr->unix_mode = hdr->unix_mode | UNIX_OTHER_READ_PERM;
1418 if (v_stat->st_mode & S_IWUSR)
1419 hdr->unix_mode = hdr->unix_mode | UNIX_OWNER_WRITE_PERM;
1420 if (v_stat->st_mode & S_IWGRP)
1421 hdr->unix_mode = hdr->unix_mode | UNIX_GROUP_WRITE_PERM;
1422 if (v_stat->st_mode & S_IWOTH)
1423 hdr->unix_mode = hdr->unix_mode | UNIX_OTHER_WRITE_PERM;
1424 if (v_stat->st_mode & S_IXUSR)
1425 hdr->unix_mode = hdr->unix_mode | UNIX_OWNER_EXEC_PERM;
1426 if (v_stat->st_mode & S_IXGRP)
1427 hdr->unix_mode = hdr->unix_mode | UNIX_GROUP_EXEC_PERM;
1428 if (v_stat->st_mode & S_IXOTH)
1429 hdr->unix_mode = hdr->unix_mode | UNIX_OTHER_EXEC_PERM;
1430 if (v_stat->st_mode & S_ISUID)
1431 hdr->unix_mode = hdr->unix_mode | UNIX_SETUID;
1432 if (v_stat->st_mode & S_ISGID)
1433 hdr->unix_mode = hdr->unix_mode | UNIX_SETGID;
1434 #endif /* __DJGPP__ */
1436 hdr->unix_mode = v_stat->st_mode;
1439 hdr->unix_uid = v_stat->st_uid;
1440 hdr->unix_gid = v_stat->st_gid;
1442 #if INCLUDE_OWNER_NAME_IN_HEADER
1445 struct passwd *ent = getpwuid(hdr->unix_uid);
1448 strncpy(hdr->user, ent->pw_name, sizeof(hdr->user));
1449 if (hdr->user[sizeof(hdr->user)-1])
1450 hdr->user[sizeof(hdr->user)-1] = 0;
1456 struct group *ent = getgrgid(hdr->unix_gid);
1459 strncpy(hdr->group, ent->gr_name, sizeof(hdr->group));
1460 if (hdr->group[sizeof(hdr->group)-1])
1461 hdr->group[sizeof(hdr->group)-1] = 0;
1465 #endif /* INCLUDE_OWNER_NAME_IN_HEADER */
1466 if (is_directory(v_stat)) {
1467 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1468 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1469 hdr->original_size = 0;
1470 if (len > 0 && hdr->name[len - 1] != '/') {
1471 if (len < sizeof(hdr->name)-1)
1472 strcpy(&hdr->name[len++], "/"); /* ok */
1474 warning("the length of dirname \"%s\" is too long.",
1480 if (is_symlink(v_stat)) {
1481 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1482 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1483 hdr->original_size = 0;
1484 readlink(name, hdr->realname, sizeof(hdr->realname));
1490 write_unix_info(hdr)
1493 /* UNIX specific informations */
1495 put_word(5); /* size */
1496 put_byte(0x50); /* permission */
1497 put_word(hdr->unix_mode);
1499 put_word(7); /* size */
1500 put_byte(0x51); /* gid and uid */
1501 put_word(hdr->unix_gid);
1502 put_word(hdr->unix_uid);
1504 if (hdr->group[0]) {
1505 int len = strlen(hdr->group);
1506 put_word(len + 3); /* size */
1507 put_byte(0x52); /* group name */
1508 put_bytes(hdr->group, len);
1512 int len = strlen(hdr->user);
1513 put_word(len + 3); /* size */
1514 put_byte(0x53); /* user name */
1515 put_bytes(hdr->user, len);
1518 if (hdr->header_level == 1) {
1519 put_word(7); /* size */
1520 put_byte(0x54); /* time stamp */
1521 put_longword(hdr->unix_last_modified_stamp);
1526 write_header_level0(data, hdr, pathname)
1528 char *data, *pathname;
1535 memset(data, 0, LZHEADER_STORAGE);
1537 put_byte(0x00); /* header size */
1538 put_byte(0x00); /* check sum */
1539 put_bytes(hdr->method, 5);
1540 put_longword(hdr->packed_size);
1541 put_longword(hdr->original_size);
1542 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1543 put_byte(hdr->attribute);
1544 put_byte(hdr->header_level); /* level 0 */
1546 /* write pathname (level 0 header contains the directory part) */
1547 name_length = strlen(pathname);
1549 limit = 255 - I_GENERIC_HEADER_SIZE + 2;
1551 limit = 255 - I_LEVEL0_HEADER_SIZE + 2;
1553 if (name_length > limit) {
1554 warning("the length of pathname \"%s\" is too long.", pathname);
1555 name_length = limit;
1557 put_byte(name_length);
1558 put_bytes(pathname, name_length);
1561 if (generic_format) {
1562 header_size = I_GENERIC_HEADER_SIZE + name_length - 2;
1563 data[I_HEADER_SIZE] = header_size;
1564 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1566 /* write old-style extend header */
1567 put_byte(EXTEND_UNIX);
1568 put_byte(CURRENT_UNIX_MINOR_VERSION);
1569 put_longword(hdr->unix_last_modified_stamp);
1570 put_word(hdr->unix_mode);
1571 put_word(hdr->unix_uid);
1572 put_word(hdr->unix_gid);
1574 /* size of extended header is 12 */
1575 header_size = I_LEVEL0_HEADER_SIZE + name_length - 2;
1576 data[I_HEADER_SIZE] = header_size;
1577 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1580 return header_size + 2;
1584 write_header_level1(data, hdr, pathname)
1586 char *data, *pathname;
1588 int name_length, dir_length, limit;
1589 char *basename, *dirname;
1591 char *extend_header_top;
1592 size_t extend_header_size;
1594 basename = strrchr(pathname, LHA_PATHSEP);
1597 name_length = strlen(basename);
1599 dir_length = basename - dirname;
1602 basename = pathname;
1603 name_length = strlen(basename);
1609 memset(data, 0, LZHEADER_STORAGE);
1611 put_byte(0x00); /* header size */
1612 put_byte(0x00); /* check sum */
1613 put_bytes(hdr->method, 5);
1614 put_longword(hdr->packed_size);
1615 put_longword(hdr->original_size);
1616 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1618 put_byte(hdr->header_level); /* level 1 */
1620 /* level 1 header: write filename (basename only) */
1621 limit = 255 - I_LEVEL1_HEADER_SIZE + 2;
1622 if (name_length > limit) {
1623 put_byte(0); /* name length */
1626 put_byte(name_length);
1627 put_bytes(basename, name_length);
1635 put_byte(EXTEND_UNIX);
1637 /* write extend header from here. */
1639 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1640 header_size = extend_header_top - data - 2;
1642 /* write filename and dirname */
1644 if (name_length > limit) {
1645 put_word(name_length + 3); /* size */
1646 put_byte(0x01); /* filename */
1647 put_bytes(basename, name_length);
1650 if (dir_length > 0) {
1651 put_word(dir_length + 3); /* size */
1652 put_byte(0x02); /* dirname */
1653 put_bytes(dirname, dir_length);
1656 if (!generic_format)
1657 write_unix_info(hdr);
1659 put_word(0x0000); /* next header size */
1661 extend_header_size = put_ptr - extend_header_top;
1662 /* On level 1 header, the packed size field is contains the ext-header */
1663 hdr->packed_size += put_ptr - extend_header_top;
1665 /* put `skip size' */
1666 setup_put(data + I_PACKED_SIZE);
1667 put_longword(hdr->packed_size);
1669 data[I_HEADER_SIZE] = header_size;
1670 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1672 return header_size + extend_header_size + 2;
1676 write_header_level2(data, hdr, pathname)
1678 char *data, *pathname;
1680 int name_length, dir_length;
1681 char *basename, *dirname;
1683 char *extend_header_top;
1684 char *headercrc_ptr;
1687 basename = strrchr(pathname, LHA_PATHSEP);
1690 name_length = strlen(basename);
1692 dir_length = basename - dirname;
1695 basename = pathname;
1696 name_length = strlen(basename);
1702 memset(data, 0, LZHEADER_STORAGE);
1704 put_word(0x0000); /* header size */
1705 put_bytes(hdr->method, 5);
1706 put_longword(hdr->packed_size);
1707 put_longword(hdr->original_size);
1708 put_longword(hdr->unix_last_modified_stamp);
1710 put_byte(hdr->header_level); /* level 2 */
1717 put_byte(EXTEND_UNIX);
1719 /* write extend header from here. */
1721 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1723 /* write common header */
1726 headercrc_ptr = put_ptr;
1727 put_word(0x0000); /* header CRC */
1729 /* write filename and dirname */
1730 /* must have this header, even if the name_length is 0. */
1731 put_word(name_length + 3); /* size */
1732 put_byte(0x01); /* filename */
1733 put_bytes(basename, name_length);
1735 if (dir_length > 0) {
1736 put_word(dir_length + 3); /* size */
1737 put_byte(0x02); /* dirname */
1738 put_bytes(dirname, dir_length);
1741 if (!generic_format)
1742 write_unix_info(hdr);
1744 put_word(0x0000); /* next header size */
1746 header_size = put_ptr - data;
1747 if ((header_size & 0xff) == 0) {
1748 /* cannot put zero at the first byte on level 2 header. */
1749 /* adjust header size. */
1750 put_byte(0); /* padding */
1754 /* put header size */
1755 setup_put(data + I_HEADER_SIZE);
1756 put_word(header_size);
1758 /* put header CRC in extended header */
1759 INITIALIZE_CRC(hcrc);
1760 hcrc = calccrc(hcrc, data, (unsigned int) header_size);
1761 setup_put(headercrc_ptr);
1768 write_header(fp, hdr)
1773 char data[LZHEADER_STORAGE];
1775 int archive_kanji_code = CODE_SJIS;
1776 int system_kanji_code = default_system_kanji_code;
1777 char *archive_delim = "\377";
1778 char *system_delim = "/";
1779 int filename_case = NONE;
1780 char pathname[FILENAME_LENGTH];
1782 if (optional_archive_kanji_code)
1783 archive_kanji_code = optional_archive_kanji_code;
1784 if (optional_system_kanji_code)
1785 system_kanji_code = optional_system_kanji_code;
1787 if (generic_format && convertcase)
1788 filename_case = TO_UPPER;
1790 if (hdr->header_level == 0) {
1791 archive_delim = "\\";
1794 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1796 p = strchr(hdr->name, '|');
1798 error("symlink name \"%s\" contains '|' char. change it into '_'",
1802 if (xsnprintf(pathname, sizeof(pathname),
1803 "%s|%s", hdr->name, hdr->realname) == -1)
1804 error("file name is too long (%s -> %s)", hdr->name, hdr->realname);
1807 strncpy(pathname, hdr->name, sizeof(pathname));
1808 pathname[sizeof(pathname)-1] = 0;
1811 convert_filename(pathname, strlen(pathname), sizeof(pathname),
1814 system_delim, archive_delim, filename_case);
1816 switch (hdr->header_level) {
1818 header_size = write_header_level0(data, hdr, pathname);
1821 header_size = write_header_level1(data, hdr, pathname);
1824 header_size = write_header_level2(data, hdr, pathname);
1827 error("Unknown level header (level %d)", hdr->header_level);
1831 if (fwrite(data, header_size, 1, fp) == 0)
1832 fatal_error("Cannot write to temporary file");
1835 #if MULTIBYTE_FILENAME
1837 #if defined(__APPLE__) /* Added by Hiroto Sakai */
1839 #include <CoreFoundation/CFString.h>
1840 #include <CoreFoundation/CFStringEncodingExt.h>
1842 /* this is not need for Mac OS X v 10.2 later */
1844 kCFStringEncodingAllowLossyConversion = 1,
1845 kCFStringEncodingBasicDirectionLeftToRight = (1 << 1),
1846 kCFStringEncodingBasicDirectionRightToLeft = (1 << 2),
1847 kCFStringEncodingSubstituteCombinings = (1 << 3),
1848 kCFStringEncodingComposeCombinings = (1 << 4),
1849 kCFStringEncodingIgnoreCombinings = (1 << 5),
1850 kCFStringEncodingUseCanonical = (1 << 6),
1851 kCFStringEncodingUseHFSPlusCanonical = (1 << 7),
1852 kCFStringEncodingPrependBOM = (1 << 8),
1853 kCFStringEncodingDisableCorporateArea = (1 << 9),
1854 kCFStringEncodingASCIICompatibleConversion = (1 << 10),
1858 ConvertEncodingToUTF8(const char* inCStr,
1859 char* outUTF8Buffer,
1860 int outUTF8BufferLength,
1861 unsigned long scriptEncoding,
1862 unsigned long flags)
1864 unsigned long unicodeChars;
1865 unsigned long srcCharsUsed;
1866 unsigned long usedByteLen = 0;
1867 UniChar uniStr[512];
1868 unsigned long cfResult;
1870 cfResult = CFStringEncodingBytesToUnicode(scriptEncoding,
1878 if (cfResult == 0) {
1879 cfResult = CFStringEncodingUnicodeToBytes(kCFStringEncodingUTF8,
1884 (char*)outUTF8Buffer,
1885 outUTF8BufferLength - 1,
1887 outUTF8Buffer[usedByteLen] = '\0';
1894 ConvertUTF8ToEncoding(const char* inUTF8Buf,
1895 int inUTF8BufLength,
1896 char* outCStrBuffer,
1897 int outCStrBufferLength,
1898 unsigned long scriptEncoding,
1899 unsigned long flags)
1901 unsigned long unicodeChars;
1902 unsigned long srcCharsUsed;
1903 unsigned long usedByteLen = 0;
1904 UniChar uniStr[256];
1905 unsigned long cfResult;
1907 cfResult = CFStringEncodingBytesToUnicode(kCFStringEncodingUTF8,
1915 if (cfResult == 0) {
1916 cfResult = CFStringEncodingUnicodeToBytes(scriptEncoding,
1921 (char*)outCStrBuffer,
1922 outCStrBufferLength - 1,
1924 outCStrBuffer[usedByteLen] = '\0';
1934 ConvertEncodingByIconv(const char *src, char *dst, int dstsize,
1935 const char *srcEnc, const char *dstEnc)
1938 static char szTmpBuf[2048];
1944 dst_p = &szTmpBuf[0];
1945 iLen = (size_t)sizeof(szTmpBuf)-1;
1946 src_p = (char *)src;
1947 sLen = (size_t)strlen(src);
1948 memset(szTmpBuf, 0, sizeof(szTmpBuf));
1949 memset(dst, 0, dstsize);
1951 ic = iconv_open(dstEnc, srcEnc);
1952 if (ic == (iconv_t)-1) {
1953 error("iconv_open() failure: %s", strerror(errno));
1957 if (iconv(ic, &src_p, &sLen, &dst_p, &iLen) == (size_t)-1) {
1958 error("iconv() failure: %s", strerror(errno));
1963 strncpy(dst, szTmpBuf, dstsize);
1969 #endif /* defined(__APPLE__) */
1972 sjis_to_utf8(char *dst, const char *src, size_t dstsize)
1974 #if defined(__APPLE__)
1976 if (ConvertEncodingToUTF8(src, dst, dstsize,
1977 kCFStringEncodingDOSJapanese,
1978 kCFStringEncodingUseHFSPlusCanonical) == 0)
1981 if (ConvertEncodingByIconv(src, dst, dstsize, "SJIS", "UTF-8") != -1)
1984 error("not support utf-8 conversion");
1987 if (dstsize < 1) return dst;
1989 return strncpy(dst, src, dstsize-1);
1993 utf8_to_sjis(char *dst, const char *src, size_t dstsize)
1995 #if defined(__APPLE__)
1999 srclen = strlen(src);
2000 if (ConvertUTF8ToEncoding(src, srclen, dst, dstsize,
2001 kCFStringEncodingDOSJapanese,
2002 kCFStringEncodingUseHFSPlusCanonical) == 0)
2005 if (ConvertEncodingByIconv(src, dst, dstsize, "UTF-8", "SJIS") != -1)
2008 error("not support utf-8 conversion");
2011 if (dstsize < 1) return dst;
2013 return strncpy(dst, src, dstsize-1);
2017 * SJIS <-> EUC ÊÑ´¹´Ø¿ô
2018 * ¡ÖÆüËܸì¾ðÊó½èÍý¡× ¥½¥Õ¥È¥Ð¥ó¥¯(³ô)
2019 * ¤è¤êÈ´¿è(by Koji Arai)
2022 euc2sjis(int *p1, int *p2)
2024 unsigned char c1 = *p1 & 0x7f;
2025 unsigned char c2 = *p2 & 0x7f;
2026 int rowoff = c1 < 0x5f ? 0x70 : 0xb0;
2027 int celoff = c1 % 2 ? (c2 > 0x5f ? 0x20 : 0x1f) : 0x7e;
2028 *p1 = ((c1 + 1) >> 1) + rowoff;
2029 *p2 += celoff - 0x80;
2033 sjis2euc(int *p1, int *p2)
2035 unsigned char c1 = *p1;
2036 unsigned char c2 = *p2;
2037 int adjust = c2 < 0x9f;
2038 int rowoff = c1 < 0xa0 ? 0x70 : 0xb0;
2039 int celoff = adjust ? (c2 > 0x7f ? 0x20 : 0x1f) : 0x7e;
2040 *p1 = ((c1 - rowoff) << 1) - adjust;
2051 case '0': case '1': case '2': case '3': case '4':
2052 case '5': case '6': case '7': case '8': case '9':
2055 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2056 return c - 'a' + 10;
2058 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2059 return c - 'A' + 10;
2069 case 0: case 1: case 2: case 3: case 4:
2070 case 5: case 6: case 7: case 8: case 9:
2073 case 10: case 11: case 12: case 13: case 14: case 15:
2074 return c + 'a' - 10;
2082 cap_to_sjis(char *dst, const char *src, size_t dstsize)
2085 size_t len = strlen(src);
2088 for (i = j = 0; i < len && i < dstsize; i++) {
2089 if (src[i] != ':') {
2095 a = hex2int((unsigned char)src[i]);
2096 b = hex2int((unsigned char)src[i+1]);
2098 if (a == -1 || b == -1) {
2101 strncpy(dst+j, src+i, dstsize-j);
2108 dst[j++] = a * 16 + b;
2115 sjis_to_cap(char *dst, const char *src, size_t dstsize)
2118 size_t len = strlen(src);
2121 for (i = j = 0; i < len && i < dstsize; i++) {
2122 if (src[i] == ':') {
2123 strncpy(dst+j, ":3a", dstsize-j);
2128 if (isprint(src[i])) {
2133 if (j + 3 >= dstsize) {
2138 a = int2hex((unsigned char)src[i] / 16);
2139 b = int2hex((unsigned char)src[i] % 16);
2148 #endif /* MULTIBYTE_FILENAME */