xref: /freebsd/lib/libc/locale/cXXrtomb_iconv.h (revision 069ac184)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/queue.h>
30 
31 #include <assert.h>
32 #include <errno.h>
33 #include <langinfo.h>
34 #include <uchar.h>
35 
36 #include "../iconv/citrus_hash.h"
37 #include "../iconv/citrus_module.h"
38 #include "../iconv/citrus_iconv.h"
39 #include "mblocal.h"
40 
41 typedef struct {
42 	bool			initialized;
43 	struct _citrus_iconv	iconv;
44 	union {
45 		charXX_t	widechar[SRCBUF_LEN];
46 		char		bytes[sizeof(charXX_t) * SRCBUF_LEN];
47 	} srcbuf;
48 	size_t			srcbuf_len;
49 } _ConversionState;
50 _Static_assert(sizeof(_ConversionState) <= sizeof(mbstate_t),
51     "Size of _ConversionState must not exceed mbstate_t's size.");
52 
53 size_t
54 cXXrtomb_l(char * __restrict s, charXX_t c, mbstate_t * __restrict ps,
55     locale_t locale)
56 {
57 	_ConversionState *cs;
58 	struct _citrus_iconv *handle;
59 	char *src, *dst;
60 	size_t srcleft, dstleft, invlen;
61 	int err;
62 
63 	FIX_LOCALE(locale);
64 	if (ps == NULL)
65 		ps = &(XLOCALE_CTYPE(locale)->cXXrtomb);
66 	cs = (_ConversionState *)ps;
67 	handle = &cs->iconv;
68 
69 	/* Reinitialize mbstate_t. */
70 	if (s == NULL || !cs->initialized) {
71 		if (_citrus_iconv_open(&handle, UTF_XX_INTERNAL,
72 		    nl_langinfo_l(CODESET, locale)) != 0) {
73 			cs->initialized = false;
74 			errno = EINVAL;
75 			return (-1);
76 		}
77 		cs->srcbuf_len = 0;
78 		cs->initialized = true;
79 		if (s == NULL)
80 			return (1);
81 	}
82 
83 	assert(cs->srcbuf_len < sizeof(cs->srcbuf.widechar) / sizeof(charXX_t));
84 	cs->srcbuf.widechar[cs->srcbuf_len++] = c;
85 
86 	/* Perform conversion. */
87 	src = cs->srcbuf.bytes;
88 	srcleft = cs->srcbuf_len * sizeof(charXX_t);
89 	dst = s;
90 	dstleft = MB_CUR_MAX_L(locale);
91 	err = _citrus_iconv_convert(handle, &src, &srcleft, &dst, &dstleft,
92 	    _CITRUS_ICONV_F_HIDE_INVALID, &invlen);
93 
94 	/* Character is part of a surrogate pair. We need more input. */
95 	if (err == EINVAL)
96 		return (0);
97 	cs->srcbuf_len = 0;
98 
99 	/* Illegal sequence. */
100 	if (dst == s) {
101 		errno = EILSEQ;
102 		return ((size_t)-1);
103 	}
104 	return (dst - s);
105 }
106 
107 size_t
108 cXXrtomb(char * __restrict s, charXX_t c, mbstate_t * __restrict ps)
109 {
110 
111 	return (cXXrtomb_l(s, c, ps, __get_locale()));
112 }
113