2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1997
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 Adding for Japanese language by <fujita@ainix.isac.co.jp> 1994.9.5
22 and extend coding system to EUC/SJIS/JIS/HEX at 1994.10.11
23 and add all jis codes sequence type at 1995.8.16
24 Notes: Hexadecimal code by <ohki@gssm.otuka.tsukuba.ac.jp>
30 /* coding system keep in */
31 int coding_system
= SJIS_CODE
;
33 /* jis si/so sequence */
34 char jis_kso
= JIS_KSO
;
35 char jis_ksi
= JIS_KSI
;
36 char hex_tag
= HEXTAG
;
38 /*******************************************************************
40 ********************************************************************/
41 /*******************************************************************
42 search token from S1 separated any char of S2
43 S1 contain SHIFT JIS chars.
44 ********************************************************************/
45 char *sj_strtok(char *s1
, char *s2
)
47 static char *s
= NULL
;
56 if (is_shift_jis (*s1
)) {
58 } else if (is_kana (*s1
)) {
61 char *p
= strchr (s2
, *s1
);
80 /*******************************************************************
81 search string S2 from S1
82 S1 contain SHIFT JIS chars.
83 ********************************************************************/
84 char *sj_strstr(char *s1
, char *s2
)
86 int len
= strlen ((char *) s2
);
91 if (strncmp (s1
, s2
, len
) == 0)
94 if (is_shift_jis (*s1
)) {
103 /*******************************************************************
104 Search char C from beginning of S.
105 S contain SHIFT JIS chars.
106 ********************************************************************/
107 char *sj_strchr (char *s
, int c
)
112 if (is_shift_jis (*s
)) {
121 /*******************************************************************
122 Search char C end of S.
123 S contain SHIFT JIS chars.
124 ********************************************************************/
125 char *sj_strrchr(char *s
, int c
)
133 if (is_shift_jis (*s
)) {
142 /*******************************************************************
144 ********************************************************************/
145 /* convesion buffer */
146 static char cvtbuf
[1024];
148 /*******************************************************************
150 ********************************************************************/
151 static int euc2sjis (int hi
, int lo
)
154 return ((hi
/ 2 + (hi
< 0xdf ? 0x31 : 0x71)) << 8) |
155 (lo
- (lo
>= 0xe0 ? 0x60 : 0x61));
157 return ((hi
/ 2 + (hi
< 0xdf ? 0x30 : 0x70)) << 8) | (lo
- 2);
160 static int sjis2euc (int hi
, int lo
)
163 return ((hi
* 2 - (hi
>= 0xe0 ? 0xe0 : 0x60)) << 8) | (lo
+ 2);
165 return ((hi
* 2 - (hi
>= 0xe0 ? 0xe1 : 0x61)) << 8) |
166 (lo
+ (lo
>= 0x7f ? 0x60 : 0x61));
169 /*******************************************************************
170 Convert FROM contain SHIFT JIS codes to EUC codes
171 return converted buffer
172 ********************************************************************/
173 static char *sj_to_euc(char *from
, BOOL overwrite
)
178 save
= (char *) from
;
179 for (out
= cvtbuf
; *from
;) {
180 if (is_shift_jis (*from
)) {
181 int code
= sjis2euc ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
182 *out
++ = (code
>> 8) & 0xff;
185 } else if (is_kana (*from
)) {
194 strcpy((char *) save
, (char *) cvtbuf
);
195 return (char *) save
;
201 /*******************************************************************
202 Convert FROM contain EUC codes to SHIFT JIS codes
203 return converted buffer
204 ********************************************************************/
205 static char *euc_to_sj(char *from
, BOOL overwrite
)
210 save
= (char *) from
;
211 for (out
= cvtbuf
; *from
; ) {
212 if (is_euc (*from
)) {
213 int code
= euc2sjis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
214 *out
++ = (code
>> 8) & 0xff;
217 } else if (is_euc_kana (*from
)) {
226 strcpy(save
, (char *) cvtbuf
);
233 /*******************************************************************
234 JIS7,JIS8,JUNET <-> SJIS
235 ********************************************************************/
236 static int sjis2jis(int hi
, int lo
)
239 return ((hi
* 2 - (hi
>= 0xe0 ? 0x160 : 0xe0)) << 8) | (lo
- 0x7e);
241 return ((hi
* 2 - (hi
>= 0xe0 ? 0x161 : 0xe1)) << 8) |
242 (lo
- (lo
>= 0x7f ? 0x20 : 0x1f));
245 static int jis2sjis(int hi
, int lo
)
248 return ((hi
/ 2 + (hi
< 0x5f ? 0x71 : 0xb1)) << 8) |
249 (lo
+ (lo
>= 0x60 ? 0x20 : 0x1f));
251 return ((hi
/ 2 + (hi
< 0x5f ? 0x70 : 0xb0)) << 8) | (lo
+ 0x7e);
254 /*******************************************************************
255 Convert FROM contain JIS codes to SHIFT JIS codes
256 return converted buffer
257 ********************************************************************/
258 static char *jis8_to_sj(char *from
, BOOL overwrite
)
265 save
= (char *) from
;
266 for (out
= cvtbuf
; *from
;) {
267 if (is_esc (*from
)) {
268 if (is_so1 (from
[1]) && is_so2 (from
[2])) {
271 } else if (is_si1 (from
[1]) && is_si2 (from
[2])) {
274 } else { /* sequence error */
286 int code
= jis2sjis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
287 *out
++ = (code
>> 8) & 0xff;
297 strcpy (save
, (char *) cvtbuf
);
304 /*******************************************************************
305 Convert FROM contain SHIFT JIS codes to JIS codes
306 return converted buffer
307 ********************************************************************/
308 static char *sj_to_jis8(char *from
, BOOL overwrite
)
315 save
= (char *) from
;
316 for (out
= cvtbuf
; *from
; ) {
317 if (is_shift_jis (*from
)) {
320 case _KJ_ROMAN
: /* to KANJI */
327 code
= sjis2jis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
328 *out
++ = (code
>> 8) & 0xff;
333 case _KJ_KANJI
: /* to ROMAN/KANA */
344 case _KJ_KANJI
: /* to ROMAN/KANA */
353 strcpy (save
, (char *) cvtbuf
);
360 /*******************************************************************
361 Convert FROM contain 7 bits JIS codes to SHIFT JIS codes
362 return converted buffer
363 ********************************************************************/
364 static char *jis7_to_sj(char *from
, BOOL overwrite
)
371 save
= (char *) from
;
372 for (out
= cvtbuf
; *from
;) {
373 if (is_esc (*from
)) {
374 if (is_so1 (from
[1]) && is_so2 (from
[2])) {
377 } else if (is_si1 (from
[1]) && is_si2 (from
[2])) {
380 } else { /* sequence error */
383 } else if (is_so (*from
)) {
384 shifted
= _KJ_KANA
; /* to KANA */
386 } else if (is_si (*from
)) {
387 shifted
= _KJ_ROMAN
; /* to ROMAN */
398 int code
= jis2sjis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
399 *out
++ = (code
>> 8) & 0xff;
405 *out
++ = ((int) from
[0]) + 0x80;
412 strcpy (save
, (char *) cvtbuf
);
419 /*******************************************************************
420 Convert FROM contain SHIFT JIS codes to 7 bits JIS codes
421 return converted buffer
422 ********************************************************************/
423 static char *sj_to_jis7(char *from
, BOOL overwrite
)
430 save
= (char *) from
;
431 for (out
= cvtbuf
; *from
; ) {
432 if (is_shift_jis (*from
)) {
436 *out
++ = jis_si
; /* to ROMAN and through down */
437 case _KJ_ROMAN
: /* to KANJI */
444 code
= sjis2jis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
445 *out
++ = (code
>> 8) & 0xff;
448 } else if (is_kana (from
[0])) {
450 case _KJ_KANJI
: /* to ROMAN */
454 case _KJ_ROMAN
: /* to KANA */
459 *out
++ = ((int) *from
++) - 0x80;
463 *out
++ = jis_si
; /* to ROMAN */
466 case _KJ_KANJI
: /* to ROMAN */
478 *out
++ = jis_si
; /* to ROMAN */
480 case _KJ_KANJI
: /* to ROMAN */
488 strcpy (save
, (char *) cvtbuf
);
495 /*******************************************************************
496 Convert FROM contain 7 bits JIS(junet) codes to SHIFT JIS codes
497 return converted buffer
498 ********************************************************************/
499 static char *junet_to_sj(char *from
, BOOL overwrite
)
506 save
= (char *) from
;
507 for (out
= cvtbuf
; *from
;) {
508 if (is_esc (*from
)) {
509 if (is_so1 (from
[1]) && is_so2 (from
[2])) {
512 } else if (is_si1 (from
[1]) && is_si2 (from
[2])) {
515 } else if (is_juk1(from
[1]) && is_juk2 (from
[2])) {
518 } else { /* sequence error */
530 int code
= jis2sjis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
531 *out
++ = (code
>> 8) & 0xff;
537 *out
++ = ((int) from
[0]) + 0x80;
544 strcpy (save
, (char *) cvtbuf
);
551 /*******************************************************************
552 Convert FROM contain SHIFT JIS codes to 7 bits JIS(junet) codes
553 return converted buffer
554 ********************************************************************/
555 static char *sj_to_junet(char *from
, BOOL overwrite
)
562 save
= (char *) from
;
563 for (out
= cvtbuf
; *from
; ) {
564 if (is_shift_jis (*from
)) {
568 case _KJ_ROMAN
: /* to KANJI */
575 code
= sjis2jis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
576 *out
++ = (code
>> 8) & 0xff;
579 } else if (is_kana (from
[0])) {
581 case _KJ_KANJI
: /* to ROMAN */
582 case _KJ_ROMAN
: /* to KANA */
584 *out
++ = junet_kana1
;
585 *out
++ = junet_kana2
;
589 *out
++ = ((int) *from
++) - 0x80;
593 case _KJ_KANJI
: /* to ROMAN */
605 case _KJ_KANJI
: /* to ROMAN */
613 strcpy (save
, (char *) cvtbuf
);
620 /*******************************************************************
622 ********************************************************************/
623 /* ":xx" -> a byte */
624 static char *hex_to_sj(char *from
, BOOL overwrite
)
631 if (*sp
== hex_tag
&& isxdigit (sp
[1]) && isxdigit (sp
[2])) {
632 *dp
++ = (hex2bin (sp
[1])<<4) | (hex2bin (sp
[2]));
639 strcpy ((char *) from
, (char *) cvtbuf
);
640 return (char *) from
;
646 /*******************************************************************
648 ********************************************************************/
649 static char *sj_to_hex(char *from
, BOOL overwrite
)
651 unsigned char *sp
, *dp
;
653 sp
= (unsigned char*) from
;
654 dp
= (unsigned char*) cvtbuf
;
658 *dp
++ = bin2hex (((*sp
)>>4)&0x0f);
659 *dp
++ = bin2hex ((*sp
)&0x0f);
661 } else if (is_shift_jis (*sp
) && is_shift_jis2 (sp
[1])) {
663 *dp
++ = bin2hex (((*sp
)>>4)&0x0f);
664 *dp
++ = bin2hex ((*sp
)&0x0f);
667 *dp
++ = bin2hex (((*sp
)>>4)&0x0f);
668 *dp
++ = bin2hex ((*sp
)&0x0f);
675 strcpy ((char *) from
, (char *) cvtbuf
);
676 return (char *) from
;
682 /*******************************************************************
684 ********************************************************************/
685 static char *sj_to_cap(char *from
, BOOL overwrite
)
687 unsigned char *sp
, *dp
;
689 sp
= (unsigned char*) from
;
690 dp
= (unsigned char*) cvtbuf
;
694 *dp
++ = bin2hex (((*sp
)>>4)&0x0f);
695 *dp
++ = bin2hex ((*sp
)&0x0f);
703 strcpy ((char *) from
, (char *) cvtbuf
);
704 return (char *) from
;
710 /*******************************************************************
712 ********************************************************************/
713 static char *sj_to_sj(char *from
, BOOL overwrite
)
716 strcpy (cvtbuf
, (char *) from
);
719 return (char *) from
;
723 /************************************************************************
725 _dos_to_unix _unix_to_dos
726 ************************************************************************/
728 char *(*_dos_to_unix
)(char *str
, BOOL overwrite
) = sj_to_sj
;
729 char *(*_unix_to_dos
)(char *str
, BOOL overwrite
) = sj_to_sj
;
731 static int setup_string_function(int codes
)
736 _dos_to_unix
= sj_to_sj
;
737 _unix_to_dos
= sj_to_sj
;
742 _dos_to_unix
= sj_to_euc
;
743 _unix_to_dos
= euc_to_sj
;
747 _dos_to_unix
= sj_to_jis7
;
748 _unix_to_dos
= jis7_to_sj
;
752 _dos_to_unix
= sj_to_jis8
;
753 _unix_to_dos
= jis8_to_sj
;
757 _dos_to_unix
= sj_to_junet
;
758 _unix_to_dos
= junet_to_sj
;
762 _dos_to_unix
= sj_to_hex
;
763 _unix_to_dos
= hex_to_sj
;
767 _dos_to_unix
= sj_to_cap
;
768 _unix_to_dos
= hex_to_sj
;
775 * Interpret coding system.
777 int interpret_coding_system(char *str
, int def
)
781 if (strequal (str
, "sjis")) {
783 } else if (strequal (str
, "euc")) {
785 } else if (strequal (str
, "cap")) {
788 } else if (strequal (str
, "hex")) {
791 } else if (strncasecmp (str
, "hex", 3)) {
793 hex_tag
= (str
[3] ? str
[3] : HEXTAG
);
794 } else if (strequal (str
, "j8bb")) {
798 } else if (strequal (str
, "j8bj") || strequal (str
, "jis8")) {
802 } else if (strequal (str
, "j8bh")) {
806 } else if (strequal (str
, "j8@b")) {
810 } else if (strequal (str
, "j8@j")) {
814 } else if (strequal (str
, "j8@h")) {
818 } else if (strequal (str
, "j7bb")) {
822 } else if (strequal (str
, "j7bj") || strequal (str
, "jis7")) {
826 } else if (strequal (str
, "j7bh")) {
830 } else if (strequal (str
, "j7@b")) {
834 } else if (strequal (str
, "j7@j")) {
838 } else if (strequal (str
, "j7@h")) {
842 } else if (strequal (str
, "jubb")) {
846 } else if (strequal (str
, "jubj") || strequal (str
, "junet")) {
850 } else if (strequal (str
, "jubh")) {
854 } else if (strequal (str
, "ju@b")) {
858 } else if (strequal (str
, "ju@j")) {
862 } else if (strequal (str
, "ju@h")) {
867 return setup_string_function (codes
);