1219019Sgabor/* $FreeBSD$ */ 2219019Sgabor/* $NetBSD: citrus_dechanyu.c,v 1.3 2008/06/14 16:01:07 tnozaki Exp $ */ 3219019Sgabor 4219019Sgabor/*- 5219019Sgabor * Copyright (c)2007 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#include <sys/cdefs.h> 30219019Sgabor#include <sys/types.h> 31219019Sgabor 32219019Sgabor#include <assert.h> 33219019Sgabor#include <errno.h> 34219019Sgabor#include <limits.h> 35219019Sgabor#include <stddef.h> 36219019Sgabor#include <stdint.h> 37219019Sgabor#include <stdio.h> 38219019Sgabor#include <stdlib.h> 39219019Sgabor#include <string.h> 40219019Sgabor#include <wchar.h> 41219019Sgabor 42219019Sgabor#include "citrus_namespace.h" 43219019Sgabor#include "citrus_types.h" 44219019Sgabor#include "citrus_bcs.h" 45219019Sgabor#include "citrus_module.h" 46219019Sgabor#include "citrus_stdenc.h" 47219019Sgabor#include "citrus_dechanyu.h" 48219019Sgabor 49219019Sgabor/* ---------------------------------------------------------------------- 50219019Sgabor * private stuffs used by templates 51219019Sgabor */ 52219019Sgabor 53219019Sgabortypedef struct { 54219019Sgabor size_t chlen; 55219019Sgabor char ch[4]; 56219019Sgabor} _DECHanyuState; 57219019Sgabor 58219019Sgabortypedef struct { 59219019Sgabor int dummy; 60219019Sgabor} _DECHanyuEncodingInfo; 61219019Sgabor 62219019Sgabor#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 63219019Sgabor#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.__CONCAT(s_,_func_) 64219019Sgabor 65219019Sgabor#define _FUNCNAME(m) __CONCAT(_citrus_DECHanyu_,m) 66219019Sgabor#define _ENCODING_INFO _DECHanyuEncodingInfo 67219019Sgabor#define _ENCODING_STATE _DECHanyuState 68219019Sgabor#define _ENCODING_MB_CUR_MAX(_ei_) 4 69219019Sgabor#define _ENCODING_IS_STATE_DEPENDENT 0 70219019Sgabor#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 71219019Sgabor 72219019Sgaborstatic __inline void 73219019Sgabor/*ARGSUSED*/ 74219019Sgabor_citrus_DECHanyu_init_state(_DECHanyuEncodingInfo * __restrict ei __unused, 75219019Sgabor _DECHanyuState * __restrict psenc) 76219019Sgabor{ 77219019Sgabor 78219019Sgabor psenc->chlen = 0; 79219019Sgabor} 80219019Sgabor 81260264Sdim#if 0 82219019Sgaborstatic __inline void 83219019Sgabor/*ARGSUSED*/ 84219019Sgabor_citrus_DECHanyu_pack_state(_DECHanyuEncodingInfo * __restrict ei __unused, 85219019Sgabor void * __restrict pspriv, const _DECHanyuState * __restrict psenc) 86219019Sgabor{ 87219019Sgabor 88219019Sgabor memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); 89219019Sgabor} 90219019Sgabor 91219019Sgaborstatic __inline void 92219019Sgabor/*ARGSUSED*/ 93219019Sgabor_citrus_DECHanyu_unpack_state(_DECHanyuEncodingInfo * __restrict ei __unused, 94219019Sgabor _DECHanyuState * __restrict psenc, 95219019Sgabor const void * __restrict pspriv) 96219019Sgabor{ 97219019Sgabor 98219019Sgabor memcpy((void *)psenc, pspriv, sizeof(*psenc)); 99219019Sgabor} 100260264Sdim#endif 101219019Sgabor 102219019Sgaborstatic void 103219019Sgabor/*ARGSUSED*/ 104219019Sgabor_citrus_DECHanyu_encoding_module_uninit(_DECHanyuEncodingInfo *ei __unused) 105219019Sgabor{ 106219019Sgabor 107219019Sgabor /* ei may be null */ 108219019Sgabor} 109219019Sgabor 110219019Sgaborstatic int 111219019Sgabor/*ARGSUSED*/ 112219019Sgabor_citrus_DECHanyu_encoding_module_init(_DECHanyuEncodingInfo * __restrict ei __unused, 113219019Sgabor const void * __restrict var __unused, size_t lenvar __unused) 114219019Sgabor{ 115219019Sgabor 116219019Sgabor /* ei may be null */ 117219019Sgabor return (0); 118219019Sgabor} 119219019Sgabor 120219019Sgaborstatic __inline bool 121219019Sgaboris_singlebyte(int c) 122219019Sgabor{ 123219019Sgabor 124219019Sgabor return (c <= 0x7F); 125219019Sgabor} 126219019Sgabor 127219019Sgaborstatic __inline bool 128219019Sgaboris_leadbyte(int c) 129219019Sgabor{ 130219019Sgabor 131219019Sgabor return (c >= 0xA1 && c <= 0xFE); 132219019Sgabor} 133219019Sgabor 134219019Sgaborstatic __inline bool 135219019Sgaboris_trailbyte(int c) 136219019Sgabor{ 137219019Sgabor 138219019Sgabor c &= ~0x80; 139219019Sgabor return (c >= 0x21 && c <= 0x7E); 140219019Sgabor} 141219019Sgabor 142219019Sgaborstatic __inline bool 143219019Sgaboris_hanyu1(int c) 144219019Sgabor{ 145219019Sgabor 146219019Sgabor return (c == 0xC2); 147219019Sgabor} 148219019Sgabor 149219019Sgaborstatic __inline bool 150219019Sgaboris_hanyu2(int c) 151219019Sgabor{ 152219019Sgabor 153219019Sgabor return (c == 0xCB); 154219019Sgabor} 155219019Sgabor 156219019Sgabor#define HANYUBIT 0xC2CB0000 157219019Sgabor 158219019Sgaborstatic __inline bool 159219019Sgaboris_94charset(int c) 160219019Sgabor{ 161219019Sgabor 162219019Sgabor return (c >= 0x21 && c <= 0x7E); 163219019Sgabor} 164219019Sgabor 165219019Sgaborstatic int 166219019Sgabor/*ARGSUSED*/ 167219019Sgabor_citrus_DECHanyu_mbrtowc_priv(_DECHanyuEncodingInfo * __restrict ei, 168219019Sgabor wchar_t * __restrict pwc, char ** __restrict s, size_t n, 169219019Sgabor _DECHanyuState * __restrict psenc, size_t * __restrict nresult) 170219019Sgabor{ 171219019Sgabor char *s0; 172219019Sgabor wchar_t wc; 173219019Sgabor int ch; 174219019Sgabor 175219019Sgabor if (*s == NULL) { 176219019Sgabor _citrus_DECHanyu_init_state(ei, psenc); 177219019Sgabor *nresult = _ENCODING_IS_STATE_DEPENDENT; 178219019Sgabor return (0); 179219019Sgabor } 180219019Sgabor s0 = *s; 181219019Sgabor 182219019Sgabor wc = (wchar_t)0; 183219019Sgabor switch (psenc->chlen) { 184219019Sgabor case 0: 185219019Sgabor if (n-- < 1) 186219019Sgabor goto restart; 187219019Sgabor ch = *s0++ & 0xFF; 188219019Sgabor if (is_singlebyte(ch)) { 189219019Sgabor if (pwc != NULL) 190219019Sgabor *pwc = (wchar_t)ch; 191219019Sgabor *nresult = (size_t)((ch == 0) ? 0 : 1); 192219019Sgabor *s = s0; 193219019Sgabor return (0); 194219019Sgabor } 195219019Sgabor if (!is_leadbyte(ch)) 196219019Sgabor goto ilseq; 197219019Sgabor psenc->ch[psenc->chlen++] = ch; 198219019Sgabor break; 199219019Sgabor case 1: 200219019Sgabor ch = psenc->ch[0] & 0xFF; 201219019Sgabor if (!is_leadbyte(ch)) 202219019Sgabor return (EINVAL); 203219019Sgabor break; 204219019Sgabor case 2: case 3: 205219019Sgabor ch = psenc->ch[0] & 0xFF; 206219019Sgabor if (is_hanyu1(ch)) { 207219019Sgabor ch = psenc->ch[1] & 0xFF; 208219019Sgabor if (is_hanyu2(ch)) { 209219019Sgabor wc |= (wchar_t)HANYUBIT; 210219019Sgabor break; 211219019Sgabor } 212219019Sgabor } 213219019Sgabor /*FALLTHROUGH*/ 214219019Sgabor default: 215219019Sgabor return (EINVAL); 216219019Sgabor } 217219019Sgabor 218219019Sgabor switch (psenc->chlen) { 219219019Sgabor case 1: 220219019Sgabor if (is_hanyu1(ch)) { 221219019Sgabor if (n-- < 1) 222219019Sgabor goto restart; 223219019Sgabor ch = *s0++ & 0xFF; 224219019Sgabor if (!is_hanyu2(ch)) 225219019Sgabor goto ilseq; 226219019Sgabor psenc->ch[psenc->chlen++] = ch; 227219019Sgabor wc |= (wchar_t)HANYUBIT; 228219019Sgabor if (n-- < 1) 229219019Sgabor goto restart; 230219019Sgabor ch = *s0++ & 0xFF; 231219019Sgabor if (!is_leadbyte(ch)) 232219019Sgabor goto ilseq; 233219019Sgabor psenc->ch[psenc->chlen++] = ch; 234219019Sgabor } 235219019Sgabor break; 236219019Sgabor case 2: 237219019Sgabor if (n-- < 1) 238219019Sgabor goto restart; 239219019Sgabor ch = *s0++ & 0xFF; 240219019Sgabor if (!is_leadbyte(ch)) 241219019Sgabor goto ilseq; 242219019Sgabor psenc->ch[psenc->chlen++] = ch; 243219019Sgabor break; 244219019Sgabor case 3: 245219019Sgabor ch = psenc->ch[2] & 0xFF; 246219019Sgabor if (!is_leadbyte(ch)) 247219019Sgabor return (EINVAL); 248219019Sgabor } 249219019Sgabor if (n-- < 1) 250219019Sgabor goto restart; 251219019Sgabor wc |= (wchar_t)(ch << 8); 252219019Sgabor ch = *s0++ & 0xFF; 253219019Sgabor if (!is_trailbyte(ch)) 254219019Sgabor goto ilseq; 255219019Sgabor wc |= (wchar_t)ch; 256219019Sgabor if (pwc != NULL) 257219019Sgabor *pwc = wc; 258219019Sgabor *nresult = (size_t)(s0 - *s); 259219019Sgabor *s = s0; 260219019Sgabor psenc->chlen = 0; 261219019Sgabor 262219019Sgabor return (0); 263219019Sgabor 264219019Sgaborrestart: 265219019Sgabor *nresult = (size_t)-2; 266219019Sgabor *s = s0; 267219019Sgabor return (0); 268219019Sgabor 269219019Sgaborilseq: 270219019Sgabor *nresult = (size_t)-1; 271219019Sgabor return (EILSEQ); 272219019Sgabor} 273219019Sgabor 274219019Sgaborstatic int 275219019Sgabor/*ARGSUSED*/ 276219019Sgabor_citrus_DECHanyu_wcrtomb_priv(_DECHanyuEncodingInfo * __restrict ei __unused, 277219019Sgabor char * __restrict s, size_t n, wchar_t wc, 278219019Sgabor _DECHanyuState * __restrict psenc, size_t * __restrict nresult) 279219019Sgabor{ 280219019Sgabor int ch; 281219019Sgabor 282219019Sgabor if (psenc->chlen != 0) 283219019Sgabor return (EINVAL); 284219019Sgabor 285219019Sgabor /* XXX: assume wchar_t as int */ 286219019Sgabor if ((uint32_t)wc <= 0x7F) { 287219019Sgabor ch = wc & 0xFF; 288219019Sgabor } else { 289219019Sgabor if ((uint32_t)wc > 0xFFFF) { 290219019Sgabor if ((wc & ~0xFFFF) != (wchar_t)HANYUBIT) 291219019Sgabor goto ilseq; 292219019Sgabor psenc->ch[psenc->chlen++] = (wc >> 24) & 0xFF; 293219019Sgabor psenc->ch[psenc->chlen++] = (wc >> 16) & 0xFF; 294219019Sgabor wc &= 0xFFFF; 295219019Sgabor } 296219019Sgabor ch = (wc >> 8) & 0xFF; 297219019Sgabor if (!is_leadbyte(ch)) 298219019Sgabor goto ilseq; 299219019Sgabor psenc->ch[psenc->chlen++] = ch; 300219019Sgabor ch = wc & 0xFF; 301219019Sgabor if (!is_trailbyte(ch)) 302219019Sgabor goto ilseq; 303219019Sgabor } 304219019Sgabor psenc->ch[psenc->chlen++] = ch; 305219019Sgabor if (n < psenc->chlen) { 306219019Sgabor *nresult = (size_t)-1; 307219019Sgabor return (E2BIG); 308219019Sgabor } 309219019Sgabor memcpy(s, psenc->ch, psenc->chlen); 310219019Sgabor *nresult = psenc->chlen; 311219019Sgabor psenc->chlen = 0; 312219019Sgabor 313219019Sgabor return (0); 314219019Sgabor 315219019Sgaborilseq: 316219019Sgabor *nresult = (size_t)-1; 317219019Sgabor return (EILSEQ); 318219019Sgabor} 319219019Sgabor 320219019Sgaborstatic __inline int 321219019Sgabor/*ARGSUSED*/ 322219019Sgabor_citrus_DECHanyu_stdenc_wctocs(_DECHanyuEncodingInfo * __restrict ei __unused, 323219019Sgabor _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) 324219019Sgabor{ 325219019Sgabor wchar_t mask; 326219019Sgabor int plane; 327219019Sgabor 328219019Sgabor plane = 0; 329219019Sgabor mask = 0x7F; 330219019Sgabor /* XXX: assume wchar_t as int */ 331219019Sgabor if ((uint32_t)wc > 0x7F) { 332219019Sgabor if ((uint32_t)wc > 0xFFFF) { 333219019Sgabor if ((wc & ~0xFFFF) != (wchar_t)HANYUBIT) 334219019Sgabor return (EILSEQ); 335219019Sgabor plane += 2; 336219019Sgabor } 337219019Sgabor if (!is_leadbyte((wc >> 8) & 0xFF) || 338219019Sgabor !is_trailbyte(wc & 0xFF)) 339219019Sgabor return (EILSEQ); 340219019Sgabor plane += (wc & 0x80) ? 1 : 2; 341219019Sgabor mask |= 0x7F00; 342219019Sgabor } 343219019Sgabor *csid = plane; 344219019Sgabor *idx = (_index_t)(wc & mask); 345219019Sgabor 346219019Sgabor return (0); 347219019Sgabor} 348219019Sgabor 349219019Sgaborstatic __inline int 350219019Sgabor/*ARGSUSED*/ 351219019Sgabor_citrus_DECHanyu_stdenc_cstowc(_DECHanyuEncodingInfo * __restrict ei __unused, 352219019Sgabor wchar_t * __restrict wc, _csid_t csid, _index_t idx) 353219019Sgabor{ 354219019Sgabor 355219019Sgabor if (csid == 0) { 356219019Sgabor if (idx > 0x7F) 357219019Sgabor return (EILSEQ); 358219019Sgabor } else if (csid <= 4) { 359219019Sgabor if (!is_94charset(idx >> 8)) 360219019Sgabor return (EILSEQ); 361219019Sgabor if (!is_94charset(idx & 0xFF)) 362219019Sgabor return (EILSEQ); 363219019Sgabor if (csid % 2) 364219019Sgabor idx |= 0x80; 365219019Sgabor idx |= 0x8000; 366219019Sgabor if (csid > 2) 367219019Sgabor idx |= HANYUBIT; 368219019Sgabor } else 369219019Sgabor return (EILSEQ); 370219019Sgabor *wc = (wchar_t)idx; 371219019Sgabor return (0); 372219019Sgabor} 373219019Sgabor 374219019Sgaborstatic __inline int 375219019Sgabor/*ARGSUSED*/ 376219019Sgabor_citrus_DECHanyu_stdenc_get_state_desc_generic( 377219019Sgabor _DECHanyuEncodingInfo * __restrict ei __unused, 378219019Sgabor _DECHanyuState * __restrict psenc, int * __restrict rstate) 379219019Sgabor{ 380219019Sgabor 381219019Sgabor *rstate = (psenc->chlen == 0) 382219019Sgabor ? _STDENC_SDGEN_INITIAL 383219019Sgabor : _STDENC_SDGEN_INCOMPLETE_CHAR; 384219019Sgabor return (0); 385219019Sgabor} 386219019Sgabor 387219019Sgabor/* ---------------------------------------------------------------------- 388219019Sgabor * public interface for stdenc 389219019Sgabor */ 390219019Sgabor 391219019Sgabor_CITRUS_STDENC_DECLS(DECHanyu); 392219019Sgabor_CITRUS_STDENC_DEF_OPS(DECHanyu); 393219019Sgabor 394219019Sgabor#include "citrus_stdenc_template.h" 395