1219019Sgabor/* $FreeBSD$ */ 2219019Sgabor/* $NetBSD: citrus_johab.c,v 1.4 2008/06/14 16:01:07 tnozaki Exp $ */ 3219019Sgabor 4219019Sgabor/*- 5219019Sgabor * Copyright (c)2006 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 <stdbool.h> 36219019Sgabor#include <stddef.h> 37219019Sgabor#include <stdint.h> 38219019Sgabor#include <stdio.h> 39219019Sgabor#include <stdlib.h> 40219019Sgabor#include <string.h> 41219019Sgabor#include <wchar.h> 42219019Sgabor 43219019Sgabor#include "citrus_namespace.h" 44219019Sgabor#include "citrus_types.h" 45219019Sgabor#include "citrus_bcs.h" 46219019Sgabor#include "citrus_module.h" 47219019Sgabor#include "citrus_stdenc.h" 48219019Sgabor#include "citrus_johab.h" 49219019Sgabor 50219019Sgabor/* ---------------------------------------------------------------------- 51219019Sgabor * private stuffs used by templates 52219019Sgabor */ 53219019Sgabor 54219019Sgabortypedef struct { 55219019Sgabor int chlen; 56219019Sgabor char ch[2]; 57219019Sgabor} _JOHABState; 58219019Sgabor 59219019Sgabortypedef struct { 60219019Sgabor int dummy; 61219019Sgabor} _JOHABEncodingInfo; 62219019Sgabor 63219019Sgabor#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 64219019Sgabor#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ 65219019Sgabor 66219019Sgabor#define _FUNCNAME(m) _citrus_JOHAB_##m 67219019Sgabor#define _ENCODING_INFO _JOHABEncodingInfo 68219019Sgabor#define _ENCODING_STATE _JOHABState 69219019Sgabor#define _ENCODING_MB_CUR_MAX(_ei_) 2 70219019Sgabor#define _ENCODING_IS_STATE_DEPENDENT 0 71219019Sgabor#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 72219019Sgabor 73219019Sgabor 74219019Sgaborstatic __inline void 75219019Sgabor/*ARGSUSED*/ 76219019Sgabor_citrus_JOHAB_init_state(_JOHABEncodingInfo * __restrict ei __unused, 77219019Sgabor _JOHABState * __restrict psenc) 78219019Sgabor{ 79219019Sgabor 80219019Sgabor psenc->chlen = 0; 81219019Sgabor} 82219019Sgabor 83260264Sdim#if 0 84219019Sgaborstatic __inline void 85219019Sgabor/*ARGSUSED*/ 86219019Sgabor_citrus_JOHAB_pack_state(_JOHABEncodingInfo * __restrict ei __unused, 87219019Sgabor void * __restrict pspriv, const _JOHABState * __restrict psenc) 88219019Sgabor{ 89219019Sgabor 90219019Sgabor memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); 91219019Sgabor} 92219019Sgabor 93219019Sgaborstatic __inline void 94219019Sgabor/*ARGSUSED*/ 95219019Sgabor_citrus_JOHAB_unpack_state(_JOHABEncodingInfo * __restrict ei __unused, 96219019Sgabor _JOHABState * __restrict psenc, const void * __restrict pspriv) 97219019Sgabor{ 98219019Sgabor 99219019Sgabor memcpy((void *)psenc, pspriv, sizeof(*psenc)); 100219019Sgabor} 101260264Sdim#endif 102219019Sgabor 103219019Sgaborstatic void 104219019Sgabor/*ARGSUSED*/ 105219019Sgabor_citrus_JOHAB_encoding_module_uninit(_JOHABEncodingInfo *ei __unused) 106219019Sgabor{ 107219019Sgabor 108219019Sgabor /* ei may be null */ 109219019Sgabor} 110219019Sgabor 111219019Sgaborstatic int 112219019Sgabor/*ARGSUSED*/ 113219019Sgabor_citrus_JOHAB_encoding_module_init(_JOHABEncodingInfo * __restrict ei __unused, 114219019Sgabor const void * __restrict var __unused, size_t lenvar __unused) 115219019Sgabor{ 116219019Sgabor 117219019Sgabor /* ei may be null */ 118219019Sgabor return (0); 119219019Sgabor} 120219019Sgabor 121219019Sgaborstatic __inline bool 122219019Sgaborishangul(int l, int t) 123219019Sgabor{ 124219019Sgabor 125219019Sgabor return ((l >= 0x84 && l <= 0xD3) && 126219019Sgabor ((t >= 0x41 && t <= 0x7E) || (t >= 0x81 && t <= 0xFE))); 127219019Sgabor} 128219019Sgabor 129219019Sgaborstatic __inline bool 130219019Sgaborisuda(int l, int t) 131219019Sgabor{ 132219019Sgabor 133219019Sgabor return ((l == 0xD8) && 134219019Sgabor ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE))); 135219019Sgabor} 136219019Sgabor 137219019Sgaborstatic __inline bool 138219019Sgaborishanja(int l, int t) 139219019Sgabor{ 140219019Sgabor 141219019Sgabor return (((l >= 0xD9 && l <= 0xDE) || (l >= 0xE0 && l <= 0xF9)) && 142219019Sgabor ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE))); 143219019Sgabor} 144219019Sgabor 145219019Sgaborstatic int 146219019Sgabor/*ARGSUSED*/ 147219019Sgabor_citrus_JOHAB_mbrtowc_priv(_JOHABEncodingInfo * __restrict ei, 148252583Speter wchar_t * __restrict pwc, const char ** __restrict s, size_t n, 149219019Sgabor _JOHABState * __restrict psenc, size_t * __restrict nresult) 150219019Sgabor{ 151252583Speter const char *s0; 152219019Sgabor int l, t; 153219019Sgabor 154219019Sgabor if (*s == NULL) { 155219019Sgabor _citrus_JOHAB_init_state(ei, psenc); 156219019Sgabor *nresult = _ENCODING_IS_STATE_DEPENDENT; 157219019Sgabor return (0); 158219019Sgabor } 159219019Sgabor s0 = *s; 160219019Sgabor 161219019Sgabor switch (psenc->chlen) { 162219019Sgabor case 0: 163219019Sgabor if (n-- < 1) 164219019Sgabor goto restart; 165219019Sgabor l = *s0++ & 0xFF; 166219019Sgabor if (l <= 0x7F) { 167219019Sgabor if (pwc != NULL) 168219019Sgabor *pwc = (wchar_t)l; 169219019Sgabor *nresult = (l == 0) ? 0 : 1; 170219019Sgabor *s = s0; 171219019Sgabor return (0); 172219019Sgabor } 173219019Sgabor psenc->ch[psenc->chlen++] = l; 174219019Sgabor break; 175219019Sgabor case 1: 176219019Sgabor l = psenc->ch[0] & 0xFF; 177219019Sgabor break; 178219019Sgabor default: 179219019Sgabor return (EINVAL); 180219019Sgabor } 181219019Sgabor if (n-- < 1) { 182219019Sgaborrestart: 183219019Sgabor *nresult = (size_t)-2; 184219019Sgabor *s = s0; 185219019Sgabor return (0); 186219019Sgabor } 187219019Sgabor t = *s0++ & 0xFF; 188219019Sgabor if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) { 189219019Sgabor *nresult = (size_t)-1; 190219019Sgabor return (EILSEQ); 191219019Sgabor } 192219019Sgabor if (pwc != NULL) 193219019Sgabor *pwc = (wchar_t)(l << 8 | t); 194219019Sgabor *nresult = s0 - *s; 195219019Sgabor *s = s0; 196219019Sgabor psenc->chlen = 0; 197219019Sgabor 198219019Sgabor return (0); 199219019Sgabor} 200219019Sgabor 201219019Sgaborstatic int 202219019Sgabor/*ARGSUSED*/ 203219019Sgabor_citrus_JOHAB_wcrtomb_priv(_JOHABEncodingInfo * __restrict ei __unused, 204219019Sgabor char * __restrict s, size_t n, wchar_t wc, 205219019Sgabor _JOHABState * __restrict psenc, size_t * __restrict nresult) 206219019Sgabor{ 207219019Sgabor int l, t; 208219019Sgabor 209219019Sgabor if (psenc->chlen != 0) 210219019Sgabor return (EINVAL); 211219019Sgabor 212219019Sgabor /* XXX assume wchar_t as int */ 213219019Sgabor if ((uint32_t)wc <= 0x7F) { 214219019Sgabor if (n < 1) 215219019Sgabor goto e2big; 216219019Sgabor *s = wc & 0xFF; 217219019Sgabor *nresult = 1; 218219019Sgabor } else if ((uint32_t)wc <= 0xFFFF) { 219219019Sgabor if (n < 2) { 220219019Sgabore2big: 221219019Sgabor *nresult = (size_t)-1; 222219019Sgabor return (E2BIG); 223219019Sgabor } 224219019Sgabor l = (wc >> 8) & 0xFF; 225219019Sgabor t = wc & 0xFF; 226219019Sgabor if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) 227219019Sgabor goto ilseq; 228219019Sgabor *s++ = l; 229219019Sgabor *s = t; 230219019Sgabor *nresult = 2; 231219019Sgabor } else { 232219019Sgaborilseq: 233219019Sgabor *nresult = (size_t)-1; 234219019Sgabor return (EILSEQ); 235219019Sgabor } 236219019Sgabor return (0); 237219019Sgabor 238219019Sgabor} 239219019Sgabor 240219019Sgaborstatic __inline int 241219019Sgabor/*ARGSUSED*/ 242219019Sgabor_citrus_JOHAB_stdenc_wctocs(_JOHABEncodingInfo * __restrict ei __unused, 243219019Sgabor _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) 244219019Sgabor{ 245219019Sgabor int m, l, linear, t; 246219019Sgabor 247219019Sgabor /* XXX assume wchar_t as int */ 248219019Sgabor if ((uint32_t)wc <= 0x7F) { 249219019Sgabor *idx = (_index_t)wc; 250219019Sgabor *csid = 0; 251219019Sgabor } else if ((uint32_t)wc <= 0xFFFF) { 252219019Sgabor l = (wc >> 8) & 0xFF; 253219019Sgabor t = wc & 0xFF; 254219019Sgabor if (ishangul(l, t) || isuda(l, t)) { 255219019Sgabor *idx = (_index_t)wc; 256219019Sgabor *csid = 1; 257219019Sgabor } else { 258219019Sgabor if (l >= 0xD9 && l <= 0xDE) { 259219019Sgabor linear = l - 0xD9; 260219019Sgabor m = 0x21; 261219019Sgabor } else if (l >= 0xE0 && l <= 0xF9) { 262219019Sgabor linear = l - 0xE0; 263219019Sgabor m = 0x4A; 264219019Sgabor } else 265219019Sgabor return (EILSEQ); 266219019Sgabor linear *= 188; 267219019Sgabor if (t >= 0x31 && t <= 0x7E) 268219019Sgabor linear += t - 0x31; 269219019Sgabor else if (t >= 0x91 && t <= 0xFE) 270219019Sgabor linear += t - 0x43; 271219019Sgabor else 272219019Sgabor return (EILSEQ); 273219019Sgabor l = (linear / 94) + m; 274219019Sgabor t = (linear % 94) + 0x21; 275219019Sgabor *idx = (_index_t)((l << 8) | t); 276219019Sgabor *csid = 2; 277219019Sgabor } 278219019Sgabor } else 279219019Sgabor return (EILSEQ); 280219019Sgabor return (0); 281219019Sgabor} 282219019Sgabor 283219019Sgaborstatic __inline int 284219019Sgabor/*ARGSUSED*/ 285219019Sgabor_citrus_JOHAB_stdenc_cstowc(_JOHABEncodingInfo * __restrict ei __unused, 286219019Sgabor wchar_t * __restrict wc, _csid_t csid, _index_t idx) 287219019Sgabor{ 288219019Sgabor int m, n, l, linear, t; 289219019Sgabor 290219019Sgabor switch (csid) { 291219019Sgabor case 0: 292219019Sgabor case 1: 293219019Sgabor *wc = (wchar_t)idx; 294219019Sgabor break; 295219019Sgabor case 2: 296219019Sgabor if (idx >= 0x2121 && idx <= 0x2C71) { 297219019Sgabor m = 0xD9; 298219019Sgabor n = 0x21; 299219019Sgabor } else if (idx >= 0x4A21 && idx <= 0x7D7E) { 300219019Sgabor m = 0xE0; 301219019Sgabor n = 0x4A; 302219019Sgabor } else 303219019Sgabor return (EILSEQ); 304219019Sgabor l = ((idx >> 8) & 0xFF) - n; 305219019Sgabor t = (idx & 0xFF) - 0x21; 306219019Sgabor linear = (l * 94) + t; 307219019Sgabor l = (linear / 188) + m; 308219019Sgabor t = linear % 188; 309219019Sgabor t += (t <= 0x4D) ? 0x31 : 0x43; 310219019Sgabor break; 311219019Sgabor default: 312219019Sgabor return (EILSEQ); 313219019Sgabor } 314219019Sgabor return (0); 315219019Sgabor} 316219019Sgabor 317219019Sgaborstatic __inline int 318219019Sgabor/*ARGSUSED*/ 319219019Sgabor_citrus_JOHAB_stdenc_get_state_desc_generic(_JOHABEncodingInfo * __restrict ei __unused, 320219019Sgabor _JOHABState * __restrict psenc, int * __restrict rstate) 321219019Sgabor{ 322219019Sgabor 323219019Sgabor *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL : 324219019Sgabor _STDENC_SDGEN_INCOMPLETE_CHAR; 325219019Sgabor return (0); 326219019Sgabor} 327219019Sgabor 328219019Sgabor/* ---------------------------------------------------------------------- 329219019Sgabor * public interface for stdenc 330219019Sgabor */ 331219019Sgabor 332219019Sgabor_CITRUS_STDENC_DECLS(JOHAB); 333219019Sgabor_CITRUS_STDENC_DEF_OPS(JOHAB); 334219019Sgabor 335219019Sgabor#include "citrus_stdenc_template.h" 336