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