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;
283 /* special case: if `name' has small lettter, not convert case. */
284 if (from_code == CODE_SJIS && case_to == TO_LOWER) {
285 for (i = 0; i < len; i++) {
286 #ifdef MULTIBYTE_FILENAME
287 if (SJIS_FIRST_P(name[i]) && SJIS_SECOND_P(name[i+1]))
291 if (islower(name[i])) {
298 #ifdef MULTIBYTE_FILENAME
299 if (from_code == CODE_SJIS && to_code == CODE_UTF8) {
300 for (i = 0; i < len; i++) {
301 if (SJIS_FIRST_P(name[i]) && SJIS_SECOND_P(name[i+1]))
304 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
305 if (strchr(from_delim, name[i]))
309 sjis_to_utf8(tmp, name, sizeof(tmp));
310 strncpy(name, tmp, size);
313 for (i = 0; i < len; i++)
314 if (name[i] == '/') name[i] = LHA_PATHSEP;
315 from_code = CODE_UTF8;
317 else if (from_code == CODE_UTF8 && to_code == CODE_SJIS) {
318 for (i = 0; i < len; i++)
319 /* FIXME: provisionally fix for the Mac OS CoreFoundation */
320 if ((unsigned char)name[i] == LHA_PATHSEP) name[i] = '/';
321 utf8_to_sjis(tmp, name, sizeof(tmp));
322 strncpy(name, tmp, size);
325 for (i = 0; i < len; i++)
326 if (name[i] == '/') name[i] = LHA_PATHSEP;
327 from_code = CODE_SJIS;
331 for (i = 0; i < len; i ++) {
332 #ifdef MULTIBYTE_FILENAME
333 if (from_code == CODE_EUC &&
334 (unsigned char)name[i] == 0x8e) {
335 if (to_code != CODE_SJIS) {
341 memmove(name + i, name + i + 1, len - i);
345 if (from_code == CODE_SJIS && X0201_KANA_P(name[i])) {
346 if (to_code != CODE_EUC) {
350 if (len == size - 1) /* check overflow */
352 memmove(name+i+1, name+i, len-i);
358 if (from_code == CODE_EUC && (name[i] & 0x80) && (name[i+1] & 0x80)) {
360 if (to_code != CODE_SJIS) {
365 c1 = (unsigned char)name[i];
366 c2 = (unsigned char)name[i+1];
373 if (from_code == CODE_SJIS &&
374 SJIS_FIRST_P(name[i]) &&
375 SJIS_SECOND_P(name[i+1])) {
378 if (to_code != CODE_EUC) {
383 c1 = (unsigned char)name[i];
384 c2 = (unsigned char)name[i+1];
391 #endif /* MULTIBYTE_FILENAME */
395 /* transpose from_delim to to_delim */
397 if ((ptr = strchr(from_delim, name[i])) != NULL) {
398 name[i] = to_delim[ptr - from_delim];
403 if (case_to == TO_UPPER && islower(name[i])) {
404 name[i] = toupper(name[i]);
407 if (case_to == TO_LOWER && isupper(name[i])) {
408 name[i] = tolower(name[i]);
413 #ifdef MULTIBYTE_FILENAME
414 if (to_code_save == CODE_CAP) {
415 len = sjis_to_cap(tmp, name, sizeof(tmp));
416 strncpy(name, tmp, size);
420 #endif /* MULTIBYTE_FILENAME */
424 * Generic (MS-DOS style) time stamp format (localtime):
426 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
427 * |<---- year-1980 --->|<- month ->|<--- day ---->|
429 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
430 * |<--- hour --->|<---- minute --->|<- second/2 ->|
435 generic_to_unix_stamp(t)
440 #define subbits(n, off, len) (((n) >> (off)) & ((1 << (len))-1))
442 tm.tm_sec = subbits(t, 0, 5) * 2;
443 tm.tm_min = subbits(t, 5, 6);
444 tm.tm_hour = subbits(t, 11, 5);
445 tm.tm_mday = subbits(t, 16, 5);
446 tm.tm_mon = subbits(t, 21, 4) - 1;
447 tm.tm_year = subbits(t, 25, 7) + 80;
453 return timelocal(&tm);
458 unix_to_generic_stamp(t)
461 struct tm *tm = localtime(&t);
466 return ((long)(tm->tm_year << 25) +
468 (tm->tm_mday << 16) +
469 (tm->tm_hour << 11) +
475 wintime_to_unix_stamp()
479 uint64_t epoch = ((uint64_t)0x019db1de << 32) + 0xd53e8000;
480 /* 0x019db1ded53e8000ULL: 1970-01-01 00:00:00 (UTC) */
482 t = (unsigned long)get_longword();
483 t |= (uint64_t)(unsigned long)get_longword() << 32;
484 t = (t - epoch) / 10000000;
488 unsigned long t, q, x;
489 unsigned long wintime[8];
490 unsigned long epoch[8] = {0x01,0x9d,0xb1,0xde, 0xd5,0x3e,0x80,0x00};
491 /* 1970-01-01 00:00:00 (UTC) */
492 /* wintime -= epoch */
494 for (i = 7; i >= 0; i--) {
495 wintime[i] = (unsigned)get_byte() - epoch[i] - borrow;
496 borrow = (wintime[i] > 0xff) ? 1 : 0;
500 /* q = wintime / 10000000 */
502 x = 10000000; /* x: 24bit */
503 for (i = 0; i < 8; i++) {
504 t = (t << 8) + wintime[i]; /* 24bit + 8bit. t must be 32bit variable */
505 q <<= 8; /* q must be 32bit (time_t) */
517 * --------------------------------
519 * 2 or 4 next-header size [*1]
520 * --------------------------------------
521 * ext header: 1 ext-type ^
522 * ? contents | [*1] next-header size
523 * 2 or 4 next-header size v
524 * --------------------------------------
526 * on level 1, 2 header:
527 * size field is 2 bytes
529 * size field is 4 bytes
533 get_extended_header(fp, hdr, header_size, hcrc)
539 char data[LZHEADER_STORAGE];
541 char dirname[FILENAME_LENGTH];
544 ssize_t whole_size = header_size;
546 int n = 1 + hdr->size_field_length; /* `ext-type' + `next-header size' */
548 if (hdr->header_level == 0)
551 name_length = strlen(hdr->name);
553 while (header_size) {
555 if (verbose_listing && verbose > 1)
559 if (sizeof(data) < header_size) {
560 error("header size (%ld) too large.", header_size);
564 if (fread(data, header_size, 1, fp) == 0) {
565 error("Invalid header (LHa file ?)");
569 ext_type = get_byte();
573 if (verbose_listing && verbose > 1) printf(" < header crc >\n");
575 /* header crc (CRC-16) */
576 hdr->header_crc = get_word();
577 /* clear buffer for CRC calculation. */
578 data[1] = data[2] = 0;
579 skip_bytes(header_size - n - 2);
583 if (verbose_listing && verbose > 1) printf(" < filename >\n");
587 get_bytes(hdr->name, header_size-n, sizeof(hdr->name)-1);
588 hdr->name[name_length] = 0;
592 if (verbose_listing && verbose > 1) printf(" < directory >\n");
595 dir_length = get_bytes(dirname, header_size-n, sizeof(dirname)-1);
596 dirname[dir_length] = 0;
600 if (verbose_listing && verbose > 1) printf(" < MS-DOS attribute >\n");
602 /* MS-DOS attribute */
603 hdr->attribute = get_word();
607 if (verbose_listing && verbose > 1) printf(" < Windows time stamp (FILETIME) >\n");
609 /* Windows time stamp (FILETIME structure) */
610 /* it is time in 100 nano seconds since 1601-01-01 00:00:00 */
612 skip_bytes(8); /* create time is ignored */
614 /* set last modified time */
615 if (hdr->header_level >= 2)
616 skip_bytes(8); /* time_t has been already set */
618 hdr->unix_last_modified_stamp = wintime_to_unix_stamp();
620 skip_bytes(8); /* last access time is ignored */
625 if (verbose_listing && verbose > 1) printf(" < 64bits file size header >\n");
628 /* 64bits file size header (UNLHA32 extension) */
629 hdr->packed_size = get_longlongword();
630 hdr->original_size = get_longlongword();
639 if (verbose_listing && verbose > 1) printf(" < UNIX permission >\n");
641 /* UNIX permission */
642 hdr->unix_mode = get_word();
646 if (verbose_listing && verbose > 1) printf(" < UNIX gid and uid >\n");
648 /* UNIX gid and uid */
649 hdr->unix_gid = get_word();
650 hdr->unix_uid = get_word();
654 if (verbose_listing && verbose > 1) printf(" < UNIX group name >\n");
656 /* UNIX group name */
657 i = get_bytes(hdr->group, header_size-n, sizeof(hdr->group)-1);
658 hdr->group[i] = '\0';
662 if (verbose_listing && verbose > 1) printf(" < UNIX user name >\n");
665 i = get_bytes(hdr->user, header_size-n, sizeof(hdr->user)-1);
670 if (verbose_listing && verbose > 1) printf(" < UNIX last modifed time (time_t) >\n");
672 /* UNIX last modified time */
673 hdr->unix_last_modified_stamp = (time_t) get_longword();
677 /* 0x39: multi-disk header
678 0x3f: uncompressed comment
679 0x42: 64bit large file size
680 0x48-0x4f(?): reserved for authenticity verification
682 0x7e: extended attribute - platform information
683 0x7f: extended attribute - permission, owner-id and timestamp
685 0xc4: compressed comment (dict size: 4096)
686 0xc5: compressed comment (dict size: 8192)
687 0xc6: compressed comment (dict size: 16384)
688 0xc7: compressed comment (dict size: 32768)
689 0xc8: compressed comment (dict size: 65536)
690 0xd0-0xdf(?): operating systemm specific information
691 0xfc: encapsulation (another opinion)
692 0xfe: extended attribute - platform information(another opinion)
693 0xff: extended attribute - permission, owner-id and timestamp
694 (level 3 on UNLHA32) */
696 warning("unknown extended header 0x%02x", ext_type);
697 skip_bytes(header_size - n);
702 *hcrc = calccrc(*hcrc, data, header_size);
704 if (hdr->size_field_length == 2)
705 whole_size += header_size = get_word();
707 whole_size += header_size = get_longword();
710 /* concatenate dirname and filename */
712 if (name_length + dir_length >= sizeof(hdr->name)) {
713 warning("the length of pathname \"%s%s\" is too long.",
715 name_length = sizeof(hdr->name) - dir_length - 1;
716 hdr->name[name_length] = 0;
718 strcat(dirname, hdr->name); /* ok */
719 strcpy(hdr->name, dirname); /* ok */
720 name_length += dir_length;
726 #define I_HEADER_SIZE 0 /* level 0,1,2 */
727 #define I_HEADER_CHECKSUM 1 /* level 0,1 */
728 #define I_METHOD 2 /* level 0,1,2,3 */
729 #define I_PACKED_SIZE 7 /* level 0,1,2,3 */
730 #define I_ATTRIBUTE 19 /* level 0,1,2,3 */
731 #define I_HEADER_LEVEL 20 /* level 0,1,2,3 */
733 #define COMMON_HEADER_SIZE 21 /* size of common part */
735 #define I_GENERIC_HEADER_SIZE 24 /* + name_length */
736 #define I_LEVEL0_HEADER_SIZE 36 /* + name_length (unix extended) */
737 #define I_LEVEL1_HEADER_SIZE 27 /* + name_length */
738 #define I_LEVEL2_HEADER_SIZE 26 /* + padding */
739 #define I_LEVEL3_HEADER_SIZE 32
745 * offset size field name
746 * ----------------------------------
747 * 0 1 header size [*1]
749 * ---------------------------------------
751 * 7 4 packed size [*2] |
752 * 11 4 original size |
755 * 19 1 attribute | [*1] header size (X+Y+22)
756 * 20 1 level (0x00 fixed) |
759 * X +22 2 file crc (CRC-16) |
760 * X +24 Y ext-header(old style) v
761 * -------------------------------------------------
763 * : | [*2] packed size
765 * -------------------------------------------------
767 * ext-header(old style)
781 * bit6 archive bit (need to backup)
785 get_header_level0(fp, hdr, data)
796 hdr->size_field_length = 2; /* in bytes */
797 hdr->header_size = header_size = get_byte();
798 checksum = get_byte();
800 if (fread(data + COMMON_HEADER_SIZE,
801 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
802 error("Invalid header (LHarc file ?)");
803 return FALSE; /* finish */
806 if (calc_sum(data + I_METHOD, header_size) != checksum) {
807 error("Checksum error (LHarc file?)");
811 get_bytes(hdr->method, 5, sizeof(hdr->method));
812 hdr->packed_size = (unsigned long)get_longword();
813 hdr->original_size = (unsigned long)get_longword();
814 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
815 hdr->attribute = get_byte(); /* MS-DOS attribute */
816 hdr->header_level = get_byte();
817 name_length = get_byte();
818 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
821 /* defaults for other type */
822 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
826 extend_size = header_size+2 - name_length - 24;
828 if (extend_size < 0) {
829 if (extend_size == -2) {
830 /* CRC field is not given */
831 hdr->extend_type = EXTEND_GENERIC;
832 hdr->has_crc = FALSE;
837 error("Unkonwn header (lha file?)");
842 hdr->crc = get_word();
844 if (extend_size == 0)
847 hdr->extend_type = get_byte();
850 if (hdr->extend_type == EXTEND_UNIX) {
851 if (extend_size >= 11) {
852 hdr->minor_version = get_byte();
853 hdr->unix_last_modified_stamp = (time_t) get_longword();
854 hdr->unix_mode = get_word();
855 hdr->unix_uid = get_word();
856 hdr->unix_gid = get_word();
859 hdr->extend_type = EXTEND_GENERIC;
863 skip_bytes(extend_size);
865 hdr->header_size += 2;
873 * offset size field name
874 * -----------------------------------
875 * 0 1 header size [*1]
877 * -------------------------------------
879 * 7 4 skip size [*2] |
880 * 11 4 original size |
883 * 19 1 attribute (0x20 fixed) | [*1] header size (X+Y+25)
884 * 20 1 level (0x01 fixed) |
887 * X+ 22 2 file crc (CRC-16) |
890 * X+Y+25 2 next-header size v
891 * -------------------------------------------------
892 * X+Y+27 Z ext-header ^
894 * ----------------------------------- | [*2] skip size
897 * -------------------------------------------------
901 get_header_level1(fp, hdr, data)
912 hdr->size_field_length = 2; /* in bytes */
913 hdr->header_size = header_size = get_byte();
914 checksum = get_byte();
916 if (fread(data + COMMON_HEADER_SIZE,
917 header_size + 2 - COMMON_HEADER_SIZE, 1, fp) == 0) {
918 error("Invalid header (LHarc file ?)");
919 return FALSE; /* finish */
922 if (calc_sum(data + I_METHOD, header_size) != checksum) {
923 error("Checksum error (LHarc file?)");
927 get_bytes(hdr->method, 5, sizeof(hdr->method));
928 hdr->packed_size = (unsigned long)get_longword(); /* skip size */
929 hdr->original_size = (unsigned long)get_longword();
930 hdr->unix_last_modified_stamp = generic_to_unix_stamp(get_longword());
931 hdr->attribute = get_byte(); /* 0x20 fixed */
932 hdr->header_level = get_byte();
934 name_length = get_byte();
935 i = get_bytes(hdr->name, name_length, sizeof(hdr->name)-1);
938 /* defaults for other type */
939 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
944 hdr->crc = get_word();
945 hdr->extend_type = get_byte();
947 dummy = header_size+2 - name_length - I_LEVEL1_HEADER_SIZE;
949 skip_bytes(dummy); /* skip old style extend header */
951 extend_size = get_word();
952 extend_size = get_extended_header(fp, hdr, extend_size, 0);
953 if (extend_size == -1)
956 /* On level 1 header, size fields should be adjusted. */
957 /* the `packed_size' field contains the extended header size. */
958 /* the `header_size' field does not. */
959 hdr->packed_size -= extend_size;
960 hdr->header_size += extend_size + 2;
969 * offset size field name
970 * --------------------------------------------------
971 * 0 2 total header size [*1] ^
972 * ----------------------- |
974 * 7 4 packed size [*2] |
975 * 11 4 original size |
977 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
978 * 20 1 level (0x02 fixed) | (X+26+(1))
979 * 21 2 file crc (CRC-16) |
981 * 24 2 next-header size |
982 * ----------------------------------- |
985 * ----------------------------------- |
986 * X +26 (1) padding v
987 * -------------------------------------------------
989 * : | [*2] packed size
991 * -------------------------------------------------
995 get_header_level2(fp, hdr, data)
1001 ssize_t extend_size;
1005 hdr->size_field_length = 2; /* in bytes */
1006 hdr->header_size = header_size = get_word();
1008 if (fread(data + COMMON_HEADER_SIZE,
1009 I_LEVEL2_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
1010 error("Invalid header (LHarc file ?)");
1011 return FALSE; /* finish */
1014 get_bytes(hdr->method, 5, sizeof(hdr->method));
1015 hdr->packed_size = (unsigned long)get_longword();
1016 hdr->original_size = (unsigned long)get_longword();
1017 hdr->unix_last_modified_stamp = get_longword();
1018 hdr->attribute = get_byte(); /* reserved */
1019 hdr->header_level = get_byte();
1021 /* defaults for other type */
1022 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1026 hdr->has_crc = TRUE;
1027 hdr->crc = get_word();
1028 hdr->extend_type = get_byte();
1029 extend_size = get_word();
1031 INITIALIZE_CRC(hcrc);
1032 hcrc = calccrc(hcrc, data, get_ptr - data);
1034 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1035 if (extend_size == -1)
1038 padding = header_size - I_LEVEL2_HEADER_SIZE - extend_size;
1039 while (padding--) /* padding should be 0 or 1 */
1040 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1042 if (hdr->header_crc != hcrc)
1043 error("header CRC error");
1052 * offset size field name
1053 * --------------------------------------------------
1054 * 0 2 size field length (4 fixed) ^
1056 * 7 4 packed size [*2] |
1057 * 11 4 original size |
1059 * 19 1 RESERVED (0x20 fixed) | [*1] total header size
1060 * 20 1 level (0x03 fixed) | (X+32)
1061 * 21 2 file crc (CRC-16) |
1063 * 24 4 total header size [*1] |
1064 * 28 4 next-header size |
1065 * ----------------------------------- |
1068 * -------------------------------------------------
1070 * : | [*2] packed size
1072 * -------------------------------------------------
1076 get_header_level3(fp, hdr, data)
1082 ssize_t extend_size;
1086 hdr->size_field_length = get_word();
1088 if (fread(data + COMMON_HEADER_SIZE,
1089 I_LEVEL3_HEADER_SIZE - COMMON_HEADER_SIZE, 1, fp) == 0) {
1090 error("Invalid header (LHarc file ?)");
1091 return FALSE; /* finish */
1094 get_bytes(hdr->method, 5, sizeof(hdr->method));
1095 hdr->packed_size = (unsigned long)get_longword();
1096 hdr->original_size = (unsigned long)get_longword();
1097 hdr->unix_last_modified_stamp = get_longword();
1098 hdr->attribute = get_byte(); /* reserved */
1099 hdr->header_level = get_byte();
1101 /* defaults for other type */
1102 hdr->unix_mode = UNIX_FILE_REGULAR | UNIX_RW_RW_RW;
1106 hdr->has_crc = TRUE;
1107 hdr->crc = get_word();
1108 hdr->extend_type = get_byte();
1109 hdr->header_size = header_size = get_longword();
1110 extend_size = get_longword();
1112 INITIALIZE_CRC(hcrc);
1113 hcrc = calccrc(hcrc, data, get_ptr - data);
1115 extend_size = get_extended_header(fp, hdr, extend_size, &hcrc);
1116 if (extend_size == -1)
1119 padding = header_size - I_LEVEL3_HEADER_SIZE - extend_size;
1120 while (padding--) /* padding should be 0 */
1121 hcrc = UPDATE_CRC(hcrc, fgetc(fp));
1123 if (hdr->header_crc != hcrc)
1124 error("header CRC error");
1134 char data[LZHEADER_STORAGE];
1136 int archive_kanji_code = CODE_SJIS;
1137 int system_kanji_code = default_system_kanji_code;
1138 char *archive_delim = "\377\\"; /* `\' is for level 0 header and
1140 char *system_delim = "//";
1141 int filename_case = NONE;
1144 memset(hdr, 0, sizeof(LzHeader));
1148 if ((end_mark = getc(fp)) == EOF || end_mark == 0) {
1149 return FALSE; /* finish */
1153 if (fread(data + 1, COMMON_HEADER_SIZE - 1, 1, fp) == 0) {
1154 error("Invalid header (LHarc file ?)");
1155 return FALSE; /* finish */
1158 switch (data[I_HEADER_LEVEL]) {
1160 if (get_header_level0(fp, hdr, data) == FALSE)
1164 if (get_header_level1(fp, hdr, data) == FALSE)
1168 if (get_header_level2(fp, hdr, data) == FALSE)
1172 if (get_header_level3(fp, hdr, data) == FALSE)
1176 error("Unknown level header (level %d)", data[I_HEADER_LEVEL]);
1180 /* filename conversion */
1181 switch (hdr->extend_type) {
1183 filename_case = convertcase ? TO_LOWER : NONE;
1190 filename_case = NONE;
1194 archive_delim = "\377/:\\";
1195 /* `\' is for level 0 header and broken archive. */
1196 system_delim = "/://";
1197 filename_case = NONE;
1201 filename_case = convertcase ? TO_LOWER : NONE;
1205 if (optional_archive_kanji_code)
1206 archive_kanji_code = optional_archive_kanji_code;
1207 if (optional_system_kanji_code)
1208 system_kanji_code = optional_system_kanji_code;
1209 if (optional_archive_delim)
1210 archive_delim = optional_archive_delim;
1211 if (optional_system_delim)
1212 system_delim = optional_system_delim;
1213 if (optional_filename_case)
1214 filename_case = optional_filename_case;
1216 /* kanji code and delimiter conversion */
1217 convert_filename(hdr->name, strlen(hdr->name), sizeof(hdr->name),
1220 archive_delim, system_delim, filename_case);
1222 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1224 /* split symbolic link */
1225 p = strchr(hdr->name, '|');
1227 /* hdr->name is symbolic link name */
1228 /* hdr->realname is real name */
1230 strcpy(hdr->realname, p+1); /* ok */
1233 error("unknown symlink name \"%s\"", hdr->name);
1239 /* skip SFX header */
1244 unsigned char buffer[64 * 1024]; /* max seek size */
1248 n = fread(buffer, 1, sizeof(buffer), fp);
1250 for (p = buffer; p < buffer + n; p++) {
1251 if (! (p[I_METHOD]=='-' &&
1252 (p[I_METHOD+1]=='l' || p[I_METHOD+1]=='p') &&
1253 p[I_METHOD+4]=='-'))
1255 /* found "-[lp]??-" keyword (as METHOD type string) */
1257 /* level 0 or 1 header */
1258 if ((p[I_HEADER_LEVEL] == 0 || p[I_HEADER_LEVEL] == 1)
1259 && p[I_HEADER_SIZE] > 20
1260 && p[I_HEADER_CHECKSUM] == calc_sum(p+2, p[I_HEADER_SIZE])) {
1261 if (fseeko(fp, (p - buffer) - n, SEEK_CUR) == -1)
1262 fatal_error("cannot seek header");
1266 /* level 2 header */
1267 if (p[I_HEADER_LEVEL] == 2
1268 && p[I_HEADER_SIZE] >= 24
1269 && p[I_ATTRIBUTE] == 0x20) {
1270 if (fseeko(fp, (p - buffer) - n, SEEK_CUR) == -1)
1271 fatal_error("cannot seek header");
1276 if (fseeko(fp, -n, SEEK_CUR) == -1)
1277 fatal_error("cannot seek header");
1282 /* remove leading `xxxx/..' */
1284 remove_leading_dots(char *path)
1289 if (strcmp(first, "..") == 0) {
1290 warning("Removing leading `..' from member name.");
1291 return first+1; /* change to "." */
1294 if (strstr(first, "..") == 0)
1297 while (path && *path) {
1299 if (strcmp(path, "..") == 0)
1300 ptr = path = path+2;
1301 else if (strncmp(path, "../", 3) == 0)
1302 ptr = path = path+3;
1304 path = strchr(path, '/');
1306 if (path && *path == '/') {
1312 warning("Removing leading `%.*s' from member name.", ptr-first, first);
1320 copy_path_element(char *dst, const char *src, int size)
1324 if (size < 1) return 0;
1326 for (i = 0; i < size; i++) {
1330 if (dst[i] == '/') {
1344 remove leading "xxx/../"
1345 remove "./", "././", "././ ... ./"
1346 remove duplicated "/"
1349 canon_path(char *newpath, char *path, size_t size)
1353 path = remove_leading_dots(path);
1356 if (path[0] == '.' && path[1] == '/')
1360 len = copy_path_element(newpath, path, size);
1369 /* remove duplicated '/' */
1370 while (*path == '/') path++;
1373 /* When newpath is empty, set "." */
1375 strcpy(newpath, ".");
1379 return newpath - p; /* string length */
1383 init_header(name, v_stat, hdr)
1385 struct stat *v_stat;
1390 memset(hdr, 0, sizeof(LzHeader));
1392 /* the `method' member is rewrote by the encoding function.
1393 but need set for empty files */
1394 memcpy(hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STORAGE);
1396 hdr->packed_size = 0;
1397 hdr->original_size = v_stat->st_size;
1398 hdr->attribute = GENERIC_ATTRIBUTE;
1399 hdr->header_level = header_level;
1401 len = canon_path(hdr->name, name, sizeof(hdr->name));
1404 hdr->extend_type = EXTEND_UNIX;
1405 hdr->unix_last_modified_stamp = v_stat->st_mtime;
1406 /* since 00:00:00 JAN.1.1970 */
1407 #ifdef NOT_COMPATIBLE_MODE
1408 /* Please need your modification in this space. */
1411 if (S_ISREG(v_stat->st_mode))
1412 hdr->unix_mode = hdr->unix_mode | UNIX_FILE_REGULAR;
1413 if (S_ISDIR(v_stat->st_mode))
1414 hdr->unix_mode = hdr->unix_mode | UNIX_FILE_DIRECTORY;
1415 if (S_ISLNK(v_stat->st_mode))
1416 hdr->unix_mode = hdr->unix_mode | UNIX_FILE_SYMLINK;
1417 if (v_stat->st_mode & S_IRUSR)
1418 hdr->unix_mode = hdr->unix_mode | UNIX_OWNER_READ_PERM;
1419 if (v_stat->st_mode & S_IRGRP)
1420 hdr->unix_mode = hdr->unix_mode | UNIX_GROUP_READ_PERM;
1421 if (v_stat->st_mode & S_IROTH)
1422 hdr->unix_mode = hdr->unix_mode | UNIX_OTHER_READ_PERM;
1423 if (v_stat->st_mode & S_IWUSR)
1424 hdr->unix_mode = hdr->unix_mode | UNIX_OWNER_WRITE_PERM;
1425 if (v_stat->st_mode & S_IWGRP)
1426 hdr->unix_mode = hdr->unix_mode | UNIX_GROUP_WRITE_PERM;
1427 if (v_stat->st_mode & S_IWOTH)
1428 hdr->unix_mode = hdr->unix_mode | UNIX_OTHER_WRITE_PERM;
1429 if (v_stat->st_mode & S_IXUSR)
1430 hdr->unix_mode = hdr->unix_mode | UNIX_OWNER_EXEC_PERM;
1431 if (v_stat->st_mode & S_IXGRP)
1432 hdr->unix_mode = hdr->unix_mode | UNIX_GROUP_EXEC_PERM;
1433 if (v_stat->st_mode & S_IXOTH)
1434 hdr->unix_mode = hdr->unix_mode | UNIX_OTHER_EXEC_PERM;
1435 if (v_stat->st_mode & S_ISUID)
1436 hdr->unix_mode = hdr->unix_mode | UNIX_SETUID;
1437 if (v_stat->st_mode & S_ISGID)
1438 hdr->unix_mode = hdr->unix_mode | UNIX_SETGID;
1439 #endif /* __DJGPP__ */
1441 hdr->unix_mode = v_stat->st_mode;
1444 hdr->unix_uid = v_stat->st_uid;
1445 hdr->unix_gid = v_stat->st_gid;
1447 #if INCLUDE_OWNER_NAME_IN_HEADER
1450 struct passwd *ent = getpwuid(hdr->unix_uid);
1453 strncpy(hdr->user, ent->pw_name, sizeof(hdr->user));
1454 if (hdr->user[sizeof(hdr->user)-1])
1455 hdr->user[sizeof(hdr->user)-1] = 0;
1461 struct group *ent = getgrgid(hdr->unix_gid);
1464 strncpy(hdr->group, ent->gr_name, sizeof(hdr->group));
1465 if (hdr->group[sizeof(hdr->group)-1])
1466 hdr->group[sizeof(hdr->group)-1] = 0;
1470 #endif /* INCLUDE_OWNER_NAME_IN_HEADER */
1471 if (is_directory(v_stat)) {
1472 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1473 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1474 hdr->original_size = 0;
1475 if (len > 0 && hdr->name[len - 1] != '/') {
1476 if (len < sizeof(hdr->name)-1)
1477 strcpy(&hdr->name[len++], "/"); /* ok */
1479 warning("the length of dirname \"%s\" is too long.",
1485 if (is_symlink(v_stat)) {
1486 memcpy(hdr->method, LZHDIRS_METHOD, METHOD_TYPE_STORAGE);
1487 hdr->attribute = GENERIC_DIRECTORY_ATTRIBUTE;
1488 hdr->original_size = 0;
1489 readlink(name, hdr->realname, sizeof(hdr->realname));
1495 write_unix_info(hdr)
1498 /* UNIX specific informations */
1500 put_word(5); /* size */
1501 put_byte(0x50); /* permission */
1502 put_word(hdr->unix_mode);
1504 put_word(7); /* size */
1505 put_byte(0x51); /* gid and uid */
1506 put_word(hdr->unix_gid);
1507 put_word(hdr->unix_uid);
1509 if (hdr->group[0]) {
1510 int len = strlen(hdr->group);
1511 put_word(len + 3); /* size */
1512 put_byte(0x52); /* group name */
1513 put_bytes(hdr->group, len);
1517 int len = strlen(hdr->user);
1518 put_word(len + 3); /* size */
1519 put_byte(0x53); /* user name */
1520 put_bytes(hdr->user, len);
1523 if (hdr->header_level == 1) {
1524 put_word(7); /* size */
1525 put_byte(0x54); /* time stamp */
1526 put_longword(hdr->unix_last_modified_stamp);
1531 write_header_level0(data, hdr, pathname)
1533 char *data, *pathname;
1540 memset(data, 0, LZHEADER_STORAGE);
1542 put_byte(0x00); /* header size */
1543 put_byte(0x00); /* check sum */
1544 put_bytes(hdr->method, 5);
1545 put_longword(hdr->packed_size);
1546 put_longword(hdr->original_size);
1547 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1548 put_byte(hdr->attribute);
1549 put_byte(hdr->header_level); /* level 0 */
1551 /* write pathname (level 0 header contains the directory part) */
1552 name_length = strlen(pathname);
1554 limit = 255 - I_GENERIC_HEADER_SIZE + 2;
1556 limit = 255 - I_LEVEL0_HEADER_SIZE + 2;
1558 if (name_length > limit) {
1559 warning("the length of pathname \"%s\" is too long.", pathname);
1560 name_length = limit;
1562 put_byte(name_length);
1563 put_bytes(pathname, name_length);
1566 if (generic_format) {
1567 header_size = I_GENERIC_HEADER_SIZE + name_length - 2;
1568 data[I_HEADER_SIZE] = header_size;
1569 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1571 /* write old-style extend header */
1572 put_byte(EXTEND_UNIX);
1573 put_byte(CURRENT_UNIX_MINOR_VERSION);
1574 put_longword(hdr->unix_last_modified_stamp);
1575 put_word(hdr->unix_mode);
1576 put_word(hdr->unix_uid);
1577 put_word(hdr->unix_gid);
1579 /* size of extended header is 12 */
1580 header_size = I_LEVEL0_HEADER_SIZE + name_length - 2;
1581 data[I_HEADER_SIZE] = header_size;
1582 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1585 return header_size + 2;
1589 write_header_level1(data, hdr, pathname)
1591 char *data, *pathname;
1593 int name_length, dir_length, limit;
1594 char *basename, *dirname;
1596 char *extend_header_top;
1597 size_t extend_header_size;
1599 basename = strrchr(pathname, LHA_PATHSEP);
1602 name_length = strlen(basename);
1604 dir_length = basename - dirname;
1607 basename = pathname;
1608 name_length = strlen(basename);
1614 memset(data, 0, LZHEADER_STORAGE);
1616 put_byte(0x00); /* header size */
1617 put_byte(0x00); /* check sum */
1618 put_bytes(hdr->method, 5);
1619 put_longword(hdr->packed_size);
1620 put_longword(hdr->original_size);
1621 put_longword(unix_to_generic_stamp(hdr->unix_last_modified_stamp));
1623 put_byte(hdr->header_level); /* level 1 */
1625 /* level 1 header: write filename (basename only) */
1626 limit = 255 - I_LEVEL1_HEADER_SIZE + 2;
1627 if (name_length > limit) {
1628 put_byte(0); /* name length */
1631 put_byte(name_length);
1632 put_bytes(basename, name_length);
1640 put_byte(EXTEND_UNIX);
1642 /* write extend header from here. */
1644 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1645 header_size = extend_header_top - data - 2;
1647 /* write filename and dirname */
1649 if (name_length > limit) {
1650 put_word(name_length + 3); /* size */
1651 put_byte(0x01); /* filename */
1652 put_bytes(basename, name_length);
1655 if (dir_length > 0) {
1656 put_word(dir_length + 3); /* size */
1657 put_byte(0x02); /* dirname */
1658 put_bytes(dirname, dir_length);
1661 if (!generic_format)
1662 write_unix_info(hdr);
1664 put_word(0x0000); /* next header size */
1666 extend_header_size = put_ptr - extend_header_top;
1667 /* On level 1 header, the packed size field is contains the ext-header */
1668 hdr->packed_size += put_ptr - extend_header_top;
1670 /* put `skip size' */
1671 setup_put(data + I_PACKED_SIZE);
1672 put_longword(hdr->packed_size);
1674 data[I_HEADER_SIZE] = header_size;
1675 data[I_HEADER_CHECKSUM] = calc_sum(data + I_METHOD, header_size);
1677 return header_size + extend_header_size + 2;
1681 write_header_level2(data, hdr, pathname)
1683 char *data, *pathname;
1685 int name_length, dir_length;
1686 char *basename, *dirname;
1688 char *extend_header_top;
1689 char *headercrc_ptr;
1692 basename = strrchr(pathname, LHA_PATHSEP);
1695 name_length = strlen(basename);
1697 dir_length = basename - dirname;
1700 basename = pathname;
1701 name_length = strlen(basename);
1707 memset(data, 0, LZHEADER_STORAGE);
1709 put_word(0x0000); /* header size */
1710 put_bytes(hdr->method, 5);
1711 put_longword(hdr->packed_size);
1712 put_longword(hdr->original_size);
1713 put_longword(hdr->unix_last_modified_stamp);
1715 put_byte(hdr->header_level); /* level 2 */
1722 put_byte(EXTEND_UNIX);
1724 /* write extend header from here. */
1726 extend_header_top = put_ptr+2; /* +2 for the field `next header size' */
1728 /* write common header */
1731 headercrc_ptr = put_ptr;
1732 put_word(0x0000); /* header CRC */
1734 /* write filename and dirname */
1735 /* must have this header, even if the name_length is 0. */
1736 put_word(name_length + 3); /* size */
1737 put_byte(0x01); /* filename */
1738 put_bytes(basename, name_length);
1740 if (dir_length > 0) {
1741 put_word(dir_length + 3); /* size */
1742 put_byte(0x02); /* dirname */
1743 put_bytes(dirname, dir_length);
1746 if (!generic_format)
1747 write_unix_info(hdr);
1749 put_word(0x0000); /* next header size */
1751 header_size = put_ptr - data;
1752 if ((header_size & 0xff) == 0) {
1753 /* cannot put zero at the first byte on level 2 header. */
1754 /* adjust header size. */
1755 put_byte(0); /* padding */
1759 /* put header size */
1760 setup_put(data + I_HEADER_SIZE);
1761 put_word(header_size);
1763 /* put header CRC in extended header */
1764 INITIALIZE_CRC(hcrc);
1765 hcrc = calccrc(hcrc, data, (unsigned int) header_size);
1766 setup_put(headercrc_ptr);
1773 write_header(fp, hdr)
1778 char data[LZHEADER_STORAGE];
1780 int archive_kanji_code = CODE_SJIS;
1781 int system_kanji_code = default_system_kanji_code;
1782 char *archive_delim = "\377";
1783 char *system_delim = "/";
1784 int filename_case = NONE;
1785 char pathname[FILENAME_LENGTH];
1787 if (optional_archive_kanji_code)
1788 archive_kanji_code = optional_archive_kanji_code;
1789 if (optional_system_kanji_code)
1790 system_kanji_code = optional_system_kanji_code;
1792 if (generic_format && convertcase)
1793 filename_case = TO_UPPER;
1795 if (hdr->header_level == 0) {
1796 archive_delim = "\\";
1799 if ((hdr->unix_mode & UNIX_FILE_SYMLINK) == UNIX_FILE_SYMLINK) {
1801 p = strchr(hdr->name, '|');
1803 error("symlink name \"%s\" contains '|' char. change it into '_'",
1807 if (xsnprintf(pathname, sizeof(pathname),
1808 "%s|%s", hdr->name, hdr->realname) == -1)
1809 error("file name is too long (%s -> %s)", hdr->name, hdr->realname);
1812 strncpy(pathname, hdr->name, sizeof(pathname));
1813 pathname[sizeof(pathname)-1] = 0;
1816 convert_filename(pathname, strlen(pathname), sizeof(pathname),
1819 system_delim, archive_delim, filename_case);
1821 switch (hdr->header_level) {
1823 header_size = write_header_level0(data, hdr, pathname);
1826 header_size = write_header_level1(data, hdr, pathname);
1829 header_size = write_header_level2(data, hdr, pathname);
1832 error("Unknown level header (level %d)", hdr->header_level);
1836 if (fwrite(data, header_size, 1, fp) == 0)
1837 fatal_error("Cannot write to temporary file");
1840 #if MULTIBYTE_FILENAME
1842 #if defined(__APPLE__) /* Added by Hiroto Sakai */
1844 #include <CoreFoundation/CFString.h>
1845 #include <CoreFoundation/CFStringEncodingExt.h>
1847 /* this is not need for Mac OS X v 10.2 later */
1849 kCFStringEncodingAllowLossyConversion = 1,
1850 kCFStringEncodingBasicDirectionLeftToRight = (1 << 1),
1851 kCFStringEncodingBasicDirectionRightToLeft = (1 << 2),
1852 kCFStringEncodingSubstituteCombinings = (1 << 3),
1853 kCFStringEncodingComposeCombinings = (1 << 4),
1854 kCFStringEncodingIgnoreCombinings = (1 << 5),
1855 kCFStringEncodingUseCanonical = (1 << 6),
1856 kCFStringEncodingUseHFSPlusCanonical = (1 << 7),
1857 kCFStringEncodingPrependBOM = (1 << 8),
1858 kCFStringEncodingDisableCorporateArea = (1 << 9),
1859 kCFStringEncodingASCIICompatibleConversion = (1 << 10),
1863 ConvertEncodingToUTF8(const char* inCStr,
1864 char* outUTF8Buffer,
1865 int outUTF8BufferLength,
1866 unsigned long scriptEncoding,
1867 unsigned long flags)
1869 unsigned long unicodeChars;
1870 unsigned long srcCharsUsed;
1871 unsigned long usedByteLen = 0;
1872 UniChar uniStr[512];
1873 unsigned long cfResult;
1875 cfResult = CFStringEncodingBytesToUnicode(scriptEncoding,
1883 if (cfResult == 0) {
1884 cfResult = CFStringEncodingUnicodeToBytes(kCFStringEncodingUTF8,
1889 (char*)outUTF8Buffer,
1890 outUTF8BufferLength - 1,
1892 outUTF8Buffer[usedByteLen] = '\0';
1899 ConvertUTF8ToEncoding(const char* inUTF8Buf,
1900 int inUTF8BufLength,
1901 char* outCStrBuffer,
1902 int outCStrBufferLength,
1903 unsigned long scriptEncoding,
1904 unsigned long flags)
1906 unsigned long unicodeChars;
1907 unsigned long srcCharsUsed;
1908 unsigned long usedByteLen = 0;
1909 UniChar uniStr[256];
1910 unsigned long cfResult;
1912 cfResult = CFStringEncodingBytesToUnicode(kCFStringEncodingUTF8,
1920 if (cfResult == 0) {
1921 cfResult = CFStringEncodingUnicodeToBytes(scriptEncoding,
1926 (char*)outCStrBuffer,
1927 outCStrBufferLength - 1,
1929 outCStrBuffer[usedByteLen] = '\0';
1939 ConvertEncodingByIconv(const char *src, char *dst, int dstsize,
1940 const char *srcEnc, const char *dstEnc)
1943 static char szTmpBuf[2048];
1949 dst_p = &szTmpBuf[0];
1950 iLen = (size_t)sizeof(szTmpBuf)-1;
1951 src_p = (char *)src;
1952 sLen = (size_t)strlen(src);
1953 memset(szTmpBuf, 0, sizeof(szTmpBuf));
1954 memset(dst, 0, dstsize);
1956 ic = iconv_open(dstEnc, srcEnc);
1957 if (ic == (iconv_t)-1) {
1958 error("iconv_open() failure: %s", strerror(errno));
1962 if (iconv(ic, &src_p, &sLen, &dst_p, &iLen) == (size_t)-1) {
1963 error("iconv() failure: %s", strerror(errno));
1968 strncpy(dst, szTmpBuf, dstsize);
1974 #endif /* defined(__APPLE__) */
1977 sjis_to_utf8(char *dst, const char *src, size_t dstsize)
1979 #if defined(__APPLE__)
1981 if (ConvertEncodingToUTF8(src, dst, dstsize,
1982 kCFStringEncodingDOSJapanese,
1983 kCFStringEncodingUseHFSPlusCanonical) == 0)
1986 if (ConvertEncodingByIconv(src, dst, dstsize, "SJIS", "UTF-8") != -1)
1989 error("not support utf-8 conversion");
1992 if (dstsize < 1) return dst;
1994 return strncpy(dst, src, dstsize-1);
1998 utf8_to_sjis(char *dst, const char *src, size_t dstsize)
2000 #if defined(__APPLE__)
2004 srclen = strlen(src);
2005 if (ConvertUTF8ToEncoding(src, srclen, dst, dstsize,
2006 kCFStringEncodingDOSJapanese,
2007 kCFStringEncodingUseHFSPlusCanonical) == 0)
2010 if (ConvertEncodingByIconv(src, dst, dstsize, "UTF-8", "SJIS") != -1)
2013 error("not support utf-8 conversion");
2016 if (dstsize < 1) return dst;
2018 return strncpy(dst, src, dstsize-1);
2023 * 「日本語情報処理」 ソフトバンク(株)
2024 * より抜粋(by Koji Arai)
2027 euc2sjis(int *p1, int *p2)
2029 unsigned char c1 = *p1 & 0x7f;
2030 unsigned char c2 = *p2 & 0x7f;
2031 int rowoff = c1 < 0x5f ? 0x70 : 0xb0;
2032 int celoff = c1 % 2 ? (c2 > 0x5f ? 0x20 : 0x1f) : 0x7e;
2033 *p1 = ((c1 + 1) >> 1) + rowoff;
2034 *p2 += celoff - 0x80;
2038 sjis2euc(int *p1, int *p2)
2040 unsigned char c1 = *p1;
2041 unsigned char c2 = *p2;
2042 int adjust = c2 < 0x9f;
2043 int rowoff = c1 < 0xa0 ? 0x70 : 0xb0;
2044 int celoff = adjust ? (c2 > 0x7f ? 0x20 : 0x1f) : 0x7e;
2045 *p1 = ((c1 - rowoff) << 1) - adjust;
2056 case '0': case '1': case '2': case '3': case '4':
2057 case '5': case '6': case '7': case '8': case '9':
2060 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2061 return c - 'a' + 10;
2063 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2064 return c - 'A' + 10;
2074 case 0: case 1: case 2: case 3: case 4:
2075 case 5: case 6: case 7: case 8: case 9:
2078 case 10: case 11: case 12: case 13: case 14: case 15:
2079 return c + 'a' - 10;
2087 cap_to_sjis(char *dst, const char *src, size_t dstsize)
2090 size_t len = strlen(src);
2093 for (i = j = 0; i < len && i < dstsize; i++) {
2094 if (src[i] != ':') {
2100 a = hex2int((unsigned char)src[i]);
2101 b = hex2int((unsigned char)src[i+1]);
2103 if (a == -1 || b == -1) {
2106 strncpy(dst+j, src+i, dstsize-j);
2113 dst[j++] = a * 16 + b;
2120 sjis_to_cap(char *dst, const char *src, size_t dstsize)
2123 size_t len = strlen(src);
2126 for (i = j = 0; i < len && i < dstsize; i++) {
2127 if (src[i] == ':') {
2128 strncpy(dst+j, ":3a", dstsize-j);
2133 if (isprint(src[i])) {
2138 if (j + 3 >= dstsize) {
2143 a = int2hex((unsigned char)src[i] / 16);
2144 b = int2hex((unsigned char)src[i] % 16);
2153 #endif /* MULTIBYTE_FILENAME */