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