1 /* $NetBSD: citrus_euc.c,v 1.14 2009/01/11 02:46:24 christos Exp $ */ 2 3 /*- 4 * Copyright (c)2002 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 29 /*- 30 * SPDX-License-Identifier: BSD-3-Clause 31 * 32 * Copyright (c) 1993 33 * The Regents of the University of California. All rights reserved. 34 * 35 * This code is derived from software contributed to Berkeley by 36 * Paul Borman at Krystal Technologies. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 #include <sys/cdefs.h> 64 #include <sys/types.h> 65 66 #include <assert.h> 67 #include <errno.h> 68 #include <limits.h> 69 #include <stddef.h> 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <wchar.h> 74 75 #include "citrus_namespace.h" 76 #include "citrus_bcs.h" 77 #include "citrus_types.h" 78 #include "citrus_module.h" 79 #include "citrus_stdenc.h" 80 #include "citrus_euc.h" 81 82 83 /* ---------------------------------------------------------------------- 84 * private stuffs used by templates 85 */ 86 87 typedef struct { 88 int chlen; 89 char ch[3]; 90 } _EUCState; 91 92 typedef struct { 93 wchar_t bits[4]; 94 wchar_t mask; 95 unsigned count[4]; 96 unsigned mb_cur_max; 97 } _EUCEncodingInfo; 98 99 #define _SS2 0x008e 100 #define _SS3 0x008f 101 102 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 103 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ 104 105 #define _FUNCNAME(m) _citrus_EUC_##m 106 #define _ENCODING_INFO _EUCEncodingInfo 107 #define _ENCODING_STATE _EUCState 108 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max 109 #define _ENCODING_IS_STATE_DEPENDENT 0 110 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 111 112 113 static __inline int 114 _citrus_EUC_cs(unsigned int c) 115 { 116 117 c &= 0xff; 118 119 return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0); 120 } 121 122 static __inline int 123 _citrus_EUC_parse_variable(_EUCEncodingInfo *ei, const void *var, 124 size_t lenvar __unused) 125 { 126 char *e; 127 const char *v; 128 int x; 129 130 /* parse variable string */ 131 if (!var) 132 return (EFTYPE); 133 134 v = (const char *)var; 135 136 while (*v == ' ' || *v == '\t') 137 ++v; 138 139 ei->mb_cur_max = 1; 140 for (x = 0; x < 4; ++x) { 141 ei->count[x] = (int)_bcs_strtol(v, (char **)&e, 0); 142 if (v == e || !(v = e) || ei->count[x] < 1 || ei->count[x] > 4) { 143 return (EFTYPE); 144 } 145 if (ei->mb_cur_max < ei->count[x]) 146 ei->mb_cur_max = ei->count[x]; 147 while (*v == ' ' || *v == '\t') 148 ++v; 149 ei->bits[x] = (int)_bcs_strtol(v, (char **)&e, 0); 150 if (v == e || !(v = e)) { 151 return (EFTYPE); 152 } 153 while (*v == ' ' || *v == '\t') 154 ++v; 155 } 156 ei->mask = (int)_bcs_strtol(v, (char **)&e, 0); 157 if (v == e || !(v = e)) { 158 return (EFTYPE); 159 } 160 161 return (0); 162 } 163 164 165 static __inline void 166 /*ARGSUSED*/ 167 _citrus_EUC_init_state(_EUCEncodingInfo *ei __unused, _EUCState *s) 168 { 169 170 memset(s, 0, sizeof(*s)); 171 } 172 173 #if 0 174 static __inline void 175 /*ARGSUSED*/ 176 _citrus_EUC_pack_state(_EUCEncodingInfo *ei __unused, void *pspriv, 177 const _EUCState *s) 178 { 179 180 memcpy(pspriv, (const void *)s, sizeof(*s)); 181 } 182 183 static __inline void 184 /*ARGSUSED*/ 185 _citrus_EUC_unpack_state(_EUCEncodingInfo *ei __unused, _EUCState *s, 186 const void *pspriv) 187 { 188 189 memcpy((void *)s, pspriv, sizeof(*s)); 190 } 191 #endif 192 193 static int 194 _citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, char **s, 195 size_t n, _EUCState *psenc, size_t *nresult) 196 { 197 wchar_t wchar; 198 int c, chlenbak, cs, len; 199 char *s0, *s1 = NULL; 200 201 s0 = *s; 202 203 if (s0 == NULL) { 204 _citrus_EUC_init_state(ei, psenc); 205 *nresult = 0; /* state independent */ 206 return (0); 207 } 208 209 chlenbak = psenc->chlen; 210 211 /* make sure we have the first byte in the buffer */ 212 switch (psenc->chlen) { 213 case 0: 214 if (n < 1) 215 goto restart; 216 psenc->ch[0] = *s0++; 217 psenc->chlen = 1; 218 n--; 219 break; 220 case 1: 221 case 2: 222 break; 223 default: 224 /* illgeal state */ 225 goto encoding_error; 226 } 227 228 c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)]; 229 if (c == 0) 230 goto encoding_error; 231 while (psenc->chlen < c) { 232 if (n < 1) 233 goto restart; 234 psenc->ch[psenc->chlen] = *s0++; 235 psenc->chlen++; 236 n--; 237 } 238 *s = s0; 239 240 switch (cs) { 241 case 3: 242 case 2: 243 /* skip SS2/SS3 */ 244 len = c - 1; 245 s1 = &psenc->ch[1]; 246 break; 247 case 1: 248 case 0: 249 len = c; 250 s1 = &psenc->ch[0]; 251 break; 252 default: 253 goto encoding_error; 254 } 255 wchar = 0; 256 while (len-- > 0) 257 wchar = (wchar << 8) | (*s1++ & 0xff); 258 wchar = (wchar & ~ei->mask) | ei->bits[cs]; 259 260 psenc->chlen = 0; 261 if (pwc) 262 *pwc = wchar; 263 *nresult = wchar ? (size_t)(c - chlenbak) : 0; 264 return (0); 265 266 encoding_error: 267 psenc->chlen = 0; 268 *nresult = (size_t)-1; 269 return (EILSEQ); 270 271 restart: 272 *nresult = (size_t)-2; 273 *s = s0; 274 return (0); 275 } 276 277 static int 278 _citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc, 279 _EUCState *psenc __unused, size_t *nresult) 280 { 281 wchar_t m, nm; 282 unsigned int cs; 283 int ret; 284 short i; 285 286 m = wc & ei->mask; 287 nm = wc & ~m; 288 289 for (cs = 0; cs < sizeof(ei->count) / sizeof(ei->count[0]); cs++) 290 if (m == ei->bits[cs]) 291 break; 292 /* fallback case - not sure if it is necessary */ 293 if (cs == sizeof(ei->count) / sizeof(ei->count[0])) 294 cs = 1; 295 296 i = ei->count[cs]; 297 if (n < (unsigned)i) { 298 ret = E2BIG; 299 goto err; 300 } 301 m = (cs) ? 0x80 : 0x00; 302 switch (cs) { 303 case 2: 304 *s++ = _SS2; 305 i--; 306 break; 307 case 3: 308 *s++ = _SS3; 309 i--; 310 break; 311 } 312 313 while (i-- > 0) 314 *s++ = ((nm >> (i << 3)) & 0xff) | m; 315 316 *nresult = (size_t)ei->count[cs]; 317 return (0); 318 319 err: 320 *nresult = (size_t)-1; 321 return (ret); 322 } 323 324 static __inline int 325 /*ARGSUSED*/ 326 _citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei, 327 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) 328 { 329 wchar_t m, nm; 330 331 m = wc & ei->mask; 332 nm = wc & ~m; 333 334 *csid = (_citrus_csid_t)m; 335 *idx = (_citrus_index_t)nm; 336 337 return (0); 338 } 339 340 static __inline int 341 /*ARGSUSED*/ 342 _citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei, 343 wchar_t * __restrict wc, _csid_t csid, _index_t idx) 344 { 345 346 if ((csid & ~ei->mask) != 0 || (idx & ei->mask) != 0) 347 return (EINVAL); 348 349 *wc = (wchar_t)csid | (wchar_t)idx; 350 351 return (0); 352 } 353 354 static __inline int 355 /*ARGSUSED*/ 356 _citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei __unused, 357 _EUCState * __restrict psenc, int * __restrict rstate) 358 { 359 360 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL : 361 _STDENC_SDGEN_INCOMPLETE_CHAR; 362 return (0); 363 } 364 365 static int 366 /*ARGSUSED*/ 367 _citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei, 368 const void * __restrict var, size_t lenvar) 369 { 370 371 return (_citrus_EUC_parse_variable(ei, var, lenvar)); 372 } 373 374 static void 375 /*ARGSUSED*/ 376 _citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei __unused) 377 { 378 379 } 380 381 /* ---------------------------------------------------------------------- 382 * public interface for stdenc 383 */ 384 385 _CITRUS_STDENC_DECLS(EUC); 386 _CITRUS_STDENC_DEF_OPS(EUC); 387 388 #include "citrus_stdenc_template.h" 389