15d9d0b528387242011cdcc6190c9e598cfe3a07yy * CDDL HEADER START
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * The contents of this file are subject to the terms of the
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Common Development and Distribution License (the "License").
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * You may not use this file except in compliance with the License.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * See the License for the specific language governing permissions
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * and limitations under the License.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * When distributing Covered Code, include this CDDL HEADER in each
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * If applicable, add the following below this CDDL HEADER, with the
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * fields enclosed by brackets "[]" replaced with your own identifying
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * information: Portions Copyright [yyyy] [name of copyright owner]
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * CDDL HEADER END
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Use is subject to license terms.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy#pragma ident "%Z%%M% %I% %E% SMI"
15d9d0b528387242011cdcc6190c9e598cfe3a07yystatic int8_t gb2312_to_utf8(uchar_t byte1, uchar_t byte2, uchar_t *ob,
15d9d0b528387242011cdcc6190c9e598cfe3a07yystatic int8_t gbk_to_utf8(uint32_t gbk_val, uchar_t *ob, uchar_t *obtail,
15d9d0b528387242011cdcc6190c9e598cfe3a07yystatic int8_t utf8_to_gb2312(uint32_t utf8, uchar_t **inbuf, uchar_t *ibtail,
15d9d0b528387242011cdcc6190c9e598cfe3a07yystatic int8_t utf8_to_gbk(uint32_t utf8, uchar_t **inbuf, uchar_t *ibtail,
15d9d0b528387242011cdcc6190c9e598cfe3a07yystatic int8_t utf8_to_gb18030(uint32_t utf8, uchar_t **inbuf, uchar_t *ibtail,
15d9d0b528387242011cdcc6190c9e598cfe3a07yystatic void *
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return ((void *)KICONV_SC_GB18030);
15d9d0b528387242011cdcc6190c9e598cfe3a07yystatic void *
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return ((void *)KICONV_SC_GBK);
15d9d0b528387242011cdcc6190c9e598cfe3a07yystatic void *
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return ((void *)KICONV_SC_EUCCN);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (0);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Encoding convertor from UTF-8 to GB18030.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconv_to_gb18030(void *kcd, char **inbuf, size_t *inbytesleft,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return kiconv_utf8_to_cck(kcd, inbuf, inbytesleft, outbuf,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * String based encoding convertor from UTF-8 to GB18030.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconvstr_to_gb18030(char *inarray, size_t *inlen, char *outarray,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy (uchar_t *)outarray, outlen, flag, errno, utf8_to_gb18030);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Encoding convertor from GB18030 to UTF-8.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconv_fr_gb18030(void *kcd, char **inbuf, size_t *inbytesleft,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* Check on the kiconv code conversion descriptor. */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* If this is a state reset request, process and return. */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (0);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Issue EILSEQ error if the first byte is not a
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * valid GB18030 leading byte.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy if (sz < 0) {
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * String based encoding convertor from GB18030 to UTF-8.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconvstr_fr_gb18030(char *inarray, size_t *inlen, char *outarray,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy if (sz < 0) {
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Encoding convertor from UTF-8 to GBK.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconv_to_gbk(void *kcd, char **inbuf, size_t *inbytesleft,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return kiconv_utf8_to_cck(kcd, inbuf, inbytesleft, outbuf,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * String based encoding convertor from UTF-8 to GBK.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconvstr_to_gbk(char *inarray, size_t *inlen, char *outarray,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Encoding convertor from GBK to UTF-8.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconv_fr_gbk(void *kcd, char **inbuf, size_t *inbytesleft,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* Check on the kiconv code conversion descriptor. */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* If this is a state reset request, process and return. */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (0);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Issue EILSEQ error if the first byte is not a
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * valid GBK leading byte.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Issue EINVAL error if input buffer has an incomplete
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * character at the end of the buffer.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Issue EILSEQ error if the remaining byte is not
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * a valid GBK byte.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* Now we have a valid GBK character. */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy if (sz < 0) {
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * String based encoding convertor from GBK to UTF-8.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconvstr_fr_gbk(char *inarray, size_t *inlen, char *outarray,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy if (sz < 0) {
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Encoding convertor from UTF-8 to EUC-CN.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconv_to_euccn(void *kcd, char **inbuf, size_t *inbytesleft,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return kiconv_utf8_to_cck(kcd, inbuf, inbytesleft, outbuf,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * String based encoding convertor from UTF-8 to EUC-CN.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconvstr_to_euccn(char *inarray, size_t *inlen, char *outarray,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy (uchar_t *)outarray, outlen, flag, errno, utf8_to_gb2312);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Encoding converto from EUC-CN to UTF-8 code.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconv_fr_euccn(void *kcd, char **inbuf, size_t *inbytesleft,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* Check on the kiconv code conversion descriptor. */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* If this is a state reset request, process and return. */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (0);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Issue EILSEQ error if the first byte is not a
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * valid GB2312 leading byte.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Issue EINVAL error if input buffer has an incomplete
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * character at the end of the buffer.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Issue EILSEQ error if the remaining byte is not
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * a valid GB2312 byte.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* Now we have a valid GB2312 character */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy sz = gb2312_to_utf8(*ib, *(ib + 1), ob, obtail, &ret_val);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy if (sz < 0) {
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * String based encoding convertor from EUC-CN to UTF-8.
15d9d0b528387242011cdcc6190c9e598cfe3a07yykiconvstr_fr_euccn(char *inarray, size_t *inlen, char *outarray,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy sz = gb2312_to_utf8(*ib, *(ib + 1), ob, obtail, &ret_val);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy if (sz < 0) {
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Convert single GB2312 character to UTF-8.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Return: > 0 - Converted successfully
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * = -1 - E2BIG
15d9d0b528387242011cdcc6190c9e598cfe3a07yygb2312_to_utf8(uchar_t b1, uchar_t b2, uchar_t *ob, uchar_t *obtail,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* index = (b1 - KICONV_EUC_START) * 94 + b2 - KICONV_EUC_START; */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy index = KICONV_GB2312_UTF8_MAX - 1; /* Map to 0xEFBFBD */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (-1);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * As kiconv_gb2312_utf8 contain muliple KICONV_UTF8_REPLACEMENT_CHAR
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * elements, so need to ckeck more.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (sz);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Convert single GB18030 or GBK character to UTF-8.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Return: > 0 - Converted successfully
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * = -1 - E2BIG
15d9d0b528387242011cdcc6190c9e598cfe3a07yygbk_to_utf8(uint32_t gbk_val, uchar_t *ob, uchar_t *obtail, size_t *ret_val,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * u32 = ((gbk_val >> 24) - 0x90) * 12600 +
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * (((gbk_val & 0xFF0000) >> 16) - 0x30) * 1260 +
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * (((gbk_val & 0xFF00) >> 8) - 0x81) * 10 +
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * (gbk_val & 0xFF - 0x30)+
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * KICONV_SC_PLANE1_UCS4_START;
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (-1);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (sz);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Convert single UTF-8 character to GB18030.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Return: > 0 - Converted successfully
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * = -1 - E2BIG
15d9d0b528387242011cdcc6190c9e598cfe3a07yy/* ARGSUSED */
15d9d0b528387242011cdcc6190c9e598cfe3a07yyutf8_to_gb18030(uint32_t utf8, uchar_t **inbuf, uchar_t *ibtail,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* Four bytes GB18030 [0x90308130, 0xe339fe39] handling. */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy u32 = (((utf8 & 0x07000000) >> 6) | ((utf8 & 0x3F0000) >> 4) |
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (-1);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Convert single UTF-8 character to GBK.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Return: > 0 - Converted successfully
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * = -1 - E2BIG
15d9d0b528387242011cdcc6190c9e598cfe3a07yy/* ARGSUSED */
15d9d0b528387242011cdcc6190c9e598cfe3a07yyutf8_to_gbk(uint32_t utf8, uchar_t **inbuf, uchar_t *ibtail,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy /* GBK and GB18030 share the same table, so check the length. */
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (-1);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Convert single UTF-8 character to GB2312.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Return: > 0 - Converted successfully
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * = -1 - E2BIG
15d9d0b528387242011cdcc6190c9e598cfe3a07yy/* ARGSUSED */
15d9d0b528387242011cdcc6190c9e598cfe3a07yyutf8_to_gb2312(uint32_t utf8, uchar_t **inbuf, uchar_t *intail,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy return (-1);
15d9d0b528387242011cdcc6190c9e598cfe3a07yy "gb18030", "utf-8", kiconv_open_to_cck, kiconv_to_gb18030,
15d9d0b528387242011cdcc6190c9e598cfe3a07yy sizeof (kiconv_sc_ops_tbl) / sizeof (kiconv_sc_ops_tbl[0]),
15d9d0b528387242011cdcc6190c9e598cfe3a07yy "kiconv Simplified Chinese module 1.0",
15d9d0b528387242011cdcc6190c9e598cfe3a07yy cmn_err(CE_WARN, "kiconv_sc: failed to load kernel module");
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * If this module is being used, then, we cannot remove the module.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * The following checking will catch pretty much all usual cases.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * Any remaining will be catached by the kiconv_unregister_module()
15d9d0b528387242011cdcc6190c9e598cfe3a07yy * during mod_remove() at below.
15d9d0b528387242011cdcc6190c9e598cfe3a07yy cmn_err(CE_WARN, "kiconv_sc: failed to remove kernel module");