1 /* $NetBSD: citrus_johab.c,v 1.3 2007/10/23 15:28:25 tnozaki Exp $ */ 2 3 /*- 4 * Copyright (c)2006 Citrus Project, 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 #include <sys/cdefs.h> 29 #if defined(LIBC_SCCS) && !defined(lint) 30 __RCSID("$NetBSD: citrus_johab.c,v 1.3 2007/10/23 15:28:25 tnozaki Exp $"); 31 #endif /* LIBC_SCCS and not lint */ 32 33 #include <sys/types.h> 34 #include <assert.h> 35 #include <errno.h> 36 #include <string.h> 37 #include <stdint.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <stddef.h> 41 #include <locale.h> 42 #include <wchar.h> 43 #include <limits.h> 44 45 #include "citrus_namespace.h" 46 #include "citrus_types.h" 47 #include "citrus_bcs.h" 48 #include "citrus_module.h" 49 #include "citrus_ctype.h" 50 #include "citrus_stdenc.h" 51 #include "citrus_johab.h" 52 53 /* ---------------------------------------------------------------------- 54 * private stuffs used by templates 55 */ 56 57 typedef struct { 58 int chlen; 59 char ch[2]; 60 } _JOHABState; 61 62 typedef struct { 63 int dummy; 64 } _JOHABEncodingInfo; 65 66 typedef struct { 67 _JOHABEncodingInfo ei; 68 struct { 69 /* for future multi-locale facility */ 70 _JOHABState s_mblen; 71 _JOHABState s_mbrlen; 72 _JOHABState s_mbrtowc; 73 _JOHABState s_mbtowc; 74 _JOHABState s_mbsrtowcs; 75 _JOHABState s_wcrtomb; 76 _JOHABState s_wcsrtombs; 77 _JOHABState s_wctomb; 78 } states; 79 } _JOHABCTypeInfo; 80 81 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 82 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ 83 84 #define _FUNCNAME(m) _citrus_JOHAB_##m 85 #define _ENCODING_INFO _JOHABEncodingInfo 86 #define _CTYPE_INFO _JOHABCTypeInfo 87 #define _ENCODING_STATE _JOHABState 88 #define _ENCODING_MB_CUR_MAX(_ei_) 2 89 #define _ENCODING_IS_STATE_DEPENDENT 0 90 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 91 92 93 static __inline void 94 /*ARGSUSED*/ 95 _citrus_JOHAB_init_state(_JOHABEncodingInfo * __restrict ei, 96 _JOHABState * __restrict psenc) 97 { 98 /* ei may be null */ 99 _DIAGASSERT(psenc != NULL); 100 101 psenc->chlen = 0; 102 } 103 104 static __inline void 105 /*ARGSUSED*/ 106 _citrus_JOHAB_pack_state(_JOHABEncodingInfo * __restrict ei, 107 void * __restrict pspriv, 108 const _JOHABState * __restrict psenc) 109 { 110 /* ei may be null */ 111 _DIAGASSERT(pspriv != NULL); 112 _DIAGASSERT(psenc != NULL); 113 114 memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); 115 } 116 117 static __inline void 118 /*ARGSUSED*/ 119 _citrus_JOHAB_unpack_state(_JOHABEncodingInfo * __restrict ei, 120 _JOHABState * __restrict psenc, 121 const void * __restrict pspriv) 122 { 123 /* ei may be null */ 124 _DIAGASSERT(psenc != NULL); 125 _DIAGASSERT(pspriv != NULL); 126 127 memcpy((void *)psenc, pspriv, sizeof(*psenc)); 128 } 129 130 static void 131 /*ARGSUSED*/ 132 _citrus_JOHAB_encoding_module_uninit(_JOHABEncodingInfo *ei) 133 { 134 /* ei may be null */ 135 } 136 137 static int 138 /*ARGSUSED*/ 139 _citrus_JOHAB_encoding_module_init(_JOHABEncodingInfo * __restrict ei, 140 const void * __restrict var, size_t lenvar) 141 { 142 /* ei may be null */ 143 return 0; 144 } 145 146 static __inline int 147 ishangul(int l, int t) 148 { 149 150 return (l >= 0x84 && l <= 0xD3) && 151 ((t >= 0x41 && t <= 0x7E) || (t >= 0x81 && t <= 0xFE)); 152 } 153 154 static __inline int 155 isuda(int l, int t) 156 { 157 return (l == 0xD8) && 158 ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE)); 159 } 160 161 static __inline int 162 ishanja(int l, int t) 163 { 164 return ((l >= 0xD9 && l <= 0xDE) || (l >= 0xE0 && l <= 0xF9)) && 165 ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE)); 166 } 167 168 static int 169 /*ARGSUSED*/ 170 _citrus_JOHAB_mbrtowc_priv(_JOHABEncodingInfo * __restrict ei, 171 wchar_t * __restrict pwc, const char ** __restrict s, size_t n, 172 _JOHABState * __restrict psenc, size_t * __restrict nresult) 173 { 174 const char *s0; 175 int l, t; 176 177 /* ei may be unused */ 178 _DIAGASSERT(s != NULL); 179 _DIAGASSERT(psenc != NULL); 180 _DIAGASSERT(nresult != 0); 181 182 if (*s == NULL) { 183 _citrus_JOHAB_init_state(ei, psenc); 184 *nresult = _ENCODING_IS_STATE_DEPENDENT; 185 return 0; 186 } 187 s0 = *s; 188 189 switch (psenc->chlen) { 190 case 0: 191 if (n-- < 1) 192 goto restart; 193 l = *s0++ & 0xFF; 194 if (l <= 0x7F) { 195 if (pwc != NULL) 196 *pwc = (wchar_t)l; 197 *nresult = (l == 0) ? 0 : 1; 198 *s = s0; 199 return 0; 200 } 201 psenc->ch[psenc->chlen++] = l; 202 break; 203 case 1: 204 l = psenc->ch[0] & 0xFF; 205 break; 206 default: 207 return EINVAL; 208 } 209 if (n-- < 1) { 210 restart: 211 *nresult = (size_t)-2; 212 *s = s0; 213 return 0; 214 } 215 t = *s0++ & 0xFF; 216 if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) { 217 *nresult = (size_t)-1; 218 return EILSEQ; 219 } 220 if (pwc != NULL) 221 *pwc = (wchar_t)(l << 8 | t); 222 *nresult = s0 - *s; 223 *s = s0; 224 psenc->chlen = 0; 225 226 return 0; 227 } 228 229 static int 230 /*ARGSUSED*/ 231 _citrus_JOHAB_wcrtomb_priv(_JOHABEncodingInfo * __restrict ei, 232 char * __restrict s, size_t n, wchar_t wc, 233 _JOHABState * __restrict psenc, size_t * __restrict nresult) 234 { 235 int l, t; 236 237 /* ei may be unused */ 238 _DIAGASSERT(s != NULL); 239 _DIAGASSERT(psenc != NULL); 240 _DIAGASSERT(nresult != NULL); 241 242 if (psenc->chlen != 0) 243 return EINVAL; 244 245 /* XXX assume wchar_t as int */ 246 if ((uint32_t)wc <= 0x7F) { 247 if (n < 1) 248 goto e2big; 249 *s = wc & 0xFF; 250 *nresult = 1; 251 } else if ((uint32_t)wc <= 0xFFFF) { 252 if (n < 2) { 253 e2big: 254 *nresult = (size_t)-1; 255 return E2BIG; 256 } 257 l = (wc >> 8) & 0xFF; 258 t = wc & 0xFF; 259 if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) 260 goto ilseq; 261 *s++ = l; 262 *s = t; 263 *nresult = 2; 264 } else { 265 ilseq: 266 *nresult = (size_t)-1; 267 return EILSEQ; 268 } 269 return 0; 270 271 } 272 273 static __inline int 274 /*ARGSUSED*/ 275 _citrus_JOHAB_stdenc_wctocs(_JOHABEncodingInfo * __restrict ei, 276 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) 277 { 278 int m, l, t, linear; 279 280 /* ei may be unused */ 281 _DIAGASSERT(csid != NULL); 282 _DIAGASSERT(idx != NULL); 283 284 /* XXX assume wchar_t as int */ 285 if ((uint32_t)wc <= 0x7F) { 286 *idx = (_index_t)wc; 287 *csid = 0; 288 } else if ((uint32_t)wc <= 0xFFFF) { 289 l = (wc >> 8) & 0xFF; 290 t = wc & 0xFF; 291 if (ishangul(l, t) || isuda(l, t)) { 292 *idx = (_index_t)wc; 293 *csid = 1; 294 } else { 295 if (l >= 0xD9 && l <= 0xDE) { 296 linear = l - 0xD9; 297 m = 0x21; 298 } else if (l >= 0xE0 && l <= 0xF9) { 299 linear = l - 0xE0; 300 m = 0x4A; 301 } else { 302 return EILSEQ; 303 } 304 linear *= 188; 305 if (t >= 0x31 && t <= 0x7E) { 306 linear += t - 0x31; 307 } else if (t >= 0x91 && t <= 0xFE) { 308 linear += t - 0x43; 309 } else { 310 return EILSEQ; 311 } 312 l = (linear / 94) + m; 313 t = (linear % 94) + 0x21; 314 *idx = (_index_t)((l << 8) | t); 315 *csid = 2; 316 } 317 } else { 318 return EILSEQ; 319 } 320 return 0; 321 } 322 323 static __inline int 324 /*ARGSUSED*/ 325 _citrus_JOHAB_stdenc_cstowc(_JOHABEncodingInfo * __restrict ei, 326 wchar_t * __restrict wc, _csid_t csid, _index_t idx) 327 { 328 int m, n, l, t, linear; 329 330 /* ei may be unused */ 331 _DIAGASSERT(wc != NULL); 332 333 switch (csid) { 334 case 0: 335 case 1: 336 *wc = (wchar_t)idx; 337 break; 338 case 2: 339 if (idx >= 0x2121 && idx <= 0x2C71) { 340 m = 0xD9; 341 n = 0x21; 342 } else if (idx >= 0x4A21 && idx <= 0x7D7E) { 343 m = 0xE0; 344 n = 0x4A; 345 } else { 346 return EILSEQ; 347 } 348 l = ((idx >> 8) & 0xFF) - n; 349 t = (idx & 0xFF) - 0x21; 350 linear = (l * 94) + t; 351 l = (linear / 188) + m; 352 t = linear % 188; 353 t += (t <= 0x4D) ? 0x31 : 0x43; 354 break; 355 default: 356 return EILSEQ; 357 } 358 return 0; 359 } 360 361 static __inline int 362 /*ARGSUSED*/ 363 _citrus_JOHAB_stdenc_get_state_desc_generic(_JOHABEncodingInfo * __restrict ei, 364 _JOHABState * __restrict psenc, int * __restrict rstate) 365 { 366 /* ei may be unused */ 367 _DIAGASSERT(psenc != NULL); 368 _DIAGASSERT(rstate != NULL); 369 370 *rstate = (psenc->chlen == 0) 371 ? _STDENC_SDGEN_INITIAL 372 : _STDENC_SDGEN_INCOMPLETE_CHAR; 373 return 0; 374 } 375 376 /* ---------------------------------------------------------------------- 377 * public interface for ctype 378 */ 379 380 _CITRUS_CTYPE_DECLS(JOHAB); 381 _CITRUS_CTYPE_DEF_OPS(JOHAB); 382 383 #include "citrus_ctype_template.h" 384 385 386 /* ---------------------------------------------------------------------- 387 * public interface for stdenc 388 */ 389 390 _CITRUS_STDENC_DECLS(JOHAB); 391 _CITRUS_STDENC_DEF_OPS(JOHAB); 392 393 #include "citrus_stdenc_template.h" 394