10d5acd74SJohn Marino /*-
20d5acd74SJohn Marino * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
30d5acd74SJohn Marino * All rights reserved.
40d5acd74SJohn Marino *
50d5acd74SJohn Marino * Redistribution and use in source and binary forms, with or without
60d5acd74SJohn Marino * modification, are permitted provided that the following conditions
70d5acd74SJohn Marino * are met:
80d5acd74SJohn Marino * 1. Redistributions of source code must retain the above copyright
90d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer.
100d5acd74SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
110d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer in the
120d5acd74SJohn Marino * documentation and/or other materials provided with the distribution.
130d5acd74SJohn Marino *
140d5acd74SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
150d5acd74SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160d5acd74SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
170d5acd74SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
180d5acd74SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
190d5acd74SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
200d5acd74SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
210d5acd74SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
220d5acd74SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
230d5acd74SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
240d5acd74SJohn Marino * SUCH DAMAGE.
250d5acd74SJohn Marino *
26*0db70a6aSJohn Marino * $FreeBSD: head/lib/libc/locale/cXXrtomb_iconv.h 281550 2015-04-15 09:09:20Z tijl $
270d5acd74SJohn Marino */
280d5acd74SJohn Marino
290d5acd74SJohn Marino
300d5acd74SJohn Marino #include <sys/queue.h>
310d5acd74SJohn Marino
320d5acd74SJohn Marino #include <assert.h>
330d5acd74SJohn Marino #include <errno.h>
340d5acd74SJohn Marino #include <langinfo.h>
350d5acd74SJohn Marino #include <uchar.h>
360d5acd74SJohn Marino
370d5acd74SJohn Marino #include "../citrus/citrus_hash.h"
380d5acd74SJohn Marino #include "../citrus/citrus_module.h"
390d5acd74SJohn Marino #include "../citrus/citrus_iconv.h"
400d5acd74SJohn Marino #include "xlocale_private.h"
410d5acd74SJohn Marino
420d5acd74SJohn Marino typedef struct {
430d5acd74SJohn Marino bool initialized;
440d5acd74SJohn Marino struct _citrus_iconv iconv;
450d5acd74SJohn Marino union {
460d5acd74SJohn Marino charXX_t widechar[SRCBUF_LEN];
470d5acd74SJohn Marino char bytes[sizeof(charXX_t) * SRCBUF_LEN];
480d5acd74SJohn Marino } srcbuf;
490d5acd74SJohn Marino size_t srcbuf_len;
500d5acd74SJohn Marino } _ConversionState;
510d5acd74SJohn Marino _Static_assert(sizeof(_ConversionState) <= sizeof(mbstate_t),
520d5acd74SJohn Marino "Size of _ConversionState must not exceed mbstate_t's size.");
530d5acd74SJohn Marino
540d5acd74SJohn Marino size_t
cXXrtomb_l(char * __restrict s,charXX_t c,mbstate_t * __restrict ps,locale_t locale)550d5acd74SJohn Marino cXXrtomb_l(char * __restrict s, charXX_t c, mbstate_t * __restrict ps,
560d5acd74SJohn Marino locale_t locale)
570d5acd74SJohn Marino {
580d5acd74SJohn Marino _ConversionState *cs;
590d5acd74SJohn Marino struct _citrus_iconv *handle;
60*0db70a6aSJohn Marino char *src, *dst;
610d5acd74SJohn Marino size_t srcleft, dstleft, invlen;
620d5acd74SJohn Marino int err;
630d5acd74SJohn Marino
640d5acd74SJohn Marino FIX_LOCALE(locale);
650d5acd74SJohn Marino if (ps == NULL)
660d5acd74SJohn Marino ps = &locale->cXXrtomb;
670d5acd74SJohn Marino cs = (_ConversionState *)ps;
680d5acd74SJohn Marino handle = &cs->iconv;
690d5acd74SJohn Marino
700d5acd74SJohn Marino /* Reinitialize mbstate_t. */
710d5acd74SJohn Marino if (s == NULL || !cs->initialized) {
720d5acd74SJohn Marino if (_citrus_iconv_open(&handle, UTF_XX_INTERNAL,
730d5acd74SJohn Marino nl_langinfo_l(CODESET, locale)) != 0) {
740d5acd74SJohn Marino cs->initialized = false;
750d5acd74SJohn Marino errno = EINVAL;
760d5acd74SJohn Marino return (-1);
770d5acd74SJohn Marino }
780d5acd74SJohn Marino handle->cv_shared->ci_discard_ilseq = true;
790d5acd74SJohn Marino handle->cv_shared->ci_hooks = NULL;
800d5acd74SJohn Marino cs->srcbuf_len = 0;
810d5acd74SJohn Marino cs->initialized = true;
820d5acd74SJohn Marino if (s == NULL)
830d5acd74SJohn Marino return (1);
840d5acd74SJohn Marino }
850d5acd74SJohn Marino
860d5acd74SJohn Marino assert(cs->srcbuf_len < sizeof(cs->srcbuf.widechar) / sizeof(charXX_t));
870d5acd74SJohn Marino cs->srcbuf.widechar[cs->srcbuf_len++] = c;
880d5acd74SJohn Marino
890d5acd74SJohn Marino /* Perform conversion. */
900d5acd74SJohn Marino src = cs->srcbuf.bytes;
910d5acd74SJohn Marino srcleft = cs->srcbuf_len * sizeof(charXX_t);
920d5acd74SJohn Marino dst = s;
930d5acd74SJohn Marino dstleft = MB_CUR_MAX_L(locale);
940d5acd74SJohn Marino err = _citrus_iconv_convert(handle, &src, &srcleft, &dst, &dstleft,
950d5acd74SJohn Marino 0, &invlen);
960d5acd74SJohn Marino
970d5acd74SJohn Marino /* Character is part of a surrogate pair. We need more input. */
980d5acd74SJohn Marino if (err == EINVAL)
990d5acd74SJohn Marino return (0);
1000d5acd74SJohn Marino cs->srcbuf_len = 0;
1010d5acd74SJohn Marino
1020d5acd74SJohn Marino /* Illegal sequence. */
1030d5acd74SJohn Marino if (dst == s) {
1040d5acd74SJohn Marino errno = EILSEQ;
1050d5acd74SJohn Marino return ((size_t)-1);
1060d5acd74SJohn Marino }
1070d5acd74SJohn Marino return (dst - s);
1080d5acd74SJohn Marino }
1090d5acd74SJohn Marino
1100d5acd74SJohn Marino size_t
cXXrtomb(char * __restrict s,charXX_t c,mbstate_t * __restrict ps)1110d5acd74SJohn Marino cXXrtomb(char * __restrict s, charXX_t c, mbstate_t * __restrict ps)
1120d5acd74SJohn Marino {
1130d5acd74SJohn Marino
1140d5acd74SJohn Marino return (cXXrtomb_l(s, c, ps, __get_locale()));
1150d5acd74SJohn Marino }
116