1
/* $Xorg: lcJis.c,v 1.3 2000/08/17 19:45:18 cpqbld Exp $ */
3
* Copyright 1992, 1993 by TOSHIBA Corp.
5
* Permission to use, copy, modify, and distribute this software and its
6
* documentation for any purpose and without fee is hereby granted, provided
7
* that the above copyright notice appear in all copies and that both that
8
* copyright notice and this permission notice appear in supporting
9
* documentation, and that the name of TOSHIBA not be used in advertising
10
* or publicity pertaining to distribution of the software without specific,
11
* written prior permission. TOSHIBA make no representations about the
12
* suitability of this software for any purpose. It is provided "as is"
13
* without express or implied warranty.
15
* TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
16
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
17
* TOSHIBA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
18
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
19
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
20
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23
* Author: Katsuhisa Yano TOSHIBA Corp.
24
* mopi@osa.ilab.toshiba.co.jp
26
/* $XFree86: xc/lib/X11/lcJis.c,v 1.10 2003/01/20 04:05:31 dawes Exp $ */
29
* A Japanese JIS locale.
30
* Supports: all locales with codeset JIS7.
31
* How: Provides converters for JIS.
32
* Platforms: Only those defining X_LOCALE (only Lynx, Linux-libc5, OS/2).
38
#include "XlcGeneric.h"
41
#if !defined(macII) && !defined(X_LOCALE)
45
typedef struct _StateRec {
48
XlcCharSet GL_charset;
49
XlcCharSet GR_charset;
56
State state = (State) conv->state;
57
XLCdGenericPart *gen = XLC_GENERIC_PART(state->lcd);
60
codeset = gen->initial_state_GL;
61
if (codeset && codeset->charset_list)
62
state->GL_charset = *codeset->charset_list;
63
codeset = gen->initial_state_GR;
64
if (codeset && codeset->charset_list)
65
state->GR_charset = *codeset->charset_list;
67
if (state->GL_charset == NULL)
68
if ((codeset = *gen->codeset_list) != NULL)
69
state->GL_charset = *codeset->charset_list;
78
const char *start = src;
80
while (length-- > 0) {
81
if (*src++ != *encoding++)
83
if (*encoding == '\0')
100
State state = (State) conv->state;
101
XLCd lcd = state->lcd;
104
unsigned char *mb_parse_table;
105
ParseInfo *parse_list, parse_info;
107
int length, number, encoding_len = 0;
110
src = *((const char **) from);
111
dst = *((char **) to);
113
mb_parse_table = XLC_GENERIC(lcd, mb_parse_table);
114
if (mb_parse_table != NULL) {
115
number = mb_parse_table[(unsigned char) *src];
117
parse_list = XLC_GENERIC(lcd, mb_parse_list) + number - 1;
118
for ( ; (parse_info = *parse_list) != NULL; parse_list++) {
119
encoding_len = compare(src, parse_info->encoding, *from_left);
120
if (encoding_len > 0) {
121
switch (parse_info->type) {
124
charset = *parse_info->codeset->charset_list;
129
charset = *parse_info->codeset->charset_list;
130
if (parse_info->type == E_LSL)
131
state->GL_charset = charset;
133
state->GR_charset = charset;
137
charset = state->GL_charset;
140
charset = state->GR_charset;
150
if ((*src & 0x80) && state->GR_charset)
151
charset = state->GR_charset;
153
charset = state->GL_charset;
156
if (charset == NULL ||
157
(num_args == 2 && (XlcCharSet) args[1] != charset))
160
length = charset->char_size;
161
if (length > *from_left - encoding_len)
165
if (length > *to_left)
167
if (charset->side == XlcGL) {
168
for (i = 0; i < length; i++)
169
*dst++ = *src++ & 0x7f;
170
} else if (charset->side == XlcGR) {
171
for (i = 0; i < length; i++)
172
*dst++ = *src++ | 0x80;
174
for (i = 0; i < length; i++)
177
*to = (XPointer) dst;
181
*from = (XPointer) src;
182
*from_left -= encoding_len + length;
183
state->charset = charset;
185
*((XlcCharSet *) args[0]) = charset;
200
XlcCharSet charset = NULL;
201
XPointer tmp_args[2], save_from = *from;
202
int ret, unconv_num = 0, tmp_num = 1;
204
tmp_args[0] = (XPointer) &charset;
206
while (*from_left > 0 && *to_left > 0) {
207
ret = mbtocs(conv, from, from_left, to, to_left, tmp_args, tmp_num);
211
if (tmp_num == 1 && charset) {
212
tmp_args[1] = (XPointer) charset;
217
if (save_from == *from)
221
*((XlcCharSet *) args[0]) = charset;
229
const wchar_t *wcstr)
232
unsigned long wc_encoding;
235
wc_encoding = *wcstr & XLC_GENERIC(lcd, wc_encode_mask);
236
num = XLC_GENERIC(lcd, codeset_num);
237
codeset = XLC_GENERIC(lcd, codeset_list);
239
if (wc_encoding == (*codeset)->wc_encoding)
257
State state = (State) conv->state;
258
XLCd lcd = state->lcd;
259
const wchar_t *wcptr;
265
unsigned long wc_encoding;
266
int wcstr_len, buf_len;
268
if (from == NULL || *from == NULL)
271
wcptr = *((const wchar_t **) from);
272
bufptr = *((char **) to);
273
wcstr_len = *from_left;
276
codeset = wc_parse_codeset(lcd, wcptr);
279
wc_encoding = codeset->wc_encoding;
281
if (wcstr_len < buf_len / codeset->length)
282
buf_len = wcstr_len * codeset->length;
284
for ( ; wcstr_len > 0 && buf_len > 0; wcptr++, wcstr_len--) {
286
if ((wch & XLC_GENERIC(lcd, wc_encode_mask)) != wc_encoding)
288
length = codeset->length;
293
if ((*codeset->charset_list)->side == XlcGL) {
295
*tmpptr-- = (unsigned char) (wch & 0x7f);
296
wch >>= (wchar_t)XLC_GENERIC(lcd, wc_shift_bits);
298
} else if ((*codeset->charset_list)->side == XlcGR) {
300
*tmpptr-- = (unsigned char) (wch | 0x80);
301
wch >>= (wchar_t)XLC_GENERIC(lcd, wc_shift_bits);
305
*tmpptr-- = (unsigned char) wch;
306
wch >>= (wchar_t)XLC_GENERIC(lcd, wc_shift_bits);
312
*((XlcCharSet *) args[0]) = *codeset->charset_list;
314
*from_left -= wcptr - *((wchar_t **) from);
315
*from = (XPointer) wcptr;
317
*to_left -= bufptr - *((char **) to);
324
GetCodeSetFromCharSet(
328
CodeSet *codeset = XLC_GENERIC(lcd, codeset_list);
329
XlcCharSet *charset_list;
330
int codeset_num, num_charsets;
332
codeset_num = XLC_GENERIC(lcd, codeset_num);
334
for ( ; codeset_num-- > 0; codeset++) {
335
num_charsets = (*codeset)->num_charsets;
336
charset_list = (*codeset)->charset_list;
338
for ( ; num_charsets-- > 0; charset_list++)
339
if (*charset_list == charset)
343
return (CodeSet) NULL;
356
State state = (State) conv->state;
361
int num, encoding_len = 0;
367
csptr = *((const char **) from);
368
bufptr = *((char **) to);
369
csstr_len = *from_left;
375
charset = (XlcCharSet) args[0];
377
codeset = GetCodeSetFromCharSet(state->lcd, charset);
382
if (codeset->parse_info) {
383
switch (type = codeset->parse_info->type) {
385
encoding_len = strlen(codeset->parse_info->encoding);
390
if (charset == state->GL_charset)
393
if (charset == state->GR_charset)
396
encoding_len = strlen(codeset->parse_info->encoding);
397
if (encoding_len > buf_len)
399
cvt_length += encoding_len;
401
strcpy(bufptr, codeset->parse_info->encoding);
402
bufptr += encoding_len;
404
buf_len -= encoding_len;
407
state->GL_charset = charset;
409
state->GR_charset = charset;
416
csstr_len /= codeset->length;
417
buf_len /= codeset->length + encoding_len;
418
if (csstr_len < buf_len)
421
cvt_length += buf_len * (encoding_len + codeset->length);
425
strcpy(bufptr, codeset->parse_info->encoding);
426
bufptr += encoding_len;
428
num = codeset->length;
429
if (codeset->side == XlcGL) {
431
*bufptr++ = *csptr++ & 0x7f;
432
} else if (codeset->side == XlcGR) {
434
*bufptr++ = *csptr++ | 0x80;
437
*bufptr++ = *csptr++;
442
*from_left -= csptr - *((char **) from);
443
*from = (XPointer) csptr;
446
*to = (XPointer) bufptr;
447
*to_left -= cvt_length;
462
State state = (State) conv->state;
463
XLCd lcd = state->lcd;
469
unsigned long code_mask, wc_encoding;
470
int num, length, wc_shift_bits;
473
csptr = *((const char **) from);
474
bufptr = *((wchar_t **) to);
475
csstr_len = *from_left;
481
codeset = GetCodeSetFromCharSet(lcd, (XlcCharSet) args[0]);
485
length = codeset->length;
487
if (csstr_len < buf_len)
490
code_mask = ~XLC_GENERIC(lcd, wc_encode_mask);
491
wc_encoding = codeset->wc_encoding;
492
wc_shift_bits = XLC_GENERIC(lcd, wc_shift_bits);
498
wch = (wchar_t) (*csptr++ & 0x7f);
501
wch = (wch << wc_shift_bits) | (*csptr++ & 0x7f);
503
*bufptr++ = (wch & code_mask) | wc_encoding;
507
*from_left -= csptr - *((char **) from);
508
*from = (XPointer) csptr;
511
*to = (XPointer) bufptr;
522
Xfree((char *) conv->state);
525
Xfree((char *) conv);
531
XlcConvMethods methods)
536
conv = (XlcConv) Xmalloc(sizeof(XlcConvRec));
538
return (XlcConv) NULL;
540
conv->methods = (XlcConvMethods) Xmalloc(sizeof(XlcConvMethodsRec));
541
if (conv->methods == NULL)
543
*conv->methods = *methods;
544
if (XLC_PUBLIC(lcd, is_state_depend))
545
conv->methods->reset = init_state;
547
conv->state = (XPointer) Xmalloc(sizeof(StateRec));
548
if (conv->state == NULL)
550
bzero((char *) conv->state, sizeof(StateRec));
552
state = (State) conv->state;
559
close_converter(conv);
561
return (XlcConv) NULL;
564
static XlcConvMethodsRec mbstocs_methods = {
573
const char *from_type,
577
return create_conv(from_lcd, &mbstocs_methods);
580
static XlcConvMethodsRec wcstocs_methods = {
589
const char *from_type,
593
return create_conv(from_lcd, &wcstocs_methods);
596
static XlcConvMethodsRec mbtocs_methods = {
605
const char *from_type,
609
return create_conv(from_lcd, &mbtocs_methods);
612
static XlcConvMethodsRec cstombs_methods = {
621
const char *from_type,
625
return create_conv(from_lcd, &cstombs_methods);
628
static XlcConvMethodsRec cstowcs_methods = {
637
const char *from_type,
641
return create_conv(from_lcd, &cstowcs_methods);
655
const char *src = *((const char **) from);
656
wchar_t *dst = *((wchar_t **) to);
657
int src_left = *from_left;
658
int dst_left = *to_left;
661
while (src_left > 0 && dst_left > 0) {
662
length = mbtowc(dst, src, src_left);
679
if (*from_left == src_left)
682
*from = (XPointer) src;
684
*to = (XPointer) dst;
685
*from_left = src_left;
701
const wchar_t *src = *((const wchar_t **) from);
702
char *dst = *((char **) to);
703
int src_left = *from_left;
704
int dst_left = *to_left;
707
while (src_left > 0 && dst_left > 0) {
708
length = wctomb(dst, *src); /* XXX */
709
if (length < 0 || dst_left < length)
724
if (*from_left == src_left)
727
*from = (XPointer) src;
728
*to = (XPointer) dst;
729
*from_left = src_left;
745
const wchar_t *src = *((const wchar_t **) from);
747
XlcCharSet charset = NULL;
748
XPointer tmp_args[2], tmp_from, save_from = *from;
750
int length, ret, src_left = *from_left;
751
int unconv_num = 0, tmp_num = 1;
753
tmp_args[0] = (XPointer) &charset;
755
while (src_left > 0 && *to_left > 0) {
757
length = wctomb(tmp, wch);
766
tmp_from = (XPointer) tmp;
767
ret = mbtocs(conv, &tmp_from, &length, to, to_left, tmp_args, tmp_num);
771
if (tmp_num == 1 && charset) {
772
tmp_args[1] = (XPointer) charset;
780
if (save_from == (XPointer) src)
783
*from = (XPointer) src;
784
*from_left = src_left;
787
*((XlcCharSet *) args[0]) = charset;
792
#define DefineLocalBuf char local_buf[BUFSIZ]
793
#define AllocLocalBuf(length) (length > BUFSIZ ? (char*) Xmalloc(length) : local_buf)
794
#define FreeLocalBuf(ptr) if (ptr != local_buf) Xfree(ptr)
806
XLCd lcd = ((State) conv->state)->lcd;
808
XPointer buf, save_buf;
809
int length, left, ret;
811
left = length = *to_left * XLC_PUBLIC(lcd, mb_cur_max);
812
buf = save_buf = (XPointer) AllocLocalBuf(length);
816
ret = cstombs(conv, from, from_left, &buf, &left, args, num_args);
822
if (stdc_mbstowcs(conv, &buf, &length, to, to_left, args, num_args) < 0)
826
FreeLocalBuf(save_buf);
831
static XlcConvMethodsRec stdc_mbstowcs_methods = {
840
const char *from_type,
844
return create_conv(from_lcd, &stdc_mbstowcs_methods);
847
static XlcConvMethodsRec stdc_wcstombs_methods = {
856
const char *from_type,
860
return create_conv(from_lcd, &stdc_wcstombs_methods);
863
static XlcConvMethodsRec stdc_wcstocs_methods = {
872
const char *from_type,
876
return create_conv(from_lcd, &stdc_wcstocs_methods);
879
static XlcConvMethodsRec stdc_cstowcs_methods = {
888
const char *from_type,
892
return create_conv(from_lcd, &stdc_cstowcs_methods);
902
XLCdGenericPart *gen;
905
lcd = _XlcCreateLC(name, _XlcGenericMethods);
909
if (!XLC_PUBLIC_PART(lcd)->codeset ||
910
(_XlcCompareISOLatin1(XLC_PUBLIC_PART(lcd)->codeset, "JIS7"))) {
915
_XlcSetConverter(lcd, XlcNMultiByte, lcd, XlcNChar, open_mbtocs);
916
_XlcSetConverter(lcd, XlcNMultiByte, lcd, XlcNCharSet, open_mbstocs);
917
_XlcSetConverter(lcd, XlcNCharSet, lcd, XlcNMultiByte, open_cstombs);
920
gen = XLC_GENERIC_PART(lcd);
922
if (gen->use_stdc_env == True) {
923
_XlcSetConverter(lcd,XlcNMultiByte,lcd,XlcNWideChar,open_stdc_mbstowcs);
924
_XlcSetConverter(lcd,XlcNWideChar,lcd,XlcNMultiByte,open_stdc_wcstombs);
926
if (gen->force_convert_to_mb == True) {
927
_XlcSetConverter(lcd, XlcNWideChar, lcd, XlcNCharSet,open_stdc_wcstocs);
928
_XlcSetConverter(lcd, XlcNCharSet, lcd, XlcNWideChar,open_stdc_cstowcs);
931
_XlcSetConverter(lcd, XlcNWideChar, lcd, XlcNCharSet, open_wcstocs);
932
_XlcSetConverter(lcd, XlcNCharSet, lcd, XlcNWideChar, open_cstowcs);
937
_XlcAddUtf8Converters(lcd);
944
#endif /* X_LOCALE */