c32rtomb.c revision 1.3 1 1.3 riastrad /* $NetBSD: c32rtomb.c,v 1.3 2024/08/17 21:24:53 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad /*
30 1.1 riastrad * c32rtomb(s, c32, ps)
31 1.1 riastrad *
32 1.1 riastrad * Encode the Unicode UTF-32 code unit c32, which must not be a
33 1.1 riastrad * surrogate code point, into the multibyte buffer s under the
34 1.1 riastrad * current locale, using multibyte encoding state ps. A UTF-32
35 1.1 riastrad * code unit is also a Unicode scalar value, which is any Unicode
36 1.1 riastrad * code point except a surrogate.
37 1.1 riastrad *
38 1.1 riastrad * Return the number of bytes stored on success, or (size_t)-1 on
39 1.1 riastrad * error with errno set to EILSEQ.
40 1.1 riastrad *
41 1.1 riastrad * At most MB_CUR_MAX bytes will be stored.
42 1.1 riastrad *
43 1.1 riastrad * References:
44 1.1 riastrad *
45 1.1 riastrad * The Unicode Standard, Version 15.0 -- Core Specification, The
46 1.1 riastrad * Unicode Consortium, Sec. 3.8 `Surrogates', p. 119.
47 1.1 riastrad * https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf
48 1.1 riastrad * https://web.archive.org/web/20240718101254/https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf
49 1.1 riastrad */
50 1.1 riastrad
51 1.1 riastrad #include <sys/cdefs.h>
52 1.3 riastrad __RCSID("$NetBSD: c32rtomb.c,v 1.3 2024/08/17 21:24:53 riastradh Exp $");
53 1.2 riastrad
54 1.2 riastrad #include "namespace.h"
55 1.1 riastrad
56 1.1 riastrad #include <sys/types.h> /* broken citrus_*.h */
57 1.1 riastrad #include <sys/queue.h> /* broken citrus_*.h */
58 1.1 riastrad
59 1.1 riastrad #include <assert.h>
60 1.1 riastrad #include <errno.h>
61 1.1 riastrad #include <langinfo.h>
62 1.1 riastrad #include <limits.h>
63 1.3 riastrad #include <locale.h>
64 1.1 riastrad #include <paths.h>
65 1.1 riastrad #include <stddef.h>
66 1.1 riastrad #include <stdlib.h>
67 1.1 riastrad #include <uchar.h>
68 1.1 riastrad #include <wchar.h>
69 1.1 riastrad
70 1.1 riastrad #include "citrus_types.h" /* broken citrus_iconv.h */
71 1.1 riastrad #include "citrus_module.h" /* broken citrus_iconv.h */
72 1.1 riastrad #include "citrus_hash.h" /* broken citrus_iconv.h */
73 1.1 riastrad #include "citrus_iconv.h"
74 1.3 riastrad #include "setlocale_local.h"
75 1.1 riastrad
76 1.2 riastrad #ifdef __weak_alias
77 1.2 riastrad __weak_alias(c32rtomb,_c32rtomb)
78 1.3 riastrad __weak_alias(c32rtomb_l,_c32rtomb_l)
79 1.2 riastrad #endif
80 1.2 riastrad
81 1.1 riastrad size_t
82 1.1 riastrad c32rtomb(char *restrict s, char32_t c32, mbstate_t *restrict ps)
83 1.1 riastrad {
84 1.3 riastrad
85 1.3 riastrad return c32rtomb_l(s, c32, ps, _current_locale());
86 1.3 riastrad }
87 1.3 riastrad
88 1.3 riastrad size_t
89 1.3 riastrad c32rtomb_l(char *restrict s, char32_t c32, mbstate_t *restrict ps,
90 1.3 riastrad locale_t loc)
91 1.3 riastrad {
92 1.1 riastrad char buf[MB_LEN_MAX];
93 1.1 riastrad struct _citrus_iconv *iconv = NULL;
94 1.1 riastrad char srcbuf[4];
95 1.1 riastrad const char *src;
96 1.1 riastrad char *dst;
97 1.1 riastrad size_t srcleft, dstleft, inval, len;
98 1.1 riastrad int error, errno_save;
99 1.1 riastrad
100 1.1 riastrad /*
101 1.1 riastrad * Save errno in case _citrus_iconv_* clobbers it.
102 1.1 riastrad */
103 1.1 riastrad errno_save = errno;
104 1.1 riastrad
105 1.1 riastrad /*
106 1.1 riastrad * `If s is a null pointer, the c32rtomb function is equivalent
107 1.1 riastrad * to the call
108 1.1 riastrad *
109 1.1 riastrad * c32rtomb(buf, L'\0', ps)
110 1.1 riastrad *
111 1.1 riastrad * where buf is an internal buffer.'
112 1.1 riastrad */
113 1.1 riastrad if (s == NULL) {
114 1.1 riastrad s = buf;
115 1.1 riastrad c32 = L'\0';
116 1.1 riastrad }
117 1.1 riastrad
118 1.1 riastrad /*
119 1.1 riastrad * Reject surrogates.
120 1.1 riastrad */
121 1.1 riastrad if (c32 >= 0xd800 && c32 <= 0xdfff) {
122 1.1 riastrad errno = EILSEQ;
123 1.1 riastrad len = (size_t)-1;
124 1.1 riastrad goto out;
125 1.1 riastrad }
126 1.1 riastrad
127 1.1 riastrad /*
128 1.1 riastrad * Open an iconv handle to convert UTF-32LE to locale-dependent
129 1.1 riastrad * multibyte output.
130 1.1 riastrad */
131 1.1 riastrad if ((error = _citrus_iconv_open(&iconv, _PATH_ICONV, "utf-32le",
132 1.3 riastrad nl_langinfo_l(CODESET, loc))) != 0) {
133 1.1 riastrad errno = EIO; /* XXX? */
134 1.1 riastrad len = (size_t)-1;
135 1.1 riastrad goto out;
136 1.1 riastrad }
137 1.1 riastrad
138 1.1 riastrad /*
139 1.1 riastrad * Convert from UTF-32LE in our buffer.
140 1.1 riastrad */
141 1.1 riastrad le32enc(srcbuf, c32);
142 1.1 riastrad src = srcbuf;
143 1.1 riastrad srcleft = sizeof(srcbuf);
144 1.1 riastrad dst = s;
145 1.1 riastrad dstleft = MB_CUR_MAX;
146 1.1 riastrad error = _citrus_iconv_convert(iconv,
147 1.1 riastrad &src, &srcleft,
148 1.1 riastrad &dst, &dstleft,
149 1.1 riastrad _CITRUS_ICONV_F_HIDE_INVALID, &inval);
150 1.1 riastrad if (error) { /* can't be incomplete, must be error */
151 1.1 riastrad errno = error;
152 1.1 riastrad len = (size_t)-1;
153 1.1 riastrad goto out;
154 1.1 riastrad }
155 1.1 riastrad _DIAGASSERT(srcleft == 0);
156 1.1 riastrad _DIAGASSERT(dstleft <= MB_CUR_MAX);
157 1.1 riastrad
158 1.1 riastrad /*
159 1.1 riastrad * If we didn't produce any output, that means the scalar value
160 1.1 riastrad * c32 can't be encoded in the current locale, so treat it as
161 1.1 riastrad * EILSEQ.
162 1.1 riastrad */
163 1.1 riastrad len = MB_CUR_MAX - dstleft;
164 1.1 riastrad if (len == 0) {
165 1.1 riastrad errno = EILSEQ;
166 1.1 riastrad len = (size_t)-1;
167 1.1 riastrad goto out;
168 1.1 riastrad }
169 1.1 riastrad
170 1.1 riastrad /*
171 1.1 riastrad * Make sure we preserve errno on success.
172 1.1 riastrad */
173 1.1 riastrad errno = errno_save;
174 1.1 riastrad
175 1.1 riastrad out: errno_save = errno;
176 1.1 riastrad _citrus_iconv_close(iconv);
177 1.1 riastrad errno = errno_save;
178 1.1 riastrad return len;
179 1.1 riastrad }
180