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