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/mbrtocXX_iconv.h 281550 2015-04-15 09:09:20Z tijl $
270d5acd74SJohn Marino */
280d5acd74SJohn Marino
290d5acd74SJohn Marino #include <sys/queue.h>
300d5acd74SJohn Marino
310d5acd74SJohn Marino #include <assert.h>
320d5acd74SJohn Marino #include <errno.h>
330d5acd74SJohn Marino #include <langinfo.h>
340d5acd74SJohn Marino #include <limits.h>
350d5acd74SJohn Marino #include <string.h>
360d5acd74SJohn Marino #include <uchar.h>
370d5acd74SJohn Marino
380d5acd74SJohn Marino #include "../citrus/citrus_hash.h"
390d5acd74SJohn Marino #include "../citrus/citrus_module.h"
400d5acd74SJohn Marino #include "../citrus/citrus_iconv.h"
410d5acd74SJohn Marino #include "xlocale_private.h"
420d5acd74SJohn Marino
430d5acd74SJohn Marino typedef struct {
440d5acd74SJohn Marino bool initialized;
450d5acd74SJohn Marino struct _citrus_iconv iconv;
460d5acd74SJohn Marino char srcbuf[MB_LEN_MAX];
470d5acd74SJohn Marino size_t srcbuf_len;
480d5acd74SJohn Marino union {
490d5acd74SJohn Marino charXX_t widechar[DSTBUF_LEN];
500d5acd74SJohn Marino char bytes[sizeof(charXX_t) * DSTBUF_LEN];
510d5acd74SJohn Marino } dstbuf;
520d5acd74SJohn Marino size_t dstbuf_len;
530d5acd74SJohn Marino } _ConversionState;
540d5acd74SJohn Marino _Static_assert(sizeof(_ConversionState) <= sizeof(mbstate_t),
550d5acd74SJohn Marino "Size of _ConversionState must not exceed mbstate_t's size.");
560d5acd74SJohn Marino
570d5acd74SJohn Marino size_t
mbrtocXX_l(charXX_t * __restrict pc,const char * __restrict s,size_t n,mbstate_t * __restrict ps,locale_t locale)580d5acd74SJohn Marino mbrtocXX_l(charXX_t * __restrict pc, const char * __restrict s, size_t n,
590d5acd74SJohn Marino mbstate_t * __restrict ps, locale_t locale)
600d5acd74SJohn Marino {
610d5acd74SJohn Marino _ConversionState *cs;
620d5acd74SJohn Marino struct _citrus_iconv *handle;
630d5acd74SJohn Marino size_t i, retval;
640d5acd74SJohn Marino charXX_t retchar;
650d5acd74SJohn Marino
660d5acd74SJohn Marino FIX_LOCALE(locale);
670d5acd74SJohn Marino if (ps == NULL)
680d5acd74SJohn Marino ps = &locale->mbrtocXX;
690d5acd74SJohn Marino cs = (_ConversionState *)ps;
700d5acd74SJohn Marino handle = &cs->iconv;
710d5acd74SJohn Marino
720d5acd74SJohn Marino /* Reinitialize mbstate_t. */
730d5acd74SJohn Marino if (s == NULL || !cs->initialized) {
740d5acd74SJohn Marino if (_citrus_iconv_open(&handle,
750d5acd74SJohn Marino nl_langinfo_l(CODESET, locale), UTF_XX_INTERNAL) != 0) {
760d5acd74SJohn Marino cs->initialized = false;
770d5acd74SJohn Marino errno = EINVAL;
780d5acd74SJohn Marino return (-1);
790d5acd74SJohn Marino }
800d5acd74SJohn Marino handle->cv_shared->ci_discard_ilseq = true;
810d5acd74SJohn Marino handle->cv_shared->ci_hooks = NULL;
820d5acd74SJohn Marino cs->srcbuf_len = cs->dstbuf_len = 0;
830d5acd74SJohn Marino cs->initialized = true;
840d5acd74SJohn Marino if (s == NULL)
850d5acd74SJohn Marino return (0);
860d5acd74SJohn Marino }
870d5acd74SJohn Marino
880d5acd74SJohn Marino /* See if we still have characters left from the previous invocation. */
890d5acd74SJohn Marino if (cs->dstbuf_len > 0) {
900d5acd74SJohn Marino retval = (size_t)-3;
910d5acd74SJohn Marino goto return_char;
920d5acd74SJohn Marino }
930d5acd74SJohn Marino
940d5acd74SJohn Marino /* Fill up the read buffer as far as possible. */
950d5acd74SJohn Marino if (n > sizeof(cs->srcbuf) - cs->srcbuf_len)
960d5acd74SJohn Marino n = sizeof(cs->srcbuf) - cs->srcbuf_len;
970d5acd74SJohn Marino memcpy(cs->srcbuf + cs->srcbuf_len, s, n);
980d5acd74SJohn Marino
990d5acd74SJohn Marino /* Convert as few characters to the dst buffer as possible. */
1000d5acd74SJohn Marino for (i = 0; ; i++) {
101*0db70a6aSJohn Marino char *src, *dst;
1020d5acd74SJohn Marino size_t srcleft, dstleft, invlen;
1030d5acd74SJohn Marino int err;
1040d5acd74SJohn Marino
1050d5acd74SJohn Marino src = cs->srcbuf;
1060d5acd74SJohn Marino srcleft = cs->srcbuf_len + n;
1070d5acd74SJohn Marino dst = cs->dstbuf.bytes;
1080d5acd74SJohn Marino dstleft = i * sizeof(charXX_t);
1090d5acd74SJohn Marino assert(srcleft <= sizeof(cs->srcbuf) &&
1100d5acd74SJohn Marino dstleft <= sizeof(cs->dstbuf.bytes));
1110d5acd74SJohn Marino err = _citrus_iconv_convert(handle, &src, &srcleft,
1120d5acd74SJohn Marino &dst, &dstleft, 0, &invlen);
1130d5acd74SJohn Marino cs->dstbuf_len = (dst - cs->dstbuf.bytes) / sizeof(charXX_t);
1140d5acd74SJohn Marino
1150d5acd74SJohn Marino /* Got new character(s). Return the first. */
1160d5acd74SJohn Marino if (cs->dstbuf_len > 0) {
1170d5acd74SJohn Marino assert(src - cs->srcbuf > cs->srcbuf_len);
1180d5acd74SJohn Marino retval = src - cs->srcbuf - cs->srcbuf_len;
1190d5acd74SJohn Marino cs->srcbuf_len = 0;
1200d5acd74SJohn Marino goto return_char;
1210d5acd74SJohn Marino }
1220d5acd74SJohn Marino
1230d5acd74SJohn Marino /* Increase dst buffer size, to obtain the surrogate pair. */
1240d5acd74SJohn Marino if (err == E2BIG)
1250d5acd74SJohn Marino continue;
1260d5acd74SJohn Marino
1270d5acd74SJohn Marino /* Illegal sequence. */
1280d5acd74SJohn Marino if (invlen > 0) {
1290d5acd74SJohn Marino cs->srcbuf_len = 0;
1300d5acd74SJohn Marino errno = EILSEQ;
1310d5acd74SJohn Marino return ((size_t)-1);
1320d5acd74SJohn Marino }
1330d5acd74SJohn Marino
1340d5acd74SJohn Marino /* Save unprocessed remainder for the next invocation. */
1350d5acd74SJohn Marino memmove(cs->srcbuf, src, srcleft);
1360d5acd74SJohn Marino cs->srcbuf_len = srcleft;
1370d5acd74SJohn Marino return ((size_t)-2);
1380d5acd74SJohn Marino }
1390d5acd74SJohn Marino
1400d5acd74SJohn Marino return_char:
1410d5acd74SJohn Marino retchar = cs->dstbuf.widechar[0];
1420d5acd74SJohn Marino memmove(&cs->dstbuf.widechar[0], &cs->dstbuf.widechar[1],
1430d5acd74SJohn Marino --cs->dstbuf_len * sizeof(charXX_t));
1440d5acd74SJohn Marino if (pc != NULL)
1450d5acd74SJohn Marino *pc = retchar;
1460d5acd74SJohn Marino if (retchar == 0)
1470d5acd74SJohn Marino return (0);
1480d5acd74SJohn Marino return (retval);
1490d5acd74SJohn Marino }
1500d5acd74SJohn Marino
1510d5acd74SJohn Marino size_t
mbrtocXX(charXX_t * __restrict pc,const char * __restrict s,size_t n,mbstate_t * __restrict ps)1520d5acd74SJohn Marino mbrtocXX(charXX_t * __restrict pc, const char * __restrict s, size_t n,
1530d5acd74SJohn Marino mbstate_t * __restrict ps)
1540d5acd74SJohn Marino {
1550d5acd74SJohn Marino
1560d5acd74SJohn Marino return (mbrtocXX_l(pc, s, n, ps, __get_locale()));
1570d5acd74SJohn Marino }
158