citrus_euctw.c revision 281550
1219019Sgabor/* $FreeBSD: head/lib/libiconv_modules/EUCTW/citrus_euctw.c 281550 2015-04-15 09:09:20Z tijl $ */ 2219019Sgabor/* $NetBSD: citrus_euctw.c,v 1.11 2008/06/14 16:01:07 tnozaki Exp $ */ 3219019Sgabor 4219019Sgabor/*- 5219019Sgabor * Copyright (c)2002 Citrus Project, 6219019Sgabor * All rights reserved. 7219019Sgabor * 8219019Sgabor * Redistribution and use in source and binary forms, with or without 9219019Sgabor * modification, are permitted provided that the following conditions 10219019Sgabor * are met: 11219019Sgabor * 1. Redistributions of source code must retain the above copyright 12219019Sgabor * notice, this list of conditions and the following disclaimer. 13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14219019Sgabor * notice, this list of conditions and the following disclaimer in the 15219019Sgabor * documentation and/or other materials provided with the distribution. 16219019Sgabor * 17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27219019Sgabor * SUCH DAMAGE. 28219019Sgabor */ 29219019Sgabor 30219019Sgabor/*- 31219019Sgabor * Copyright (c)1999 Citrus Project, 32219019Sgabor * All rights reserved. 33219019Sgabor * 34219019Sgabor * Redistribution and use in source and binary forms, with or without 35219019Sgabor * modification, are permitted provided that the following conditions 36219019Sgabor * are met: 37219019Sgabor * 1. Redistributions of source code must retain the above copyright 38219019Sgabor * notice, this list of conditions and the following disclaimer. 39219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 40219019Sgabor * notice, this list of conditions and the following disclaimer in the 41219019Sgabor * documentation and/or other materials provided with the distribution. 42219019Sgabor * 43219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 44219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 47219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53219019Sgabor * SUCH DAMAGE. 54219019Sgabor * 55219019Sgabor * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $ 56219019Sgabor */ 57219019Sgabor 58219019Sgabor#include <sys/cdefs.h> 59219019Sgabor#include <sys/types.h> 60219019Sgabor 61219019Sgabor#include <assert.h> 62219019Sgabor#include <errno.h> 63219019Sgabor#include <limits.h> 64219019Sgabor#include <stddef.h> 65219019Sgabor#include <stdio.h> 66219019Sgabor#include <stdlib.h> 67219019Sgabor#include <string.h> 68219019Sgabor#include <wchar.h> 69219019Sgabor 70219019Sgabor#include "citrus_namespace.h" 71219019Sgabor#include "citrus_types.h" 72219019Sgabor#include "citrus_module.h" 73219019Sgabor#include "citrus_stdenc.h" 74219019Sgabor#include "citrus_euctw.h" 75219019Sgabor 76219019Sgabor 77219019Sgabor/* ---------------------------------------------------------------------- 78219019Sgabor * private stuffs used by templates 79219019Sgabor */ 80219019Sgabor 81219019Sgabortypedef struct { 82219019Sgabor int chlen; 83219019Sgabor char ch[4]; 84219019Sgabor} _EUCTWState; 85219019Sgabor 86219019Sgabortypedef struct { 87219019Sgabor int dummy; 88219019Sgabor} _EUCTWEncodingInfo; 89219019Sgabor 90219019Sgabor#define _SS2 0x008e 91219019Sgabor#define _SS3 0x008f 92219019Sgabor 93219019Sgabor#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 94219019Sgabor#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ 95219019Sgabor 96219019Sgabor#define _FUNCNAME(m) _citrus_EUCTW_##m 97219019Sgabor#define _ENCODING_INFO _EUCTWEncodingInfo 98219019Sgabor#define _ENCODING_STATE _EUCTWState 99219019Sgabor#define _ENCODING_MB_CUR_MAX(_ei_) 4 100219019Sgabor#define _ENCODING_IS_STATE_DEPENDENT 0 101219019Sgabor#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 102219019Sgabor 103219019Sgaborstatic __inline int 104219019Sgabor_citrus_EUCTW_cs(unsigned int c) 105219019Sgabor{ 106219019Sgabor 107219019Sgabor c &= 0xff; 108219019Sgabor 109219019Sgabor return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0); 110219019Sgabor} 111219019Sgabor 112219019Sgaborstatic __inline int 113219019Sgabor_citrus_EUCTW_count(int cs) 114219019Sgabor{ 115219019Sgabor 116219019Sgabor switch (cs) { 117219019Sgabor case 0: 118219019Sgabor /*FALLTHROUGH*/ 119219019Sgabor case 1: 120219019Sgabor /*FALLTHROUGH*/ 121219019Sgabor case 2: 122263986Stijl return (1 << cs); 123219019Sgabor case 3: 124219019Sgabor abort(); 125219019Sgabor /*NOTREACHED*/ 126219019Sgabor } 127219019Sgabor return (0); 128219019Sgabor} 129219019Sgabor 130219019Sgaborstatic __inline void 131219019Sgabor/*ARGSUSED*/ 132219019Sgabor_citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei __unused, 133219019Sgabor _EUCTWState * __restrict s) 134219019Sgabor{ 135219019Sgabor 136219019Sgabor memset(s, 0, sizeof(*s)); 137219019Sgabor} 138219019Sgabor 139260003Sdim#if 0 140219019Sgaborstatic __inline void 141219019Sgabor/*ARGSUSED*/ 142219019Sgabor_citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei __unused, 143219019Sgabor void * __restrict pspriv, const _EUCTWState * __restrict s) 144219019Sgabor{ 145219019Sgabor 146219019Sgabor memcpy(pspriv, (const void *)s, sizeof(*s)); 147219019Sgabor} 148219019Sgabor 149219019Sgaborstatic __inline void 150219019Sgabor/*ARGSUSED*/ 151219019Sgabor_citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei __unused, 152219019Sgabor _EUCTWState * __restrict s, const void * __restrict pspriv) 153219019Sgabor{ 154219019Sgabor 155219019Sgabor memcpy((void *)s, pspriv, sizeof(*s)); 156219019Sgabor} 157260003Sdim#endif 158219019Sgabor 159219019Sgaborstatic int 160219019Sgabor/*ARGSUSED*/ 161219019Sgabor_citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei, 162219019Sgabor const void * __restrict var __unused, size_t lenvar __unused) 163219019Sgabor{ 164219019Sgabor 165219019Sgabor memset((void *)ei, 0, sizeof(*ei)); 166219019Sgabor 167219019Sgabor return (0); 168219019Sgabor} 169219019Sgabor 170219019Sgaborstatic void 171219019Sgabor/*ARGSUSED*/ 172219019Sgabor_citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei __unused) 173219019Sgabor{ 174219019Sgabor 175219019Sgabor} 176219019Sgabor 177219019Sgaborstatic int 178219019Sgabor_citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei, 179281550Stijl wchar_t * __restrict pwc, char ** __restrict s, 180219019Sgabor size_t n, _EUCTWState * __restrict psenc, size_t * __restrict nresult) 181219019Sgabor{ 182281550Stijl char *s0; 183219019Sgabor wchar_t wchar; 184219019Sgabor int c, chlenbak, cs; 185219019Sgabor 186219019Sgabor s0 = *s; 187219019Sgabor 188219019Sgabor if (s0 == NULL) { 189219019Sgabor _citrus_EUCTW_init_state(ei, psenc); 190219019Sgabor *nresult = 0; /* state independent */ 191219019Sgabor return (0); 192219019Sgabor } 193219019Sgabor 194219019Sgabor chlenbak = psenc->chlen; 195219019Sgabor 196219019Sgabor /* make sure we have the first byte in the buffer */ 197219019Sgabor switch (psenc->chlen) { 198219019Sgabor case 0: 199219019Sgabor if (n < 1) 200219019Sgabor goto restart; 201219019Sgabor psenc->ch[0] = *s0++; 202219019Sgabor psenc->chlen = 1; 203219019Sgabor n--; 204219019Sgabor break; 205219019Sgabor case 1: 206219019Sgabor case 2: 207219019Sgabor break; 208219019Sgabor default: 209219019Sgabor /* illgeal state */ 210219019Sgabor goto ilseq; 211219019Sgabor } 212219019Sgabor 213219019Sgabor c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff)); 214219019Sgabor if (c == 0) 215219019Sgabor goto ilseq; 216219019Sgabor while (psenc->chlen < c) { 217219019Sgabor if (n < 1) 218219019Sgabor goto ilseq; 219219019Sgabor psenc->ch[psenc->chlen] = *s0++; 220219019Sgabor psenc->chlen++; 221219019Sgabor n--; 222219019Sgabor } 223219019Sgabor 224219019Sgabor wchar = 0; 225219019Sgabor switch (cs) { 226219019Sgabor case 0: 227219019Sgabor if (psenc->ch[0] & 0x80) 228219019Sgabor goto ilseq; 229219019Sgabor wchar = psenc->ch[0] & 0xff; 230219019Sgabor break; 231219019Sgabor case 1: 232219019Sgabor if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80)) 233219019Sgabor goto ilseq; 234219019Sgabor wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); 235219019Sgabor wchar |= 'G' << 24; 236219019Sgabor break; 237219019Sgabor case 2: 238219019Sgabor if ((unsigned char)psenc->ch[1] < 0xa1 || 239219019Sgabor 0xa7 < (unsigned char)psenc->ch[1]) 240219019Sgabor goto ilseq; 241219019Sgabor if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80)) 242219019Sgabor goto ilseq; 243219019Sgabor wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff); 244219019Sgabor wchar |= ('G' + psenc->ch[1] - 0xa1) << 24; 245219019Sgabor break; 246219019Sgabor default: 247219019Sgabor goto ilseq; 248219019Sgabor } 249219019Sgabor 250219019Sgabor *s = s0; 251219019Sgabor psenc->chlen = 0; 252219019Sgabor 253219019Sgabor if (pwc) 254219019Sgabor *pwc = wchar; 255219019Sgabor *nresult = wchar ? c - chlenbak : 0; 256219019Sgabor return (0); 257219019Sgabor 258219019Sgaborilseq: 259219019Sgabor psenc->chlen = 0; 260219019Sgabor *nresult = (size_t)-1; 261219019Sgabor return (EILSEQ); 262219019Sgabor 263219019Sgaborrestart: 264219019Sgabor *s = s0; 265219019Sgabor *nresult = (size_t)-1; 266219019Sgabor return (0); 267219019Sgabor} 268219019Sgabor 269219019Sgaborstatic int 270219019Sgabor_citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei __unused, 271219019Sgabor char * __restrict s, size_t n, wchar_t wc, 272219019Sgabor _EUCTWState * __restrict psenc __unused, size_t * __restrict nresult) 273219019Sgabor{ 274219019Sgabor wchar_t cs, v; 275219019Sgabor int clen, i, ret; 276219019Sgabor size_t len; 277219019Sgabor 278219019Sgabor cs = wc & 0x7f000080; 279219019Sgabor clen = 1; 280219019Sgabor if (wc & 0x00007f00) 281219019Sgabor clen = 2; 282219019Sgabor if ((wc & 0x007f0000) && !(wc & 0x00800000)) 283219019Sgabor clen = 3; 284219019Sgabor 285219019Sgabor if (clen == 1 && cs == 0x00000000) { 286219019Sgabor /* ASCII */ 287219019Sgabor len = 1; 288219019Sgabor if (n < len) { 289219019Sgabor ret = E2BIG; 290219019Sgabor goto err; 291219019Sgabor } 292219019Sgabor v = wc & 0x0000007f; 293219019Sgabor } else if (clen == 2 && cs == ('G' << 24)) { 294219019Sgabor /* CNS-11643-1 */ 295219019Sgabor len = 2; 296219019Sgabor if (n < len) { 297219019Sgabor ret = E2BIG; 298219019Sgabor goto err; 299219019Sgabor } 300219019Sgabor v = wc & 0x00007f7f; 301219019Sgabor v |= 0x00008080; 302219019Sgabor } else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') { 303219019Sgabor /* CNS-11643-[2-7] */ 304219019Sgabor len = 4; 305219019Sgabor if (n < len) { 306219019Sgabor ret = E2BIG; 307219019Sgabor goto err; 308219019Sgabor } 309219019Sgabor *s++ = _SS2; 310219019Sgabor *s++ = (cs >> 24) - 'H' + 0xa2; 311219019Sgabor v = wc & 0x00007f7f; 312219019Sgabor v |= 0x00008080; 313219019Sgabor } else { 314219019Sgabor ret = EILSEQ; 315219019Sgabor goto err; 316219019Sgabor } 317219019Sgabor 318219019Sgabor i = clen; 319219019Sgabor while (i-- > 0) 320219019Sgabor *s++ = (v >> (i << 3)) & 0xff; 321219019Sgabor 322219019Sgabor *nresult = len; 323219019Sgabor return (0); 324219019Sgabor 325219019Sgaborerr: 326219019Sgabor *nresult = (size_t)-1; 327219019Sgabor return (ret); 328219019Sgabor} 329219019Sgabor 330219019Sgaborstatic __inline int 331219019Sgabor/*ARGSUSED*/ 332219019Sgabor_citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei __unused, 333219019Sgabor _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) 334219019Sgabor{ 335219019Sgabor 336219019Sgabor *csid = (_csid_t)(wc >> 24) & 0xFF; 337219019Sgabor *idx = (_index_t)(wc & 0x7F7F); 338219019Sgabor 339219019Sgabor return (0); 340219019Sgabor} 341219019Sgabor 342219019Sgaborstatic __inline int 343219019Sgabor/*ARGSUSED*/ 344219019Sgabor_citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei __unused, 345219019Sgabor wchar_t * __restrict wc, _csid_t csid, _index_t idx) 346219019Sgabor{ 347219019Sgabor 348219019Sgabor if (csid == 0) { 349219019Sgabor if ((idx & ~0x7F) != 0) 350219019Sgabor return (EINVAL); 351219019Sgabor *wc = (wchar_t)idx; 352219019Sgabor } else { 353219019Sgabor if (csid < 'G' || csid > 'M' || (idx & ~0x7F7F) != 0) 354219019Sgabor return (EINVAL); 355219019Sgabor *wc = (wchar_t)idx | ((wchar_t)csid << 24); 356219019Sgabor } 357219019Sgabor 358219019Sgabor return (0); 359219019Sgabor} 360219019Sgabor 361219019Sgaborstatic __inline int 362219019Sgabor/*ARGSUSED*/ 363219019Sgabor_citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei __unused, 364219019Sgabor _EUCTWState * __restrict psenc, int * __restrict rstate) 365219019Sgabor{ 366219019Sgabor 367219019Sgabor *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL : 368219019Sgabor _STDENC_SDGEN_INCOMPLETE_CHAR; 369219019Sgabor return (0); 370219019Sgabor} 371219019Sgabor 372219019Sgabor/* ---------------------------------------------------------------------- 373219019Sgabor * public interface for stdenc 374219019Sgabor */ 375219019Sgabor 376219019Sgabor_CITRUS_STDENC_DECLS(EUCTW); 377219019Sgabor_CITRUS_STDENC_DEF_OPS(EUCTW); 378219019Sgabor 379219019Sgabor#include "citrus_stdenc_template.h" 380