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 #if 0 84 static __inline void 85 /*ARGSUSED*/ 86 _citrus_JOHAB_pack_state(_JOHABEncodingInfo * __restrict ei __unused, 87 void * __restrict pspriv, const _JOHABState * __restrict psenc) 88 { 89 90 memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); 91 } 92 93 static __inline void 94 /*ARGSUSED*/ 95 _citrus_JOHAB_unpack_state(_JOHABEncodingInfo * __restrict ei __unused, 96 _JOHABState * __restrict psenc, const void * __restrict pspriv) 97 { 98 99 memcpy((void *)psenc, pspriv, sizeof(*psenc)); 100 } 101 #endif 102 103 static void 104 /*ARGSUSED*/ 105 _citrus_JOHAB_encoding_module_uninit(_JOHABEncodingInfo *ei __unused) 106 { 107 108 /* ei may be null */ 109 } 110 111 static int 112 /*ARGSUSED*/ 113 _citrus_JOHAB_encoding_module_init(_JOHABEncodingInfo * __restrict ei __unused, 114 const void * __restrict var __unused, size_t lenvar __unused) 115 { 116 117 /* ei may be null */ 118 return (0); 119 } 120 121 static __inline bool 122 ishangul(int l, int t) 123 { 124 125 return ((l >= 0x84 && l <= 0xD3) && 126 ((t >= 0x41 && t <= 0x7E) || (t >= 0x81 && t <= 0xFE))); 127 } 128 129 static __inline bool 130 isuda(int l, int t) 131 { 132 133 return ((l == 0xD8) && 134 ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE))); 135 } 136 137 static __inline bool 138 ishanja(int l, int t) 139 { 140 141 return (((l >= 0xD9 && l <= 0xDE) || (l >= 0xE0 && l <= 0xF9)) && 142 ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE))); 143 } 144 145 static int 146 /*ARGSUSED*/ 147 _citrus_JOHAB_mbrtowc_priv(_JOHABEncodingInfo * __restrict ei, 148 wchar_t * __restrict pwc, const char ** __restrict s, size_t n, 149 _JOHABState * __restrict psenc, size_t * __restrict nresult) 150 { 151 const char *s0; 152 int l, t; 153 154 if (*s == NULL) { 155 _citrus_JOHAB_init_state(ei, psenc); 156 *nresult = _ENCODING_IS_STATE_DEPENDENT; 157 return (0); 158 } 159 s0 = *s; 160 161 switch (psenc->chlen) { 162 case 0: 163 if (n-- < 1) 164 goto restart; 165 l = *s0++ & 0xFF; 166 if (l <= 0x7F) { 167 if (pwc != NULL) 168 *pwc = (wchar_t)l; 169 *nresult = (l == 0) ? 0 : 1; 170 *s = s0; 171 return (0); 172 } 173 psenc->ch[psenc->chlen++] = l; 174 break; 175 case 1: 176 l = psenc->ch[0] & 0xFF; 177 break; 178 default: 179 return (EINVAL); 180 } 181 if (n-- < 1) { 182 restart: 183 *nresult = (size_t)-2; 184 *s = s0; 185 return (0); 186 } 187 t = *s0++ & 0xFF; 188 if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) { 189 *nresult = (size_t)-1; 190 return (EILSEQ); 191 } 192 if (pwc != NULL) 193 *pwc = (wchar_t)(l << 8 | t); 194 *nresult = s0 - *s; 195 *s = s0; 196 psenc->chlen = 0; 197 198 return (0); 199 } 200 201 static int 202 /*ARGSUSED*/ 203 _citrus_JOHAB_wcrtomb_priv(_JOHABEncodingInfo * __restrict ei __unused, 204 char * __restrict s, size_t n, wchar_t wc, 205 _JOHABState * __restrict psenc, size_t * __restrict nresult) 206 { 207 int l, t; 208 209 if (psenc->chlen != 0) 210 return (EINVAL); 211 212 /* XXX assume wchar_t as int */ 213 if ((uint32_t)wc <= 0x7F) { 214 if (n < 1) 215 goto e2big; 216 *s = wc & 0xFF; 217 *nresult = 1; 218 } else if ((uint32_t)wc <= 0xFFFF) { 219 if (n < 2) { 220 e2big: 221 *nresult = (size_t)-1; 222 return (E2BIG); 223 } 224 l = (wc >> 8) & 0xFF; 225 t = wc & 0xFF; 226 if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) 227 goto ilseq; 228 *s++ = l; 229 *s = t; 230 *nresult = 2; 231 } else { 232 ilseq: 233 *nresult = (size_t)-1; 234 return (EILSEQ); 235 } 236 return (0); 237 238 } 239 240 static __inline int 241 /*ARGSUSED*/ 242 _citrus_JOHAB_stdenc_wctocs(_JOHABEncodingInfo * __restrict ei __unused, 243 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) 244 { 245 int m, l, linear, t; 246 247 /* XXX assume wchar_t as int */ 248 if ((uint32_t)wc <= 0x7F) { 249 *idx = (_index_t)wc; 250 *csid = 0; 251 } else if ((uint32_t)wc <= 0xFFFF) { 252 l = (wc >> 8) & 0xFF; 253 t = wc & 0xFF; 254 if (ishangul(l, t) || isuda(l, t)) { 255 *idx = (_index_t)wc; 256 *csid = 1; 257 } else { 258 if (l >= 0xD9 && l <= 0xDE) { 259 linear = l - 0xD9; 260 m = 0x21; 261 } else if (l >= 0xE0 && l <= 0xF9) { 262 linear = l - 0xE0; 263 m = 0x4A; 264 } else 265 return (EILSEQ); 266 linear *= 188; 267 if (t >= 0x31 && t <= 0x7E) 268 linear += t - 0x31; 269 else if (t >= 0x91 && t <= 0xFE) 270 linear += t - 0x43; 271 else 272 return (EILSEQ); 273 l = (linear / 94) + m; 274 t = (linear % 94) + 0x21; 275 *idx = (_index_t)((l << 8) | t); 276 *csid = 2; 277 } 278 } else 279 return (EILSEQ); 280 return (0); 281 } 282 283 static __inline int 284 /*ARGSUSED*/ 285 _citrus_JOHAB_stdenc_cstowc(_JOHABEncodingInfo * __restrict ei __unused, 286 wchar_t * __restrict wc, _csid_t csid, _index_t idx) 287 { 288 int m, n, l, linear, t; 289 290 switch (csid) { 291 case 0: 292 case 1: 293 *wc = (wchar_t)idx; 294 break; 295 case 2: 296 if (idx >= 0x2121 && idx <= 0x2C71) { 297 m = 0xD9; 298 n = 0x21; 299 } else if (idx >= 0x4A21 && idx <= 0x7D7E) { 300 m = 0xE0; 301 n = 0x4A; 302 } else 303 return (EILSEQ); 304 l = ((idx >> 8) & 0xFF) - n; 305 t = (idx & 0xFF) - 0x21; 306 linear = (l * 94) + t; 307 l = (linear / 188) + m; 308 t = linear % 188; 309 t += (t <= 0x4D) ? 0x31 : 0x43; 310 break; 311 default: 312 return (EILSEQ); 313 } 314 return (0); 315 } 316 317 static __inline int 318 /*ARGSUSED*/ 319 _citrus_JOHAB_stdenc_get_state_desc_generic(_JOHABEncodingInfo * __restrict ei __unused, 320 _JOHABState * __restrict psenc, int * __restrict rstate) 321 { 322 323 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL : 324 _STDENC_SDGEN_INCOMPLETE_CHAR; 325 return (0); 326 } 327 328 /* ---------------------------------------------------------------------- 329 * public interface for stdenc 330 */ 331 332 _CITRUS_STDENC_DECLS(JOHAB); 333 _CITRUS_STDENC_DEF_OPS(JOHAB); 334 335 #include "citrus_stdenc_template.h" 336