Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) 1999 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | * |
24 | | * Purpose: |
25 | | * A merge of Bjorn Reese's format() function and Daniel's dsprintf() |
26 | | * 1.0. A full blooded printf() clone with full support for <num>$ |
27 | | * everywhere (parameters, widths and precisions) including variabled |
28 | | * sized parameters (like doubles, long longs, long doubles and even |
29 | | * void * in 64-bit architectures). |
30 | | * |
31 | | * Current restrictions: |
32 | | * - Max 128 parameters |
33 | | * - No 'long double' support. |
34 | | * |
35 | | * If you ever want truly portable and good *printf() clones, the project that |
36 | | * took on from here is named 'Trio' and you find more details on the trio web |
37 | | * page at https://daniel.haxx.se/projects/trio/ |
38 | | */ |
39 | | |
40 | | #include "curl_setup.h" |
41 | | #include "dynbuf.h" |
42 | | #include <curl/mprintf.h> |
43 | | |
44 | | #include "curl_memory.h" |
45 | | /* The last #include file should be: */ |
46 | | #include "memdebug.h" |
47 | | |
48 | | /* |
49 | | * If SIZEOF_SIZE_T has not been defined, default to the size of long. |
50 | | */ |
51 | | |
52 | | #ifdef HAVE_LONGLONG |
53 | 328M | # define LONG_LONG_TYPE long long |
54 | | # define HAVE_LONG_LONG_TYPE |
55 | | #else |
56 | | # if defined(_MSC_VER) && (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64) |
57 | | # define LONG_LONG_TYPE __int64 |
58 | | # define HAVE_LONG_LONG_TYPE |
59 | | # else |
60 | | # undef LONG_LONG_TYPE |
61 | | # undef HAVE_LONG_LONG_TYPE |
62 | | # endif |
63 | | #endif |
64 | | |
65 | | /* |
66 | | * Non-ANSI integer extensions |
67 | | */ |
68 | | |
69 | | #if (defined(__BORLANDC__) && (__BORLANDC__ >= 0x520)) || \ |
70 | | (defined(__POCC__) && defined(_MSC_VER)) || \ |
71 | | (defined(_WIN32_WCE)) || \ |
72 | | (defined(__MINGW32__)) || \ |
73 | | (defined(_MSC_VER) && (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64)) |
74 | | # define MP_HAVE_INT_EXTENSIONS |
75 | | #endif |
76 | | |
77 | | /* |
78 | | * Max integer data types that mprintf.c is capable |
79 | | */ |
80 | | |
81 | | #ifdef HAVE_LONG_LONG_TYPE |
82 | 328M | # define mp_intmax_t LONG_LONG_TYPE |
83 | 328M | # define mp_uintmax_t unsigned LONG_LONG_TYPE |
84 | | #else |
85 | | # define mp_intmax_t long |
86 | | # define mp_uintmax_t unsigned long |
87 | | #endif |
88 | | |
89 | | #define BUFFSIZE 326 /* buffer for long-to-str and float-to-str calcs, should |
90 | | fit negative DBL_MAX (317 letters) */ |
91 | 1.48G | #define MAX_PARAMETERS 128 /* lame static limit */ |
92 | | |
93 | | #ifdef __AMIGA__ |
94 | | # undef FORMAT_INT |
95 | | #endif |
96 | | |
97 | | /* Lower-case digits. */ |
98 | | static const char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
99 | | |
100 | | /* Upper-case digits. */ |
101 | | static const char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
102 | | |
103 | | #define OUTCHAR(x) \ |
104 | 1.15G | do { \ |
105 | 1.10G | if(stream((unsigned char)(x), (FILE *)data) != -1) \ |
106 | 1.10G | done++; \ |
107 | 1.10G | else \ |
108 | 1.10G | return done; /* return immediately on failure */ \ |
109 | 1.10G | } while(0) |
110 | | |
111 | | /* Data type to read from the arglist */ |
112 | | typedef enum { |
113 | | FORMAT_UNKNOWN = 0, |
114 | | FORMAT_STRING, |
115 | | FORMAT_PTR, |
116 | | FORMAT_INT, |
117 | | FORMAT_INTPTR, |
118 | | FORMAT_LONG, |
119 | | FORMAT_LONGLONG, |
120 | | FORMAT_DOUBLE, |
121 | | FORMAT_LONGDOUBLE, |
122 | | FORMAT_WIDTH /* For internal use */ |
123 | | } FormatType; |
124 | | |
125 | | /* conversion and display flags */ |
126 | | enum { |
127 | | FLAGS_NEW = 0, |
128 | | FLAGS_SPACE = 1<<0, |
129 | | FLAGS_SHOWSIGN = 1<<1, |
130 | | FLAGS_LEFT = 1<<2, |
131 | | FLAGS_ALT = 1<<3, |
132 | | FLAGS_SHORT = 1<<4, |
133 | | FLAGS_LONG = 1<<5, |
134 | | FLAGS_LONGLONG = 1<<6, |
135 | | FLAGS_LONGDOUBLE = 1<<7, |
136 | | FLAGS_PAD_NIL = 1<<8, |
137 | | FLAGS_UNSIGNED = 1<<9, |
138 | | FLAGS_OCTAL = 1<<10, |
139 | | FLAGS_HEX = 1<<11, |
140 | | FLAGS_UPPER = 1<<12, |
141 | | FLAGS_WIDTH = 1<<13, /* '*' or '*<num>$' used */ |
142 | | FLAGS_WIDTHPARAM = 1<<14, /* width PARAMETER was specified */ |
143 | | FLAGS_PREC = 1<<15, /* precision was specified */ |
144 | | FLAGS_PRECPARAM = 1<<16, /* precision PARAMETER was specified */ |
145 | | FLAGS_CHAR = 1<<17, /* %c story */ |
146 | | FLAGS_FLOATE = 1<<18, /* %e or %E */ |
147 | | FLAGS_FLOATG = 1<<19 /* %g or %G */ |
148 | | }; |
149 | | |
150 | | struct va_stack { |
151 | | FormatType type; |
152 | | int flags; |
153 | | long width; /* width OR width parameter number */ |
154 | | long precision; /* precision OR precision parameter number */ |
155 | | union { |
156 | | char *str; |
157 | | void *ptr; |
158 | | union { |
159 | | mp_intmax_t as_signed; |
160 | | mp_uintmax_t as_unsigned; |
161 | | } num; |
162 | | double dnum; |
163 | | } data; |
164 | | }; |
165 | | |
166 | | struct nsprintf { |
167 | | char *buffer; |
168 | | size_t length; |
169 | | size_t max; |
170 | | }; |
171 | | |
172 | | struct asprintf { |
173 | | struct dynbuf *b; |
174 | | bool fail; /* if an alloc has failed and thus the output is not the complete |
175 | | data */ |
176 | | }; |
177 | | |
178 | | static long dprintf_DollarString(char *input, char **end) |
179 | 396M | { |
180 | 396M | int number = 0; |
181 | 886M | while(ISDIGIT(*input)) { |
182 | 489M | if(number < MAX_PARAMETERS) { |
183 | 489M | number *= 10; |
184 | 489M | number += *input - '0'; |
185 | 489M | } |
186 | 489M | input++; |
187 | 489M | } |
188 | 396M | if(number <= MAX_PARAMETERS && ('$' == *input)) { |
189 | 0 | *end = ++input; |
190 | 0 | return number; |
191 | 0 | } |
192 | 396M | return 0; |
193 | 396M | } |
194 | | |
195 | | static bool dprintf_IsQualifierNoDollar(const char *fmt) |
196 | 321M | { |
197 | | #if defined(MP_HAVE_INT_EXTENSIONS) |
198 | | if(!strncmp(fmt, "I32", 3) || !strncmp(fmt, "I64", 3)) { |
199 | | return TRUE; |
200 | | } |
201 | | #endif |
202 | | |
203 | 321M | switch(*fmt) { |
204 | 2.98k | case '-': case '+': case ' ': case '#': case '.': |
205 | 122M | case '0': case '1': case '2': case '3': case '4': |
206 | 122M | case '5': case '6': case '7': case '8': case '9': |
207 | 122M | case 'h': case 'l': case 'L': case 'z': case 'q': |
208 | 122M | case '*': case 'O': |
209 | | #if defined(MP_HAVE_INT_EXTENSIONS) |
210 | | case 'I': |
211 | | #endif |
212 | 122M | return TRUE; |
213 | | |
214 | 198M | default: |
215 | 198M | return FALSE; |
216 | 321M | } |
217 | 321M | } |
218 | | |
219 | | /****************************************************************** |
220 | | * |
221 | | * Pass 1: |
222 | | * Create an index with the type of each parameter entry and its |
223 | | * value (may vary in size) |
224 | | * |
225 | | * Returns zero on success. |
226 | | * |
227 | | ******************************************************************/ |
228 | | |
229 | | static int dprintf_Pass1(const char *format, struct va_stack *vto, |
230 | | char **endpos, va_list arglist) |
231 | 136M | { |
232 | 136M | char *fmt = (char *)format; |
233 | 136M | int param_num = 0; |
234 | 136M | long this_param; |
235 | 136M | long width; |
236 | 136M | long precision; |
237 | 136M | int flags; |
238 | 136M | long max_param = 0; |
239 | 136M | long i; |
240 | | |
241 | 791M | while(*fmt) { |
242 | 654M | if(*fmt++ == '%') { |
243 | 320M | if(*fmt == '%') { |
244 | 122M | fmt++; |
245 | 122M | continue; /* while */ |
246 | 122M | } |
247 | | |
248 | 198M | flags = FLAGS_NEW; |
249 | | |
250 | | /* Handle the positional case (N$) */ |
251 | | |
252 | 198M | param_num++; |
253 | | |
254 | 198M | this_param = dprintf_DollarString(fmt, &fmt); |
255 | 198M | if(0 == this_param) |
256 | | /* we got no positional, get the next counter */ |
257 | 198M | this_param = param_num; |
258 | | |
259 | 198M | if(this_param > max_param) |
260 | 198M | max_param = this_param; |
261 | | |
262 | | /* |
263 | | * The parameter with number 'i' should be used. Next, we need |
264 | | * to get SIZE and TYPE of the parameter. Add the information |
265 | | * to our array. |
266 | | */ |
267 | | |
268 | 198M | width = 0; |
269 | 198M | precision = 0; |
270 | | |
271 | | /* Handle the flags */ |
272 | | |
273 | 321M | while(dprintf_IsQualifierNoDollar(fmt)) { |
274 | | #if defined(MP_HAVE_INT_EXTENSIONS) |
275 | | if(!strncmp(fmt, "I32", 3)) { |
276 | | flags |= FLAGS_LONG; |
277 | | fmt += 3; |
278 | | } |
279 | | else if(!strncmp(fmt, "I64", 3)) { |
280 | | flags |= FLAGS_LONGLONG; |
281 | | fmt += 3; |
282 | | } |
283 | | else |
284 | | #endif |
285 | | |
286 | 122M | switch(*fmt++) { |
287 | 0 | case ' ': |
288 | 0 | flags |= FLAGS_SPACE; |
289 | 0 | break; |
290 | 0 | case '+': |
291 | 0 | flags |= FLAGS_SHOWSIGN; |
292 | 0 | break; |
293 | 0 | case '-': |
294 | 0 | flags |= FLAGS_LEFT; |
295 | 0 | flags &= ~FLAGS_PAD_NIL; |
296 | 0 | break; |
297 | 0 | case '#': |
298 | 0 | flags |= FLAGS_ALT; |
299 | 0 | break; |
300 | 2.98k | case '.': |
301 | 2.98k | if('*' == *fmt) { |
302 | | /* The precision is picked from a specified parameter */ |
303 | | |
304 | 2.98k | flags |= FLAGS_PRECPARAM; |
305 | 2.98k | fmt++; |
306 | 2.98k | param_num++; |
307 | | |
308 | 2.98k | i = dprintf_DollarString(fmt, &fmt); |
309 | 2.98k | if(i) |
310 | 0 | precision = i; |
311 | 2.98k | else |
312 | 2.98k | precision = param_num; |
313 | | |
314 | 2.98k | if(precision > max_param) |
315 | 2.98k | max_param = precision; |
316 | 2.98k | } |
317 | 0 | else { |
318 | 0 | flags |= FLAGS_PREC; |
319 | 0 | precision = strtol(fmt, &fmt, 10); |
320 | 0 | } |
321 | 2.98k | break; |
322 | 0 | case 'h': |
323 | 0 | flags |= FLAGS_SHORT; |
324 | 0 | break; |
325 | | #if defined(MP_HAVE_INT_EXTENSIONS) |
326 | | case 'I': |
327 | | #if (SIZEOF_CURL_OFF_T > SIZEOF_LONG) |
328 | | flags |= FLAGS_LONGLONG; |
329 | | #else |
330 | | flags |= FLAGS_LONG; |
331 | | #endif |
332 | | break; |
333 | | #endif |
334 | 311k | case 'l': |
335 | 311k | if(flags & FLAGS_LONG) |
336 | 0 | flags |= FLAGS_LONGLONG; |
337 | 311k | else |
338 | 311k | flags |= FLAGS_LONG; |
339 | 311k | break; |
340 | 0 | case 'L': |
341 | 0 | flags |= FLAGS_LONGDOUBLE; |
342 | 0 | break; |
343 | 0 | case 'q': |
344 | 0 | flags |= FLAGS_LONGLONG; |
345 | 0 | break; |
346 | 128 | case 'z': |
347 | | /* the code below generates a warning if -Wunreachable-code is |
348 | | used */ |
349 | | #if (SIZEOF_SIZE_T > SIZEOF_LONG) |
350 | | flags |= FLAGS_LONGLONG; |
351 | | #else |
352 | 128 | flags |= FLAGS_LONG; |
353 | 128 | #endif |
354 | 128 | break; |
355 | 0 | case 'O': |
356 | | #if (SIZEOF_CURL_OFF_T > SIZEOF_LONG) |
357 | | flags |= FLAGS_LONGLONG; |
358 | | #else |
359 | 0 | flags |= FLAGS_LONG; |
360 | 0 | #endif |
361 | 0 | break; |
362 | 122M | case '0': |
363 | 122M | if(!(flags & FLAGS_LEFT)) |
364 | 122M | flags |= FLAGS_PAD_NIL; |
365 | | /* FALLTHROUGH */ |
366 | 122M | case '1': case '2': case '3': case '4': |
367 | 122M | case '5': case '6': case '7': case '8': case '9': |
368 | 122M | flags |= FLAGS_WIDTH; |
369 | 122M | width = strtol(fmt-1, &fmt, 10); |
370 | 122M | break; |
371 | 0 | case '*': /* Special case */ |
372 | 0 | flags |= FLAGS_WIDTHPARAM; |
373 | 0 | param_num++; |
374 | |
|
375 | 0 | i = dprintf_DollarString(fmt, &fmt); |
376 | 0 | if(i) |
377 | 0 | width = i; |
378 | 0 | else |
379 | 0 | width = param_num; |
380 | 0 | if(width > max_param) |
381 | 0 | max_param = width; |
382 | 0 | break; |
383 | 0 | case '\0': |
384 | 0 | fmt--; |
385 | 0 | default: |
386 | 0 | break; |
387 | 122M | } |
388 | 122M | } /* switch */ |
389 | | |
390 | | /* Handle the specifier */ |
391 | | |
392 | 198M | i = this_param - 1; |
393 | | |
394 | 198M | if((i < 0) || (i >= MAX_PARAMETERS)) |
395 | | /* out of allowed range */ |
396 | 0 | return 1; |
397 | | |
398 | 198M | switch (*fmt) { |
399 | 0 | case 'S': |
400 | 0 | flags |= FLAGS_ALT; |
401 | | /* FALLTHROUGH */ |
402 | 46.5M | case 's': |
403 | 46.5M | vto[i].type = FORMAT_STRING; |
404 | 46.5M | break; |
405 | 0 | case 'n': |
406 | 0 | vto[i].type = FORMAT_INTPTR; |
407 | 0 | break; |
408 | 0 | case 'p': |
409 | 0 | vto[i].type = FORMAT_PTR; |
410 | 0 | break; |
411 | 321k | case 'd': case 'i': |
412 | 321k | vto[i].type = FORMAT_INT; |
413 | 321k | break; |
414 | 541k | case 'u': |
415 | 541k | vto[i].type = FORMAT_INT; |
416 | 541k | flags |= FLAGS_UNSIGNED; |
417 | 541k | break; |
418 | 0 | case 'o': |
419 | 0 | vto[i].type = FORMAT_INT; |
420 | 0 | flags |= FLAGS_OCTAL; |
421 | 0 | break; |
422 | 122M | case 'x': |
423 | 122M | vto[i].type = FORMAT_INT; |
424 | 122M | flags |= FLAGS_HEX|FLAGS_UNSIGNED; |
425 | 122M | break; |
426 | 238k | case 'X': |
427 | 238k | vto[i].type = FORMAT_INT; |
428 | 238k | flags |= FLAGS_HEX|FLAGS_UPPER|FLAGS_UNSIGNED; |
429 | 238k | break; |
430 | 28.6M | case 'c': |
431 | 28.6M | vto[i].type = FORMAT_INT; |
432 | 28.6M | flags |= FLAGS_CHAR; |
433 | 28.6M | break; |
434 | 0 | case 'f': |
435 | 0 | vto[i].type = FORMAT_DOUBLE; |
436 | 0 | break; |
437 | 0 | case 'e': |
438 | 0 | vto[i].type = FORMAT_DOUBLE; |
439 | 0 | flags |= FLAGS_FLOATE; |
440 | 0 | break; |
441 | 0 | case 'E': |
442 | 0 | vto[i].type = FORMAT_DOUBLE; |
443 | 0 | flags |= FLAGS_FLOATE|FLAGS_UPPER; |
444 | 0 | break; |
445 | 0 | case 'g': |
446 | 0 | vto[i].type = FORMAT_DOUBLE; |
447 | 0 | flags |= FLAGS_FLOATG; |
448 | 0 | break; |
449 | 0 | case 'G': |
450 | 0 | vto[i].type = FORMAT_DOUBLE; |
451 | 0 | flags |= FLAGS_FLOATG|FLAGS_UPPER; |
452 | 0 | break; |
453 | 0 | default: |
454 | 0 | vto[i].type = FORMAT_UNKNOWN; |
455 | 0 | break; |
456 | 198M | } /* switch */ |
457 | | |
458 | 198M | vto[i].flags = flags; |
459 | 198M | vto[i].width = width; |
460 | 198M | vto[i].precision = precision; |
461 | | |
462 | 198M | if(flags & FLAGS_WIDTHPARAM) { |
463 | | /* we have the width specified from a parameter, so we make that |
464 | | parameter's info setup properly */ |
465 | 0 | long k = width - 1; |
466 | 0 | if((k < 0) || (k >= MAX_PARAMETERS)) |
467 | | /* out of allowed range */ |
468 | 0 | return 1; |
469 | 0 | vto[i].width = k; |
470 | 0 | vto[k].type = FORMAT_WIDTH; |
471 | 0 | vto[k].flags = FLAGS_NEW; |
472 | | /* can't use width or precision of width! */ |
473 | 0 | vto[k].width = 0; |
474 | 0 | vto[k].precision = 0; |
475 | 0 | } |
476 | 198M | if(flags & FLAGS_PRECPARAM) { |
477 | | /* we have the precision specified from a parameter, so we make that |
478 | | parameter's info setup properly */ |
479 | 2.98k | long k = precision - 1; |
480 | 2.98k | if((k < 0) || (k >= MAX_PARAMETERS)) |
481 | | /* out of allowed range */ |
482 | 0 | return 1; |
483 | 2.98k | vto[i].precision = k; |
484 | 2.98k | vto[k].type = FORMAT_WIDTH; |
485 | 2.98k | vto[k].flags = FLAGS_NEW; |
486 | | /* can't use width or precision of width! */ |
487 | 2.98k | vto[k].width = 0; |
488 | 2.98k | vto[k].precision = 0; |
489 | 2.98k | } |
490 | 198M | *endpos++ = fmt + ((*fmt == '\0') ? 0 : 1); /* end of this sequence */ |
491 | 198M | } |
492 | 654M | } |
493 | | |
494 | | /* Read the arg list parameters into our data list */ |
495 | 335M | for(i = 0; i<max_param; i++) { |
496 | | /* Width/precision arguments must be read before the main argument |
497 | | they are attached to */ |
498 | 198M | if(vto[i].flags & FLAGS_WIDTHPARAM) { |
499 | 0 | vto[vto[i].width].data.num.as_signed = |
500 | 0 | (mp_intmax_t)va_arg(arglist, int); |
501 | 0 | } |
502 | 198M | if(vto[i].flags & FLAGS_PRECPARAM) { |
503 | 2.98k | vto[vto[i].precision].data.num.as_signed = |
504 | 2.98k | (mp_intmax_t)va_arg(arglist, int); |
505 | 2.98k | } |
506 | | |
507 | 198M | switch(vto[i].type) { |
508 | 46.5M | case FORMAT_STRING: |
509 | 46.5M | vto[i].data.str = va_arg(arglist, char *); |
510 | 46.5M | break; |
511 | | |
512 | 0 | case FORMAT_INTPTR: |
513 | 0 | case FORMAT_UNKNOWN: |
514 | 0 | case FORMAT_PTR: |
515 | 0 | vto[i].data.ptr = va_arg(arglist, void *); |
516 | 0 | break; |
517 | | |
518 | 151M | case FORMAT_INT: |
519 | 151M | #ifdef HAVE_LONG_LONG_TYPE |
520 | 151M | if((vto[i].flags & FLAGS_LONGLONG) && (vto[i].flags & FLAGS_UNSIGNED)) |
521 | 0 | vto[i].data.num.as_unsigned = |
522 | 0 | (mp_uintmax_t)va_arg(arglist, mp_uintmax_t); |
523 | 151M | else if(vto[i].flags & FLAGS_LONGLONG) |
524 | 0 | vto[i].data.num.as_signed = |
525 | 0 | (mp_intmax_t)va_arg(arglist, mp_intmax_t); |
526 | 151M | else |
527 | 151M | #endif |
528 | 151M | { |
529 | 151M | if((vto[i].flags & FLAGS_LONG) && (vto[i].flags & FLAGS_UNSIGNED)) |
530 | 128 | vto[i].data.num.as_unsigned = |
531 | 128 | (mp_uintmax_t)va_arg(arglist, unsigned long); |
532 | 151M | else if(vto[i].flags & FLAGS_LONG) |
533 | 311k | vto[i].data.num.as_signed = |
534 | 311k | (mp_intmax_t)va_arg(arglist, long); |
535 | 151M | else if(vto[i].flags & FLAGS_UNSIGNED) |
536 | 123M | vto[i].data.num.as_unsigned = |
537 | 123M | (mp_uintmax_t)va_arg(arglist, unsigned int); |
538 | 28.6M | else |
539 | 28.6M | vto[i].data.num.as_signed = |
540 | 28.6M | (mp_intmax_t)va_arg(arglist, int); |
541 | 151M | } |
542 | 151M | break; |
543 | | |
544 | 0 | case FORMAT_DOUBLE: |
545 | 0 | vto[i].data.dnum = va_arg(arglist, double); |
546 | 0 | break; |
547 | | |
548 | 2.98k | case FORMAT_WIDTH: |
549 | | /* Argument has been read. Silently convert it into an integer |
550 | | * for later use |
551 | | */ |
552 | 2.98k | vto[i].type = FORMAT_INT; |
553 | 2.98k | break; |
554 | | |
555 | 0 | default: |
556 | 0 | break; |
557 | 198M | } |
558 | 198M | } |
559 | | |
560 | 136M | return 0; |
561 | | |
562 | 136M | } |
563 | | |
564 | | static int dprintf_formatf( |
565 | | void *data, /* untouched by format(), just sent to the stream() function in |
566 | | the second argument */ |
567 | | /* function pointer called for each output character */ |
568 | | int (*stream)(int, FILE *), |
569 | | const char *format, /* %-formatted string */ |
570 | | va_list ap_save) /* list of parameters */ |
571 | 136M | { |
572 | | /* Base-36 digits for numbers. */ |
573 | 136M | const char *digits = lower_digits; |
574 | | |
575 | | /* Pointer into the format string. */ |
576 | 136M | char *f; |
577 | | |
578 | | /* Number of characters written. */ |
579 | 136M | int done = 0; |
580 | | |
581 | 136M | long param; /* current parameter to read */ |
582 | 136M | long param_num = 0; /* parameter counter */ |
583 | | |
584 | 136M | struct va_stack vto[MAX_PARAMETERS]; |
585 | 136M | char *endpos[MAX_PARAMETERS]; |
586 | 136M | char **end; |
587 | 136M | char work[BUFFSIZE]; |
588 | 136M | struct va_stack *p; |
589 | | |
590 | | /* 'workend' points to the final buffer byte position, but with an extra |
591 | | byte as margin to avoid the (false?) warning Coverity gives us |
592 | | otherwise */ |
593 | 136M | char *workend = &work[sizeof(work) - 2]; |
594 | | |
595 | | /* Do the actual %-code parsing */ |
596 | 136M | if(dprintf_Pass1(format, vto, endpos, ap_save)) |
597 | 0 | return 0; |
598 | | |
599 | 136M | end = &endpos[0]; /* the initial end-position from the list dprintf_Pass1() |
600 | | created for us */ |
601 | | |
602 | 136M | f = (char *)format; |
603 | 465M | while(*f != '\0') { |
604 | | /* Format spec modifiers. */ |
605 | 328M | int is_alt; |
606 | | |
607 | | /* Width of a field. */ |
608 | 328M | long width; |
609 | | |
610 | | /* Precision of a field. */ |
611 | 328M | long prec; |
612 | | |
613 | | /* Decimal integer is negative. */ |
614 | 328M | int is_neg; |
615 | | |
616 | | /* Base of a number to be written. */ |
617 | 328M | unsigned long base; |
618 | | |
619 | | /* Integral values to be written. */ |
620 | 328M | mp_uintmax_t num; |
621 | | |
622 | | /* Used to convert negative in positive. */ |
623 | 328M | mp_intmax_t signed_num; |
624 | | |
625 | 328M | char *w; |
626 | | |
627 | 328M | if(*f != '%') { |
628 | | /* This isn't a format spec, so write everything out until the next one |
629 | | OR end of string is reached. */ |
630 | 135M | do { |
631 | 135M | OUTCHAR(*f); |
632 | 135M | } while(*++f && ('%' != *f)); |
633 | 7.47M | continue; |
634 | 7.47M | } |
635 | | |
636 | 320M | ++f; |
637 | | |
638 | | /* Check for "%%". Note that although the ANSI standard lists |
639 | | '%' as a conversion specifier, it says "The complete format |
640 | | specification shall be `%%'," so we can avoid all the width |
641 | | and precision processing. */ |
642 | 320M | if(*f == '%') { |
643 | 122M | ++f; |
644 | 122M | OUTCHAR('%'); |
645 | 122M | continue; |
646 | 122M | } |
647 | | |
648 | | /* If this is a positional parameter, the position must follow immediately |
649 | | after the %, thus create a %<num>$ sequence */ |
650 | 198M | param = dprintf_DollarString(f, &f); |
651 | | |
652 | 198M | if(!param) |
653 | 198M | param = param_num; |
654 | 0 | else |
655 | 0 | --param; |
656 | | |
657 | 198M | param_num++; /* increase this always to allow "%2$s %1$s %s" and then the |
658 | | third %s will pick the 3rd argument */ |
659 | | |
660 | 198M | p = &vto[param]; |
661 | | |
662 | | /* pick up the specified width */ |
663 | 198M | if(p->flags & FLAGS_WIDTHPARAM) { |
664 | 0 | width = (long)vto[p->width].data.num.as_signed; |
665 | 0 | param_num++; /* since the width is extracted from a parameter, we |
666 | | must skip that to get to the next one properly */ |
667 | 0 | if(width < 0) { |
668 | | /* "A negative field width is taken as a '-' flag followed by a |
669 | | positive field width." */ |
670 | 0 | width = -width; |
671 | 0 | p->flags |= FLAGS_LEFT; |
672 | 0 | p->flags &= ~FLAGS_PAD_NIL; |
673 | 0 | } |
674 | 0 | } |
675 | 198M | else |
676 | 198M | width = p->width; |
677 | | |
678 | | /* pick up the specified precision */ |
679 | 198M | if(p->flags & FLAGS_PRECPARAM) { |
680 | 2.98k | prec = (long)vto[p->precision].data.num.as_signed; |
681 | 2.98k | param_num++; /* since the precision is extracted from a parameter, we |
682 | | must skip that to get to the next one properly */ |
683 | 2.98k | if(prec < 0) |
684 | | /* "A negative precision is taken as if the precision were |
685 | | omitted." */ |
686 | 0 | prec = -1; |
687 | 2.98k | } |
688 | 198M | else if(p->flags & FLAGS_PREC) |
689 | 0 | prec = p->precision; |
690 | 198M | else |
691 | 198M | prec = -1; |
692 | | |
693 | 198M | is_alt = (p->flags & FLAGS_ALT) ? 1 : 0; |
694 | | |
695 | 198M | switch(p->type) { |
696 | 151M | case FORMAT_INT: |
697 | 151M | num = p->data.num.as_unsigned; |
698 | 151M | if(p->flags & FLAGS_CHAR) { |
699 | | /* Character. */ |
700 | 28.6M | if(!(p->flags & FLAGS_LEFT)) |
701 | 28.6M | while(--width > 0) |
702 | 0 | OUTCHAR(' '); |
703 | 28.6M | OUTCHAR((char) num); |
704 | 28.6M | if(p->flags & FLAGS_LEFT) |
705 | 0 | while(--width > 0) |
706 | 0 | OUTCHAR(' '); |
707 | 28.6M | break; |
708 | 28.6M | } |
709 | 123M | if(p->flags & FLAGS_OCTAL) { |
710 | | /* Octal unsigned integer. */ |
711 | 0 | base = 8; |
712 | 0 | goto unsigned_number; |
713 | 0 | } |
714 | 123M | else if(p->flags & FLAGS_HEX) { |
715 | | /* Hexadecimal unsigned integer. */ |
716 | | |
717 | 122M | digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits; |
718 | 122M | base = 16; |
719 | 122M | goto unsigned_number; |
720 | 122M | } |
721 | 862k | else if(p->flags & FLAGS_UNSIGNED) { |
722 | | /* Decimal unsigned integer. */ |
723 | 541k | base = 10; |
724 | 541k | goto unsigned_number; |
725 | 541k | } |
726 | | |
727 | | /* Decimal integer. */ |
728 | 321k | base = 10; |
729 | | |
730 | 321k | is_neg = (p->data.num.as_signed < (mp_intmax_t)0) ? 1 : 0; |
731 | 321k | if(is_neg) { |
732 | | /* signed_num might fail to hold absolute negative minimum by 1 */ |
733 | 23 | signed_num = p->data.num.as_signed + (mp_intmax_t)1; |
734 | 23 | signed_num = -signed_num; |
735 | 23 | num = (mp_uintmax_t)signed_num; |
736 | 23 | num += (mp_uintmax_t)1; |
737 | 23 | } |
738 | | |
739 | 321k | goto number; |
740 | | |
741 | 123M | unsigned_number: |
742 | | /* Unsigned number of base BASE. */ |
743 | 123M | is_neg = 0; |
744 | | |
745 | 123M | number: |
746 | | /* Number of base BASE. */ |
747 | | |
748 | | /* Supply a default precision if none was given. */ |
749 | 123M | if(prec == -1) |
750 | 123M | prec = 1; |
751 | | |
752 | | /* Put the number in WORK. */ |
753 | 123M | w = workend; |
754 | 369M | while(num > 0) { |
755 | 246M | *w-- = digits[num % base]; |
756 | 246M | num /= base; |
757 | 246M | } |
758 | 123M | width -= (long)(workend - w); |
759 | 123M | prec -= (long)(workend - w); |
760 | | |
761 | 123M | if(is_alt && base == 8 && prec <= 0) { |
762 | 0 | *w-- = '0'; |
763 | 0 | --width; |
764 | 0 | } |
765 | | |
766 | 123M | if(prec > 0) { |
767 | 333k | width -= prec; |
768 | 666k | while(prec-- > 0 && w >= work) |
769 | 333k | *w-- = '0'; |
770 | 333k | } |
771 | | |
772 | 123M | if(is_alt && base == 16) |
773 | 0 | width -= 2; |
774 | | |
775 | 123M | if(is_neg || (p->flags & FLAGS_SHOWSIGN) || (p->flags & FLAGS_SPACE)) |
776 | 23 | --width; |
777 | | |
778 | 123M | if(!(p->flags & FLAGS_LEFT) && !(p->flags & FLAGS_PAD_NIL)) |
779 | 854k | while(width-- > 0) |
780 | 0 | OUTCHAR(' '); |
781 | | |
782 | 123M | if(is_neg) |
783 | 23 | OUTCHAR('-'); |
784 | 123M | else if(p->flags & FLAGS_SHOWSIGN) |
785 | 0 | OUTCHAR('+'); |
786 | 123M | else if(p->flags & FLAGS_SPACE) |
787 | 0 | OUTCHAR(' '); |
788 | | |
789 | 123M | if(is_alt && base == 16) { |
790 | 0 | OUTCHAR('0'); |
791 | 0 | if(p->flags & FLAGS_UPPER) |
792 | 0 | OUTCHAR('X'); |
793 | 0 | else |
794 | 0 | OUTCHAR('x'); |
795 | 0 | } |
796 | | |
797 | 123M | if(!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL)) |
798 | 122M | while(width-- > 0) |
799 | 325k | OUTCHAR('0'); |
800 | | |
801 | | /* Write the number. */ |
802 | 369M | while(++w <= workend) { |
803 | 246M | OUTCHAR(*w); |
804 | 246M | } |
805 | | |
806 | 123M | if(p->flags & FLAGS_LEFT) |
807 | 0 | while(width-- > 0) |
808 | 0 | OUTCHAR(' '); |
809 | 123M | break; |
810 | | |
811 | 123M | case FORMAT_STRING: |
812 | | /* String. */ |
813 | 46.5M | { |
814 | 46.5M | static const char null[] = "(nil)"; |
815 | 46.5M | const char *str; |
816 | 46.5M | size_t len; |
817 | | |
818 | 46.5M | str = (char *) p->data.str; |
819 | 46.5M | if(!str) { |
820 | | /* Write null[] if there's space. */ |
821 | 3 | if(prec == -1 || prec >= (long) sizeof(null) - 1) { |
822 | 3 | str = null; |
823 | 3 | len = sizeof(null) - 1; |
824 | | /* Disable quotes around (nil) */ |
825 | 3 | p->flags &= (~FLAGS_ALT); |
826 | 3 | } |
827 | 0 | else { |
828 | 0 | str = ""; |
829 | 0 | len = 0; |
830 | 0 | } |
831 | 3 | } |
832 | 46.5M | else if(prec != -1) |
833 | 2.98k | len = (size_t)prec; |
834 | 46.5M | else if(*str == '\0') |
835 | 36.3M | len = 0; |
836 | 10.1M | else |
837 | 10.1M | len = strlen(str); |
838 | | |
839 | 46.5M | width -= (len > LONG_MAX) ? LONG_MAX : (long)len; |
840 | | |
841 | 46.5M | if(p->flags & FLAGS_ALT) |
842 | 0 | OUTCHAR('"'); |
843 | | |
844 | 46.5M | if(!(p->flags&FLAGS_LEFT)) |
845 | 46.5M | while(width-- > 0) |
846 | 0 | OUTCHAR(' '); |
847 | | |
848 | 622M | for(; len && *str; len--) |
849 | 575M | OUTCHAR(*str++); |
850 | 46.5M | if(p->flags&FLAGS_LEFT) |
851 | 0 | while(width-- > 0) |
852 | 0 | OUTCHAR(' '); |
853 | | |
854 | 46.5M | if(p->flags & FLAGS_ALT) |
855 | 0 | OUTCHAR('"'); |
856 | 46.5M | } |
857 | 46.5M | break; |
858 | | |
859 | 46.5M | case FORMAT_PTR: |
860 | | /* Generic pointer. */ |
861 | 0 | { |
862 | 0 | void *ptr; |
863 | 0 | ptr = (void *) p->data.ptr; |
864 | 0 | if(ptr) { |
865 | | /* If the pointer is not NULL, write it as a %#x spec. */ |
866 | 0 | base = 16; |
867 | 0 | digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits; |
868 | 0 | is_alt = 1; |
869 | 0 | num = (size_t) ptr; |
870 | 0 | is_neg = 0; |
871 | 0 | goto number; |
872 | 0 | } |
873 | 0 | else { |
874 | | /* Write "(nil)" for a nil pointer. */ |
875 | 0 | static const char strnil[] = "(nil)"; |
876 | 0 | const char *point; |
877 | |
|
878 | 0 | width -= (long)(sizeof(strnil) - 1); |
879 | 0 | if(p->flags & FLAGS_LEFT) |
880 | 0 | while(width-- > 0) |
881 | 0 | OUTCHAR(' '); |
882 | 0 | for(point = strnil; *point != '\0'; ++point) |
883 | 0 | OUTCHAR(*point); |
884 | 0 | if(!(p->flags & FLAGS_LEFT)) |
885 | 0 | while(width-- > 0) |
886 | 0 | OUTCHAR(' '); |
887 | 0 | } |
888 | 0 | } |
889 | 0 | break; |
890 | | |
891 | 0 | case FORMAT_DOUBLE: |
892 | 0 | { |
893 | 0 | char formatbuf[32]="%"; |
894 | 0 | char *fptr = &formatbuf[1]; |
895 | 0 | size_t left = sizeof(formatbuf)-strlen(formatbuf); |
896 | 0 | int len; |
897 | |
|
898 | 0 | width = -1; |
899 | 0 | if(p->flags & FLAGS_WIDTH) |
900 | 0 | width = p->width; |
901 | 0 | else if(p->flags & FLAGS_WIDTHPARAM) |
902 | 0 | width = (long)vto[p->width].data.num.as_signed; |
903 | |
|
904 | 0 | prec = -1; |
905 | 0 | if(p->flags & FLAGS_PREC) |
906 | 0 | prec = p->precision; |
907 | 0 | else if(p->flags & FLAGS_PRECPARAM) |
908 | 0 | prec = (long)vto[p->precision].data.num.as_signed; |
909 | |
|
910 | 0 | if(p->flags & FLAGS_LEFT) |
911 | 0 | *fptr++ = '-'; |
912 | 0 | if(p->flags & FLAGS_SHOWSIGN) |
913 | 0 | *fptr++ = '+'; |
914 | 0 | if(p->flags & FLAGS_SPACE) |
915 | 0 | *fptr++ = ' '; |
916 | 0 | if(p->flags & FLAGS_ALT) |
917 | 0 | *fptr++ = '#'; |
918 | |
|
919 | 0 | *fptr = 0; |
920 | |
|
921 | 0 | if(width >= 0) { |
922 | 0 | if(width >= (long)sizeof(work)) |
923 | 0 | width = sizeof(work)-1; |
924 | | /* RECURSIVE USAGE */ |
925 | 0 | len = curl_msnprintf(fptr, left, "%ld", width); |
926 | 0 | fptr += len; |
927 | 0 | left -= len; |
928 | 0 | } |
929 | 0 | if(prec >= 0) { |
930 | | /* for each digit in the integer part, we can have one less |
931 | | precision */ |
932 | 0 | size_t maxprec = sizeof(work) - 2; |
933 | 0 | double val = p->data.dnum; |
934 | 0 | if(width > 0 && prec <= width) |
935 | 0 | maxprec -= width; |
936 | 0 | while(val >= 10.0) { |
937 | 0 | val /= 10; |
938 | 0 | maxprec--; |
939 | 0 | } |
940 | |
|
941 | 0 | if(prec > (long)maxprec) |
942 | 0 | prec = (long)maxprec-1; |
943 | 0 | if(prec < 0) |
944 | 0 | prec = 0; |
945 | | /* RECURSIVE USAGE */ |
946 | 0 | len = curl_msnprintf(fptr, left, ".%ld", prec); |
947 | 0 | fptr += len; |
948 | 0 | } |
949 | 0 | if(p->flags & FLAGS_LONG) |
950 | 0 | *fptr++ = 'l'; |
951 | |
|
952 | 0 | if(p->flags & FLAGS_FLOATE) |
953 | 0 | *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'E':'e'); |
954 | 0 | else if(p->flags & FLAGS_FLOATG) |
955 | 0 | *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'G' : 'g'); |
956 | 0 | else |
957 | 0 | *fptr++ = 'f'; |
958 | |
|
959 | 0 | *fptr = 0; /* and a final null-termination */ |
960 | |
|
961 | 0 | #ifdef __clang__ |
962 | 0 | #pragma clang diagnostic push |
963 | 0 | #pragma clang diagnostic ignored "-Wformat-nonliteral" |
964 | 0 | #endif |
965 | | /* NOTE NOTE NOTE!! Not all sprintf implementations return number of |
966 | | output characters */ |
967 | 0 | #ifdef HAVE_SNPRINTF |
968 | 0 | (snprintf)(work, sizeof(work), formatbuf, p->data.dnum); |
969 | | #else |
970 | | (sprintf)(work, formatbuf, p->data.dnum); |
971 | | #endif |
972 | 0 | #ifdef __clang__ |
973 | 0 | #pragma clang diagnostic pop |
974 | 0 | #endif |
975 | 0 | DEBUGASSERT(strlen(work) <= sizeof(work)); |
976 | 0 | for(fptr = work; *fptr; fptr++) |
977 | 0 | OUTCHAR(*fptr); |
978 | 0 | } |
979 | 0 | break; |
980 | | |
981 | 0 | case FORMAT_INTPTR: |
982 | | /* Answer the count of characters written. */ |
983 | 0 | #ifdef HAVE_LONG_LONG_TYPE |
984 | 0 | if(p->flags & FLAGS_LONGLONG) |
985 | 0 | *(LONG_LONG_TYPE *) p->data.ptr = (LONG_LONG_TYPE)done; |
986 | 0 | else |
987 | 0 | #endif |
988 | 0 | if(p->flags & FLAGS_LONG) |
989 | 0 | *(long *) p->data.ptr = (long)done; |
990 | 0 | else if(!(p->flags & FLAGS_SHORT)) |
991 | 0 | *(int *) p->data.ptr = (int)done; |
992 | 0 | else |
993 | 0 | *(short *) p->data.ptr = (short)done; |
994 | 0 | break; |
995 | | |
996 | 0 | default: |
997 | 0 | break; |
998 | 198M | } |
999 | 198M | f = *end++; /* goto end of %-code */ |
1000 | | |
1001 | 198M | } |
1002 | 136M | return done; |
1003 | 136M | } |
1004 | | |
1005 | | /* fputc() look-alike */ |
1006 | | static int addbyter(int output, FILE *data) |
1007 | 35.0M | { |
1008 | 35.0M | struct nsprintf *infop = (struct nsprintf *)data; |
1009 | 35.0M | unsigned char outc = (unsigned char)output; |
1010 | | |
1011 | 35.0M | if(infop->length < infop->max) { |
1012 | | /* only do this if we haven't reached max length yet */ |
1013 | 35.0M | infop->buffer[0] = outc; /* store */ |
1014 | 35.0M | infop->buffer++; /* increase pointer */ |
1015 | 35.0M | infop->length++; /* we are now one byte larger */ |
1016 | 35.0M | return outc; /* fputc() returns like this on success */ |
1017 | 35.0M | } |
1018 | 0 | return -1; |
1019 | 35.0M | } |
1020 | | |
1021 | | int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format, |
1022 | | va_list ap_save) |
1023 | 7.80M | { |
1024 | 7.80M | int retcode; |
1025 | 7.80M | struct nsprintf info; |
1026 | | |
1027 | 7.80M | info.buffer = buffer; |
1028 | 7.80M | info.length = 0; |
1029 | 7.80M | info.max = maxlength; |
1030 | | |
1031 | 7.80M | retcode = dprintf_formatf(&info, addbyter, format, ap_save); |
1032 | 7.80M | if(info.max) { |
1033 | | /* we terminate this with a zero byte */ |
1034 | 7.80M | if(info.max == info.length) { |
1035 | | /* we're at maximum, scrap the last letter */ |
1036 | 0 | info.buffer[-1] = 0; |
1037 | 0 | DEBUGASSERT(retcode); |
1038 | 0 | retcode--; /* don't count the nul byte */ |
1039 | 0 | } |
1040 | 7.80M | else |
1041 | 7.80M | info.buffer[0] = 0; |
1042 | 7.80M | } |
1043 | 7.80M | return retcode; |
1044 | 7.80M | } |
1045 | | |
1046 | | int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) |
1047 | 7.80M | { |
1048 | 7.80M | int retcode; |
1049 | 7.80M | va_list ap_save; /* argument pointer */ |
1050 | 7.80M | va_start(ap_save, format); |
1051 | 7.80M | retcode = curl_mvsnprintf(buffer, maxlength, format, ap_save); |
1052 | 7.80M | va_end(ap_save); |
1053 | 7.80M | return retcode; |
1054 | 7.80M | } |
1055 | | |
1056 | | /* fputc() look-alike */ |
1057 | | static int alloc_addbyter(int output, FILE *data) |
1058 | 1.07G | { |
1059 | 1.07G | struct asprintf *infop = (struct asprintf *)data; |
1060 | 1.07G | unsigned char outc = (unsigned char)output; |
1061 | | |
1062 | 1.07G | if(Curl_dyn_addn(infop->b, &outc, 1)) { |
1063 | 45 | infop->fail = 1; |
1064 | 45 | return -1; /* fail */ |
1065 | 45 | } |
1066 | 1.07G | return outc; /* fputc() returns like this on success */ |
1067 | 1.07G | } |
1068 | | |
1069 | | extern int Curl_dyn_vprintf(struct dynbuf *dyn, |
1070 | | const char *format, va_list ap_save); |
1071 | | |
1072 | | /* appends the formatted string, returns 0 on success, 1 on error */ |
1073 | | int Curl_dyn_vprintf(struct dynbuf *dyn, const char *format, va_list ap_save) |
1074 | 122M | { |
1075 | 122M | struct asprintf info; |
1076 | 122M | info.b = dyn; |
1077 | 122M | info.fail = 0; |
1078 | | |
1079 | 122M | (void)dprintf_formatf(&info, alloc_addbyter, format, ap_save); |
1080 | 122M | if(info.fail) { |
1081 | 45 | Curl_dyn_free(info.b); |
1082 | 45 | return 1; |
1083 | 45 | } |
1084 | 122M | return 0; |
1085 | 122M | } |
1086 | | |
1087 | | char *curl_mvaprintf(const char *format, va_list ap_save) |
1088 | 6.45M | { |
1089 | 6.45M | struct asprintf info; |
1090 | 6.45M | struct dynbuf dyn; |
1091 | 6.45M | info.b = &dyn; |
1092 | 6.45M | Curl_dyn_init(info.b, DYN_APRINTF); |
1093 | 6.45M | info.fail = 0; |
1094 | | |
1095 | 6.45M | (void)dprintf_formatf(&info, alloc_addbyter, format, ap_save); |
1096 | 6.45M | if(info.fail) { |
1097 | 0 | Curl_dyn_free(info.b); |
1098 | 0 | return NULL; |
1099 | 0 | } |
1100 | 6.45M | if(Curl_dyn_len(info.b)) |
1101 | 6.45M | return Curl_dyn_ptr(info.b); |
1102 | 0 | return strdup(""); |
1103 | 6.45M | } |
1104 | | |
1105 | | char *curl_maprintf(const char *format, ...) |
1106 | 139k | { |
1107 | 139k | va_list ap_save; |
1108 | 139k | char *s; |
1109 | 139k | va_start(ap_save, format); |
1110 | 139k | s = curl_mvaprintf(format, ap_save); |
1111 | 139k | va_end(ap_save); |
1112 | 139k | return s; |
1113 | 139k | } |
1114 | | |
1115 | | static int storebuffer(int output, FILE *data) |
1116 | 0 | { |
1117 | 0 | char **buffer = (char **)data; |
1118 | 0 | unsigned char outc = (unsigned char)output; |
1119 | 0 | **buffer = outc; |
1120 | 0 | (*buffer)++; |
1121 | 0 | return outc; /* act like fputc() ! */ |
1122 | 0 | } |
1123 | | |
1124 | | int curl_msprintf(char *buffer, const char *format, ...) |
1125 | 0 | { |
1126 | 0 | va_list ap_save; /* argument pointer */ |
1127 | 0 | int retcode; |
1128 | 0 | va_start(ap_save, format); |
1129 | 0 | retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save); |
1130 | 0 | va_end(ap_save); |
1131 | 0 | *buffer = 0; /* we terminate this with a zero byte */ |
1132 | 0 | return retcode; |
1133 | 0 | } |
1134 | | |
1135 | | int curl_mprintf(const char *format, ...) |
1136 | 0 | { |
1137 | 0 | int retcode; |
1138 | 0 | va_list ap_save; /* argument pointer */ |
1139 | 0 | va_start(ap_save, format); |
1140 | |
|
1141 | 0 | retcode = dprintf_formatf(stdout, fputc, format, ap_save); |
1142 | 0 | va_end(ap_save); |
1143 | 0 | return retcode; |
1144 | 0 | } |
1145 | | |
1146 | | int curl_mfprintf(FILE *whereto, const char *format, ...) |
1147 | 6.40k | { |
1148 | 6.40k | int retcode; |
1149 | 6.40k | va_list ap_save; /* argument pointer */ |
1150 | 6.40k | va_start(ap_save, format); |
1151 | 6.40k | retcode = dprintf_formatf(whereto, fputc, format, ap_save); |
1152 | 6.40k | va_end(ap_save); |
1153 | 6.40k | return retcode; |
1154 | 6.40k | } |
1155 | | |
1156 | | int curl_mvsprintf(char *buffer, const char *format, va_list ap_save) |
1157 | 0 | { |
1158 | 0 | int retcode; |
1159 | 0 | retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save); |
1160 | 0 | *buffer = 0; /* we terminate this with a zero byte */ |
1161 | 0 | return retcode; |
1162 | 0 | } |
1163 | | |
1164 | | int curl_mvprintf(const char *format, va_list ap_save) |
1165 | 0 | { |
1166 | 0 | return dprintf_formatf(stdout, fputc, format, ap_save); |
1167 | 0 | } |
1168 | | |
1169 | | int curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save) |
1170 | 0 | { |
1171 | 0 | return dprintf_formatf(whereto, fputc, format, ap_save); |
1172 | 0 | } |