xref: /freebsd/lib/libc/stdio/xprintf_str.c (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2005 Poul-Henning Kamp
5  * Copyright (c) 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Chris Torek.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <namespace.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <limits.h>
41 #include <stdint.h>
42 #include <assert.h>
43 #include <wchar.h>
44 #include "printf.h"
45 
46 /*
47  * Convert a wide character string argument for the %ls format to a multibyte
48  * string representation. If not -1, prec specifies the maximum number of
49  * bytes to output, and also means that we can't assume that the wide char.
50  * string ends is null-terminated.
51  */
52 static char *
53 __wcsconv(wchar_t *wcsarg, int prec)
54 {
55 	static const mbstate_t initial;
56 	mbstate_t mbs;
57 	char buf[MB_LEN_MAX];
58 	wchar_t *p;
59 	char *convbuf;
60 	size_t clen, nbytes;
61 
62 	/* Allocate space for the maximum number of bytes we could output. */
63 	if (prec < 0) {
64 		p = wcsarg;
65 		mbs = initial;
66 		nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
67 		if (nbytes == (size_t)-1)
68 			return (NULL);
69 	} else {
70 		/*
71 		 * Optimisation: if the output precision is small enough,
72 		 * just allocate enough memory for the maximum instead of
73 		 * scanning the string.
74 		 */
75 		if (prec < 128)
76 			nbytes = prec;
77 		else {
78 			nbytes = 0;
79 			p = wcsarg;
80 			mbs = initial;
81 			for (;;) {
82 				clen = wcrtomb(buf, *p++, &mbs);
83 				if (clen == 0 || clen == (size_t)-1 ||
84 				    (int)(nbytes + clen) > prec)
85 					break;
86 				nbytes += clen;
87 			}
88 		}
89 	}
90 	if ((convbuf = malloc(nbytes + 1)) == NULL)
91 		return (NULL);
92 
93 	/* Fill the output buffer. */
94 	p = wcsarg;
95 	mbs = initial;
96 	if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p,
97 	    nbytes, &mbs)) == (size_t)-1) {
98 		free(convbuf);
99 		return (NULL);
100 	}
101 	convbuf[nbytes] = '\0';
102 	return (convbuf);
103 }
104 
105 
106 /* 's' ---------------------------------------------------------------*/
107 
108 int
109 __printf_arginfo_str(const struct printf_info *pi, size_t n, int *argt)
110 {
111 
112 	assert (n > 0);
113 	if (pi->is_long || pi->spec == 'C')
114 		argt[0] = PA_WSTRING;
115 	else
116 		argt[0] = PA_STRING;
117 	return (1);
118 }
119 
120 int
121 __printf_render_str(struct __printf_io *io, const struct printf_info *pi, const void *const *arg)
122 {
123 	const char *p;
124 	wchar_t *wcp;
125 	char *convbuf;
126 	int l;
127 
128 	if (pi->is_long || pi->spec == 'S') {
129 		wcp = *((wint_t **)arg[0]);
130 		if (wcp == NULL)
131 			return (__printf_out(io, pi, "(null)", 6));
132 		convbuf = __wcsconv(wcp, pi->prec);
133 		if (convbuf == NULL)
134 			return (-1);
135 		l = __printf_out(io, pi, convbuf, strlen(convbuf));
136 		free(convbuf);
137 		return (l);
138 	}
139 	p = *((char **)arg[0]);
140 	if (p == NULL)
141 		return (__printf_out(io, pi, "(null)", 6));
142 	l = strlen(p);
143 	if (pi->prec >= 0 && pi->prec < l)
144 		l = pi->prec;
145 	return (__printf_out(io, pi, p, l));
146 }
147 
148 /* 'c' ---------------------------------------------------------------*/
149 
150 int
151 __printf_arginfo_chr(const struct printf_info *pi, size_t n, int *argt)
152 {
153 
154 	assert (n > 0);
155 	if (pi->is_long || pi->spec == 'C')
156 		argt[0] = PA_WCHAR;
157 	else
158 		argt[0] = PA_INT;
159 	return (1);
160 }
161 
162 int
163 __printf_render_chr(struct __printf_io *io, const struct printf_info *pi, const void *const *arg)
164 {
165 	int i;
166 	wint_t ii;
167 	unsigned char c;
168 	static const mbstate_t initial;		/* XXX: this is bogus! */
169 	mbstate_t mbs;
170 	size_t mbseqlen;
171 	char buf[MB_CUR_MAX];
172 
173 	if (pi->is_long || pi->spec == 'C') {
174 		ii = *((wint_t *)arg[0]);
175 
176 		mbs = initial;
177 		mbseqlen = wcrtomb(buf, (wchar_t)ii, &mbs);
178 		if (mbseqlen == (size_t) -1)
179 			return (-1);
180 		return (__printf_out(io, pi, buf, mbseqlen));
181 	}
182 	i = *((int *)arg[0]);
183 	c = i;
184 	i = __printf_out(io, pi, &c, 1);
185 	__printf_flush(io);
186 	return (i);
187 }
188