vasnprintf.c revision 1.3.10.1 1 1.1 christos /* vsprintf with automatic memory allocation.
2 1.1 christos Copyright (C) 1999, 2002-2005 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This program is free software; you can redistribute it and/or modify
5 1.1 christos it under the terms of the GNU General Public License as published by
6 1.1 christos the Free Software Foundation; either version 2, or (at your option)
7 1.1 christos any later version.
8 1.1 christos
9 1.1 christos This program is distributed in the hope that it will be useful,
10 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
11 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 1.1 christos GNU General Public License for more details.
13 1.1 christos
14 1.1 christos You should have received a copy of the GNU General Public License along
15 1.1 christos with this program; if not, write to the Free Software Foundation,
16 1.1 christos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 1.3.10.1 snj #include <sys/cdefs.h>
18 1.3.10.1 snj __RCSID("$NetBSD: vasnprintf.c,v 1.3.10.1 2017/05/13 06:23:23 snj Exp $");
19 1.3.10.1 snj
20 1.1 christos
21 1.1 christos /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
22 1.1 christos This must come before <config.h> because <config.h> may include
23 1.1 christos <features.h>, and once <features.h> has been included, it's too late. */
24 1.1 christos #ifndef _GNU_SOURCE
25 1.1 christos # define _GNU_SOURCE 1
26 1.1 christos #endif
27 1.1 christos
28 1.1 christos #ifdef HAVE_CONFIG_H
29 1.1 christos # include <config.h>
30 1.1 christos #endif
31 1.2 christos #ifdef __SSP__
32 1.2 christos #undef HAVE_ALLOCA
33 1.2 christos #endif
34 1.2 christos #ifndef __NetBSD__
35 1.1 christos #ifndef IN_LIBINTL
36 1.1 christos # include <alloca.h>
37 1.1 christos #endif
38 1.2 christos #endif
39 1.1 christos
40 1.1 christos /* Specification. */
41 1.1 christos #if WIDE_CHAR_VERSION
42 1.1 christos # include "vasnwprintf.h"
43 1.1 christos #else
44 1.1 christos # include "vasnprintf.h"
45 1.1 christos #endif
46 1.1 christos
47 1.1 christos #include <stdio.h> /* snprintf(), sprintf() */
48 1.1 christos #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
49 1.1 christos #include <string.h> /* memcpy(), strlen() */
50 1.1 christos #include <errno.h> /* errno */
51 1.1 christos #include <limits.h> /* CHAR_BIT, INT_MAX */
52 1.1 christos #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
53 1.1 christos #if WIDE_CHAR_VERSION
54 1.1 christos # include "wprintf-parse.h"
55 1.1 christos #else
56 1.1 christos # include "printf-parse.h"
57 1.1 christos #endif
58 1.1 christos
59 1.1 christos /* Checked size_t computations. */
60 1.1 christos #include "xsize.h"
61 1.1 christos
62 1.1 christos /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
63 1.1 christos #ifndef EOVERFLOW
64 1.1 christos # define EOVERFLOW E2BIG
65 1.1 christos #endif
66 1.1 christos
67 1.1 christos #ifdef HAVE_WCHAR_T
68 1.1 christos # ifdef HAVE_WCSLEN
69 1.1 christos # define local_wcslen wcslen
70 1.1 christos # else
71 1.1 christos /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
72 1.1 christos a dependency towards this library, here is a local substitute.
73 1.1 christos Define this substitute only once, even if this file is included
74 1.1 christos twice in the same compilation unit. */
75 1.1 christos # ifndef local_wcslen_defined
76 1.1 christos # define local_wcslen_defined 1
77 1.1 christos static size_t
78 1.1 christos local_wcslen (const wchar_t *s)
79 1.1 christos {
80 1.1 christos const wchar_t *ptr;
81 1.1 christos
82 1.1 christos for (ptr = s; *ptr != (wchar_t) 0; ptr++)
83 1.1 christos ;
84 1.1 christos return ptr - s;
85 1.1 christos }
86 1.1 christos # endif
87 1.1 christos # endif
88 1.1 christos #endif
89 1.1 christos
90 1.1 christos #if WIDE_CHAR_VERSION
91 1.1 christos # define VASNPRINTF vasnwprintf
92 1.1 christos # define CHAR_T wchar_t
93 1.1 christos # define DIRECTIVE wchar_t_directive
94 1.1 christos # define DIRECTIVES wchar_t_directives
95 1.1 christos # define PRINTF_PARSE wprintf_parse
96 1.1 christos # define USE_SNPRINTF 1
97 1.1 christos # if HAVE_DECL__SNWPRINTF
98 1.1 christos /* On Windows, the function swprintf() has a different signature than
99 1.1 christos on Unix; we use the _snwprintf() function instead. */
100 1.1 christos # define SNPRINTF _snwprintf
101 1.1 christos # else
102 1.1 christos /* Unix. */
103 1.1 christos # define SNPRINTF swprintf
104 1.1 christos # endif
105 1.1 christos #else
106 1.1 christos # define VASNPRINTF vasnprintf
107 1.1 christos # define CHAR_T char
108 1.1 christos # define DIRECTIVE char_directive
109 1.1 christos # define DIRECTIVES char_directives
110 1.1 christos # define PRINTF_PARSE printf_parse
111 1.1 christos # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
112 1.1 christos # if HAVE_DECL__SNPRINTF
113 1.1 christos /* Windows. */
114 1.1 christos # define SNPRINTF _snprintf
115 1.1 christos # else
116 1.1 christos /* Unix. */
117 1.1 christos # define SNPRINTF snprintf
118 1.1 christos # endif
119 1.1 christos #endif
120 1.1 christos
121 1.1 christos CHAR_T *
122 1.1 christos VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
123 1.1 christos {
124 1.1 christos DIRECTIVES d;
125 1.1 christos arguments a;
126 1.1 christos
127 1.1 christos if (PRINTF_PARSE (format, &d, &a) < 0)
128 1.1 christos {
129 1.1 christos errno = EINVAL;
130 1.1 christos return NULL;
131 1.1 christos }
132 1.1 christos
133 1.1 christos #define CLEANUP() \
134 1.1 christos free (d.dir); \
135 1.1 christos if (a.arg) \
136 1.1 christos free (a.arg);
137 1.1 christos
138 1.1 christos if (printf_fetchargs (args, &a) < 0)
139 1.1 christos {
140 1.1 christos CLEANUP ();
141 1.1 christos errno = EINVAL;
142 1.1 christos return NULL;
143 1.1 christos }
144 1.1 christos
145 1.1 christos {
146 1.1 christos size_t buf_neededlength;
147 1.1 christos CHAR_T *buf;
148 1.1 christos CHAR_T *buf_malloced;
149 1.1 christos const CHAR_T *cp;
150 1.1 christos size_t i;
151 1.1 christos DIRECTIVE *dp;
152 1.1 christos /* Output string accumulator. */
153 1.1 christos CHAR_T *result;
154 1.1 christos size_t allocated;
155 1.1 christos size_t length;
156 1.1 christos
157 1.1 christos /* Allocate a small buffer that will hold a directive passed to
158 1.1 christos sprintf or snprintf. */
159 1.1 christos buf_neededlength =
160 1.1 christos xsum4 (7, d.max_width_length, d.max_precision_length, 6);
161 1.1 christos #if HAVE_ALLOCA
162 1.1 christos if (buf_neededlength < 4000 / sizeof (CHAR_T))
163 1.1 christos {
164 1.1 christos buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
165 1.1 christos buf_malloced = NULL;
166 1.1 christos }
167 1.1 christos else
168 1.1 christos #endif
169 1.1 christos {
170 1.1 christos size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
171 1.1 christos if (size_overflow_p (buf_memsize))
172 1.1 christos goto out_of_memory_1;
173 1.1 christos buf = (CHAR_T *) malloc (buf_memsize);
174 1.1 christos if (buf == NULL)
175 1.1 christos goto out_of_memory_1;
176 1.1 christos buf_malloced = buf;
177 1.1 christos }
178 1.1 christos
179 1.1 christos if (resultbuf != NULL)
180 1.1 christos {
181 1.1 christos result = resultbuf;
182 1.1 christos allocated = *lengthp;
183 1.1 christos }
184 1.1 christos else
185 1.1 christos {
186 1.1 christos result = NULL;
187 1.1 christos allocated = 0;
188 1.1 christos }
189 1.1 christos length = 0;
190 1.1 christos /* Invariants:
191 1.1 christos result is either == resultbuf or == NULL or malloc-allocated.
192 1.1 christos If length > 0, then result != NULL. */
193 1.1 christos
194 1.1 christos /* Ensures that allocated >= needed. Aborts through a jump to
195 1.1 christos out_of_memory if needed is SIZE_MAX or otherwise too big. */
196 1.1 christos #define ENSURE_ALLOCATION(needed) \
197 1.1 christos if ((needed) > allocated) \
198 1.1 christos { \
199 1.1 christos size_t memory_size; \
200 1.1 christos CHAR_T *memory; \
201 1.1 christos \
202 1.1 christos allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
203 1.1 christos if ((needed) > allocated) \
204 1.1 christos allocated = (needed); \
205 1.1 christos memory_size = xtimes (allocated, sizeof (CHAR_T)); \
206 1.1 christos if (size_overflow_p (memory_size)) \
207 1.1 christos goto out_of_memory; \
208 1.1 christos if (result == resultbuf || result == NULL) \
209 1.1 christos memory = (CHAR_T *) malloc (memory_size); \
210 1.1 christos else \
211 1.1 christos memory = (CHAR_T *) realloc (result, memory_size); \
212 1.1 christos if (memory == NULL) \
213 1.1 christos goto out_of_memory; \
214 1.1 christos if (result == resultbuf && length > 0) \
215 1.1 christos memcpy (memory, result, length * sizeof (CHAR_T)); \
216 1.1 christos result = memory; \
217 1.1 christos }
218 1.1 christos
219 1.1 christos for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
220 1.1 christos {
221 1.1 christos if (cp != dp->dir_start)
222 1.1 christos {
223 1.1 christos size_t n = dp->dir_start - cp;
224 1.1 christos size_t augmented_length = xsum (length, n);
225 1.1 christos
226 1.1 christos ENSURE_ALLOCATION (augmented_length);
227 1.1 christos memcpy (result + length, cp, n * sizeof (CHAR_T));
228 1.1 christos length = augmented_length;
229 1.1 christos }
230 1.1 christos if (i == d.count)
231 1.1 christos break;
232 1.1 christos
233 1.1 christos /* Execute a single directive. */
234 1.1 christos if (dp->conversion == '%')
235 1.1 christos {
236 1.1 christos size_t augmented_length;
237 1.1 christos
238 1.1 christos if (!(dp->arg_index == ARG_NONE))
239 1.1 christos abort ();
240 1.1 christos augmented_length = xsum (length, 1);
241 1.1 christos ENSURE_ALLOCATION (augmented_length);
242 1.1 christos result[length] = '%';
243 1.1 christos length = augmented_length;
244 1.1 christos }
245 1.1 christos else
246 1.1 christos {
247 1.1 christos if (!(dp->arg_index != ARG_NONE))
248 1.1 christos abort ();
249 1.1 christos
250 1.1 christos if (dp->conversion == 'n')
251 1.1 christos {
252 1.1 christos switch (a.arg[dp->arg_index].type)
253 1.1 christos {
254 1.1 christos case TYPE_COUNT_SCHAR_POINTER:
255 1.1 christos *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
256 1.1 christos break;
257 1.1 christos case TYPE_COUNT_SHORT_POINTER:
258 1.1 christos *a.arg[dp->arg_index].a.a_count_short_pointer = length;
259 1.1 christos break;
260 1.1 christos case TYPE_COUNT_INT_POINTER:
261 1.1 christos *a.arg[dp->arg_index].a.a_count_int_pointer = length;
262 1.1 christos break;
263 1.1 christos case TYPE_COUNT_LONGINT_POINTER:
264 1.1 christos *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
265 1.1 christos break;
266 1.1 christos #ifdef HAVE_LONG_LONG
267 1.1 christos case TYPE_COUNT_LONGLONGINT_POINTER:
268 1.1 christos *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
269 1.1 christos break;
270 1.1 christos #endif
271 1.1 christos default:
272 1.1 christos abort ();
273 1.1 christos }
274 1.1 christos }
275 1.1 christos else
276 1.1 christos {
277 1.1 christos arg_type type = a.arg[dp->arg_index].type;
278 1.1 christos CHAR_T *p;
279 1.1 christos unsigned int prefix_count;
280 1.1 christos int prefixes[2];
281 1.1 christos #if !USE_SNPRINTF
282 1.1 christos size_t tmp_length;
283 1.1 christos CHAR_T tmpbuf[700];
284 1.1 christos CHAR_T *tmp;
285 1.1 christos
286 1.1 christos /* Allocate a temporary buffer of sufficient size for calling
287 1.1 christos sprintf. */
288 1.1 christos {
289 1.1 christos size_t width;
290 1.1 christos size_t precision;
291 1.1 christos
292 1.1 christos width = 0;
293 1.1 christos if (dp->width_start != dp->width_end)
294 1.1 christos {
295 1.1 christos if (dp->width_arg_index != ARG_NONE)
296 1.1 christos {
297 1.1 christos int arg;
298 1.1 christos
299 1.1 christos if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
300 1.1 christos abort ();
301 1.1 christos arg = a.arg[dp->width_arg_index].a.a_int;
302 1.1 christos width = (arg < 0 ? (unsigned int) (-arg) : arg);
303 1.1 christos }
304 1.1 christos else
305 1.1 christos {
306 1.1 christos const CHAR_T *digitp = dp->width_start;
307 1.1 christos
308 1.1 christos do
309 1.1 christos width = xsum (xtimes (width, 10), *digitp++ - '0');
310 1.1 christos while (digitp != dp->width_end);
311 1.1 christos }
312 1.1 christos }
313 1.1 christos
314 1.1 christos precision = 6;
315 1.1 christos if (dp->precision_start != dp->precision_end)
316 1.1 christos {
317 1.1 christos if (dp->precision_arg_index != ARG_NONE)
318 1.1 christos {
319 1.1 christos int arg;
320 1.1 christos
321 1.1 christos if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
322 1.1 christos abort ();
323 1.1 christos arg = a.arg[dp->precision_arg_index].a.a_int;
324 1.1 christos precision = (arg < 0 ? 0 : arg);
325 1.1 christos }
326 1.1 christos else
327 1.1 christos {
328 1.1 christos const CHAR_T *digitp = dp->precision_start + 1;
329 1.1 christos
330 1.1 christos precision = 0;
331 1.1 christos while (digitp != dp->precision_end)
332 1.1 christos precision = xsum (xtimes (precision, 10), *digitp++ - '0');
333 1.1 christos }
334 1.1 christos }
335 1.1 christos
336 1.1 christos switch (dp->conversion)
337 1.1 christos {
338 1.1 christos
339 1.1 christos case 'd': case 'i': case 'u':
340 1.1 christos # ifdef HAVE_LONG_LONG
341 1.1 christos if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
342 1.1 christos tmp_length =
343 1.1 christos (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
344 1.1 christos * 0.30103 /* binary -> decimal */
345 1.1 christos * 2 /* estimate for FLAG_GROUP */
346 1.1 christos )
347 1.1 christos + 1 /* turn floor into ceil */
348 1.1 christos + 1; /* account for leading sign */
349 1.1 christos else
350 1.1 christos # endif
351 1.1 christos if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
352 1.1 christos tmp_length =
353 1.1 christos (unsigned int) (sizeof (unsigned long) * CHAR_BIT
354 1.1 christos * 0.30103 /* binary -> decimal */
355 1.1 christos * 2 /* estimate for FLAG_GROUP */
356 1.1 christos )
357 1.1 christos + 1 /* turn floor into ceil */
358 1.1 christos + 1; /* account for leading sign */
359 1.1 christos else
360 1.1 christos tmp_length =
361 1.1 christos (unsigned int) (sizeof (unsigned int) * CHAR_BIT
362 1.1 christos * 0.30103 /* binary -> decimal */
363 1.1 christos * 2 /* estimate for FLAG_GROUP */
364 1.1 christos )
365 1.1 christos + 1 /* turn floor into ceil */
366 1.1 christos + 1; /* account for leading sign */
367 1.1 christos break;
368 1.1 christos
369 1.1 christos case 'o':
370 1.1 christos # ifdef HAVE_LONG_LONG
371 1.1 christos if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
372 1.1 christos tmp_length =
373 1.1 christos (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
374 1.1 christos * 0.333334 /* binary -> octal */
375 1.1 christos )
376 1.1 christos + 1 /* turn floor into ceil */
377 1.1 christos + 1; /* account for leading sign */
378 1.1 christos else
379 1.1 christos # endif
380 1.1 christos if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
381 1.1 christos tmp_length =
382 1.1 christos (unsigned int) (sizeof (unsigned long) * CHAR_BIT
383 1.1 christos * 0.333334 /* binary -> octal */
384 1.1 christos )
385 1.1 christos + 1 /* turn floor into ceil */
386 1.1 christos + 1; /* account for leading sign */
387 1.1 christos else
388 1.1 christos tmp_length =
389 1.1 christos (unsigned int) (sizeof (unsigned int) * CHAR_BIT
390 1.1 christos * 0.333334 /* binary -> octal */
391 1.1 christos )
392 1.1 christos + 1 /* turn floor into ceil */
393 1.1 christos + 1; /* account for leading sign */
394 1.1 christos break;
395 1.1 christos
396 1.1 christos case 'x': case 'X':
397 1.1 christos # ifdef HAVE_LONG_LONG
398 1.1 christos if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
399 1.1 christos tmp_length =
400 1.1 christos (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
401 1.1 christos * 0.25 /* binary -> hexadecimal */
402 1.1 christos )
403 1.1 christos + 1 /* turn floor into ceil */
404 1.1 christos + 2; /* account for leading sign or alternate form */
405 1.1 christos else
406 1.1 christos # endif
407 1.1 christos if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
408 1.1 christos tmp_length =
409 1.1 christos (unsigned int) (sizeof (unsigned long) * CHAR_BIT
410 1.1 christos * 0.25 /* binary -> hexadecimal */
411 1.1 christos )
412 1.1 christos + 1 /* turn floor into ceil */
413 1.1 christos + 2; /* account for leading sign or alternate form */
414 1.1 christos else
415 1.1 christos tmp_length =
416 1.1 christos (unsigned int) (sizeof (unsigned int) * CHAR_BIT
417 1.1 christos * 0.25 /* binary -> hexadecimal */
418 1.1 christos )
419 1.1 christos + 1 /* turn floor into ceil */
420 1.1 christos + 2; /* account for leading sign or alternate form */
421 1.1 christos break;
422 1.1 christos
423 1.1 christos case 'f': case 'F':
424 1.1 christos # ifdef HAVE_LONG_DOUBLE
425 1.1 christos if (type == TYPE_LONGDOUBLE)
426 1.1 christos tmp_length =
427 1.1 christos (unsigned int) (LDBL_MAX_EXP
428 1.1 christos * 0.30103 /* binary -> decimal */
429 1.1 christos * 2 /* estimate for FLAG_GROUP */
430 1.1 christos )
431 1.1 christos + 1 /* turn floor into ceil */
432 1.1 christos + 10; /* sign, decimal point etc. */
433 1.1 christos else
434 1.1 christos # endif
435 1.1 christos tmp_length =
436 1.1 christos (unsigned int) (DBL_MAX_EXP
437 1.1 christos * 0.30103 /* binary -> decimal */
438 1.1 christos * 2 /* estimate for FLAG_GROUP */
439 1.1 christos )
440 1.1 christos + 1 /* turn floor into ceil */
441 1.1 christos + 10; /* sign, decimal point etc. */
442 1.1 christos tmp_length = xsum (tmp_length, precision);
443 1.1 christos break;
444 1.1 christos
445 1.1 christos case 'e': case 'E': case 'g': case 'G':
446 1.1 christos case 'a': case 'A':
447 1.1 christos tmp_length =
448 1.1 christos 12; /* sign, decimal point, exponent etc. */
449 1.1 christos tmp_length = xsum (tmp_length, precision);
450 1.1 christos break;
451 1.1 christos
452 1.1 christos case 'c':
453 1.1 christos # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
454 1.1 christos if (type == TYPE_WIDE_CHAR)
455 1.1 christos tmp_length = MB_CUR_MAX;
456 1.1 christos else
457 1.1 christos # endif
458 1.1 christos tmp_length = 1;
459 1.1 christos break;
460 1.1 christos
461 1.1 christos case 's':
462 1.1 christos # ifdef HAVE_WCHAR_T
463 1.1 christos if (type == TYPE_WIDE_STRING)
464 1.1 christos {
465 1.1 christos tmp_length =
466 1.1 christos local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
467 1.1 christos
468 1.1 christos # if !WIDE_CHAR_VERSION
469 1.1 christos tmp_length = xtimes (tmp_length, MB_CUR_MAX);
470 1.1 christos # endif
471 1.1 christos }
472 1.1 christos else
473 1.1 christos # endif
474 1.1 christos tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
475 1.1 christos break;
476 1.1 christos
477 1.1 christos case 'p':
478 1.1 christos tmp_length =
479 1.1 christos (unsigned int) (sizeof (void *) * CHAR_BIT
480 1.1 christos * 0.25 /* binary -> hexadecimal */
481 1.1 christos )
482 1.1 christos + 1 /* turn floor into ceil */
483 1.1 christos + 2; /* account for leading 0x */
484 1.1 christos break;
485 1.1 christos
486 1.1 christos default:
487 1.1 christos abort ();
488 1.1 christos }
489 1.1 christos
490 1.1 christos if (tmp_length < width)
491 1.1 christos tmp_length = width;
492 1.1 christos
493 1.1 christos tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
494 1.1 christos }
495 1.1 christos
496 1.1 christos if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
497 1.1 christos tmp = tmpbuf;
498 1.1 christos else
499 1.1 christos {
500 1.1 christos size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
501 1.1 christos
502 1.1 christos if (size_overflow_p (tmp_memsize))
503 1.1 christos /* Overflow, would lead to out of memory. */
504 1.1 christos goto out_of_memory;
505 1.1 christos tmp = (CHAR_T *) malloc (tmp_memsize);
506 1.1 christos if (tmp == NULL)
507 1.1 christos /* Out of memory. */
508 1.1 christos goto out_of_memory;
509 1.1 christos }
510 1.1 christos #endif
511 1.1 christos
512 1.1 christos /* Construct the format string for calling snprintf or
513 1.1 christos sprintf. */
514 1.1 christos p = buf;
515 1.1 christos *p++ = '%';
516 1.1 christos if (dp->flags & FLAG_GROUP)
517 1.1 christos *p++ = '\'';
518 1.1 christos if (dp->flags & FLAG_LEFT)
519 1.1 christos *p++ = '-';
520 1.1 christos if (dp->flags & FLAG_SHOWSIGN)
521 1.1 christos *p++ = '+';
522 1.1 christos if (dp->flags & FLAG_SPACE)
523 1.1 christos *p++ = ' ';
524 1.1 christos if (dp->flags & FLAG_ALT)
525 1.1 christos *p++ = '#';
526 1.1 christos if (dp->flags & FLAG_ZERO)
527 1.1 christos *p++ = '0';
528 1.1 christos if (dp->width_start != dp->width_end)
529 1.1 christos {
530 1.1 christos size_t n = dp->width_end - dp->width_start;
531 1.1 christos memcpy (p, dp->width_start, n * sizeof (CHAR_T));
532 1.1 christos p += n;
533 1.1 christos }
534 1.1 christos if (dp->precision_start != dp->precision_end)
535 1.1 christos {
536 1.1 christos size_t n = dp->precision_end - dp->precision_start;
537 1.1 christos memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
538 1.1 christos p += n;
539 1.1 christos }
540 1.1 christos
541 1.1 christos switch (type)
542 1.1 christos {
543 1.1 christos #ifdef HAVE_LONG_LONG
544 1.1 christos case TYPE_LONGLONGINT:
545 1.1 christos case TYPE_ULONGLONGINT:
546 1.1 christos *p++ = 'l';
547 1.1 christos /*FALLTHROUGH*/
548 1.1 christos #endif
549 1.1 christos case TYPE_LONGINT:
550 1.1 christos case TYPE_ULONGINT:
551 1.1 christos #ifdef HAVE_WINT_T
552 1.1 christos case TYPE_WIDE_CHAR:
553 1.1 christos #endif
554 1.1 christos #ifdef HAVE_WCHAR_T
555 1.1 christos case TYPE_WIDE_STRING:
556 1.1 christos #endif
557 1.1 christos *p++ = 'l';
558 1.1 christos break;
559 1.1 christos #ifdef HAVE_LONG_DOUBLE
560 1.1 christos case TYPE_LONGDOUBLE:
561 1.1 christos *p++ = 'L';
562 1.1 christos break;
563 1.1 christos #endif
564 1.1 christos default:
565 1.1 christos break;
566 1.1 christos }
567 1.1 christos *p = dp->conversion;
568 1.1 christos p[1] = '\0';
569 1.1 christos
570 1.1 christos /* Construct the arguments for calling snprintf or sprintf. */
571 1.1 christos prefix_count = 0;
572 1.1 christos if (dp->width_arg_index != ARG_NONE)
573 1.1 christos {
574 1.1 christos if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
575 1.1 christos abort ();
576 1.1 christos prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
577 1.1 christos }
578 1.1 christos if (dp->precision_arg_index != ARG_NONE)
579 1.1 christos {
580 1.1 christos if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
581 1.1 christos abort ();
582 1.1 christos prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
583 1.1 christos }
584 1.1 christos
585 1.1 christos #if USE_SNPRINTF
586 1.1 christos /* Prepare checking whether snprintf returns the count
587 1.1 christos via %n. */
588 1.1 christos ENSURE_ALLOCATION (xsum (length, 1));
589 1.1 christos result[length] = '\0';
590 1.1 christos #endif
591 1.1 christos
592 1.1 christos for (;;)
593 1.1 christos {
594 1.1 christos size_t maxlen;
595 1.1 christos int count;
596 1.1 christos
597 1.1 christos maxlen = allocated - length;
598 1.1 christos count = -1;
599 1.1 christos
600 1.1 christos #if USE_SNPRINTF
601 1.1 christos # define SNPRINTF_BUF(arg) \
602 1.1 christos switch (prefix_count) \
603 1.1 christos { \
604 1.1 christos case 0: \
605 1.3 christos count = SNPRINTF (result + length, maxlen, buf, \
606 1.3 christos arg); \
607 1.1 christos break; \
608 1.1 christos case 1: \
609 1.3 christos count = SNPRINTF (result + length, maxlen, buf, \
610 1.3 christos prefixes[0], arg); \
611 1.1 christos break; \
612 1.1 christos case 2: \
613 1.3 christos count = SNPRINTF (result + length, maxlen, buf, \
614 1.3 christos prefixes[0], prefixes[1], arg); \
615 1.1 christos break; \
616 1.1 christos default: \
617 1.1 christos abort (); \
618 1.1 christos }
619 1.1 christos #else
620 1.1 christos # define SNPRINTF_BUF(arg) \
621 1.1 christos switch (prefix_count) \
622 1.1 christos { \
623 1.1 christos case 0: \
624 1.1 christos count = sprintf (tmp, buf, arg); \
625 1.1 christos break; \
626 1.1 christos case 1: \
627 1.1 christos count = sprintf (tmp, buf, prefixes[0], arg); \
628 1.1 christos break; \
629 1.1 christos case 2: \
630 1.1 christos count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
631 1.1 christos arg); \
632 1.1 christos break; \
633 1.1 christos default: \
634 1.1 christos abort (); \
635 1.1 christos }
636 1.1 christos #endif
637 1.1 christos
638 1.1 christos switch (type)
639 1.1 christos {
640 1.1 christos case TYPE_SCHAR:
641 1.1 christos {
642 1.1 christos int arg = a.arg[dp->arg_index].a.a_schar;
643 1.1 christos SNPRINTF_BUF (arg);
644 1.1 christos }
645 1.1 christos break;
646 1.1 christos case TYPE_UCHAR:
647 1.1 christos {
648 1.1 christos unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
649 1.1 christos SNPRINTF_BUF (arg);
650 1.1 christos }
651 1.1 christos break;
652 1.1 christos case TYPE_SHORT:
653 1.1 christos {
654 1.1 christos int arg = a.arg[dp->arg_index].a.a_short;
655 1.1 christos SNPRINTF_BUF (arg);
656 1.1 christos }
657 1.1 christos break;
658 1.1 christos case TYPE_USHORT:
659 1.1 christos {
660 1.1 christos unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
661 1.1 christos SNPRINTF_BUF (arg);
662 1.1 christos }
663 1.1 christos break;
664 1.1 christos case TYPE_INT:
665 1.1 christos {
666 1.1 christos int arg = a.arg[dp->arg_index].a.a_int;
667 1.1 christos SNPRINTF_BUF (arg);
668 1.1 christos }
669 1.1 christos break;
670 1.1 christos case TYPE_UINT:
671 1.1 christos {
672 1.1 christos unsigned int arg = a.arg[dp->arg_index].a.a_uint;
673 1.1 christos SNPRINTF_BUF (arg);
674 1.1 christos }
675 1.1 christos break;
676 1.1 christos case TYPE_LONGINT:
677 1.1 christos {
678 1.1 christos long int arg = a.arg[dp->arg_index].a.a_longint;
679 1.1 christos SNPRINTF_BUF (arg);
680 1.1 christos }
681 1.1 christos break;
682 1.1 christos case TYPE_ULONGINT:
683 1.1 christos {
684 1.1 christos unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
685 1.1 christos SNPRINTF_BUF (arg);
686 1.1 christos }
687 1.1 christos break;
688 1.1 christos #ifdef HAVE_LONG_LONG
689 1.1 christos case TYPE_LONGLONGINT:
690 1.1 christos {
691 1.1 christos long long int arg = a.arg[dp->arg_index].a.a_longlongint;
692 1.1 christos SNPRINTF_BUF (arg);
693 1.1 christos }
694 1.1 christos break;
695 1.1 christos case TYPE_ULONGLONGINT:
696 1.1 christos {
697 1.1 christos unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
698 1.1 christos SNPRINTF_BUF (arg);
699 1.1 christos }
700 1.1 christos break;
701 1.1 christos #endif
702 1.1 christos case TYPE_DOUBLE:
703 1.1 christos {
704 1.1 christos double arg = a.arg[dp->arg_index].a.a_double;
705 1.1 christos SNPRINTF_BUF (arg);
706 1.1 christos }
707 1.1 christos break;
708 1.1 christos #ifdef HAVE_LONG_DOUBLE
709 1.1 christos case TYPE_LONGDOUBLE:
710 1.1 christos {
711 1.1 christos long double arg = a.arg[dp->arg_index].a.a_longdouble;
712 1.1 christos SNPRINTF_BUF (arg);
713 1.1 christos }
714 1.1 christos break;
715 1.1 christos #endif
716 1.1 christos case TYPE_CHAR:
717 1.1 christos {
718 1.1 christos int arg = a.arg[dp->arg_index].a.a_char;
719 1.1 christos SNPRINTF_BUF (arg);
720 1.1 christos }
721 1.1 christos break;
722 1.1 christos #ifdef HAVE_WINT_T
723 1.1 christos case TYPE_WIDE_CHAR:
724 1.1 christos {
725 1.1 christos wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
726 1.1 christos SNPRINTF_BUF (arg);
727 1.1 christos }
728 1.1 christos break;
729 1.1 christos #endif
730 1.1 christos case TYPE_STRING:
731 1.1 christos {
732 1.1 christos const char *arg = a.arg[dp->arg_index].a.a_string;
733 1.1 christos SNPRINTF_BUF (arg);
734 1.1 christos }
735 1.1 christos break;
736 1.1 christos #ifdef HAVE_WCHAR_T
737 1.1 christos case TYPE_WIDE_STRING:
738 1.1 christos {
739 1.1 christos const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
740 1.1 christos SNPRINTF_BUF (arg);
741 1.1 christos }
742 1.1 christos break;
743 1.1 christos #endif
744 1.1 christos case TYPE_POINTER:
745 1.1 christos {
746 1.1 christos void *arg = a.arg[dp->arg_index].a.a_pointer;
747 1.1 christos SNPRINTF_BUF (arg);
748 1.1 christos }
749 1.1 christos break;
750 1.1 christos default:
751 1.1 christos abort ();
752 1.1 christos }
753 1.1 christos
754 1.1 christos #if USE_SNPRINTF
755 1.1 christos /* Portability: Not all implementations of snprintf()
756 1.1 christos are ISO C 99 compliant. Determine the number of
757 1.1 christos bytes that snprintf() has produced or would have
758 1.1 christos produced. */
759 1.1 christos if (count >= 0)
760 1.1 christos {
761 1.1 christos /* Verify that snprintf() has NUL-terminated its
762 1.1 christos result. */
763 1.1 christos if (count < maxlen && result[length + count] != '\0')
764 1.1 christos abort ();
765 1.1 christos }
766 1.1 christos #endif
767 1.1 christos
768 1.1 christos /* Attempt to handle failure. */
769 1.1 christos if (count < 0)
770 1.1 christos {
771 1.1 christos if (!(result == resultbuf || result == NULL))
772 1.1 christos free (result);
773 1.1 christos if (buf_malloced != NULL)
774 1.1 christos free (buf_malloced);
775 1.1 christos CLEANUP ();
776 1.1 christos errno = EINVAL;
777 1.1 christos return NULL;
778 1.1 christos }
779 1.1 christos
780 1.1 christos #if !USE_SNPRINTF
781 1.1 christos if (count >= tmp_length)
782 1.1 christos /* tmp_length was incorrectly calculated - fix the
783 1.1 christos code above! */
784 1.1 christos abort ();
785 1.1 christos #endif
786 1.1 christos
787 1.1 christos /* Make room for the result. */
788 1.1 christos if (count >= maxlen)
789 1.1 christos {
790 1.1 christos /* Need at least count bytes. But allocate
791 1.1 christos proportionally, to avoid looping eternally if
792 1.1 christos snprintf() reports a too small count. */
793 1.1 christos size_t n =
794 1.1 christos xmax (xsum (length, count), xtimes (allocated, 2));
795 1.1 christos
796 1.1 christos ENSURE_ALLOCATION (n);
797 1.1 christos #if USE_SNPRINTF
798 1.1 christos continue;
799 1.1 christos #endif
800 1.1 christos }
801 1.1 christos
802 1.1 christos #if USE_SNPRINTF
803 1.1 christos /* The snprintf() result did fit. */
804 1.1 christos #else
805 1.1 christos /* Append the sprintf() result. */
806 1.1 christos memcpy (result + length, tmp, count * sizeof (CHAR_T));
807 1.1 christos if (tmp != tmpbuf)
808 1.1 christos free (tmp);
809 1.1 christos #endif
810 1.1 christos
811 1.1 christos length += count;
812 1.1 christos break;
813 1.1 christos }
814 1.1 christos }
815 1.1 christos }
816 1.1 christos }
817 1.1 christos
818 1.1 christos /* Add the final NUL. */
819 1.1 christos ENSURE_ALLOCATION (xsum (length, 1));
820 1.1 christos result[length] = '\0';
821 1.1 christos
822 1.1 christos if (result != resultbuf && length + 1 < allocated)
823 1.1 christos {
824 1.1 christos /* Shrink the allocated memory if possible. */
825 1.1 christos CHAR_T *memory;
826 1.1 christos
827 1.1 christos memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
828 1.1 christos if (memory != NULL)
829 1.1 christos result = memory;
830 1.1 christos }
831 1.1 christos
832 1.1 christos if (buf_malloced != NULL)
833 1.1 christos free (buf_malloced);
834 1.1 christos CLEANUP ();
835 1.1 christos *lengthp = length;
836 1.1 christos if (length > INT_MAX)
837 1.1 christos goto length_overflow;
838 1.1 christos return result;
839 1.1 christos
840 1.1 christos length_overflow:
841 1.1 christos /* We could produce such a big string, but its length doesn't fit into
842 1.1 christos an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
843 1.1 christos this case. */
844 1.1 christos if (result != resultbuf)
845 1.1 christos free (result);
846 1.1 christos errno = EOVERFLOW;
847 1.1 christos return NULL;
848 1.1 christos
849 1.1 christos out_of_memory:
850 1.1 christos if (!(result == resultbuf || result == NULL))
851 1.1 christos free (result);
852 1.1 christos if (buf_malloced != NULL)
853 1.1 christos free (buf_malloced);
854 1.1 christos out_of_memory_1:
855 1.1 christos CLEANUP ();
856 1.1 christos errno = ENOMEM;
857 1.1 christos return NULL;
858 1.1 christos }
859 1.1 christos }
860 1.1 christos
861 1.1 christos #undef SNPRINTF
862 1.1 christos #undef USE_SNPRINTF
863 1.1 christos #undef PRINTF_PARSE
864 1.1 christos #undef DIRECTIVES
865 1.1 christos #undef DIRECTIVE
866 1.1 christos #undef CHAR_T
867 1.1 christos #undef VASNPRINTF
868