1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
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#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/queue.h>
33
34#include <assert.h>
35#include <errno.h>
36#include <langinfo.h>
37#include <limits.h>
38#include <string.h>
39#include <uchar.h>
40
41#include "../iconv/citrus_hash.h"
42#include "../iconv/citrus_module.h"
43#include "../iconv/citrus_iconv.h"
44#include "xlocale_private.h"
45
46typedef struct {
47	bool			initialized;
48	struct _citrus_iconv	iconv;
49	char			srcbuf[MB_LEN_MAX];
50	size_t			srcbuf_len;
51	union {
52		charXX_t	widechar[DSTBUF_LEN];
53		char		bytes[sizeof(charXX_t) * DSTBUF_LEN];
54	} dstbuf;
55	size_t			dstbuf_len;
56} _ConversionState;
57_Static_assert(sizeof(_ConversionState) <= sizeof(mbstate_t),
58    "Size of _ConversionState must not exceed mbstate_t's size.");
59
60size_t
61mbrtocXX_l(charXX_t * __restrict pc, const char * __restrict s, size_t n,
62    mbstate_t * __restrict ps, locale_t locale)
63{
64	_ConversionState *cs;
65	struct _citrus_iconv *handle;
66	size_t i, retval;
67	charXX_t retchar;
68
69	FIX_LOCALE(locale);
70	if (ps == NULL)
71		ps = &locale->mbrtocXX;
72	cs = (_ConversionState *)ps;
73	handle = &cs->iconv;
74
75	/* Reinitialize mbstate_t. */
76	if (s == NULL || !cs->initialized) {
77		if (_citrus_iconv_open(&handle,
78		    nl_langinfo_l(CODESET, locale), UTF_XX_INTERNAL) != 0) {
79			cs->initialized = false;
80			errno = EINVAL;
81			return (-1);
82		}
83		handle->cv_shared->ci_discard_ilseq = true;
84		handle->cv_shared->ci_hooks = NULL;
85		cs->srcbuf_len = cs->dstbuf_len = 0;
86		cs->initialized = true;
87		if (s == NULL)
88			return (0);
89	}
90
91	/* See if we still have characters left from the previous invocation. */
92	if (cs->dstbuf_len > 0) {
93		retval = (size_t)-3;
94		goto return_char;
95	}
96
97	/* Fill up the read buffer as far as possible. */
98	if (n > sizeof(cs->srcbuf) - cs->srcbuf_len)
99		n = sizeof(cs->srcbuf) - cs->srcbuf_len;
100	memcpy(cs->srcbuf + cs->srcbuf_len, s, n);
101
102	/* Convert as few characters to the dst buffer as possible. */
103	for (i = 0; ; i++) {
104		char *src, *dst;
105		size_t srcleft, dstleft, invlen;
106		int err;
107
108		src = cs->srcbuf;
109		srcleft = cs->srcbuf_len + n;
110		dst = cs->dstbuf.bytes;
111		dstleft = i * sizeof(charXX_t);
112		assert(srcleft <= sizeof(cs->srcbuf) &&
113		    dstleft <= sizeof(cs->dstbuf.bytes));
114		err = _citrus_iconv_convert(handle, &src, &srcleft,
115		    &dst, &dstleft, 0, &invlen);
116		cs->dstbuf_len = (dst - cs->dstbuf.bytes) / sizeof(charXX_t);
117
118		/* Got new character(s). Return the first. */
119		if (cs->dstbuf_len > 0) {
120			assert(src - cs->srcbuf > cs->srcbuf_len);
121			retval = src - cs->srcbuf - cs->srcbuf_len;
122			cs->srcbuf_len = 0;
123			goto return_char;
124		}
125
126		/* Increase dst buffer size, to obtain the surrogate pair. */
127		if (err == E2BIG)
128			continue;
129
130		/* Illegal sequence. */
131		if (invlen > 0) {
132			cs->srcbuf_len = 0;
133			errno = EILSEQ;
134			return ((size_t)-1);
135		}
136
137		/* Save unprocessed remainder for the next invocation. */
138		memmove(cs->srcbuf, src, srcleft);
139		cs->srcbuf_len = srcleft;
140		return ((size_t)-2);
141	}
142
143return_char:
144	retchar = cs->dstbuf.widechar[0];
145	memmove(&cs->dstbuf.widechar[0], &cs->dstbuf.widechar[1],
146	    --cs->dstbuf_len * sizeof(charXX_t));
147	if (pc != NULL)
148		*pc = retchar;
149	if (retchar == 0)
150		return (0);
151	return (retval);
152}
153
154size_t
155mbrtocXX(charXX_t * __restrict pc, const char * __restrict s, size_t n,
156    mbstate_t * __restrict ps)
157{
158
159	return (mbrtocXX_l(pc, s, n, ps, __get_locale()));
160}
161