2 * Copyright 1992, 1993 by TOSHIBA Corp.
4 * Permission to use, copy, modify, and distribute this software and its
5 * documentation for any purpose and without fee is hereby granted, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of TOSHIBA not be used in advertising
9 * or publicity pertaining to distribution of the software without specific,
10 * written prior permission. TOSHIBA make no representations about the
11 * suitability of this software for any purpose. It is provided "as is"
12 * without express or implied warranty.
14 * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
16 * TOSHIBA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
19 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 * Author: Katsuhisa Yano TOSHIBA Corp.
23 * mopi@osa.ilab.toshiba.co.jp
27 * A Japanese JIS locale.
28 * Supports: all locales with codeset JIS7.
29 * How: Provides converters for JIS.
30 * Platforms: Only those defining X_LOCALE (only Lynx, Linux-libc5, OS/2).
39 #include "XlcGeneric.h"
42 #if !defined(X_LOCALE)
46 typedef struct _StateRec {
49 XlcCharSet GL_charset;
50 XlcCharSet GR_charset;
57 State state = (State) conv->state;
58 XLCdGenericPart *gen = XLC_GENERIC_PART(state->lcd);
61 codeset = gen->initial_state_GL;
62 if (codeset && codeset->charset_list)
63 state->GL_charset = *codeset->charset_list;
64 codeset = gen->initial_state_GR;
65 if (codeset && codeset->charset_list)
66 state->GR_charset = *codeset->charset_list;
68 if (state->GL_charset == NULL)
69 if ((codeset = *gen->codeset_list) != NULL)
70 state->GL_charset = *codeset->charset_list;
79 const char *start = src;
81 while (length-- > 0) {
82 if (*src++ != *encoding++)
84 if (*encoding == '\0')
101 State state = (State) conv->state;
102 XLCd lcd = state->lcd;
105 unsigned char *mb_parse_table;
106 ParseInfo *parse_list, parse_info;
108 int length, number, encoding_len = 0;
111 src = *((const char **) from);
112 dst = *((char **) to);
114 mb_parse_table = XLC_GENERIC(lcd, mb_parse_table);
115 if (mb_parse_table != NULL) {
116 number = mb_parse_table[(unsigned char) *src];
118 parse_list = XLC_GENERIC(lcd, mb_parse_list) + number - 1;
119 for ( ; (parse_info = *parse_list) != NULL; parse_list++) {
120 encoding_len = compare(src, parse_info->encoding, *from_left);
121 if (encoding_len > 0) {
122 switch (parse_info->type) {
125 charset = *parse_info->codeset->charset_list;
130 charset = *parse_info->codeset->charset_list;
131 if (parse_info->type == E_LSL)
132 state->GL_charset = charset;
134 state->GR_charset = charset;
138 charset = state->GL_charset;
141 charset = state->GR_charset;
151 if ((*src & 0x80) && state->GR_charset)
152 charset = state->GR_charset;
154 charset = state->GL_charset;
157 if (charset == NULL ||
158 (num_args == 2 && (XlcCharSet) args[1] != charset))
161 length = charset->char_size;
162 if (length > *from_left - encoding_len)
166 if (length > *to_left)
168 if (charset->side == XlcGL) {
169 for (i = 0; i < length; i++)
170 *dst++ = *src++ & 0x7f;
171 } else if (charset->side == XlcGR) {
172 for (i = 0; i < length; i++)
173 *dst++ = *src++ | 0x80;
175 for (i = 0; i < length; i++)
178 *to = (XPointer) dst;
182 *from = (XPointer) src;
183 *from_left -= encoding_len + length;
184 state->charset = charset;
186 *((XlcCharSet *) args[0]) = charset;
201 XlcCharSet charset = NULL;
202 XPointer tmp_args[2], save_from = *from;
203 int ret, unconv_num = 0, tmp_num = 1;
205 tmp_args[0] = (XPointer) &charset;
207 while (*from_left > 0 && *to_left > 0) {
208 ret = mbtocs(conv, from, from_left, to, to_left, tmp_args, tmp_num);
212 if (tmp_num == 1 && charset) {
213 tmp_args[1] = (XPointer) charset;
218 if (save_from == *from)
222 *((XlcCharSet *) args[0]) = charset;
230 const wchar_t *wcstr)
233 unsigned long wc_encoding;
236 wc_encoding = *wcstr & XLC_GENERIC(lcd, wc_encode_mask);
237 num = XLC_GENERIC(lcd, codeset_num);
238 codeset = XLC_GENERIC(lcd, codeset_list);
240 if (wc_encoding == (*codeset)->wc_encoding)
258 State state = (State) conv->state;
259 XLCd lcd = state->lcd;
260 const wchar_t *wcptr;
266 unsigned long wc_encoding;
267 int wcstr_len, buf_len;
269 if (from == NULL || *from == NULL)
272 wcptr = *((const wchar_t **) from);
273 bufptr = *((char **) to);
274 wcstr_len = *from_left;
277 codeset = wc_parse_codeset(lcd, wcptr);
280 wc_encoding = codeset->wc_encoding;
282 if (wcstr_len < buf_len / codeset->length)
283 buf_len = wcstr_len * codeset->length;
285 for ( ; wcstr_len > 0 && buf_len > 0; wcptr++, wcstr_len--) {
287 if ((wch & XLC_GENERIC(lcd, wc_encode_mask)) != wc_encoding)
289 length = codeset->length;
294 if ((*codeset->charset_list)->side == XlcGL) {
296 *tmpptr-- = (unsigned char) (wch & 0x7f);
297 wch >>= (wchar_t)XLC_GENERIC(lcd, wc_shift_bits);
299 } else if ((*codeset->charset_list)->side == XlcGR) {
301 *tmpptr-- = (unsigned char) (wch | 0x80);
302 wch >>= (wchar_t)XLC_GENERIC(lcd, wc_shift_bits);
306 *tmpptr-- = (unsigned char) wch;
307 wch >>= (wchar_t)XLC_GENERIC(lcd, wc_shift_bits);
313 *((XlcCharSet *) args[0]) = *codeset->charset_list;
315 *from_left -= wcptr - *((wchar_t **) from);
316 *from = (XPointer) wcptr;
318 *to_left -= bufptr - *((char **) to);
325 GetCodeSetFromCharSet(
329 CodeSet *codeset = XLC_GENERIC(lcd, codeset_list);
330 XlcCharSet *charset_list;
331 int codeset_num, num_charsets;
333 codeset_num = XLC_GENERIC(lcd, codeset_num);
335 for ( ; codeset_num-- > 0; codeset++) {
336 num_charsets = (*codeset)->num_charsets;
337 charset_list = (*codeset)->charset_list;
339 for ( ; num_charsets-- > 0; charset_list++)
340 if (*charset_list == charset)
344 return (CodeSet) NULL;
357 State state = (State) conv->state;
362 int num, encoding_len = 0;
368 csptr = *((const char **) from);
369 bufptr = *((char **) to);
370 csstr_len = *from_left;
376 charset = (XlcCharSet) args[0];
378 codeset = GetCodeSetFromCharSet(state->lcd, charset);
383 if (codeset->parse_info) {
384 switch (type = codeset->parse_info->type) {
386 encoding_len = strlen(codeset->parse_info->encoding);
391 if (charset == state->GL_charset)
394 if (charset == state->GR_charset)
397 encoding_len = strlen(codeset->parse_info->encoding);
398 if (encoding_len > buf_len)
400 cvt_length += encoding_len;
402 strcpy(bufptr, codeset->parse_info->encoding);
403 bufptr += encoding_len;
405 buf_len -= encoding_len;
408 state->GL_charset = charset;
410 state->GR_charset = charset;
417 csstr_len /= codeset->length;
418 buf_len /= codeset->length + encoding_len;
419 if (csstr_len < buf_len)
422 cvt_length += buf_len * (encoding_len + codeset->length);
426 strcpy(bufptr, codeset->parse_info->encoding);
427 bufptr += encoding_len;
429 num = codeset->length;
430 if (codeset->side == XlcGL) {
432 *bufptr++ = *csptr++ & 0x7f;
433 } else if (codeset->side == XlcGR) {
435 *bufptr++ = *csptr++ | 0x80;
438 *bufptr++ = *csptr++;
443 *from_left -= csptr - *((char **) from);
444 *from = (XPointer) csptr;
447 *to = (XPointer) bufptr;
448 *to_left -= cvt_length;
463 State state = (State) conv->state;
464 XLCd lcd = state->lcd;
470 unsigned long code_mask, wc_encoding;
471 int num, length, wc_shift_bits;
474 csptr = *((const char **) from);
475 bufptr = *((wchar_t **) to);
476 csstr_len = *from_left;
482 codeset = GetCodeSetFromCharSet(lcd, (XlcCharSet) args[0]);
486 length = codeset->length;
488 if (csstr_len < buf_len)
491 code_mask = ~XLC_GENERIC(lcd, wc_encode_mask);
492 wc_encoding = codeset->wc_encoding;
493 wc_shift_bits = XLC_GENERIC(lcd, wc_shift_bits);
499 wch = (wchar_t) (*csptr++ & 0x7f);
502 wch = (wch << wc_shift_bits) | (*csptr++ & 0x7f);
504 *bufptr++ = (wch & code_mask) | wc_encoding;
508 *from_left -= csptr - *((char **) from);
509 *from = (XPointer) csptr;
512 *to = (XPointer) bufptr;
523 Xfree((char *) conv->state);
526 Xfree((char *) conv);
532 XlcConvMethods methods)
537 conv = (XlcConv) Xmalloc(sizeof(XlcConvRec));
539 return (XlcConv) NULL;
541 conv->methods = (XlcConvMethods) Xmalloc(sizeof(XlcConvMethodsRec));
542 if (conv->methods == NULL)
544 *conv->methods = *methods;
545 if (XLC_PUBLIC(lcd, is_state_depend))
546 conv->methods->reset = init_state;
548 conv->state = Xcalloc(1, sizeof(StateRec));
549 if (conv->state == NULL)
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 */