vsnprintf_ss.c revision 1.14 1 1.14 rillig /* $NetBSD: vsnprintf_ss.c,v 1.14 2022/04/19 20:32:16 rillig Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1990, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to Berkeley by
8 1.1 christos * Chris Torek.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. Neither the name of the University nor the names of its contributors
19 1.1 christos * may be used to endorse or promote products derived from this software
20 1.1 christos * without specific prior written permission.
21 1.1 christos *
22 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 christos * SUCH DAMAGE.
33 1.1 christos */
34 1.1 christos
35 1.1 christos #include <sys/cdefs.h>
36 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
37 1.1 christos #if 0
38 1.1 christos static char sccsid[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93";
39 1.1 christos #else
40 1.14 rillig __RCSID("$NetBSD: vsnprintf_ss.c,v 1.14 2022/04/19 20:32:16 rillig Exp $");
41 1.1 christos #endif
42 1.1 christos #endif /* LIBC_SCCS and not lint */
43 1.1 christos
44 1.1 christos #include "namespace.h"
45 1.1 christos
46 1.3 christos #include <sys/types.h>
47 1.3 christos #include <inttypes.h>
48 1.1 christos #include <assert.h>
49 1.3 christos #include <stdio.h>
50 1.1 christos #include <errno.h>
51 1.3 christos #include <stdarg.h>
52 1.3 christos #include <string.h>
53 1.1 christos #include "reentrant.h"
54 1.2 christos #include "extern.h"
55 1.1 christos #include "local.h"
56 1.1 christos
57 1.1 christos #ifdef __weak_alias
58 1.1 christos __weak_alias(vsnprintf_ss,_vsnprintf_ss)
59 1.1 christos #endif
60 1.1 christos
61 1.3 christos /*
62 1.3 christos * vsnprintf_ss: scaled down version of printf(3).
63 1.3 christos *
64 1.3 christos * this version based on vfprintf() from libc which was derived from
65 1.3 christos * software contributed to Berkeley by Chris Torek.
66 1.3 christos *
67 1.3 christos */
68 1.3 christos
69 1.3 christos /*
70 1.3 christos * macros for converting digits to letters and vice versa
71 1.3 christos */
72 1.3 christos #define to_digit(c) ((c) - '0')
73 1.3 christos #define is_digit(c) ((unsigned)to_digit(c) <= 9)
74 1.3 christos #define to_char(n) (char)((n) + '0')
75 1.3 christos
76 1.3 christos /*
77 1.3 christos * flags used during conversion.
78 1.3 christos */
79 1.3 christos #define ALT 0x001 /* alternate form */
80 1.3 christos #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
81 1.3 christos #define LADJUST 0x004 /* left adjustment */
82 1.3 christos #define LONGDBL 0x008 /* long double; unimplemented */
83 1.3 christos #define LONGINT 0x010 /* long integer */
84 1.3 christos #define QUADINT 0x020 /* quad integer */
85 1.3 christos #define SHORTINT 0x040 /* short integer */
86 1.3 christos #define MAXINT 0x080 /* intmax_t */
87 1.3 christos #define PTRINT 0x100 /* intptr_t */
88 1.3 christos #define SIZEINT 0x200 /* size_t */
89 1.3 christos #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
90 1.3 christos #define FPT 0x800 /* Floating point number */
91 1.3 christos
92 1.3 christos /*
93 1.3 christos * To extend shorts properly, we need both signed and unsigned
94 1.3 christos * argument extraction methods.
95 1.3 christos */
96 1.3 christos #define SARG() \
97 1.3 christos (flags&MAXINT ? va_arg(ap, intmax_t) : \
98 1.3 christos flags&PTRINT ? va_arg(ap, intptr_t) : \
99 1.3 christos flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
100 1.3 christos flags&QUADINT ? va_arg(ap, quad_t) : \
101 1.3 christos flags&LONGINT ? va_arg(ap, long) : \
102 1.3 christos flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
103 1.3 christos (long)va_arg(ap, int))
104 1.3 christos #define UARG() \
105 1.3 christos (flags&MAXINT ? va_arg(ap, uintmax_t) : \
106 1.3 christos flags&PTRINT ? va_arg(ap, uintptr_t) : \
107 1.3 christos flags&SIZEINT ? va_arg(ap, size_t) : \
108 1.3 christos flags&QUADINT ? va_arg(ap, u_quad_t) : \
109 1.3 christos flags&LONGINT ? va_arg(ap, u_long) : \
110 1.3 christos flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
111 1.3 christos (u_long)va_arg(ap, u_int))
112 1.3 christos
113 1.3 christos #define PUTCHAR(C) do { \
114 1.3 christos if (sbuf < tailp) \
115 1.3 christos *sbuf++ = (C); \
116 1.14 rillig } while (0)
117 1.3 christos
118 1.1 christos int
119 1.10 joerg vsnprintf_ss(char *sbuf, size_t slen, const char *fmt0, va_list ap)
120 1.1 christos {
121 1.3 christos const char *fmt; /* format string */
122 1.3 christos int ch; /* character from fmt */
123 1.3 christos int n; /* handy integer (short term usage) */
124 1.3 christos char *cp; /* handy char pointer (short term usage) */
125 1.3 christos int flags; /* flags as above */
126 1.3 christos int ret; /* return value accumulator */
127 1.3 christos int width; /* width from format (%8d), or 0 */
128 1.3 christos int prec; /* precision from format (%.3d), or -1 */
129 1.3 christos char sign; /* sign prefix (' ', '+', '-', or \0) */
130 1.3 christos
131 1.3 christos u_quad_t _uquad; /* integer arguments %[diouxX] */
132 1.3 christos enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
133 1.3 christos int dprec; /* a copy of prec if [diouxX], 0 otherwise */
134 1.3 christos int realsz; /* field size expanded by dprec */
135 1.3 christos int size; /* size of converted field or string */
136 1.3 christos const char *xdigs; /* digits for [xX] conversion */
137 1.3 christos char bf[128]; /* space for %c, %[diouxX] */
138 1.3 christos char *tailp; /* tail pointer for snprintf */
139 1.11 christos size_t len;
140 1.3 christos
141 1.3 christos static const char xdigs_lower[16] = "0123456789abcdef";
142 1.3 christos static const char xdigs_upper[16] = "0123456789ABCDEF";
143 1.1 christos
144 1.5 christos _DIAGASSERT(slen == 0 || sbuf != NULL);
145 1.6 christos _DIAGASSERT(fmt0 != NULL);
146 1.1 christos
147 1.13 christos if (slen > INT_MAX) {
148 1.13 christos errno = EOVERFLOW;
149 1.12 christos return -1;
150 1.4 christos }
151 1.4 christos
152 1.3 christos tailp = sbuf + slen;
153 1.3 christos
154 1.3 christos cp = NULL; /* XXX: shutup gcc */
155 1.3 christos size = 0; /* XXX: shutup gcc */
156 1.3 christos
157 1.3 christos fmt = fmt0;
158 1.3 christos ret = 0;
159 1.3 christos
160 1.3 christos xdigs = NULL; /* XXX: shut up gcc warning */
161 1.3 christos
162 1.3 christos /*
163 1.3 christos * Scan the format for conversions (`%' character).
164 1.3 christos */
165 1.3 christos for (;;) {
166 1.3 christos while (*fmt != '%' && *fmt) {
167 1.3 christos ret++;
168 1.9 christos PUTCHAR(*fmt);
169 1.9 christos fmt++;
170 1.3 christos }
171 1.3 christos if (*fmt == 0)
172 1.3 christos goto done;
173 1.3 christos
174 1.3 christos fmt++; /* skip over '%' */
175 1.3 christos
176 1.3 christos flags = 0;
177 1.3 christos dprec = 0;
178 1.3 christos width = 0;
179 1.3 christos prec = -1;
180 1.3 christos sign = '\0';
181 1.3 christos
182 1.3 christos rflag: ch = *fmt++;
183 1.3 christos reswitch: switch (ch) {
184 1.3 christos case ' ':
185 1.3 christos /*
186 1.3 christos * ``If the space and + flags both appear, the space
187 1.3 christos * flag will be ignored.''
188 1.3 christos * -- ANSI X3J11
189 1.3 christos */
190 1.3 christos if (!sign)
191 1.3 christos sign = ' ';
192 1.3 christos goto rflag;
193 1.3 christos case '#':
194 1.3 christos flags |= ALT;
195 1.3 christos goto rflag;
196 1.3 christos case '*':
197 1.3 christos /*
198 1.3 christos * ``A negative field width argument is taken as a
199 1.3 christos * - flag followed by a positive field width.''
200 1.3 christos * -- ANSI X3J11
201 1.3 christos * They don't exclude field widths read from args.
202 1.3 christos */
203 1.3 christos if ((width = va_arg(ap, int)) >= 0)
204 1.3 christos goto rflag;
205 1.3 christos width = -width;
206 1.3 christos /* FALLTHROUGH */
207 1.3 christos case '-':
208 1.3 christos flags |= LADJUST;
209 1.3 christos goto rflag;
210 1.3 christos case '+':
211 1.3 christos sign = '+';
212 1.3 christos goto rflag;
213 1.3 christos case '.':
214 1.3 christos if ((ch = *fmt++) == '*') {
215 1.3 christos n = va_arg(ap, int);
216 1.3 christos prec = n < 0 ? -1 : n;
217 1.3 christos goto rflag;
218 1.3 christos }
219 1.3 christos n = 0;
220 1.3 christos while (is_digit(ch)) {
221 1.3 christos n = 10 * n + to_digit(ch);
222 1.3 christos ch = *fmt++;
223 1.3 christos }
224 1.3 christos prec = n < 0 ? -1 : n;
225 1.3 christos goto reswitch;
226 1.3 christos case '0':
227 1.3 christos /*
228 1.3 christos * ``Note that 0 is taken as a flag, not as the
229 1.3 christos * beginning of a field width.''
230 1.3 christos * -- ANSI X3J11
231 1.3 christos */
232 1.3 christos flags |= ZEROPAD;
233 1.3 christos goto rflag;
234 1.3 christos case '1': case '2': case '3': case '4':
235 1.3 christos case '5': case '6': case '7': case '8': case '9':
236 1.3 christos n = 0;
237 1.3 christos do {
238 1.3 christos n = 10 * n + to_digit(ch);
239 1.3 christos ch = *fmt++;
240 1.3 christos } while (is_digit(ch));
241 1.3 christos width = n;
242 1.3 christos goto reswitch;
243 1.3 christos case 'h':
244 1.3 christos flags |= SHORTINT;
245 1.3 christos goto rflag;
246 1.3 christos case 'j':
247 1.3 christos flags |= MAXINT;
248 1.3 christos goto rflag;
249 1.3 christos case 'l':
250 1.3 christos if (*fmt == 'l') {
251 1.3 christos fmt++;
252 1.3 christos flags |= QUADINT;
253 1.3 christos } else {
254 1.3 christos flags |= LONGINT;
255 1.3 christos }
256 1.3 christos goto rflag;
257 1.3 christos case 'q':
258 1.3 christos flags |= QUADINT;
259 1.3 christos goto rflag;
260 1.3 christos case 't':
261 1.3 christos flags |= PTRINT;
262 1.3 christos goto rflag;
263 1.3 christos case 'z':
264 1.3 christos flags |= SIZEINT;
265 1.3 christos goto rflag;
266 1.3 christos case 'c':
267 1.3 christos *(cp = bf) = va_arg(ap, int);
268 1.3 christos size = 1;
269 1.3 christos sign = '\0';
270 1.3 christos break;
271 1.3 christos case 'D':
272 1.3 christos flags |= LONGINT;
273 1.3 christos /*FALLTHROUGH*/
274 1.3 christos case 'd':
275 1.3 christos case 'i':
276 1.3 christos _uquad = SARG();
277 1.3 christos if ((quad_t)_uquad < 0) {
278 1.3 christos _uquad = -_uquad;
279 1.3 christos sign = '-';
280 1.3 christos }
281 1.3 christos base = DEC;
282 1.3 christos goto number;
283 1.3 christos case 'n':
284 1.3 christos if (flags & MAXINT)
285 1.3 christos *va_arg(ap, intmax_t *) = ret;
286 1.3 christos else if (flags & PTRINT)
287 1.3 christos *va_arg(ap, intptr_t *) = ret;
288 1.3 christos else if (flags & SIZEINT)
289 1.3 christos *va_arg(ap, ssize_t *) = ret;
290 1.3 christos else if (flags & QUADINT)
291 1.3 christos *va_arg(ap, quad_t *) = ret;
292 1.3 christos else if (flags & LONGINT)
293 1.3 christos *va_arg(ap, long *) = ret;
294 1.3 christos else if (flags & SHORTINT)
295 1.3 christos *va_arg(ap, short *) = ret;
296 1.3 christos else
297 1.3 christos *va_arg(ap, int *) = ret;
298 1.3 christos continue; /* no output */
299 1.3 christos case 'O':
300 1.3 christos flags |= LONGINT;
301 1.3 christos /*FALLTHROUGH*/
302 1.3 christos case 'o':
303 1.3 christos _uquad = UARG();
304 1.3 christos base = OCT;
305 1.3 christos goto nosign;
306 1.3 christos case 'p':
307 1.3 christos /*
308 1.3 christos * ``The argument shall be a pointer to void. The
309 1.3 christos * value of the pointer is converted to a sequence
310 1.3 christos * of printable characters, in an implementation-
311 1.3 christos * defined manner.''
312 1.3 christos * -- ANSI X3J11
313 1.3 christos */
314 1.3 christos /* NOSTRICT */
315 1.3 christos _uquad = (u_long)va_arg(ap, void *);
316 1.3 christos base = HEX;
317 1.3 christos xdigs = xdigs_lower;
318 1.3 christos flags |= HEXPREFIX;
319 1.3 christos ch = 'x';
320 1.3 christos goto nosign;
321 1.3 christos case 's':
322 1.3 christos if ((cp = va_arg(ap, char *)) == NULL)
323 1.3 christos /*XXXUNCONST*/
324 1.3 christos cp = __UNCONST("(null)");
325 1.3 christos if (prec >= 0) {
326 1.3 christos /*
327 1.3 christos * can't use strlen; can only look for the
328 1.3 christos * NUL in the first `prec' characters, and
329 1.3 christos * strlen() will go further.
330 1.3 christos */
331 1.8 christos char *p = memchr(cp, 0, (size_t)prec);
332 1.3 christos
333 1.3 christos if (p != NULL) {
334 1.11 christos _DIAGASSERT(__type_fit(int, p - cp));
335 1.11 christos size = (int)(p - cp);
336 1.3 christos if (size > prec)
337 1.3 christos size = prec;
338 1.3 christos } else
339 1.3 christos size = prec;
340 1.11 christos } else {
341 1.11 christos len = strlen(cp);
342 1.11 christos _DIAGASSERT(__type_fit(int, len));
343 1.11 christos size = (int)len;
344 1.11 christos }
345 1.3 christos sign = '\0';
346 1.3 christos break;
347 1.3 christos case 'U':
348 1.3 christos flags |= LONGINT;
349 1.3 christos /*FALLTHROUGH*/
350 1.3 christos case 'u':
351 1.3 christos _uquad = UARG();
352 1.3 christos base = DEC;
353 1.3 christos goto nosign;
354 1.3 christos case 'X':
355 1.3 christos xdigs = xdigs_upper;
356 1.3 christos goto hex;
357 1.3 christos case 'x':
358 1.3 christos xdigs = xdigs_lower;
359 1.3 christos hex: _uquad = UARG();
360 1.3 christos base = HEX;
361 1.3 christos /* leading 0x/X only if non-zero */
362 1.3 christos if (flags & ALT && _uquad != 0)
363 1.3 christos flags |= HEXPREFIX;
364 1.3 christos
365 1.3 christos /* unsigned conversions */
366 1.3 christos nosign: sign = '\0';
367 1.3 christos /*
368 1.3 christos * ``... diouXx conversions ... if a precision is
369 1.3 christos * specified, the 0 flag will be ignored.''
370 1.3 christos * -- ANSI X3J11
371 1.3 christos */
372 1.3 christos number: if ((dprec = prec) >= 0)
373 1.3 christos flags &= ~ZEROPAD;
374 1.3 christos
375 1.3 christos /*
376 1.3 christos * ``The result of converting a zero value with an
377 1.3 christos * explicit precision of zero is no characters.''
378 1.3 christos * -- ANSI X3J11
379 1.3 christos */
380 1.3 christos cp = bf + sizeof(bf);
381 1.3 christos if (_uquad != 0 || prec != 0) {
382 1.3 christos /*
383 1.3 christos * Unsigned mod is hard, and unsigned mod
384 1.3 christos * by a constant is easier than that by
385 1.3 christos * a variable; hence this switch.
386 1.3 christos */
387 1.3 christos switch (base) {
388 1.3 christos case OCT:
389 1.3 christos do {
390 1.3 christos *--cp = to_char(_uquad & 7);
391 1.3 christos _uquad >>= 3;
392 1.3 christos } while (_uquad);
393 1.3 christos /* handle octal leading 0 */
394 1.3 christos if (flags & ALT && *cp != '0')
395 1.3 christos *--cp = '0';
396 1.3 christos break;
397 1.3 christos
398 1.3 christos case DEC:
399 1.3 christos /* many numbers are 1 digit */
400 1.3 christos while (_uquad >= 10) {
401 1.3 christos *--cp = to_char(_uquad % 10);
402 1.3 christos _uquad /= 10;
403 1.3 christos }
404 1.3 christos *--cp = to_char(_uquad);
405 1.3 christos break;
406 1.3 christos
407 1.3 christos case HEX:
408 1.3 christos do {
409 1.8 christos *--cp = xdigs[(size_t)_uquad & 15];
410 1.3 christos _uquad >>= 4;
411 1.3 christos } while (_uquad);
412 1.3 christos break;
413 1.3 christos
414 1.3 christos default:
415 1.3 christos /*XXXUNCONST*/
416 1.3 christos cp = __UNCONST("bug bad base");
417 1.11 christos len = strlen(cp);
418 1.11 christos _DIAGASSERT(__type_fit(int, len));
419 1.11 christos size = (int)len;
420 1.3 christos goto skipsize;
421 1.3 christos }
422 1.3 christos }
423 1.11 christos _DIAGASSERT(__type_fit(int, bf + sizeof(bf) - cp));
424 1.11 christos size = (int)(bf + sizeof(bf) - cp);
425 1.3 christos skipsize:
426 1.3 christos break;
427 1.3 christos default: /* "%?" prints ?, unless ? is NUL */
428 1.3 christos if (ch == '\0')
429 1.3 christos goto done;
430 1.3 christos /* pretend it was %c with argument ch */
431 1.3 christos cp = bf;
432 1.3 christos *cp = ch;
433 1.3 christos size = 1;
434 1.3 christos sign = '\0';
435 1.3 christos break;
436 1.3 christos }
437 1.3 christos
438 1.3 christos /*
439 1.3 christos * All reasonable formats wind up here. At this point, `cp'
440 1.3 christos * points to a string which (if not flags&LADJUST) should be
441 1.3 christos * padded out to `width' places. If flags&ZEROPAD, it should
442 1.3 christos * first be prefixed by any sign or other prefix; otherwise,
443 1.3 christos * it should be blank padded before the prefix is emitted.
444 1.3 christos * After any left-hand padding and prefixing, emit zeroes
445 1.3 christos * required by a decimal [diouxX] precision, then print the
446 1.3 christos * string proper, then emit zeroes required by any leftover
447 1.3 christos * floating precision; finally, if LADJUST, pad with blanks.
448 1.3 christos *
449 1.3 christos * Compute actual size, so we know how much to pad.
450 1.3 christos * size excludes decimal prec; realsz includes it.
451 1.3 christos */
452 1.3 christos realsz = dprec > size ? dprec : size;
453 1.3 christos if (sign)
454 1.3 christos realsz++;
455 1.3 christos else if (flags & HEXPREFIX)
456 1.3 christos realsz+= 2;
457 1.3 christos
458 1.3 christos /* adjust ret */
459 1.3 christos ret += width > realsz ? width : realsz;
460 1.3 christos
461 1.3 christos /* right-adjusting blank padding */
462 1.3 christos if ((flags & (LADJUST|ZEROPAD)) == 0) {
463 1.3 christos n = width - realsz;
464 1.3 christos while (n-- > 0)
465 1.3 christos PUTCHAR(' ');
466 1.3 christos }
467 1.3 christos
468 1.3 christos /* prefix */
469 1.3 christos if (sign) {
470 1.3 christos PUTCHAR(sign);
471 1.3 christos } else if (flags & HEXPREFIX) {
472 1.3 christos PUTCHAR('0');
473 1.3 christos PUTCHAR(ch);
474 1.3 christos }
475 1.3 christos
476 1.3 christos /* right-adjusting zero padding */
477 1.3 christos if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
478 1.3 christos n = width - realsz;
479 1.3 christos while (n-- > 0)
480 1.3 christos PUTCHAR('0');
481 1.3 christos }
482 1.3 christos
483 1.3 christos /* leading zeroes from decimal precision */
484 1.3 christos n = dprec - size;
485 1.3 christos while (n-- > 0)
486 1.3 christos PUTCHAR('0');
487 1.3 christos
488 1.3 christos /* the string or number proper */
489 1.3 christos while (size--)
490 1.3 christos PUTCHAR(*cp++);
491 1.3 christos /* left-adjusting padding (always blank) */
492 1.3 christos if (flags & LADJUST) {
493 1.3 christos n = width - realsz;
494 1.3 christos while (n-- > 0)
495 1.3 christos PUTCHAR(' ');
496 1.3 christos }
497 1.1 christos }
498 1.1 christos
499 1.3 christos done:
500 1.3 christos if (sbuf == tailp)
501 1.3 christos sbuf[-1] = '\0';
502 1.3 christos else
503 1.3 christos *sbuf = '\0';
504 1.12 christos return ret;
505 1.3 christos /* NOTREACHED */
506 1.1 christos }
507