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;
64 unsigned char *pc = (unsigned char*)p;
66 while (len--) sum += *pc++;
77 if (verbose_listing && verbose > 1)
78 printf("%02d %2d: ", get_ptr - start_ptr, 1);
80 if (verbose_listing && verbose > 1) {
82 printf("%d(0x%02x) '%c'\n", c, c, c);
84 printf("%d(0x%02x)\n", c, c);
94 if (verbose_listing && verbose > 1) {
95 printf("%02d %2d: ", get_ptr - start_ptr, len);
97 printf("0x%02x ", GET_BYTE());
98 printf("... ignored\n");
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);
140 if (verbose_listing && verbose > 1)
141 printf("%02d %2d: ", get_ptr - start_ptr, 4);
147 l = (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
149 if (verbose_listing && verbose > 1)
150 printf("%ld(0x%08lx)\n", l, l);
168 uint64_t b0, b1, b2, b3, b4, b5, b6, b7;
172 if (verbose_listing && verbose > 1)
173 printf("%02d %2d: ", get_ptr - start_ptr, 4);
184 l = (b7 << 24) + (b6 << 16) + (b5 << 8) + b4;
186 l |= (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
188 if (verbose_listing && verbose > 1)
189 printf("%lld(%#016llx)\n", l, l);
195 put_longlongword(uint64_t v)
209 get_bytes(buf, len, size)
216 if (verbose_listing && verbose > 1)
217 printf("%02d %2d: \"", get_ptr - start_ptr, len);
219 for (i = 0; i < len; i++) {
220 if (i < size) buf[i] = get_ptr[i];
222 if (verbose_listing && verbose > 1) {
224 printf("%c", buf[i]);
226 printf("\\x%02x", (unsigned char)buf[i]);
230 if (verbose_listing && verbose > 1)
233 for (i = 0; i < len && i < size; i++)
247 for (i = 0; i < len; i++)
251 /* added by Koji Arai */
253 convert_filename(name, len, size,
255 from_delim, to_delim,
258 int len; /* length of name */
259 int size; /* size of name buffer */
260 int from_code, to_code, case_to;
261 char *from_delim, *to_delim;
265 #ifdef MULTIBYTE_FILENAME
266 char tmp[FILENAME_LENGTH];
267 int to_code_save = NONE;
269 if (from_code == CODE_CAP) {
270 len = cap_to_sjis(tmp, name, sizeof(tmp));
271 strncpy(name, tmp, size);
274 from_code = CODE_SJIS;
277 if (to_code == CODE_CAP) {
278 to_code_save = CODE_CAP;
282 if (from_code == CODE_SJIS && to_code == CODE_UTF8) {
283 for (i = 0; i < len; i++) {
284 if (SJIS_FIRST_P(name[i]) && SJIS_SECOND_P(name[i+1]))
287 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
288 if (strchr(from_delim, name[i]))
292 sjis_to_utf8(tmp, name, sizeof(tmp));
293 strncpy(name, tmp, size);
296 for (i = 0; i < len; i++)
297 if (name[i] == '/') name[i] = LHA_PATHSEP;
298 from_code = CODE_UTF8;
300 else if (from_code == CODE_UTF8 && to_code == CODE_SJIS) {
301 for (i = 0; i < len; i++)
302 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
303 if ((unsigned char)name[i] == LHA_PATHSEP) name[i] = '/';
304 utf8_to_sjis(tmp, name, sizeof(tmp));
305 strncpy(name, tmp, size);
308 for (i = 0; i < len; i++)
309 if (name[i] == '/') name[i] = LHA_PATHSEP;
310 from_code = CODE_SJIS;
314 /* special case: if `name' has small lettter, not convert case. */
315 if (from_code == CODE_SJIS && case_to == TO_LOWER) {
316 for (i = 0; i < len; i++) {
317 #ifdef MULTIBYTE_FILENAME
318 if (SJIS_FIRST_P(name[i]) && SJIS_SECOND_P(name[i+1]))
322 if (islower(name[i])) {
329 for (i = 0; i < len; i ++) {
330 #ifdef MULTIBYTE_FILENAME
331 if (from_code == CODE_EUC &&
332 (unsigned char)name[i] == 0x8e) {
333 if (to_code != CODE_SJIS) {
339 memmove(name + i, name + i + 1, len - i);
343 if (from_code == CODE_SJIS && X0201_KANA_P(name[i])) {
344 if (to_code != CODE_EUC) {
348 if (len == size - 1) /* check overflow */
350 memmove(name+i+1, name+i, len-i);
356 if (from_code == CODE_EUC && (name[i] & 0x80) && (name[i+1] & 0x80)) {
358 if (to_code != CODE_SJIS) {
363 c1 = (unsigned char)name[i];
364 c2 = (unsigned char)name[i+1];
371 if (from_code == CODE_SJIS &&
372 SJIS_FIRST_P(name[i]) &&
373 SJIS_SECOND_P(name[i+1])) {
376 if (to_code != CODE_EUC) {
381 c1 = (unsigned char)name[i];
382 c2 = (unsigned char)name[i+1];
389 #endif /* MULTIBYTE_FILENAME */
393 /* transpose from_delim to to_delim */
395 if ((ptr = strchr(from_delim, name[i])) != NULL) {
396 name[i] = to_delim[ptr - from_delim];
401 if (case_to == TO_UPPER && islower(name[i])) {
402 name[i] = toupper(name[i]);
405 if (case_to == TO_LOWER && isupper(name[i])) {
406 name[i] = tolower(name[i]);
411 #ifdef MULTIBYTE_FILENAME
412 if (to_code_save == CODE_CAP) {
413 len = sjis_to_cap(tmp, name, sizeof(tmp));
414 strncpy(name, tmp, size);
418 #endif /* MULTIBYTE_FILENAME */
422 * Generic (MS-DOS style) time stamp format (localtime):
424 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
425 * |<---- year-1980 --->|<- month ->|<--- day ---->|
427 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
428 * |<--- hour --->|<---- minute --->|<- second/2 ->|
433 generic_to_unix_stamp(t)
438 #define subbits(n, off, len) (((n) >> (off)) & ((1 << (len))-1))
440 tm.tm_sec = subbits(t, 0, 5) * 2;
441 tm.tm_min = subbits(t, 5, 6);
442 tm.tm_hour = subbits(t, 11, 5);
443 tm.tm_mday = subbits(t, 16, 5);
444 tm.tm_mon = subbits(t, 21, 4) - 1;
445 tm.tm_year = subbits(t, 25, 7) + 80;
451 return timelocal(&tm);
456 unix_to_generic_stamp(t)
459 struct tm *tm = localtime(&t);
464 return ((long)(tm->tm_year << 25) +
466 (tm->tm_mday << 16) +
467 (tm->tm_hour << 11) +
473 wintime_to_unix_stamp()
477 uint64_t epoch = ((uint64_t)0x019db1de << 32) + 0xd53e8000;
478 /* 0x019db1ded53e8000ULL: 1970-01-01 00:00:00 (UTC) */
480 t = (unsigned long)get_longword();
481 t |= (uint64_t)(unsigned long)get_longword() << 32;
482 t = (t - epoch) / 10000000;
486 unsigned long t, q, x;
487 unsigned long wintime[8];
488 unsigned long epoch[8] = {0x01,0x9d,0xb1,0xde, 0xd5,0x3e,0x80,0x00};
489 /* 1970-01-01 00:00:00 (UTC) */
490 /* wintime -= epoch */
492 for (i = 7; i >= 0; i--) {
493 wintime[i] = (unsigned)get_byte() - epoch[i] - borrow;
494 borrow = (wintime[i] > 0xff) ? 1 : 0;
498 /* q = wintime / 10000000 */
500 x = 10000000; /* x: 24bit */
501 for (i = 0; i < 8; i++) {
502 t = (t << 8) + wintime[i]; /* 24bit + 8bit. t must be 32bit variable */
503 q <<= 8; /* q must be 32bit (time_t) */
515 * --------------------------------
517 * 2 or 4 next-header size [*1]
518 * --------------------------------------
519 * ext header: 1 ext-type ^
520 * ? contents | [*1] next-header size
521 * 2 or 4 next-header size v
522 * --------------------------------------
524 * on level 1, 2 header:
525 * size field is 2 bytes
527 * size field is 4 bytes
531 get_extended_header(fp, hdr, header_size, hcrc)
537 char data[LZHEADER_STORAGE];
539 char dirname[FILENAME_LENGTH];
542 ssize_t whole_size = header_size;
544 int n = 1 + hdr->size_field_length; /* `ext-type' + `next-header size' */
546 if (hdr->header_level == 0)
549 name_length = strlen(hdr->name);
551 while (header_size) {
553 if (verbose_listing && verbose > 1)
557 if (sizeof(data) < header_size) {
558 error("header size (%ld) too large.", header_size);
562 if (fread(data, header_size, 1, fp) == 0) {
563 error("Invalid header (LHa file ?)");
567 ext_type = get_byte();
571 if (verbose_listing && verbose > 1) printf(" < header crc >\n");
573 /* header crc (CRC-16) */
574 hdr->header_crc = get_word();
575 /* clear buffer for CRC calculation. */
576 data[1] = data[2] = 0;
577 skip_bytes(header_size - n - 2);
581 if (verbose_listing && verbose > 1) printf(" < filename >\n");
585 get_bytes(hdr->name, header_size-n, sizeof(hdr->name)-1);
586 hdr->name[name_length] = 0;
590 if (verbose_listing && verbose > 1) printf(" < directory >\n");
593 dir_length = get_bytes(dirname, header_size-n, sizeof(dirname)-1);
594 dirname[dir_length] = 0;
598 if (verbose_listing && verbose > 1) printf(" < MS-DOS attribute >\n");
600 /* MS-DOS attribute */
601 hdr->attribute = get_word();
605 if (verbose_listing && verbose > 1) printf(" < Windows time stamp (FILETIME) >\n");
607 /* Windows time stamp (FILETIME structure) */
608 /* it is time in 100 nano seconds since 1601-01-01 00:00:00 */
610 skip_bytes(8); /* create time is ignored */
612 /* set last modified time */
613 if (hdr->header_level >= 2)
614 skip_bytes(8); /* time_t has been already set */
616 hdr->unix_last_modified_stamp = wintime_to_unix_stamp();
618 skip_bytes(8); /* last access time is ignored */
623 if (verbose_listing && verbose > 1) printf(" < 64bits file size header >\n");
626 /* 64bits file size header (UNLHA32 extension) */
627 hdr->packed_size = get_longlongword();
628 hdr->original_size = get_longlongword();
637 if (verbose_listing && verbose > 1) printf(" < UNIX permission >\n");
639 /* UNIX permission */
640 hdr->unix_mode = get_word();
644 if (verbose_listing && verbose > 1) printf(" < UNIX gid and uid >\n");
646 /* UNIX gid and uid */
647 hdr->unix_gid = get_word();
648 hdr->unix_uid = get_word();
652 if (verbose_listing && verbose > 1) printf(" < UNIX group name >\n");
654 /* UNIX group name */
655 i = get_bytes(hdr->group, header_size-n, sizeof(hdr->group)-1);
656 hdr->group[i] = '\0';
660 if (verbose_listing && verbose > 1) printf(" < UNIX user name >\n");
663 i = get_bytes(hdr->user, header_size-n, sizeof(hdr->user)-1);
668 if (verbose_listing && verbose > 1) printf(" < UNIX last modifed time (time_t) >\n");
670 /* UNIX last modified time */
671 hdr->unix_last_modified_stamp = (time_t) get_longword();
675 /* 0x39: multi-disk header
676 0x3f: uncompressed comment
677 0x42: 64bit large file size
678 0x48-0x4f(?): reserved for authenticity verification
680 0x7e: extended attribute - platform information
681 0x7f: extended attribute - permission, owner-id and timestamp
683 0xc4: compressed comment (dict size: 4096)
684 0xc5: compressed comment (dict size: 8192)
685 0xc6: compressed comment (dict size: 16384)
686 0xc7: compressed comment (dict size: 32768)
687 0xc8: compressed comment (dict size: 65536)
688 0xd0-0xdf(?): operating systemm specific information
689 0xfc: encapsulation (another opinion)
690 0xfe: extended attribute - platform information(another opinion)
691 0xff: extended attribute - permission, owner-id and timestamp
692 (level 3 on UNLHA32) */
694 warning("unknown extended header 0x%02x", ext_type);
695 skip_bytes(header_size - n);
700 *hcrc = calccrc(*hcrc, data, header_size);
702 if (hdr->size_field_length == 2)
703 whole_size += header_size = get_word();
705 whole_size += header_size = get_longword();
708 /* concatenate dirname and filename */
710 if (name_length + dir_length >= sizeof(hdr->name)) {
711 warning("the length of pathname \"%s%s\" is too long.",
713 name_length = sizeof(hdr->name) - dir_length - 1;
714 hdr->name[name_length] = 0;
716 strcat(dirname, hdr->name); /* ok */
717 strcpy(hdr->name, dirname); /* ok */
718 name_length += dir_length;
724 #define I_HEADER_SIZE 0 /* level 0,1,2 */
725 #define I_HEADER_CHECKSUM 1 /* level 0,1 */
726 #define I_METHOD 2 /* level 0,1,2,3 */
727 #define I_PACKED_SIZE 7 /* level 0,1,2,3 */
728 #define I_ATTRIBUTE 19 /* level 0,1,2,3 */
729 #define I_HEADER_LEVEL 20 /* level 0,1,2,3 */
731 #define COMMON_HEADER_SIZE 21 /* size of common part */
733 #define I_GENERIC_HEADER_SIZE 24 /* + name_length */
734 #define I_LEVEL0_HEADER_SIZE 36 /* + name_length (unix extended) */
735 #define I_LEVEL1_HEADER_SIZE 27 /* + name_length */
736 #define I_LEVEL2_HEADER_SIZE 26 /* + padding */
737 #define I_LEVEL3_HEADER_SIZE 32
743 * offset size field name
744 * ----------------------------------
745 * 0 1 header size [*1]
747 * ---------------------------------------
749 * 7 4 packed size [*2] |
750 * 11 4 original size |
753 * 19 1 attribute | [*1] header size (X+Y+22)
754 * 20 1 level (0x00 fixed) |
757 * X +22 2 file crc (CRC-16) |
758 * X +24 Y ext-header(old style) v
759 * -------------------------------------------------
761 * : | [*2] packed size
763 * -------------------------------------------------
765 * ext-header(old style)
779 * bit6 archive bit (need to backup)
783 get_header_level0(fp, hdr, data)
794 hdr->size_field_length = 2; /* in bytes */
795 hdr->header_size = header_size = get_byte();
796 checksum = get_byte();
798 if (fread(data + COMMON_HEADER_SIZE,
799 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
800 error("Invalid header (LHarc file ?)");
801 return FALSE; /* finish */
804 if (calc_sum(data + I_METHOD, header_size) != checksum) {
805 error("Checksum error (LHarc file?)");
809 get_bytes(hdr->method, 5, sizeof(hdr->method));
810 hdr->packed_size = (unsigned long)get_longword();
811 hdr->original_size = (unsigned long)get_longword();
812 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
813 hdr->attribute = get_byte(); /* MS-DOS attribute */
814 hdr->header_level = get_byte();
815 name_length = get_byte();
816 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
819 /* defaults for other type */
820 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
824 extend_size = header_size+2 - name_length - 24;
826 if (extend_size < 0) {
827 if (extend_size == -2) {
828 /* CRC field is not given */
829 hdr->extend_type = EXTEND_GENERIC;
830 hdr->has_crc = FALSE;
835 error("Unkonwn header (lha file?)");
840 hdr->crc = get_word();
842 if (extend_size == 0)
845 hdr->extend_type = get_byte();
848 if (hdr->extend_type == EXTEND_UNIX) {
849 if (extend_size >= 11) {
850 hdr->minor_version = get_byte();
851 hdr->unix_last_modified_stamp = (time_t) get_longword();
852 hdr->unix_mode = get_word();
853 hdr->unix_uid = get_word();
854 hdr->unix_gid = get_word();
857 hdr->extend_type = EXTEND_GENERIC;
861 skip_bytes(extend_size);
863 hdr->header_size += 2;
871 * offset size field name
872 * -----------------------------------
873 * 0 1 header size [*1]
875 * -------------------------------------
877 * 7 4 skip size [*2] |
878 * 11 4 original size |
881 * 19 1 attribute (0x20 fixed) | [*1] header size (X+Y+25)
882 * 20 1 level (0x01 fixed) |
885 * X+ 22 2 file crc (CRC-16) |
888 * X+Y+25 2 next-header size v
889 * -------------------------------------------------
890 * X+Y+27 Z ext-header ^
892 * ----------------------------------- | [*2] skip size
895 * -------------------------------------------------
899 get_header_level1(fp, hdr, data)
910 hdr->size_field_length = 2; /* in bytes */
911 hdr->header_size = header_size = get_byte();
912 checksum = get_byte();
914 if (fread(data + COMMON_HEADER_SIZE,
915 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
916 error("Invalid header (LHarc file ?)");
917 return FALSE; /* finish */
920 if (calc_sum(data + I_METHOD, header_size) != checksum) {
921 error("Checksum error (LHarc file?)");
925 get_bytes(hdr->method, 5, sizeof(hdr->method));
926 hdr->packed_size = (unsigned long)get_longword(); /* skip size */
927 hdr->original_size = (unsigned long)get_longword();
928 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
929 hdr->attribute = get_byte(); /* 0x20 fixed */
930 hdr->header_level = get_byte();
932 name_length = get_byte();
933 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
936 /* defaults for other type */
937 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
942 hdr->crc = get_word();
943 hdr->extend_type = get_byte();
945 dummy = header_size+2 - name_length - I_LEVEL1_HEADER_SIZE;
947 skip_bytes(dummy); /* skip old style extend header */
949 extend_size = get_word();
950 extend_size = get_extended_header(fp, hdr, extend_size, 0);
951 if (extend_size == -1)
954 /* On level 1 header, size fields should be adjusted. */
955 /* the `packed_size' field contains the extended header size. */
956 /* the `header_size' field does not. */
957 hdr->packed_size -= extend_size;
958 hdr->header_size += extend_size + 2;
967 * offset size field name
968 * --------------------------------------------------
969 * 0 2 total header size [*1] ^
970 * ----------------------- |
972 * 7 4 packed size [*2] |
973 * 11 4 original size |
975 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
976 * 20 1 level (0x02 fixed) | (X+26+(1))
977 * 21 2 file crc (CRC-16) |
979 * 24 2 next-header size |
980 * ----------------------------------- |
983 * ----------------------------------- |
984 * X +26 (1) padding v
985 * -------------------------------------------------
987 * : | [*2] packed size
989 * -------------------------------------------------
993 get_header_level2(fp, hdr, data)
1003 hdr->size_field_length = 2; /* in bytes */
1004 hdr->header_size = header_size = get_word();
1006 if (fread(data + COMMON_HEADER_SIZE,
1007 I_LEVEL2_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
1008 error("Invalid header (LHarc file ?)");
1009 return FALSE; /* finish */
1012 get_bytes(hdr->method, 5, sizeof(hdr->method));
1013 hdr->packed_size = (unsigned long)get_longword();
1014 hdr->original_size = (unsigned long)get_longword();
1015 hdr->unix_last_modified_stamp = get_longword();
1016 hdr->attribute = get_byte(); /* reserved */
1017 hdr->header_level = get_byte();
1019 /* defaults for other type */
1020 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1024 hdr->has_crc = TRUE;
1025 hdr->crc = get_word();
1026 hdr->extend_type = get_byte();
1027 extend_size = get_word();
1029 INITIALIZE_CRC(hcrc);
1030 hcrc = calccrc(hcrc, data, get_ptr - data);
1032 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1033 if (extend_size == -1)
1036 padding = header_size - I_LEVEL2_HEADER_SIZE - extend_size;
1037 while (padding--) /* padding should be 0 or 1 */
1038 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1040 if (hdr->header_crc != hcrc)
1041 error("header CRC error");
1050 * offset size field name
1051 * --------------------------------------------------
1052 * 0 2 size field length (4 fixed) ^
1054 * 7 4 packed size [*2] |
1055 * 11 4 original size |
1057 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
1058 * 20 1 level (0x03 fixed) | (X+32)
1059 * 21 2 file crc (CRC-16) |
1061 * 24 4 total header size [*1] |
1062 * 28 4 next-header size |
1063 * ----------------------------------- |
1066 * -------------------------------------------------
1068 * : | [*2] packed size
1070 * -------------------------------------------------
1074 get_header_level3(fp, hdr, data)
1080 ssize_t extend_size;
1084 hdr->size_field_length = get_word();
1086 if (fread(data + COMMON_HEADER_SIZE,
1087 I_LEVEL3_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
1088 error("Invalid header (LHarc file ?)");
1089 return FALSE; /* finish */
1092 get_bytes(hdr->method, 5, sizeof(hdr->method));
1093 hdr->packed_size = (unsigned long)get_longword();
1094 hdr->original_size = (unsigned long)get_longword();
1095 hdr->unix_last_modified_stamp = get_longword();
1096 hdr->attribute = get_byte(); /* reserved */
1097 hdr->header_level = get_byte();
1099 /* defaults for other type */
1100 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1104 hdr->has_crc = TRUE;
1105 hdr->crc = get_word();
1106 hdr->extend_type = get_byte();
1107 hdr->header_size = header_size = get_longword();
1108 extend_size = get_longword();
1110 INITIALIZE_CRC(hcrc);
1111 hcrc = calccrc(hcrc, data, get_ptr - data);
1113 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1114 if (extend_size == -1)
1117 padding = header_size - I_LEVEL3_HEADER_SIZE - extend_size;
1118 while (padding--) /* padding should be 0 */
1119 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1121 if (hdr->header_crc != hcrc)
1122 error("header CRC error");
1132 char data[LZHEADER_STORAGE];
1134 int archive_kanji_code = CODE_SJIS;
1135 int system_kanji_code = default_system_kanji_code;
1136 char *archive_delim = "\377\\"; /* `\' is for level 0 header and
1138 char *system_delim = "//";
1139 int filename_case = NONE;
1142 memset(hdr, 0, sizeof(LzHeader));
1146 if ((end_mark = getc(fp)) == EOF || end_mark == 0) {
1147 return FALSE; /* finish */
1151 if (fread(data + 1, COMMON_HEADER_SIZE - 1, 1, fp) == 0) {
1152 error("Invalid header (LHarc file ?)");
1153 return FALSE; /* finish */
1156 switch (data[I_HEADER_LEVEL]) {
1158 if (get_header_level0(fp, hdr, data) == FALSE)
1162 if (get_header_level1(fp, hdr, data) == FALSE)
1166 if (get_header_level2(fp, hdr, data) == FALSE)
1170 if (get_header_level3(fp, hdr, data) == FALSE)
1174 error("Unknown level header (level %d)", data[I_HEADER_LEVEL]);
1178 /* filename conversion */
1179 switch (hdr->extend_type) {
1181 filename_case = convertcase ? TO_LOWER : NONE;
1188 filename_case = NONE;
1192 archive_delim = "\377/:\\";
1193 /* `\' is for level 0 header and broken archive. */
1194 system_delim = "/://";
1195 filename_case = NONE;
1199 filename_case = convertcase ? TO_LOWER : NONE;
1203 if (optional_archive_kanji_code)
1204 archive_kanji_code = optional_archive_kanji_code;
1205 if (optional_system_kanji_code)
1206 system_kanji_code = optional_system_kanji_code;
1207 if (optional_archive_delim)
1208 archive_delim = optional_archive_delim;
1209 if (optional_system_delim)
1210 system_delim = optional_system_delim;
1211 if (optional_filename_case)
1212 filename_case = optional_filename_case;
1214 /* kanji code and delimiter conversion */
1215 convert_filename(hdr->name, strlen(hdr->name), sizeof(hdr->name),
1218 archive_delim, system_delim, filename_case);
1220 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1222 /* split symbolic link */
1223 p = strchr(hdr->name, '|');
1225 /* hdr->name is symbolic link name */
1226 /* hdr->realname is real name */
1228 strcpy(hdr->realname, p+1); /* ok */
1231 error("unknown symlink name \"%s\"", hdr->name);
1237 /* skip SFX header */
1242 unsigned char buffer[64 * 1024]; /* max seek size */
1246 n = fread(buffer, 1, sizeof(buffer), fp);
1248 for (p = buffer; p < buffer + n; p++) {
1249 if (! (p[I_METHOD]=='-' &&
1250 (p[I_METHOD+1]=='l' || p[I_METHOD+1]=='p') &&
1251 p[I_METHOD+4]=='-'))
1253 /* found "-[lp]??-" keyword (as METHOD type string) */
1255 /* level 0 or 1 header */
1256 if ((p[I_HEADER_LEVEL] == 0 || p[I_HEADER_LEVEL] == 1)
1257 && p[I_HEADER_SIZE] > 20
1258 && p[I_HEADER_CHECKSUM] == calc_sum(p+2, p[I_HEADER_SIZE])) {
1259 if (fseeko(fp, (p - buffer) - n, SEEK_CUR) == -1)
1260 fatal_error("cannot seek header");
1264 /* level 2 header */
1265 if (p[I_HEADER_LEVEL] == 2
1266 && p[I_HEADER_SIZE] >= 24
1267 && p[I_ATTRIBUTE] == 0x20) {
1268 if (fseeko(fp, (p - buffer) - n, SEEK_CUR) == -1)
1269 fatal_error("cannot seek header");
1274 if (fseeko(fp, -n, SEEK_CUR) == -1)
1275 fatal_error("cannot seek header");
1280 /* remove leading `xxxx/..' */
1282 remove_leading_dots(char *path)
1287 if (strcmp(first, "..") == 0) {
1288 warning("Removing leading `..' from member name.");
1289 return first+1; /* change to "." */
1292 if (strstr(first, "..") == 0)
1295 while (path && *path) {
1297 if (strcmp(path, "..") == 0)
1298 ptr = path = path+2;
1299 else if (strncmp(path, "../", 3) == 0)
1300 ptr = path = path+3;
1302 path = strchr(path, '/');
1304 if (path && *path == '/') {
1310 warning("Removing leading `%.*s' from member name.", ptr-first, first);
1318 copy_path_element(char *dst, const char *src, int size)
1322 if (size < 1) return 0;
1324 for (i = 0; i < size; i++) {
1328 if (dst[i] == '/') {
1342 remove leading "xxx/../"
1343 remove "./", "././", "././ ... ./"
1344 remove duplicated "/"
1347 canon_path(char *newpath, char *path, size_t size)
1351 path = remove_leading_dots(path);
1354 if (path[0] == '.' && path[1] == '/')
1358 len = copy_path_element(newpath, path, size);
1367 /* remove duplicated '/' */
1368 while (*path == '/') path++;
1371 /* When newpath is empty, set "." */
1373 strcpy(newpath, ".");
1377 return newpath - p; /* string length */
1381 init_header(name, v_stat, hdr)
1383 struct stat *v_stat;
1388 memset(hdr, 0, sizeof(LzHeader));
1390 /* the `method' member is rewrote by the encoding function.
1391 but need set for empty files */
1392 memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STORAGE);
1394 hdr->packed_size = 0;
1395 hdr->original_size = v_stat->st_size;
1396 hdr->attribute = GENERIC_ATTRIBUTE;
1397 hdr->header_level = header_level;
1399 len = canon_path(hdr->name, name, sizeof(hdr->name));
1402 hdr->extend_type = EXTEND_UNIX;
1403 hdr->unix_last_modified_stamp = v_stat->st_mtime;
1404 /* since 00:00:00 JAN.1.1970 */
1405 #ifdef NOT_COMPATIBLE_MODE
1406 /* Please need your modification in this space. */
1409 if (S_ISREG(v_stat->st_mode))
1410 hdr->unix_mode = hdr->unix_mode | UNIX_FILE_REGULAR;
1411 if (S_ISDIR(v_stat->st_mode))
1412 hdr->unix_mode = hdr->unix_mode | UNIX_FILE_DIRECTORY;
1413 if (S_ISLNK(v_stat->st_mode))
1414 hdr->unix_mode = hdr->unix_mode | UNIX_FILE_SYMLINK;
1415 if (v_stat->st_mode & S_IRUSR)
1416 hdr->unix_mode = hdr->unix_mode | UNIX_OWNER_READ_PERM;
1417 if (v_stat->st_mode & S_IRGRP)
1418 hdr->unix_mode = hdr->unix_mode | UNIX_GROUP_READ_PERM;
1419 if (v_stat->st_mode & S_IROTH)
1420 hdr->unix_mode = hdr->unix_mode | UNIX_OTHER_READ_PERM;
1421 if (v_stat->st_mode & S_IWUSR)
1422 hdr->unix_mode = hdr->unix_mode | UNIX_OWNER_WRITE_PERM;
1423 if (v_stat->st_mode & S_IWGRP)
1424 hdr->unix_mode = hdr->unix_mode | UNIX_GROUP_WRITE_PERM;
1425 if (v_stat->st_mode & S_IWOTH)
1426 hdr->unix_mode = hdr->unix_mode | UNIX_OTHER_WRITE_PERM;
1427 if (v_stat->st_mode & S_IXUSR)
1428 hdr->unix_mode = hdr->unix_mode | UNIX_OWNER_EXEC_PERM;
1429 if (v_stat->st_mode & S_IXGRP)
1430 hdr->unix_mode = hdr->unix_mode | UNIX_GROUP_EXEC_PERM;
1431 if (v_stat->st_mode & S_IXOTH)
1432 hdr->unix_mode = hdr->unix_mode | UNIX_OTHER_EXEC_PERM;
1433 if (v_stat->st_mode & S_ISUID)
1434 hdr->unix_mode = hdr->unix_mode | UNIX_SETUID;
1435 if (v_stat->st_mode & S_ISGID)
1436 hdr->unix_mode = hdr->unix_mode | UNIX_SETGID;
1437 #endif /* __DJGPP__ */
1439 hdr->unix_mode = v_stat->st_mode;
1442 hdr->unix_uid = v_stat->st_uid;
1443 hdr->unix_gid = v_stat->st_gid;
1445 #if INCLUDE_OWNER_NAME_IN_HEADER
1448 struct passwd *ent = getpwuid(hdr->unix_uid);
1451 strncpy(hdr->user, ent->pw_name, sizeof(hdr->user));
1452 if (hdr->user[sizeof(hdr->user)-1])
1453 hdr->user[sizeof(hdr->user)-1] = 0;
1459 struct group *ent = getgrgid(hdr->unix_gid);
1462 strncpy(hdr->group, ent->gr_name, sizeof(hdr->group));
1463 if (hdr->group[sizeof(hdr->group)-1])
1464 hdr->group[sizeof(hdr->group)-1] = 0;
1468 #endif /* INCLUDE_OWNER_NAME_IN_HEADER */
1469 if (is_directory(v_stat)) {
1470 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1471 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1472 hdr->original_size = 0;
1473 if (len > 0 && hdr->name[len - 1] != '/') {
1474 if (len < sizeof(hdr->name)-1)
1475 strcpy(&hdr->name[len++], "/"); /* ok */
1477 warning("the length of dirname \"%s\" is too long.",
1483 if (is_symlink(v_stat)) {
1484 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1485 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1486 hdr->original_size = 0;
1487 readlink(name, hdr->realname, sizeof(hdr->realname));
1493 write_unix_info(hdr)
1496 /* UNIX specific informations */
1498 put_word(5); /* size */
1499 put_byte(0x50); /* permission */
1500 put_word(hdr->unix_mode);
1502 put_word(7); /* size */
1503 put_byte(0x51); /* gid and uid */
1504 put_word(hdr->unix_gid);
1505 put_word(hdr->unix_uid);
1507 if (hdr->group[0]) {
1508 int len = strlen(hdr->group);
1509 put_word(len + 3); /* size */
1510 put_byte(0x52); /* group name */
1511 put_bytes(hdr->group, len);
1515 int len = strlen(hdr->user);
1516 put_word(len + 3); /* size */
1517 put_byte(0x53); /* user name */
1518 put_bytes(hdr->user, len);
1521 if (hdr->header_level == 1) {
1522 put_word(7); /* size */
1523 put_byte(0x54); /* time stamp */
1524 put_longword(hdr->unix_last_modified_stamp);
1529 write_header_level0(data, hdr, pathname)
1531 char *data, *pathname;
1538 memset(data, 0, LZHEADER_STORAGE);
1540 put_byte(0x00); /* header size */
1541 put_byte(0x00); /* check sum */
1542 put_bytes(hdr->method, 5);
1543 put_longword(hdr->packed_size);
1544 put_longword(hdr->original_size);
1545 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1546 put_byte(hdr->attribute);
1547 put_byte(hdr->header_level); /* level 0 */
1549 /* write pathname (level 0 header contains the directory part) */
1550 name_length = strlen(pathname);
1552 limit = 255 - I_GENERIC_HEADER_SIZE + 2;
1554 limit = 255 - I_LEVEL0_HEADER_SIZE + 2;
1556 if (name_length > limit) {
1557 warning("the length of pathname \"%s\" is too long.", pathname);
1558 name_length = limit;
1560 put_byte(name_length);
1561 put_bytes(pathname, name_length);
1564 if (generic_format) {
1565 header_size = I_GENERIC_HEADER_SIZE + name_length - 2;
1566 data[I_HEADER_SIZE] = header_size;
1567 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1569 /* write old-style extend header */
1570 put_byte(EXTEND_UNIX);
1571 put_byte(CURRENT_UNIX_MINOR_VERSION);
1572 put_longword(hdr->unix_last_modified_stamp);
1573 put_word(hdr->unix_mode);
1574 put_word(hdr->unix_uid);
1575 put_word(hdr->unix_gid);
1577 /* size of extended header is 12 */
1578 header_size = I_LEVEL0_HEADER_SIZE + name_length - 2;
1579 data[I_HEADER_SIZE] = header_size;
1580 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1583 return header_size + 2;
1587 write_header_level1(data, hdr, pathname)
1589 char *data, *pathname;
1591 int name_length, dir_length, limit;
1592 char *basename, *dirname;
1594 char *extend_header_top;
1595 size_t extend_header_size;
1597 basename = strrchr(pathname, LHA_PATHSEP);
1600 name_length = strlen(basename);
1602 dir_length = basename - dirname;
1605 basename = pathname;
1606 name_length = strlen(basename);
1612 memset(data, 0, LZHEADER_STORAGE);
1614 put_byte(0x00); /* header size */
1615 put_byte(0x00); /* check sum */
1616 put_bytes(hdr->method, 5);
1617 put_longword(hdr->packed_size);
1618 put_longword(hdr->original_size);
1619 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1621 put_byte(hdr->header_level); /* level 1 */
1623 /* level 1 header: write filename (basename only) */
1624 limit = 255 - I_LEVEL1_HEADER_SIZE + 2;
1625 if (name_length > limit) {
1626 put_byte(0); /* name length */
1629 put_byte(name_length);
1630 put_bytes(basename, name_length);
1638 put_byte(EXTEND_UNIX);
1640 /* write extend header from here. */
1642 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1643 header_size = extend_header_top - data - 2;
1645 /* write filename and dirname */
1647 if (name_length > limit) {
1648 put_word(name_length + 3); /* size */
1649 put_byte(0x01); /* filename */
1650 put_bytes(basename, name_length);
1653 if (dir_length > 0) {
1654 put_word(dir_length + 3); /* size */
1655 put_byte(0x02); /* dirname */
1656 put_bytes(dirname, dir_length);
1659 if (!generic_format)
1660 write_unix_info(hdr);
1662 put_word(0x0000); /* next header size */
1664 extend_header_size = put_ptr - extend_header_top;
1665 /* On level 1 header, the packed size field is contains the ext-header */
1666 hdr->packed_size += put_ptr - extend_header_top;
1668 /* put `skip size' */
1669 setup_put(data + I_PACKED_SIZE);
1670 put_longword(hdr->packed_size);
1672 data[I_HEADER_SIZE] = header_size;
1673 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1675 return header_size + extend_header_size + 2;
1679 write_header_level2(data, hdr, pathname)
1681 char *data, *pathname;
1683 int name_length, dir_length;
1684 char *basename, *dirname;
1686 char *extend_header_top;
1687 char *headercrc_ptr;
1690 basename = strrchr(pathname, LHA_PATHSEP);
1693 name_length = strlen(basename);
1695 dir_length = basename - dirname;
1698 basename = pathname;
1699 name_length = strlen(basename);
1705 memset(data, 0, LZHEADER_STORAGE);
1707 put_word(0x0000); /* header size */
1708 put_bytes(hdr->method, 5);
1709 put_longword(hdr->packed_size);
1710 put_longword(hdr->original_size);
1711 put_longword(hdr->unix_last_modified_stamp);
1713 put_byte(hdr->header_level); /* level 2 */
1720 put_byte(EXTEND_UNIX);
1722 /* write extend header from here. */
1724 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1726 /* write common header */
1729 headercrc_ptr = put_ptr;
1730 put_word(0x0000); /* header CRC */
1732 /* write filename and dirname */
1733 /* must have this header, even if the name_length is 0. */
1734 put_word(name_length + 3); /* size */
1735 put_byte(0x01); /* filename */
1736 put_bytes(basename, name_length);
1738 if (dir_length > 0) {
1739 put_word(dir_length + 3); /* size */
1740 put_byte(0x02); /* dirname */
1741 put_bytes(dirname, dir_length);
1744 if (!generic_format)
1745 write_unix_info(hdr);
1747 put_word(0x0000); /* next header size */
1749 header_size = put_ptr - data;
1750 if ((header_size & 0xff) == 0) {
1751 /* cannot put zero at the first byte on level 2 header. */
1752 /* adjust header size. */
1753 put_byte(0); /* padding */
1757 /* put header size */
1758 setup_put(data + I_HEADER_SIZE);
1759 put_word(header_size);
1761 /* put header CRC in extended header */
1762 INITIALIZE_CRC(hcrc);
1763 hcrc = calccrc(hcrc, data, (unsigned int) header_size);
1764 setup_put(headercrc_ptr);
1771 write_header(fp, hdr)
1776 char data[LZHEADER_STORAGE];
1778 int archive_kanji_code = CODE_SJIS;
1779 int system_kanji_code = default_system_kanji_code;
1780 char *archive_delim = "\377";
1781 char *system_delim = "/";
1782 int filename_case = NONE;
1783 char pathname[FILENAME_LENGTH];
1785 if (optional_archive_kanji_code)
1786 archive_kanji_code = optional_archive_kanji_code;
1787 if (optional_system_kanji_code)
1788 system_kanji_code = optional_system_kanji_code;
1790 if (generic_format && convertcase)
1791 filename_case = TO_UPPER;
1793 if (hdr->header_level == 0) {
1794 archive_delim = "\\";
1797 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1799 p = strchr(hdr->name, '|');
1801 error("symlink name \"%s\" contains '|' char. change it into '_'",
1805 if (xsnprintf(pathname, sizeof(pathname),
1806 "%s|%s", hdr->name, hdr->realname) == -1)
1807 error("file name is too long (%s -> %s)", hdr->name, hdr->realname);
1810 strncpy(pathname, hdr->name, sizeof(pathname));
1811 pathname[sizeof(pathname)-1] = 0;
1814 convert_filename(pathname, strlen(pathname), sizeof(pathname),
1817 system_delim, archive_delim, filename_case);
1819 switch (hdr->header_level) {
1821 header_size = write_header_level0(data, hdr, pathname);
1824 header_size = write_header_level1(data, hdr, pathname);
1827 header_size = write_header_level2(data, hdr, pathname);
1830 error("Unknown level header (level %d)", hdr->header_level);
1834 if (fwrite(data, header_size, 1, fp) == 0)
1835 fatal_error("Cannot write to temporary file");
1838 #if MULTIBYTE_FILENAME
1840 #if defined(__APPLE__) /* Added by Hiroto Sakai */
1842 #include <CoreFoundation/CFString.h>
1843 #include <CoreFoundation/CFStringEncodingExt.h>
1845 /* this is not need for Mac OS X v 10.2 later */
1847 kCFStringEncodingAllowLossyConversion = 1,
1848 kCFStringEncodingBasicDirectionLeftToRight = (1 << 1),
1849 kCFStringEncodingBasicDirectionRightToLeft = (1 << 2),
1850 kCFStringEncodingSubstituteCombinings = (1 << 3),
1851 kCFStringEncodingComposeCombinings = (1 << 4),
1852 kCFStringEncodingIgnoreCombinings = (1 << 5),
1853 kCFStringEncodingUseCanonical = (1 << 6),
1854 kCFStringEncodingUseHFSPlusCanonical = (1 << 7),
1855 kCFStringEncodingPrependBOM = (1 << 8),
1856 kCFStringEncodingDisableCorporateArea = (1 << 9),
1857 kCFStringEncodingASCIICompatibleConversion = (1 << 10),
1861 ConvertEncodingToUTF8(const char* inCStr,
1862 char* outUTF8Buffer,
1863 int outUTF8BufferLength,
1864 unsigned long scriptEncoding,
1865 unsigned long flags)
1867 unsigned long unicodeChars;
1868 unsigned long srcCharsUsed;
1869 unsigned long usedByteLen = 0;
1870 UniChar uniStr[512];
1871 unsigned long cfResult;
1873 cfResult = CFStringEncodingBytesToUnicode(scriptEncoding,
1881 if (cfResult == 0) {
1882 cfResult = CFStringEncodingUnicodeToBytes(kCFStringEncodingUTF8,
1887 (char*)outUTF8Buffer,
1888 outUTF8BufferLength - 1,
1890 outUTF8Buffer[usedByteLen] = '\0';
1897 ConvertUTF8ToEncoding(const char* inUTF8Buf,
1898 int inUTF8BufLength,
1899 char* outCStrBuffer,
1900 int outCStrBufferLength,
1901 unsigned long scriptEncoding,
1902 unsigned long flags)
1904 unsigned long unicodeChars;
1905 unsigned long srcCharsUsed;
1906 unsigned long usedByteLen = 0;
1907 UniChar uniStr[256];
1908 unsigned long cfResult;
1910 cfResult = CFStringEncodingBytesToUnicode(kCFStringEncodingUTF8,
1918 if (cfResult == 0) {
1919 cfResult = CFStringEncodingUnicodeToBytes(scriptEncoding,
1924 (char*)outCStrBuffer,
1925 outCStrBufferLength - 1,
1927 outCStrBuffer[usedByteLen] = '\0';
1937 ConvertEncodingByIconv(const char *src, char *dst, int dstsize,
1938 const char *srcEnc, const char *dstEnc)
1941 static char szTmpBuf[2048];
1947 dst_p = &szTmpBuf[0];
1948 iLen = (size_t)sizeof(szTmpBuf)-1;
1949 src_p = (char *)src;
1950 sLen = (size_t)strlen(src);
1951 memset(szTmpBuf, 0, sizeof(szTmpBuf));
1952 memset(dst, 0, dstsize);
1954 ic = iconv_open(dstEnc, srcEnc);
1955 if (ic == (iconv_t)-1) {
1956 error("iconv_open() failure: %s", strerror(errno));
1960 if (iconv(ic, &src_p, &sLen, &dst_p, &iLen) == (size_t)-1) {
1961 error("iconv() failure: %s", strerror(errno));
1966 strncpy(dst, szTmpBuf, dstsize);
1972 #endif /* defined(__APPLE__) */
1975 sjis_to_utf8(char *dst, const char *src, size_t dstsize)
1977 #if defined(__APPLE__)
1979 if (ConvertEncodingToUTF8(src, dst, dstsize,
1980 kCFStringEncodingDOSJapanese,
1981 kCFStringEncodingUseHFSPlusCanonical) == 0)
1984 if (ConvertEncodingByIconv(src, dst, dstsize, "SJIS", "UTF-8") != -1)
1987 error("not support utf-8 conversion");
1990 if (dstsize < 1) return dst;
1992 return strncpy(dst, src, dstsize-1);
1996 utf8_to_sjis(char *dst, const char *src, size_t dstsize)
1998 #if defined(__APPLE__)
2002 srclen = strlen(src);
2003 if (ConvertUTF8ToEncoding(src, srclen, dst, dstsize,
2004 kCFStringEncodingDOSJapanese,
2005 kCFStringEncodingUseHFSPlusCanonical) == 0)
2008 if (ConvertEncodingByIconv(src, dst, dstsize, "UTF-8", "SJIS") != -1)
2011 error("not support utf-8 conversion");
2014 if (dstsize < 1) return dst;
2016 return strncpy(dst, src, dstsize-1);
2020 * SJIS <-> EUC ÊÑ´¹´Ø¿ô
2021 * ¡ÖÆüËܸì¾ðÊó½èÍý¡× ¥½¥Õ¥È¥Ð¥ó¥¯(³ô)
2022 * ¤è¤êÈ´¿è(by Koji Arai)
2025 euc2sjis(int *p1, int *p2)
2027 unsigned char c1 = *p1 & 0x7f;
2028 unsigned char c2 = *p2 & 0x7f;
2029 int rowoff = c1 < 0x5f ? 0x70 : 0xb0;
2030 int celoff = c1 % 2 ? (c2 > 0x5f ? 0x20 : 0x1f) : 0x7e;
2031 *p1 = ((c1 + 1) >> 1) + rowoff;
2032 *p2 += celoff - 0x80;
2036 sjis2euc(int *p1, int *p2)
2038 unsigned char c1 = *p1;
2039 unsigned char c2 = *p2;
2040 int adjust = c2 < 0x9f;
2041 int rowoff = c1 < 0xa0 ? 0x70 : 0xb0;
2042 int celoff = adjust ? (c2 > 0x7f ? 0x20 : 0x1f) : 0x7e;
2043 *p1 = ((c1 - rowoff) << 1) - adjust;
2054 case '0': case '1': case '2': case '3': case '4':
2055 case '5': case '6': case '7': case '8': case '9':
2058 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2059 return c - 'a' + 10;
2061 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2062 return c - 'A' + 10;
2072 case 0: case 1: case 2: case 3: case 4:
2073 case 5: case 6: case 7: case 8: case 9:
2076 case 10: case 11: case 12: case 13: case 14: case 15:
2077 return c + 'a' - 10;
2085 cap_to_sjis(char *dst, const char *src, size_t dstsize)
2088 size_t len = strlen(src);
2091 for (i = j = 0; i < len && i < dstsize; i++) {
2092 if (src[i] != ':') {
2098 a = hex2int((unsigned char)src[i]);
2099 b = hex2int((unsigned char)src[i+1]);
2101 if (a == -1 || b == -1) {
2104 strncpy(dst+j, src+i, dstsize-j);
2111 dst[j++] = a * 16 + b;
2118 sjis_to_cap(char *dst, const char *src, size_t dstsize)
2121 size_t len = strlen(src);
2124 for (i = j = 0; i < len && i < dstsize; i++) {
2125 if (src[i] == ':') {
2126 strncpy(dst+j, ":3a", dstsize-j);
2131 if (isprint(src[i])) {
2136 if (j + 3 >= dstsize) {
2141 a = int2hex((unsigned char)src[i] / 16);
2142 b = int2hex((unsigned char)src[i] % 16);
2151 #endif /* MULTIBYTE_FILENAME */