xref: /freebsd/lib/libc/stdio/xprintf_str.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*-
2  * Copyright (c) 2005 Poul-Henning Kamp
3  * Copyright (c) 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Chris Torek.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
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