2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1994
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>
31 /* coding system keep in */
32 int coding_system
= SJIS_CODE
;
34 /* jis si/so sequence */
35 char jis_kso
= JIS_KSO
;
36 char jis_ksi
= JIS_KSI
;
37 char hex_tag
= HEXTAG
;
39 /*******************************************************************
41 ********************************************************************/
42 /*******************************************************************
43 search token from S1 separated any char of S2
44 S1 contain SHIFT JIS chars.
45 ********************************************************************/
47 sj_strtok (char *s1
, const char *s2
)
49 static char *s
= NULL
;
58 if (is_shift_jis (*s1
)) {
60 } else if (is_kana (*s1
)) {
63 char *p
= strchr (s2
, *s1
);
82 /*******************************************************************
83 search string S2 from S1
84 S1 contain SHIFT JIS chars.
85 ********************************************************************/
87 sj_strstr (const char *s1
, const char *s2
)
89 register int len
= strlen ((char *) s2
);
94 if (strncmp (s1
, s2
, len
) == 0)
97 if (is_shift_jis (*s1
)) {
106 /*******************************************************************
107 Search char C from beginning of S.
108 S contain SHIFT JIS chars.
109 ********************************************************************/
111 sj_strchr (const char *s
, int c
)
116 if (is_shift_jis (*s
)) {
125 /*******************************************************************
126 Search char C end of S.
127 S contain SHIFT JIS chars.
128 ********************************************************************/
130 sj_strrchr (const char *s
, int c
)
138 if (is_shift_jis (*s
)) {
147 /*******************************************************************
149 ********************************************************************/
150 /* convesion buffer */
151 static char cvtbuf
[1024];
153 /*******************************************************************
155 ********************************************************************/
157 euc2sjis (register int hi
, register int lo
)
160 return ((hi
/ 2 + (hi
< 0xdf ? 0x31 : 0x71)) << 8) |
161 (lo
- (lo
>= 0xe0 ? 0x60 : 0x61));
163 return ((hi
/ 2 + (hi
< 0xdf ? 0x30 : 0x70)) << 8) | (lo
- 2);
167 sjis2euc (register int hi
, register int lo
)
170 return ((hi
* 2 - (hi
>= 0xe0 ? 0xe0 : 0x60)) << 8) | (lo
+ 2);
172 return ((hi
* 2 - (hi
>= 0xe0 ? 0xe1 : 0x61)) << 8) |
173 (lo
+ (lo
>= 0x7f ? 0x60 : 0x61));
176 /*******************************************************************
177 Convert FROM contain SHIFT JIS codes to EUC codes
178 return converted buffer
179 ********************************************************************/
181 sj_to_euc (const char *from
, BOOL overwrite
)
186 save
= (char *) from
;
187 for (out
= cvtbuf
; *from
;) {
188 if (is_shift_jis (*from
)) {
189 int code
= sjis2euc ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
190 *out
++ = (code
>> 8) & 0xff;
193 } else if (is_kana (*from
)) {
202 strcpy((char *) save
, (char *) cvtbuf
);
203 return (char *) save
;
209 /*******************************************************************
210 Convert FROM contain EUC codes to SHIFT JIS codes
211 return converted buffer
212 ********************************************************************/
214 euc_to_sj (const char *from
, BOOL overwrite
)
219 save
= (char *) from
;
220 for (out
= cvtbuf
; *from
; ) {
221 if (is_euc (*from
)) {
222 int code
= euc2sjis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
223 *out
++ = (code
>> 8) & 0xff;
226 } else if (is_euc_kana (*from
)) {
235 strcpy(save
, (char *) cvtbuf
);
242 /*******************************************************************
243 JIS7,JIS8,JUNET <-> SJIS
244 ********************************************************************/
246 sjis2jis (register int hi
, register int lo
)
249 return ((hi
* 2 - (hi
>= 0xe0 ? 0x160 : 0xe0)) << 8) | (lo
- 0x7e);
251 return ((hi
* 2 - (hi
>= 0xe0 ? 0x161 : 0xe1)) << 8) |
252 (lo
- (lo
>= 0x7f ? 0x20 : 0x1f));
256 jis2sjis (register int hi
, register int lo
)
259 return ((hi
/ 2 + (hi
< 0x5f ? 0x71 : 0xb1)) << 8) |
260 (lo
+ (lo
>= 0x60 ? 0x20 : 0x1f));
262 return ((hi
/ 2 + (hi
< 0x5f ? 0x70 : 0xb0)) << 8) | (lo
+ 0x7e);
265 /*******************************************************************
266 Convert FROM contain JIS codes to SHIFT JIS codes
267 return converted buffer
268 ********************************************************************/
270 jis8_to_sj (const char *from
, BOOL overwrite
)
273 register int shifted
;
277 save
= (char *) from
;
278 for (out
= cvtbuf
; *from
;) {
279 if (is_esc (*from
)) {
280 if (is_so1 (from
[1]) && is_so2 (from
[2])) {
283 } else if (is_si1 (from
[1]) && is_si2 (from
[2])) {
286 } else { /* sequence error */
298 int code
= jis2sjis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
299 *out
++ = (code
>> 8) & 0xff;
309 strcpy (save
, (char *) cvtbuf
);
316 /*******************************************************************
317 Convert FROM contain SHIFT JIS codes to JIS codes
318 return converted buffer
319 ********************************************************************/
321 sj_to_jis8 (const char *from
, BOOL overwrite
)
324 register int shifted
;
328 save
= (char *) from
;
329 for (out
= cvtbuf
; *from
; ) {
330 if (is_shift_jis (*from
)) {
333 case _KJ_ROMAN
: /* to KANJI */
340 code
= sjis2jis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
341 *out
++ = (code
>> 8) & 0xff;
346 case _KJ_KANJI
: /* to ROMAN/KANA */
357 case _KJ_KANJI
: /* to ROMAN/KANA */
366 strcpy (save
, (char *) cvtbuf
);
373 /*******************************************************************
374 Convert FROM contain 7 bits JIS codes to SHIFT JIS codes
375 return converted buffer
376 ********************************************************************/
378 jis7_to_sj (const char *from
, BOOL overwrite
)
381 register int shifted
;
385 save
= (char *) from
;
386 for (out
= cvtbuf
; *from
;) {
387 if (is_esc (*from
)) {
388 if (is_so1 (from
[1]) && is_so2 (from
[2])) {
391 } else if (is_si1 (from
[1]) && is_si2 (from
[2])) {
394 } else { /* sequence error */
397 } else if (is_so (*from
)) {
398 shifted
= _KJ_KANA
; /* to KANA */
400 } else if (is_si (*from
)) {
401 shifted
= _KJ_ROMAN
; /* to ROMAN */
412 int code
= jis2sjis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
413 *out
++ = (code
>> 8) & 0xff;
419 *out
++ = ((int) from
[0]) + 0x80;
426 strcpy (save
, (char *) cvtbuf
);
433 /*******************************************************************
434 Convert FROM contain SHIFT JIS codes to 7 bits JIS codes
435 return converted buffer
436 ********************************************************************/
438 sj_to_jis7 (const char *from
, BOOL overwrite
)
441 register int shifted
;
445 save
= (char *) from
;
446 for (out
= cvtbuf
; *from
; ) {
447 if (is_shift_jis (*from
)) {
451 *out
++ = jis_si
; /* to ROMAN and through down */
452 case _KJ_ROMAN
: /* to KANJI */
459 code
= sjis2jis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
460 *out
++ = (code
>> 8) & 0xff;
463 } else if (is_kana (from
[0])) {
465 case _KJ_KANJI
: /* to ROMAN */
469 case _KJ_ROMAN
: /* to KANA */
474 *out
++ = ((int) *from
++) - 0x80;
478 *out
++ = jis_si
; /* to ROMAN */
481 case _KJ_KANJI
: /* to ROMAN */
493 *out
++ = jis_si
; /* to ROMAN */
495 case _KJ_KANJI
: /* to ROMAN */
503 strcpy (save
, (char *) cvtbuf
);
510 /*******************************************************************
511 Convert FROM contain 7 bits JIS(junet) codes to SHIFT JIS codes
512 return converted buffer
513 ********************************************************************/
515 junet_to_sj (const char *from
, BOOL overwrite
)
518 register int shifted
;
522 save
= (char *) from
;
523 for (out
= cvtbuf
; *from
;) {
524 if (is_esc (*from
)) {
525 if (is_so1 (from
[1]) && is_so2 (from
[2])) {
528 } else if (is_si1 (from
[1]) && is_si2 (from
[2])) {
531 } else if (is_juk1(from
[1]) && is_juk2 (from
[2])) {
534 } else { /* sequence error */
546 int code
= jis2sjis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
547 *out
++ = (code
>> 8) & 0xff;
553 *out
++ = ((int) from
[0]) + 0x80;
560 strcpy (save
, (char *) cvtbuf
);
567 /*******************************************************************
568 Convert FROM contain SHIFT JIS codes to 7 bits JIS(junet) codes
569 return converted buffer
570 ********************************************************************/
572 sj_to_junet (const char *from
, BOOL overwrite
)
575 register int shifted
;
579 save
= (char *) from
;
580 for (out
= cvtbuf
; *from
; ) {
581 if (is_shift_jis (*from
)) {
585 case _KJ_ROMAN
: /* to KANJI */
592 code
= sjis2jis ((int) from
[0] & 0xff, (int) from
[1] & 0xff);
593 *out
++ = (code
>> 8) & 0xff;
596 } else if (is_kana (from
[0])) {
598 case _KJ_KANJI
: /* to ROMAN */
599 case _KJ_ROMAN
: /* to KANA */
601 *out
++ = junet_kana1
;
602 *out
++ = junet_kana2
;
606 *out
++ = ((int) *from
++) - 0x80;
610 case _KJ_KANJI
: /* to ROMAN */
622 case _KJ_KANJI
: /* to ROMAN */
630 strcpy (save
, (char *) cvtbuf
);
637 /*******************************************************************
639 ********************************************************************/
640 /* ":xx" -> a byte */
642 hex_to_sj (const char *from
, BOOL overwrite
)
649 if (*sp
== hex_tag
&& isxdigit (sp
[1]) && isxdigit (sp
[2])) {
650 *dp
++ = (hex2bin (sp
[1])<<4) | (hex2bin (sp
[2]));
657 strcpy ((char *) from
, (char *) cvtbuf
);
658 return (char *) from
;
664 /*******************************************************************
666 ********************************************************************/
668 sj_to_hex (const char *from
, BOOL overwrite
)
670 unsigned char *sp
, *dp
;
672 sp
= (unsigned char*) from
;
673 dp
= (unsigned char*) cvtbuf
;
677 *dp
++ = bin2hex (((*sp
)>>4)&0x0f);
678 *dp
++ = bin2hex ((*sp
)&0x0f);
680 } else if (is_shift_jis (*sp
) && is_shift_jis2 (sp
[1])) {
682 *dp
++ = bin2hex (((*sp
)>>4)&0x0f);
683 *dp
++ = bin2hex ((*sp
)&0x0f);
686 *dp
++ = bin2hex (((*sp
)>>4)&0x0f);
687 *dp
++ = bin2hex ((*sp
)&0x0f);
694 strcpy ((char *) from
, (char *) cvtbuf
);
695 return (char *) from
;
701 /*******************************************************************
703 ********************************************************************/
705 sj_to_cap (const char *from
, BOOL overwrite
)
707 unsigned char *sp
, *dp
;
709 sp
= (unsigned char*) from
;
710 dp
= (unsigned char*) cvtbuf
;
714 *dp
++ = bin2hex (((*sp
)>>4)&0x0f);
715 *dp
++ = bin2hex ((*sp
)&0x0f);
723 strcpy ((char *) from
, (char *) cvtbuf
);
724 return (char *) from
;
730 /*******************************************************************
732 ********************************************************************/
734 sj_to_sj (const char *from
, BOOL overwrite
)
737 strcpy (cvtbuf
, (char *) from
);
740 return (char *) from
;
744 /************************************************************************
746 _dos_to_unix _unix_to_dos
747 ************************************************************************/
749 char* (*_dos_to_unix
) (const char *str
, BOOL overwrite
) = sj_to_sj
;
750 char* (*_unix_to_dos
) (const char *str
, BOOL overwrite
) = sj_to_sj
;
753 setup_string_function (int codes
)
758 _dos_to_unix
= sj_to_sj
;
759 _unix_to_dos
= sj_to_sj
;
764 _dos_to_unix
= sj_to_euc
;
765 _unix_to_dos
= euc_to_sj
;
769 _dos_to_unix
= sj_to_jis7
;
770 _unix_to_dos
= jis7_to_sj
;
774 _dos_to_unix
= sj_to_jis8
;
775 _unix_to_dos
= jis8_to_sj
;
779 _dos_to_unix
= sj_to_junet
;
780 _unix_to_dos
= junet_to_sj
;
784 _dos_to_unix
= sj_to_hex
;
785 _unix_to_dos
= hex_to_sj
;
789 _dos_to_unix
= sj_to_cap
;
790 _unix_to_dos
= hex_to_sj
;
797 * Interpret coding system.
800 interpret_coding_system (char *str
, int def
)
804 if (strequal (str
, "sjis")) {
806 } else if (strequal (str
, "euc")) {
808 } else if (strequal (str
, "cap")) {
811 } else if (strequal (str
, "hex")) {
814 } else if (strncasecmp (str
, "hex", 3)) {
816 hex_tag
= (str
[3] ? str
[3] : HEXTAG
);
817 } else if (strequal (str
, "j8bb")) {
821 } else if (strequal (str
, "j8bj") || strequal (str
, "jis8")) {
825 } else if (strequal (str
, "j8bh")) {
829 } else if (strequal (str
, "j8@b")) {
833 } else if (strequal (str
, "j8@j")) {
837 } else if (strequal (str
, "j8@h")) {
841 } else if (strequal (str
, "j7bb")) {
845 } else if (strequal (str
, "j7bj") || strequal (str
, "jis7")) {
849 } else if (strequal (str
, "j7bh")) {
853 } else if (strequal (str
, "j7@b")) {
857 } else if (strequal (str
, "j7@j")) {
861 } else if (strequal (str
, "j7@h")) {
865 } else if (strequal (str
, "jubb")) {
869 } else if (strequal (str
, "jubj") || strequal (str
, "junet")) {
873 } else if (strequal (str
, "jubh")) {
877 } else if (strequal (str
, "ju@b")) {
881 } else if (strequal (str
, "ju@j")) {
885 } else if (strequal (str
, "ju@h")) {
890 return setup_string_function (codes
);
893 int kanji_dummy_procedure(void)