1 /* $NetBSD: citrus_viqr.c,v 1.10 2026/07/05 20:40:04 riastradh 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 */ 29 30 #include <sys/cdefs.h> 31 #if defined(LIBC_SCCS) && !defined(lint) 32 __RCSID("$NetBSD: citrus_viqr.c,v 1.10 2026/07/05 20:40:04 riastradh Exp $"); 33 #endif /* LIBC_SCCS and not lint */ 34 35 #include <sys/queue.h> 36 #include <sys/types.h> 37 #include <assert.h> 38 #include <errno.h> 39 #include <string.h> 40 #include <stdint.h> 41 #include <stdlib.h> 42 #include <stddef.h> 43 #include <wchar.h> 44 #include <limits.h> 45 46 #include "citrus_namespace.h" 47 #include "citrus_types.h" 48 #include "citrus_bcs.h" 49 #include "citrus_module.h" 50 #include "citrus_ctype.h" 51 #include "citrus_stdenc.h" 52 #include "citrus_viqr.h" 53 54 #define ESCAPE '\\' 55 56 /* 57 * this table generated from RFC 1456. 58 */ 59 static const char *mnemonic_rfc1456[0x100] = { 60 NULL , NULL , "A(?", NULL , NULL , "A(~", "A^~", NULL , 61 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 62 NULL , NULL , NULL , NULL , "Y?" , NULL , NULL , NULL , 63 NULL , "Y~" , NULL , NULL , NULL , NULL , "Y." , NULL , 64 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 65 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 66 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 67 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 68 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 69 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 70 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 71 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 72 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 73 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 74 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 75 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , 76 "A." , "A('", "A(`", "A(.", "A^'", "A^`", "A^?", "A^.", 77 "E~" , "E." , "E^'", "E^`", "E^?", "E^~", "E^.", "O^'", 78 "O^`", "O^?", "O^~", "O^.", "O+.", "O+'", "O+`", "O+?", 79 "I." , "O?" , "O." , "I?" , "U?" , "U~" , "U." , "Y`" , 80 "O~" , "a('", "a(`", "a(.", "a^'", "a^`", "a^?", "a^.", 81 "e~" , "e." , "e^'", "e^`", "e^?", "e^~", "e^.", "o^'", 82 "o^`", "o^?", "o^~", "O+~", "O+" , "o^.", "o+`", "o+?", 83 "i." , "U+.", "U+'", "U+`", "U+?", "o+" , "o+'", "U+" , 84 "A`" , "A'" , "A^" , "A~" , "A?" , "A(" , "a(?", "a(~", 85 "E`" , "E'" , "E^" , "E?" , "I`" , "I'" , "I~" , "y`" , 86 "DD" , "u+'", "O`" , "O'" , "O^" , "a." , "y?" , "u+`", 87 "u+?", "U`" , "U'" , "y~" , "y." , "Y'" , "o+~", "u+" , 88 "a`" , "a'" , "a^" , "a~" , "a?" , "a(" , "u+~", "a^~", 89 "e`" , "e'" , "e^" , "e?" , "i`" , "i'" , "i~" , "i?" , 90 "dd" , "u+.", "o`" , "o'" , "o^" , "o~" , "o?" , "o." , 91 "u." , "u`" , "u'" , "u~" , "u?" , "y'" , "o+.", "U+~", 92 }; 93 94 typedef struct { 95 const char *name; 96 wchar_t value; 97 } mnemonic_def_t; 98 99 static const mnemonic_def_t mnemonic_ext[] = { 100 /* add extra mnemonic here (should be sorted by wchar_t order). */ 101 }; 102 static const size_t mnemonic_ext_size = 103 sizeof(mnemonic_ext) / sizeof(mnemonic_def_t); 104 105 static const char * 106 mnemonic_ext_find(wchar_t wc, const mnemonic_def_t *head, size_t n) 107 { 108 const mnemonic_def_t *mid; 109 110 _DIAGASSERT(head != NULL); 111 112 for (; n > 0; n >>= 1) { 113 mid = head + (n >> 1); 114 if (mid->value == wc) { 115 return mid->name; 116 } else if (mid->value < wc) { 117 head = mid + 1; 118 --n; 119 } 120 } 121 return NULL; 122 } 123 124 struct mnemonic_t; 125 typedef TAILQ_HEAD(mnemonic_list_t, mnemonic_t) mnemonic_list_t; 126 typedef struct mnemonic_t { 127 TAILQ_ENTRY(mnemonic_t) entry; 128 int ascii; 129 struct mnemonic_t *parent; 130 mnemonic_list_t child; 131 wchar_t value; 132 } mnemonic_t; 133 134 static mnemonic_t * 135 mnemonic_list_find(mnemonic_list_t *ml, int ch) 136 { 137 mnemonic_t *m; 138 139 _DIAGASSERT(ml != NULL); 140 141 TAILQ_FOREACH(m, ml, entry) { 142 if (m->ascii == ch) 143 return m; 144 } 145 146 return NULL; 147 } 148 149 static mnemonic_t * 150 mnemonic_create_internal(mnemonic_t *parent, int ascii, wchar_t value) 151 { 152 mnemonic_t *m; 153 154 m = malloc(sizeof(*m)); 155 if (m != NULL) { 156 m->parent = parent; 157 m->ascii = ascii; 158 m->value = value; 159 TAILQ_INIT(&m->child); 160 } 161 162 return m; 163 } 164 165 static mnemonic_t * 166 mnemonic_create_root(int ascii, wchar_t value) 167 { 168 169 return mnemonic_create_internal(NULL, ascii, value); 170 } 171 172 static mnemonic_t * 173 mnemonic_create(mnemonic_t *parent, int ascii, wchar_t value) 174 { 175 176 _DIAGASSERT(parent != NULL); 177 return mnemonic_create_internal(parent, ascii, value); 178 } 179 180 static int 181 mnemonic_append_child(mnemonic_t *m, const char *s, 182 wchar_t value, wchar_t invalid) 183 { 184 int ch; 185 mnemonic_t *m0; 186 187 _DIAGASSERT(m != NULL); 188 _DIAGASSERT(s != NULL); 189 190 ch = (unsigned char)*s++; 191 if (ch == '\0') 192 return EINVAL; 193 m0 = mnemonic_list_find(&m->child, ch); 194 if (m0 == NULL) { 195 m0 = mnemonic_create(m, ch, (wchar_t)ch); 196 if (m0 == NULL) 197 return ENOMEM; 198 TAILQ_INSERT_TAIL(&m->child, m0, entry); 199 } 200 m = m0; 201 for (m0 = NULL; (ch = (unsigned char)*s) != '\0'; ++s) { 202 m0 = mnemonic_list_find(&m->child, ch); 203 if (m0 == NULL) { 204 m0 = mnemonic_create(m, ch, invalid); 205 if (m0 == NULL) 206 return ENOMEM; 207 TAILQ_INSERT_TAIL(&m->child, m0, entry); 208 } 209 m = m0; 210 } 211 if (m0 == NULL) 212 return EINVAL; 213 m0->value = value; 214 215 return 0; 216 } 217 218 static void 219 mnemonic_destroy(mnemonic_t *m) 220 { 221 mnemonic_t *m0, *n; 222 223 _DIAGASSERT(m != NULL); 224 225 TAILQ_FOREACH_SAFE(m0, &m->child, entry, n) 226 mnemonic_destroy(m0); 227 free(m); 228 } 229 230 typedef struct { 231 size_t mb_cur_max; 232 wchar_t invalid; 233 mnemonic_t *mroot; 234 } _VIQREncodingInfo; 235 236 typedef struct { 237 unsigned char chlen; 238 char ch[MB_LEN_MAX]; 239 } _VIQRState; 240 241 __CTASSERT(__arraycount(((_VIQRState *)0)->ch) <= CHAR_MAX); 242 243 typedef struct { 244 _VIQREncodingInfo ei; 245 struct { 246 /* for future multi-locale facility */ 247 _VIQRState s_mblen; 248 _VIQRState s_mbrlen; 249 _VIQRState s_mbrtowc; 250 _VIQRState s_mbtowc; 251 _VIQRState s_mbsrtowcs; 252 _VIQRState s_mbsnrtowcs; 253 _VIQRState s_wcrtomb; 254 _VIQRState s_wcsrtombs; 255 _VIQRState s_wcsnrtombs; 256 _VIQRState s_wctomb; 257 } states; 258 } _VIQRCTypeInfo; 259 260 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 261 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ 262 263 #define _FUNCNAME(m) _citrus_VIQR_##m 264 #define _ENCODING_INFO _VIQREncodingInfo 265 #define _CTYPE_INFO _VIQRCTypeInfo 266 #define _ENCODING_STATE _VIQRState 267 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max 268 #define _ENCODING_IS_STATE_DEPENDENT 1 269 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 270 271 static __inline void 272 /*ARGSUSED*/ 273 _citrus_VIQR_init_state(_VIQREncodingInfo * __restrict ei, 274 _VIQRState * __restrict psenc) 275 { 276 /* ei may be unused */ 277 _DIAGASSERT(psenc != NULL); 278 279 psenc->chlen = 0; 280 } 281 282 static __inline void 283 /*ARGSUSED*/ 284 _citrus_VIQR_pack_state(_VIQREncodingInfo * __restrict ei, 285 void *__restrict pspriv, const _VIQRState * __restrict psenc) 286 { 287 /* ei may be unused */ 288 _DIAGASSERT(pspriv != NULL); 289 _DIAGASSERT(psenc != NULL); 290 291 memcpy(pspriv, (const void *)psenc, sizeof(*psenc)); 292 } 293 294 static __inline void 295 /*ARGSUSED*/ 296 _citrus_VIQR_unpack_state(_VIQREncodingInfo * __restrict ei, 297 _VIQRState * __restrict psenc, const void * __restrict pspriv) 298 { 299 /* ei may be unused */ 300 _DIAGASSERT(psenc != NULL); 301 _DIAGASSERT(pspriv != NULL); 302 303 memcpy((void *)psenc, pspriv, sizeof(*psenc)); 304 } 305 306 static int 307 _citrus_VIQR_mbrtowc_priv(_VIQREncodingInfo * __restrict ei, 308 wchar_t * __restrict pwc, const char ** __restrict s, size_t n, 309 _VIQRState * __restrict psenc, size_t * __restrict nresult) 310 { 311 const char *s0; 312 wchar_t wc; 313 mnemonic_t *m, *m0; 314 size_t i; 315 int ch, escape; 316 317 _DIAGASSERT(ei != NULL); 318 /* pwc may be null */ 319 _DIAGASSERT(s != NULL); 320 _DIAGASSERT(psenc != NULL); 321 _DIAGASSERT(nresult != NULL); 322 323 if (*s == NULL) { 324 _citrus_VIQR_init_state(ei, psenc); 325 *nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT; 326 return 0; 327 } 328 s0 = *s; 329 330 i = 0; 331 m = ei->mroot; 332 for (escape = 0;;) { 333 _DIAGASSERT(psenc->chlen < __arraycount(psenc->ch)); 334 _DIAGASSERT(i <= psenc->chlen); 335 if (psenc->chlen == i) { 336 if (n-- < 1) { 337 *s = s0; 338 *nresult = (size_t)-2; 339 return 0; 340 } 341 _DIAGASSERT(psenc->chlen < __arraycount(psenc->ch)); 342 psenc->ch[psenc->chlen++] = *s0++; 343 } 344 ch = (unsigned char)psenc->ch[i++]; 345 if (ch == ESCAPE) { 346 if (m != ei->mroot) 347 break; 348 escape = 1; 349 continue; 350 } 351 if (escape != 0) 352 break; 353 m0 = mnemonic_list_find(&m->child, ch); 354 if (m0 == NULL) 355 break; 356 m = m0; 357 } 358 while (m != ei->mroot) { 359 --i; 360 if (m->value != ei->invalid) 361 break; 362 m = m->parent; 363 } 364 if (ch == ESCAPE && m != ei->mroot) 365 ++i; 366 _DIAGASSERT(i <= psenc->chlen); 367 _DIAGASSERT(psenc->chlen <= sizeof(psenc->ch)); 368 psenc->chlen -= i; 369 memmove(&psenc->ch[0], &psenc->ch[i], psenc->chlen); 370 wc = (m == ei->mroot) ? (wchar_t)ch : m->value; 371 if (pwc != NULL) 372 *pwc = wc; 373 *nresult = (size_t)(wc == 0 ? 0 : s0 - *s); 374 *s = s0; 375 376 return 0; 377 } 378 379 static int 380 _citrus_VIQR_wcrtomb_priv(_VIQREncodingInfo * __restrict ei, 381 char * __restrict s, size_t n, wchar_t wc, 382 _VIQRState * __restrict psenc, size_t * __restrict nresult) 383 { 384 mnemonic_t *m; 385 int ch; 386 const char *p; 387 388 _DIAGASSERT(ei != NULL); 389 _DIAGASSERT(s != NULL); 390 _DIAGASSERT(psenc != NULL); 391 _DIAGASSERT(nresult != NULL); 392 393 switch (psenc->chlen) { 394 case 0: 395 break; 396 case 1: 397 if (n-- < 1) 398 goto e2big; 399 break; 400 default: 401 return EINVAL; 402 } 403 m = NULL; 404 if ((uint32_t)wc <= 0xFF) { 405 p = mnemonic_rfc1456[wc & 0xFF]; 406 if (p != NULL) 407 goto mnemonic_found; 408 if (n-- < 1) 409 goto e2big; 410 ch = (unsigned int)wc; 411 m = ei->mroot; 412 if (psenc->chlen > 0) { 413 m = mnemonic_list_find(&m->child, psenc->ch[0]); 414 if (m == NULL) 415 return EINVAL; 416 psenc->ch[0] = ESCAPE; 417 } 418 if (mnemonic_list_find(&m->child, ch) == NULL) { 419 psenc->chlen = 0; 420 m = NULL; 421 } 422 _DIAGASSERT(psenc->chlen < __arraycount(psenc->ch)); 423 psenc->ch[psenc->chlen++] = ch; 424 } else { 425 p = mnemonic_ext_find(wc, &mnemonic_ext[0], mnemonic_ext_size); 426 if (p == NULL) { 427 *nresult = (size_t)-1; 428 return EILSEQ; 429 } else { 430 mnemonic_found: 431 psenc->chlen = 0; 432 while (*p != '\0') { 433 if (n-- < 1) 434 goto e2big; 435 _DIAGASSERT(psenc->chlen < 436 __arraycount(psenc->ch)); 437 psenc->ch[psenc->chlen++] = *p++; 438 } 439 } 440 } 441 _DIAGASSERT(psenc->chlen <= sizeof(psenc->ch)); 442 memcpy(s, psenc->ch, psenc->chlen); 443 *nresult = psenc->chlen; 444 if (m == ei->mroot) { 445 psenc->ch[0] = ch; 446 psenc->chlen = 1; 447 } else { 448 psenc->chlen = 0; 449 } 450 451 return 0; 452 453 e2big: 454 *nresult = (size_t)-1; 455 return E2BIG; 456 } 457 458 static int 459 /* ARGSUSED */ 460 _citrus_VIQR_put_state_reset(_VIQREncodingInfo * __restrict ei, 461 char * __restrict s, size_t n, _VIQRState * __restrict psenc, 462 size_t * __restrict nresult) 463 { 464 /* ei may be unused */ 465 _DIAGASSERT(s != NULL); 466 _DIAGASSERT(psenc != NULL); 467 _DIAGASSERT(nresult != NULL); 468 469 switch (psenc->chlen) { 470 case 0: case 1: 471 break; 472 default: 473 return EINVAL; 474 } 475 *nresult = 0; 476 psenc->chlen = 0; 477 478 return 0; 479 } 480 481 static __inline int 482 /*ARGSUSED*/ 483 _citrus_VIQR_stdenc_wctocs(_VIQREncodingInfo * __restrict ei, 484 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc) 485 { 486 /* ei may be unused */ 487 _DIAGASSERT(csid != NULL); 488 _DIAGASSERT(idx != NULL); 489 490 *csid = 0; 491 *idx = (_index_t)wc; 492 493 return 0; 494 } 495 496 static __inline int 497 /*ARGSUSED*/ 498 _citrus_VIQR_stdenc_cstowc(_VIQREncodingInfo * __restrict ei, 499 wchar_t * __restrict pwc, _csid_t csid, _index_t idx) 500 { 501 /* ei may be unused */ 502 _DIAGASSERT(pwc != NULL); 503 504 if (csid != 0) 505 return EILSEQ; 506 *pwc = (wchar_t)idx; 507 508 return 0; 509 } 510 511 static void 512 _citrus_VIQR_encoding_module_uninit(_VIQREncodingInfo *ei) 513 { 514 _DIAGASSERT(ei != NULL); 515 516 mnemonic_destroy(ei->mroot); 517 } 518 519 static int 520 /*ARGSUSED*/ 521 _citrus_VIQR_encoding_module_init(_VIQREncodingInfo * __restrict ei, 522 const void * __restrict var, size_t lenvar) 523 { 524 int errnum; 525 const char *s; 526 size_t i, n; 527 const mnemonic_def_t *p; 528 529 _DIAGASSERT(ei != NULL); 530 /* var may be unused */ 531 532 ei->mb_cur_max = 1; 533 ei->invalid = (wchar_t)-1; 534 ei->mroot = mnemonic_create_root('\0', ei->invalid); 535 if (ei->mroot == NULL) 536 return ENOMEM; 537 for (i = 0; i < sizeof(mnemonic_rfc1456) / sizeof(const char *); ++i) { 538 s = mnemonic_rfc1456[i]; 539 if (s == NULL) 540 continue; 541 n = strlen(s); 542 _DIAGASSERT(n <= MB_LEN_MAX); 543 if (ei->mb_cur_max < n) 544 ei->mb_cur_max = n; 545 errnum = mnemonic_append_child(ei->mroot, 546 s, (wchar_t)i, ei->invalid); 547 if (errnum != 0) { 548 _citrus_VIQR_encoding_module_uninit(ei); 549 return errnum; 550 } 551 } 552 for (i = 0; i < mnemonic_ext_size; ++i) { 553 p = &mnemonic_ext[i]; 554 _DIAGASSERT(p != NULL && p->name != NULL); 555 n = strlen(p->name); 556 _DIAGASSERT(n <= MB_LEN_MAX); 557 if (ei->mb_cur_max < n) 558 ei->mb_cur_max = n; 559 errnum = mnemonic_append_child(ei->mroot, 560 p->name, p->value, ei->invalid); 561 if (errnum != 0) { 562 _citrus_VIQR_encoding_module_uninit(ei); 563 return errnum; 564 } 565 } 566 567 return 0; 568 } 569 570 static __inline int 571 /*ARGSUSED*/ 572 _citrus_VIQR_stdenc_get_state_desc_generic(_VIQREncodingInfo * __restrict ei, 573 _VIQRState * __restrict psenc, int * __restrict rstate) 574 { 575 /* ei may be unused */ 576 _DIAGASSERT(psenc != NULL); 577 _DIAGASSERT(rstate != NULL); 578 579 *rstate = (psenc->chlen == 0) 580 ? _STDENC_SDGEN_INITIAL 581 : _STDENC_SDGEN_INCOMPLETE_CHAR; 582 583 return 0; 584 } 585 586 /* ---------------------------------------------------------------------- 587 * public interface for ctype 588 */ 589 590 _CITRUS_CTYPE_DECLS(VIQR); 591 _CITRUS_CTYPE_DEF_OPS(VIQR); 592 593 #include "citrus_ctype_template.h" 594 595 /* ---------------------------------------------------------------------- 596 * public interface for stdenc 597 */ 598 599 _CITRUS_STDENC_DECLS(VIQR); 600 _CITRUS_STDENC_DEF_OPS(VIQR); 601 602 #include "citrus_stdenc_template.h" 603