📄 table.c
字号:
/* * Copyright (c) 2003-2004, Artem B. Bityuckiy * Copyright (c) 1999,2000, Konstantin Chuguev. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include "cesbi.h"#if defined (ICONV_TO_UCS_CES_TABLE) \ || defined (ICONV_FROM_UCS_CES_TABLE) #include <_ansi.h>#include <reent.h>#include <newlib.h>#include <sys/types.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <sys/iconvnls.h>#include "../lib/endian.h"#include "../lib/local.h"#include "../lib/ucsconv.h"#include "../ccs/ccs.h"/* * Table-based CES converter is implemented here. Table-based CES converter * deals with encodings with "null" CES, like KOI8-R. In this case it is * possible to implement one generic algorithm which works with different * CCS tables. * * Table-based CES converter deals with CCS tables placed into iconv/ccs * subdirectory. First, converter tries to find needed CCS table among * linked-in tables. If not found, it tries to load it from external file * (only if corespondent capability was enabled in Newlib configuration). * * 16 bit encodings are assumed to be Big Endian. */static ucs2_t_EXFUN(find_code_size, (ucs2_t code, _CONST __uint16_t *tblp));static __inline ucs2_t_EXFUN(find_code_speed, (ucs2_t code, _CONST __uint16_t *tblp));static __inline ucs2_t_EXFUN(find_code_speed_8bit, (ucs2_t code, _CONST unsigned char *tblp));#ifdef _ICONV_ENABLE_EXTERNAL_CCSstatic _CONST iconv_ccs_desc_t *_EXFUN(load_file, (struct _reent *rptr, _CONST char *name, int direction));#endif/* * Interface data and functions implementation. */static size_t_DEFUN(table_close, (rptr, data), struct _reent *rptr _AND _VOID_PTR data){ _CONST iconv_ccs_desc_t *ccsp = (iconv_ccs_desc_t *)data; if (ccsp->type == TABLE_EXTERNAL) _free_r (rptr, (_VOID_PTR)ccsp->tbl); _free_r( rptr, (_VOID_PTR)ccsp); return 0;}#if defined (ICONV_FROM_UCS_CES_TABLE)static _VOID_PTR_DEFUN(table_init_from_ucs, (rptr, encoding), struct _reent *rptr _AND _CONST char *encoding){ int i; _CONST iconv_ccs_t *biccsp = NULL; iconv_ccs_desc_t *ccsp; for (i = 0; _iconv_ccs[i] != NULL; i++) if (strcmp (_iconv_ccs[i]->name, encoding) == 0) { biccsp = _iconv_ccs[i]; break; } if (biccsp != NULL) { if (biccsp->from_ucs == NULL || (ccsp = (iconv_ccs_desc_t *) _malloc_r (rptr, sizeof (iconv_ccs_desc_t))) == NULL) return NULL; ccsp->type = TABLE_BUILTIN; ccsp->bits = biccsp->bits; ccsp->optimization = biccsp->from_ucs_type; ccsp->tbl = biccsp->from_ucs; return (_VOID_PTR)ccsp; } #ifdef _ICONV_ENABLE_EXTERNAL_CCS return (_VOID_PTR)load_file (rptr, encoding, 1);#else return NULL;#endif}static size_t_DEFUN(table_convert_from_ucs, (data, in, outbuf, outbytesleft), _VOID_PTR data _AND ucs4_t in _AND unsigned char **outbuf _AND size_t *outbytesleft){ _CONST iconv_ccs_desc_t *ccsp = (iconv_ccs_desc_t *)data; ucs2_t code; if (in > 0xFFFF || in == INVALC) return (size_t)ICONV_CES_INVALID_CHARACTER; if (ccsp->bits == TABLE_8BIT) { code = find_code_speed_8bit ((ucs2_t)in, (_CONST unsigned char *)ccsp->tbl); if (code == INVALC) return (size_t)ICONV_CES_INVALID_CHARACTER; **outbuf = (unsigned char)code; *outbuf += 1; *outbytesleft -= 1; return 1; } else if (ccsp->optimization == TABLE_SPEED_OPTIMIZED) code = find_code_speed ((ucs2_t)in, ccsp->tbl); else code = find_code_size ((ucs2_t)in, ccsp->tbl); if (code == INVALC) return (size_t)ICONV_CES_INVALID_CHARACTER; if (*outbytesleft < 2) return (size_t)ICONV_CES_NOSPACE; /* We can't store whole word since **outbuf may be not 2-byte aligned */ **outbuf = (unsigned char)((ucs2_t)code >> 8); *(*outbuf + 1) = (unsigned char)code; *outbuf += 2; *outbytesleft -= 2; return 2; }#endif /* ICONV_FROM_UCS_CES_TABLE */#if defined (ICONV_TO_UCS_CES_TABLE)static _VOID_PTR_DEFUN(table_init_to_ucs, (rptr, encoding), struct _reent *rptr _AND _CONST char *encoding){ int i; _CONST iconv_ccs_t *biccsp = NULL; iconv_ccs_desc_t *ccsp; for (i = 0; _iconv_ccs[i] != NULL; i++) if (strcmp (_iconv_ccs[i]->name, encoding) == 0) { biccsp = _iconv_ccs[i]; break; } if (biccsp != NULL) { if (biccsp->to_ucs == NULL || (ccsp = (iconv_ccs_desc_t *) _malloc_r (rptr, sizeof (iconv_ccs_desc_t))) == NULL) return NULL; ccsp->type = TABLE_BUILTIN; ccsp->bits = biccsp->bits; ccsp->optimization = biccsp->to_ucs_type; ccsp->tbl = biccsp->to_ucs; return (_VOID_PTR)ccsp; } #ifdef _ICONV_ENABLE_EXTERNAL_CCS return (_VOID_PTR)load_file (rptr, encoding, 0);#else return NULL;#endif}static ucs4_t_DEFUN(table_convert_to_ucs, (data, inbuf, inbytesleft), _VOID_PTR data _AND _CONST unsigned char **inbuf _AND size_t *inbytesleft){ _CONST iconv_ccs_desc_t *ccsp = (iconv_ccs_desc_t *)data; ucs2_t ucs; if (ccsp->bits == TABLE_8BIT) { if (*inbytesleft < 1) return (ucs4_t)ICONV_CES_BAD_SEQUENCE; ucs = (ucs2_t)ccsp->tbl[**inbuf]; if (ucs == INVALC) return (ucs4_t)ICONV_CES_INVALID_CHARACTER; *inbytesleft -= 1; *inbuf += 1; return (ucs4_t)ucs; } if (*inbytesleft < 2) return (ucs4_t)ICONV_CES_BAD_SEQUENCE; if (ccsp->optimization == TABLE_SIZE_OPTIMIZED) ucs = find_code_size((ucs2_t)**inbuf << 8 | (ucs2_t)*(*inbuf + 1), ccsp->tbl); else ucs = find_code_speed((ucs2_t)**inbuf << 8 | (ucs2_t)*(*inbuf + 1), ccsp->tbl); if (ucs == INVALC) return (ucs4_t)ICONV_CES_INVALID_CHARACTER; *inbuf += 2; *inbytesleft -= 2; return (ucs4_t)ucs; }#endif /* ICONV_TO_UCS_CES_TABLE */static int_DEFUN(table_get_mb_cur_max, (data), _VOID_PTR data){ return ((iconv_ccs_desc_t *)data)->bits/8;}#if defined (ICONV_TO_UCS_CES_TABLE)_CONST iconv_to_ucs_ces_handlers_t_iconv_to_ucs_ces_handlers_table = { table_init_to_ucs, table_close, table_get_mb_cur_max, NULL, NULL, NULL, table_convert_to_ucs};#endif /* ICONV_FROM_UCS_CES_TABLE */#if defined (ICONV_FROM_UCS_CES_TABLE)_CONST iconv_from_ucs_ces_handlers_t_iconv_from_ucs_ces_handlers_table ={ table_init_from_ucs, table_close, table_get_mb_cur_max, NULL, NULL, NULL, table_convert_from_ucs};#endif /* ICONV_TO_UCS_CES_TABLE *//* * Supplementary functions. *//* * find_code_speed - find code in 16 bit speed-optimized table. * * PARAMETERS:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -