1219019Sgabor/* $FreeBSD$ */ 2219019Sgabor/* $NetBSD: citrus_utf8.c,v 1.17 2008/06/14 16:01:08 tnozaki Exp $ */ 3219019Sgabor 4219019Sgabor/*- 5219019Sgabor * Copyright (c)2002 Citrus Project, 6219019Sgabor * All rights reserved. 7219019Sgabor * 8219019Sgabor * Redistribution and use in source and binary forms, with or without 9219019Sgabor * modification, are permitted provided that the following conditions 10219019Sgabor * are met: 11219019Sgabor * 1. Redistributions of source code must retain the above copyright 12219019Sgabor * notice, this list of conditions and the following disclaimer. 13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14219019Sgabor * notice, this list of conditions and the following disclaimer in the 15219019Sgabor * documentation and/or other materials provided with the distribution. 16219019Sgabor * 17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27219019Sgabor * SUCH DAMAGE. 28219019Sgabor */ 29219019Sgabor 30219019Sgabor/*- 31219019Sgabor * Copyright (c) 1993 32219019Sgabor * The Regents of the University of California. All rights reserved. 33219019Sgabor * 34219019Sgabor * This code is derived from software contributed to Berkeley by 35219019Sgabor * Paul Borman at Krystal Technologies. 36219019Sgabor * 37219019Sgabor * Redistribution and use in source and binary forms, with or without 38219019Sgabor * modification, are permitted provided that the following conditions 39219019Sgabor * are met: 40219019Sgabor * 1. Redistributions of source code must retain the above copyright 41219019Sgabor * notice, this list of conditions and the following disclaimer. 42219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 43219019Sgabor * notice, this list of conditions and the following disclaimer in the 44219019Sgabor * documentation and/or other materials provided with the distribution. 45219019Sgabor * 3. Neither the name of the University nor the names of its contributors 46219019Sgabor * may be used to endorse or promote products derived from this software 47219019Sgabor * without specific prior written permission. 48219019Sgabor * 49219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59219019Sgabor * SUCH DAMAGE. 60219019Sgabor */ 61219019Sgabor 62219019Sgabor#include <sys/cdefs.h> 63219019Sgabor#include <sys/types.h> 64219019Sgabor 65219019Sgabor#include <assert.h> 66219019Sgabor#include <errno.h> 67219019Sgabor#include <limits.h> 68219019Sgabor#include <stdbool.h> 69219019Sgabor#include <stddef.h> 70219019Sgabor#include <stdio.h> 71219019Sgabor#include <stdlib.h> 72219019Sgabor#include <string.h> 73219019Sgabor#include <wchar.h> 74219019Sgabor 75219019Sgabor#include "citrus_namespace.h" 76219019Sgabor#include "citrus_types.h" 77219019Sgabor#include "citrus_module.h" 78219019Sgabor#include "citrus_stdenc.h" 79219019Sgabor#include "citrus_utf8.h" 80219019Sgabor 81219019Sgabor 82219019Sgabor/* ---------------------------------------------------------------------- 83219019Sgabor * private stuffs used by templates 84219019Sgabor */ 85219019Sgabor 86219019Sgaborstatic uint8_t _UTF8_count_array[256] = { 87219019Sgabor 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F */ 88219019Sgabor 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */ 89219019Sgabor 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */ 90219019Sgabor 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 30 - 3F */ 91219019Sgabor 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */ 92219019Sgabor 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */ 93219019Sgabor 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */ 94219019Sgabor 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70 - 7F */ 95219019Sgabor 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */ 96219019Sgabor 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */ 97219019Sgabor 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */ 98219019Sgabor 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */ 99219019Sgabor 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0 - CF */ 100219019Sgabor 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0 - DF */ 101219019Sgabor 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0 - EF */ 102219019Sgabor 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 /* F0 - FF */ 103219019Sgabor}; 104219019Sgabor 105219019Sgaborstatic uint8_t const *_UTF8_count = _UTF8_count_array; 106219019Sgabor 107219019Sgaborstatic const uint32_t _UTF8_range[] = { 108219019Sgabor 0, /*dummy*/ 109219019Sgabor 0x00000000, 0x00000080, 0x00000800, 0x00010000, 110219019Sgabor 0x00200000, 0x04000000, 0x80000000, 111219019Sgabor}; 112219019Sgabor 113219019Sgabortypedef struct { 114219019Sgabor int chlen; 115219019Sgabor char ch[6]; 116219019Sgabor} _UTF8State; 117219019Sgabor 118219019Sgabortypedef void *_UTF8EncodingInfo; 119219019Sgabor 120219019Sgabor#define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 121219019Sgabor#define _CEI_TO_STATE(_ei_, _func_) (_ei_)->states.s_##_func_ 122219019Sgabor 123219019Sgabor#define _FUNCNAME(m) _citrus_UTF8_##m 124219019Sgabor#define _ENCODING_INFO _UTF8EncodingInfo 125219019Sgabor#define _ENCODING_STATE _UTF8State 126219019Sgabor#define _ENCODING_MB_CUR_MAX(_ei_) 6 127219019Sgabor#define _ENCODING_IS_STATE_DEPENDENT 0 128219019Sgabor#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 129219019Sgabor 130219019Sgaborstatic size_t 131219019Sgabor_UTF8_findlen(wchar_t v) 132219019Sgabor{ 133219019Sgabor size_t i; 134219019Sgabor uint32_t c; 135219019Sgabor 136219019Sgabor c = (uint32_t)v; /*XXX*/ 137219019Sgabor for (i = 1; i < sizeof(_UTF8_range) / sizeof(_UTF8_range[0]) - 1; i++) 138219019Sgabor if (c >= _UTF8_range[i] && c < _UTF8_range[i + 1]) 139219019Sgabor return (i); 140219019Sgabor 141219019Sgabor return (-1); /*out of range*/ 142219019Sgabor} 143219019Sgabor 144219019Sgaborstatic __inline bool 145219019Sgabor_UTF8_surrogate(wchar_t wc) 146219019Sgabor{ 147219019Sgabor 148219019Sgabor return (wc >= 0xd800 && wc <= 0xdfff); 149219019Sgabor} 150219019Sgabor 151219019Sgaborstatic __inline void 152219019Sgabor/*ARGSUSED*/ 153219019Sgabor_citrus_UTF8_init_state(_UTF8EncodingInfo *ei __unused, _UTF8State *s) 154219019Sgabor{ 155219019Sgabor 156219019Sgabor s->chlen = 0; 157219019Sgabor} 158219019Sgabor 159260264Sdim#if 0 160219019Sgaborstatic __inline void 161219019Sgabor/*ARGSUSED*/ 162219019Sgabor_citrus_UTF8_pack_state(_UTF8EncodingInfo *ei __unused, void *pspriv, 163219019Sgabor const _UTF8State *s) 164219019Sgabor{ 165219019Sgabor 166219019Sgabor memcpy(pspriv, (const void *)s, sizeof(*s)); 167219019Sgabor} 168219019Sgabor 169219019Sgaborstatic __inline void 170219019Sgabor/*ARGSUSED*/ 171219019Sgabor_citrus_UTF8_unpack_state(_UTF8EncodingInfo *ei __unused, _UTF8State *s, 172219019Sgabor const void *pspriv) 173219019Sgabor{ 174219019Sgabor 175219019Sgabor memcpy((void *)s, pspriv, sizeof(*s)); 176219019Sgabor} 177260264Sdim#endif 178219019Sgabor 179219019Sgaborstatic int 180282275Stijl_citrus_UTF8_mbrtowc_priv(_UTF8EncodingInfo *ei, wchar_t *pwc, char **s, 181219019Sgabor size_t n, _UTF8State *psenc, size_t *nresult) 182219019Sgabor{ 183282275Stijl char *s0; 184219019Sgabor wchar_t wchar; 185219019Sgabor int i; 186219019Sgabor uint8_t c; 187219019Sgabor 188219019Sgabor s0 = *s; 189219019Sgabor 190219019Sgabor if (s0 == NULL) { 191219019Sgabor _citrus_UTF8_init_state(ei, psenc); 192219019Sgabor *nresult = 0; /* state independent */ 193219019Sgabor return (0); 194219019Sgabor } 195219019Sgabor 196219019Sgabor /* make sure we have the first byte in the buffer */ 197219019Sgabor if (psenc->chlen == 0) { 198219019Sgabor if (n-- < 1) 199219019Sgabor goto restart; 200219019Sgabor psenc->ch[psenc->chlen++] = *s0++; 201219019Sgabor } 202219019Sgabor 203219019Sgabor c = _UTF8_count[psenc->ch[0] & 0xff]; 204219019Sgabor if (c < 1 || c < psenc->chlen) 205219019Sgabor goto ilseq; 206219019Sgabor 207219019Sgabor if (c == 1) 208219019Sgabor wchar = psenc->ch[0] & 0xff; 209219019Sgabor else { 210219019Sgabor while (psenc->chlen < c) { 211219019Sgabor if (n-- < 1) 212219019Sgabor goto restart; 213219019Sgabor psenc->ch[psenc->chlen++] = *s0++; 214219019Sgabor } 215219019Sgabor wchar = psenc->ch[0] & (0x7f >> c); 216219019Sgabor for (i = 1; i < c; i++) { 217219019Sgabor if ((psenc->ch[i] & 0xc0) != 0x80) 218219019Sgabor goto ilseq; 219219019Sgabor wchar <<= 6; 220219019Sgabor wchar |= (psenc->ch[i] & 0x3f); 221219019Sgabor } 222219019Sgabor if (_UTF8_surrogate(wchar) || _UTF8_findlen(wchar) != c) 223219019Sgabor goto ilseq; 224219019Sgabor } 225219019Sgabor if (pwc != NULL) 226219019Sgabor *pwc = wchar; 227219019Sgabor *nresult = (wchar == 0) ? 0 : s0 - *s; 228219019Sgabor *s = s0; 229219019Sgabor psenc->chlen = 0; 230219019Sgabor 231219019Sgabor return (0); 232219019Sgabor 233219019Sgaborilseq: 234219019Sgabor *nresult = (size_t)-1; 235219019Sgabor return (EILSEQ); 236219019Sgabor 237219019Sgaborrestart: 238219019Sgabor *s = s0; 239219019Sgabor *nresult = (size_t)-2; 240219019Sgabor return (0); 241219019Sgabor} 242219019Sgabor 243219019Sgaborstatic int 244219019Sgabor_citrus_UTF8_wcrtomb_priv(_UTF8EncodingInfo *ei __unused, char *s, size_t n, 245219019Sgabor wchar_t wc, _UTF8State *psenc __unused, size_t *nresult) 246219019Sgabor{ 247219019Sgabor wchar_t c; 248219019Sgabor size_t cnt; 249219019Sgabor int i, ret; 250219019Sgabor 251219019Sgabor if (_UTF8_surrogate(wc)) { 252219019Sgabor ret = EILSEQ; 253219019Sgabor goto err; 254219019Sgabor } 255219019Sgabor cnt = _UTF8_findlen(wc); 256219019Sgabor if (cnt <= 0 || cnt > 6) { 257219019Sgabor /* invalid UCS4 value */ 258219019Sgabor ret = EILSEQ; 259219019Sgabor goto err; 260219019Sgabor } 261219019Sgabor if (n < cnt) { 262219019Sgabor /* bound check failure */ 263219019Sgabor ret = E2BIG; 264219019Sgabor goto err; 265219019Sgabor } 266219019Sgabor 267219019Sgabor c = wc; 268219019Sgabor if (s) { 269219019Sgabor for (i = cnt - 1; i > 0; i--) { 270219019Sgabor s[i] = 0x80 | (c & 0x3f); 271219019Sgabor c >>= 6; 272219019Sgabor } 273219019Sgabor s[0] = c; 274219019Sgabor if (cnt == 1) 275219019Sgabor s[0] &= 0x7f; 276219019Sgabor else { 277219019Sgabor s[0] &= (0x7f >> cnt); 278219019Sgabor s[0] |= ((0xff00 >> cnt) & 0xff); 279219019Sgabor } 280219019Sgabor } 281219019Sgabor 282219019Sgabor *nresult = (size_t)cnt; 283219019Sgabor return (0); 284219019Sgabor 285219019Sgaborerr: 286219019Sgabor *nresult = (size_t)-1; 287219019Sgabor return (ret); 288219019Sgabor} 289219019Sgabor 290219019Sgaborstatic __inline int 291219019Sgabor/*ARGSUSED*/ 292219019Sgabor_citrus_UTF8_stdenc_wctocs(_UTF8EncodingInfo * __restrict ei __unused, 293219019Sgabor _csid_t * __restrict csid, _index_t * __restrict idx, 294219019Sgabor wchar_t wc) 295219019Sgabor{ 296219019Sgabor 297219019Sgabor *csid = 0; 298219019Sgabor *idx = (_citrus_index_t)wc; 299219019Sgabor 300219019Sgabor return (0); 301219019Sgabor} 302219019Sgabor 303219019Sgaborstatic __inline int 304219019Sgabor/*ARGSUSED*/ 305219019Sgabor_citrus_UTF8_stdenc_cstowc(_UTF8EncodingInfo * __restrict ei __unused, 306219019Sgabor wchar_t * __restrict wc, _csid_t csid, _index_t idx) 307219019Sgabor{ 308219019Sgabor 309219019Sgabor if (csid != 0) 310219019Sgabor return (EILSEQ); 311219019Sgabor 312219019Sgabor *wc = (wchar_t)idx; 313219019Sgabor 314219019Sgabor return (0); 315219019Sgabor} 316219019Sgabor 317219019Sgaborstatic __inline int 318219019Sgabor/*ARGSUSED*/ 319219019Sgabor_citrus_UTF8_stdenc_get_state_desc_generic(_UTF8EncodingInfo * __restrict ei __unused, 320219019Sgabor _UTF8State * __restrict psenc, int * __restrict rstate) 321219019Sgabor{ 322219019Sgabor 323219019Sgabor *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL : 324219019Sgabor _STDENC_SDGEN_INCOMPLETE_CHAR; 325219019Sgabor return (0); 326219019Sgabor} 327219019Sgabor 328219019Sgaborstatic int 329219019Sgabor/*ARGSUSED*/ 330219019Sgabor_citrus_UTF8_encoding_module_init(_UTF8EncodingInfo * __restrict ei __unused, 331219019Sgabor const void * __restrict var __unused, size_t lenvar __unused) 332219019Sgabor{ 333219019Sgabor 334219019Sgabor return (0); 335219019Sgabor} 336219019Sgabor 337219019Sgaborstatic void 338219019Sgabor/*ARGSUSED*/ 339219019Sgabor_citrus_UTF8_encoding_module_uninit(_UTF8EncodingInfo *ei __unused) 340219019Sgabor{ 341219019Sgabor 342219019Sgabor} 343219019Sgabor 344219019Sgabor/* ---------------------------------------------------------------------- 345219019Sgabor * public interface for stdenc 346219019Sgabor */ 347219019Sgabor 348219019Sgabor_CITRUS_STDENC_DECLS(UTF8); 349219019Sgabor_CITRUS_STDENC_DEF_OPS(UTF8); 350219019Sgabor 351219019Sgabor#include "citrus_stdenc_template.h" 352