Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) 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 | 1.92k | # 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 | 1.92k | # define mp_intmax_t LONG_LONG_TYPE |
83 | 1.92k | # 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 | 5.59k | #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 | 2.78k | do { \ |
105 | 2.78k | if(stream((unsigned char)(x), (FILE *)data) != -1) \ |
106 | 2.78k | done++; \ |
107 | 2.78k | else \ |
108 | 2.78k | return done; /* return immediately on failure */ \ |
109 | 2.78k | } 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 | 2.23k | { |
180 | 2.23k | int number = 0; |
181 | 2.23k | while(ISDIGIT(*input)) { |
182 | 0 | if(number < MAX_PARAMETERS) { |
183 | 0 | number *= 10; |
184 | 0 | number += *input - '0'; |
185 | 0 | } |
186 | 0 | input++; |
187 | 0 | } |
188 | 2.23k | if(number <= MAX_PARAMETERS && ('$' == *input)) { |
189 | 0 | *end = ++input; |
190 | 0 | return number; |
191 | 0 | } |
192 | 2.23k | return 0; |
193 | 2.23k | } |
194 | | |
195 | | static bool dprintf_IsQualifierNoDollar(const char *fmt) |
196 | 1.16k | { |
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 | 1.16k | switch(*fmt) { |
204 | 0 | case '-': case '+': case ' ': case '#': case '.': |
205 | 0 | case '0': case '1': case '2': case '3': case '4': |
206 | 0 | case '5': case '6': case '7': case '8': case '9': |
207 | 42 | case 'h': case 'l': case 'L': case 'z': case 'q': |
208 | 42 | case '*': case 'O': |
209 | | #if defined(MP_HAVE_INT_EXTENSIONS) |
210 | | case 'I': |
211 | | #endif |
212 | 42 | return TRUE; |
213 | | |
214 | 1.11k | default: |
215 | 1.11k | return FALSE; |
216 | 1.16k | } |
217 | 1.16k | } |
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 | 311 | { |
232 | 311 | char *fmt = (char *)format; |
233 | 311 | int param_num = 0; |
234 | 311 | long this_param; |
235 | 311 | long width; |
236 | 311 | long precision; |
237 | 311 | int flags; |
238 | 311 | long max_param = 0; |
239 | 311 | long i; |
240 | | |
241 | 3.35k | while(*fmt) { |
242 | 3.04k | if(*fmt++ == '%') { |
243 | 1.11k | if(*fmt == '%') { |
244 | 0 | fmt++; |
245 | 0 | continue; /* while */ |
246 | 0 | } |
247 | | |
248 | 1.11k | flags = FLAGS_NEW; |
249 | | |
250 | | /* Handle the positional case (N$) */ |
251 | | |
252 | 1.11k | param_num++; |
253 | | |
254 | 1.11k | this_param = dprintf_DollarString(fmt, &fmt); |
255 | 1.11k | if(0 == this_param) |
256 | | /* we got no positional, get the next counter */ |
257 | 1.11k | this_param = param_num; |
258 | | |
259 | 1.11k | if(this_param > max_param) |
260 | 1.11k | 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 | 1.11k | width = 0; |
269 | 1.11k | precision = 0; |
270 | | |
271 | | /* Handle the flags */ |
272 | | |
273 | 1.16k | 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 | 42 | 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 | 0 | case '.': |
301 | 0 | if('*' == *fmt) { |
302 | | /* The precision is picked from a specified parameter */ |
303 | |
|
304 | 0 | flags |= FLAGS_PRECPARAM; |
305 | 0 | fmt++; |
306 | 0 | param_num++; |
307 | |
|
308 | 0 | i = dprintf_DollarString(fmt, &fmt); |
309 | 0 | if(i) |
310 | 0 | precision = i; |
311 | 0 | else |
312 | 0 | precision = param_num; |
313 | |
|
314 | 0 | if(precision > max_param) |
315 | 0 | max_param = precision; |
316 | 0 | } |
317 | 0 | else { |
318 | 0 | flags |= FLAGS_PREC; |
319 | 0 | precision = strtol(fmt, &fmt, 10); |
320 | 0 | } |
321 | 0 | if((flags & (FLAGS_PREC | FLAGS_PRECPARAM)) == |
322 | 0 | (FLAGS_PREC | FLAGS_PRECPARAM)) |
323 | | /* it is not permitted to use both kinds of precision for the same |
324 | | argument */ |
325 | 0 | return 1; |
326 | 0 | break; |
327 | 0 | case 'h': |
328 | 0 | flags |= FLAGS_SHORT; |
329 | 0 | break; |
330 | | #if defined(MP_HAVE_INT_EXTENSIONS) |
331 | | case 'I': |
332 | | #if (SIZEOF_CURL_OFF_T > SIZEOF_LONG) |
333 | | flags |= FLAGS_LONGLONG; |
334 | | #else |
335 | | flags |= FLAGS_LONG; |
336 | | #endif |
337 | | break; |
338 | | #endif |
339 | 42 | case 'l': |
340 | 42 | if(flags & FLAGS_LONG) |
341 | 0 | flags |= FLAGS_LONGLONG; |
342 | 42 | else |
343 | 42 | flags |= FLAGS_LONG; |
344 | 42 | break; |
345 | 0 | case 'L': |
346 | 0 | flags |= FLAGS_LONGDOUBLE; |
347 | 0 | break; |
348 | 0 | case 'q': |
349 | 0 | flags |= FLAGS_LONGLONG; |
350 | 0 | break; |
351 | 0 | case 'z': |
352 | | /* the code below generates a warning if -Wunreachable-code is |
353 | | used */ |
354 | | #if (SIZEOF_SIZE_T > SIZEOF_LONG) |
355 | | flags |= FLAGS_LONGLONG; |
356 | | #else |
357 | 0 | flags |= FLAGS_LONG; |
358 | 0 | #endif |
359 | 0 | break; |
360 | 0 | case 'O': |
361 | | #if (SIZEOF_CURL_OFF_T > SIZEOF_LONG) |
362 | | flags |= FLAGS_LONGLONG; |
363 | | #else |
364 | 0 | flags |= FLAGS_LONG; |
365 | 0 | #endif |
366 | 0 | break; |
367 | 0 | case '0': |
368 | 0 | if(!(flags & FLAGS_LEFT)) |
369 | 0 | flags |= FLAGS_PAD_NIL; |
370 | | /* FALLTHROUGH */ |
371 | 0 | case '1': case '2': case '3': case '4': |
372 | 0 | case '5': case '6': case '7': case '8': case '9': |
373 | 0 | flags |= FLAGS_WIDTH; |
374 | 0 | width = strtol(fmt-1, &fmt, 10); |
375 | 0 | break; |
376 | 0 | case '*': /* Special case */ |
377 | 0 | flags |= FLAGS_WIDTHPARAM; |
378 | 0 | param_num++; |
379 | |
|
380 | 0 | i = dprintf_DollarString(fmt, &fmt); |
381 | 0 | if(i) |
382 | 0 | width = i; |
383 | 0 | else |
384 | 0 | width = param_num; |
385 | 0 | if(width > max_param) |
386 | 0 | max_param = width; |
387 | 0 | break; |
388 | 0 | case '\0': |
389 | 0 | fmt--; |
390 | 0 | default: |
391 | 0 | break; |
392 | 42 | } |
393 | 42 | } /* switch */ |
394 | | |
395 | | /* Handle the specifier */ |
396 | | |
397 | 1.11k | i = this_param - 1; |
398 | | |
399 | 1.11k | if((i < 0) || (i >= MAX_PARAMETERS)) |
400 | | /* out of allowed range */ |
401 | 0 | return 1; |
402 | | |
403 | 1.11k | switch(*fmt) { |
404 | 0 | case 'S': |
405 | 0 | flags |= FLAGS_ALT; |
406 | | /* FALLTHROUGH */ |
407 | 0 | case 's': |
408 | 0 | vto[i].type = FORMAT_STRING; |
409 | 0 | break; |
410 | 0 | case 'n': |
411 | 0 | vto[i].type = FORMAT_INTPTR; |
412 | 0 | break; |
413 | 0 | case 'p': |
414 | 0 | vto[i].type = FORMAT_PTR; |
415 | 0 | break; |
416 | 42 | case 'd': case 'i': |
417 | 42 | vto[i].type = FORMAT_INT; |
418 | 42 | break; |
419 | 1.07k | case 'u': |
420 | 1.07k | vto[i].type = FORMAT_INT; |
421 | 1.07k | flags |= FLAGS_UNSIGNED; |
422 | 1.07k | break; |
423 | 0 | case 'o': |
424 | 0 | vto[i].type = FORMAT_INT; |
425 | 0 | flags |= FLAGS_OCTAL; |
426 | 0 | break; |
427 | 0 | case 'x': |
428 | 0 | vto[i].type = FORMAT_INT; |
429 | 0 | flags |= FLAGS_HEX|FLAGS_UNSIGNED; |
430 | 0 | break; |
431 | 0 | case 'X': |
432 | 0 | vto[i].type = FORMAT_INT; |
433 | 0 | flags |= FLAGS_HEX|FLAGS_UPPER|FLAGS_UNSIGNED; |
434 | 0 | break; |
435 | 0 | case 'c': |
436 | 0 | vto[i].type = FORMAT_INT; |
437 | 0 | flags |= FLAGS_CHAR; |
438 | 0 | break; |
439 | 0 | case 'f': |
440 | 0 | vto[i].type = FORMAT_DOUBLE; |
441 | 0 | break; |
442 | 0 | case 'e': |
443 | 0 | vto[i].type = FORMAT_DOUBLE; |
444 | 0 | flags |= FLAGS_FLOATE; |
445 | 0 | break; |
446 | 0 | case 'E': |
447 | 0 | vto[i].type = FORMAT_DOUBLE; |
448 | 0 | flags |= FLAGS_FLOATE|FLAGS_UPPER; |
449 | 0 | break; |
450 | 0 | case 'g': |
451 | 0 | vto[i].type = FORMAT_DOUBLE; |
452 | 0 | flags |= FLAGS_FLOATG; |
453 | 0 | break; |
454 | 0 | case 'G': |
455 | 0 | vto[i].type = FORMAT_DOUBLE; |
456 | 0 | flags |= FLAGS_FLOATG|FLAGS_UPPER; |
457 | 0 | break; |
458 | 0 | default: |
459 | 0 | vto[i].type = FORMAT_UNKNOWN; |
460 | 0 | break; |
461 | 1.11k | } /* switch */ |
462 | | |
463 | 1.11k | vto[i].flags = flags; |
464 | 1.11k | vto[i].width = width; |
465 | 1.11k | vto[i].precision = precision; |
466 | | |
467 | 1.11k | if(flags & FLAGS_WIDTHPARAM) { |
468 | | /* we have the width specified from a parameter, so we make that |
469 | | parameter's info setup properly */ |
470 | 0 | long k = width - 1; |
471 | 0 | if((k < 0) || (k >= MAX_PARAMETERS)) |
472 | | /* out of allowed range */ |
473 | 0 | return 1; |
474 | 0 | vto[i].width = k; |
475 | 0 | vto[k].type = FORMAT_WIDTH; |
476 | 0 | vto[k].flags = FLAGS_NEW; |
477 | | /* can't use width or precision of width! */ |
478 | 0 | vto[k].width = 0; |
479 | 0 | vto[k].precision = 0; |
480 | 0 | } |
481 | 1.11k | if(flags & FLAGS_PRECPARAM) { |
482 | | /* we have the precision specified from a parameter, so we make that |
483 | | parameter's info setup properly */ |
484 | 0 | long k = precision - 1; |
485 | 0 | if((k < 0) || (k >= MAX_PARAMETERS)) |
486 | | /* out of allowed range */ |
487 | 0 | return 1; |
488 | 0 | vto[i].precision = k; |
489 | 0 | vto[k].type = FORMAT_WIDTH; |
490 | 0 | vto[k].flags = FLAGS_NEW; |
491 | | /* can't use width or precision of width! */ |
492 | 0 | vto[k].width = 0; |
493 | 0 | vto[k].precision = 0; |
494 | 0 | } |
495 | 1.11k | *endpos++ = fmt + ((*fmt == '\0') ? 0 : 1); /* end of this sequence */ |
496 | 1.11k | } |
497 | 3.04k | } |
498 | | |
499 | | /* Read the arg list parameters into our data list */ |
500 | 1.42k | for(i = 0; i<max_param; i++) { |
501 | | /* Width/precision arguments must be read before the main argument |
502 | | they are attached to */ |
503 | 1.11k | if(vto[i].flags & FLAGS_WIDTHPARAM) { |
504 | 0 | vto[vto[i].width].data.num.as_signed = |
505 | 0 | (mp_intmax_t)va_arg(arglist, int); |
506 | 0 | } |
507 | 1.11k | if(vto[i].flags & FLAGS_PRECPARAM) { |
508 | 0 | vto[vto[i].precision].data.num.as_signed = |
509 | 0 | (mp_intmax_t)va_arg(arglist, int); |
510 | 0 | } |
511 | | |
512 | 1.11k | switch(vto[i].type) { |
513 | 0 | case FORMAT_STRING: |
514 | 0 | vto[i].data.str = va_arg(arglist, char *); |
515 | 0 | break; |
516 | | |
517 | 0 | case FORMAT_INTPTR: |
518 | 0 | case FORMAT_UNKNOWN: |
519 | 0 | case FORMAT_PTR: |
520 | 0 | vto[i].data.ptr = va_arg(arglist, void *); |
521 | 0 | break; |
522 | | |
523 | 1.11k | case FORMAT_INT: |
524 | 1.11k | #ifdef HAVE_LONG_LONG_TYPE |
525 | 1.11k | if((vto[i].flags & FLAGS_LONGLONG) && (vto[i].flags & FLAGS_UNSIGNED)) |
526 | 0 | vto[i].data.num.as_unsigned = |
527 | 0 | (mp_uintmax_t)va_arg(arglist, mp_uintmax_t); |
528 | 1.11k | else if(vto[i].flags & FLAGS_LONGLONG) |
529 | 0 | vto[i].data.num.as_signed = |
530 | 0 | (mp_intmax_t)va_arg(arglist, mp_intmax_t); |
531 | 1.11k | else |
532 | 1.11k | #endif |
533 | 1.11k | { |
534 | 1.11k | if((vto[i].flags & FLAGS_LONG) && (vto[i].flags & FLAGS_UNSIGNED)) |
535 | 0 | vto[i].data.num.as_unsigned = |
536 | 0 | (mp_uintmax_t)va_arg(arglist, unsigned long); |
537 | 1.11k | else if(vto[i].flags & FLAGS_LONG) |
538 | 42 | vto[i].data.num.as_signed = |
539 | 42 | (mp_intmax_t)va_arg(arglist, long); |
540 | 1.07k | else if(vto[i].flags & FLAGS_UNSIGNED) |
541 | 1.07k | vto[i].data.num.as_unsigned = |
542 | 1.07k | (mp_uintmax_t)va_arg(arglist, unsigned int); |
543 | 0 | else |
544 | 0 | vto[i].data.num.as_signed = |
545 | 0 | (mp_intmax_t)va_arg(arglist, int); |
546 | 1.11k | } |
547 | 1.11k | break; |
548 | | |
549 | 0 | case FORMAT_DOUBLE: |
550 | 0 | vto[i].data.dnum = va_arg(arglist, double); |
551 | 0 | break; |
552 | | |
553 | 0 | case FORMAT_WIDTH: |
554 | | /* Argument has been read. Silently convert it into an integer |
555 | | * for later use |
556 | | */ |
557 | 0 | vto[i].type = FORMAT_INT; |
558 | 0 | break; |
559 | | |
560 | 0 | default: |
561 | 0 | break; |
562 | 1.11k | } |
563 | 1.11k | } |
564 | | |
565 | 311 | return 0; |
566 | | |
567 | 311 | } |
568 | | |
569 | | static int dprintf_formatf( |
570 | | void *data, /* untouched by format(), just sent to the stream() function in |
571 | | the second argument */ |
572 | | /* function pointer called for each output character */ |
573 | | int (*stream)(int, FILE *), |
574 | | const char *format, /* %-formatted string */ |
575 | | va_list ap_save) /* list of parameters */ |
576 | 311 | { |
577 | | /* Base-36 digits for numbers. */ |
578 | 311 | const char *digits = lower_digits; |
579 | | |
580 | | /* Pointer into the format string. */ |
581 | 311 | char *f; |
582 | | |
583 | | /* Number of characters written. */ |
584 | 311 | int done = 0; |
585 | | |
586 | 311 | long param; /* current parameter to read */ |
587 | 311 | long param_num = 0; /* parameter counter */ |
588 | | |
589 | 311 | struct va_stack vto[MAX_PARAMETERS]; |
590 | 311 | char *endpos[MAX_PARAMETERS]; |
591 | 311 | char **end; |
592 | 311 | char work[BUFFSIZE]; |
593 | 311 | struct va_stack *p; |
594 | | |
595 | | /* 'workend' points to the final buffer byte position, but with an extra |
596 | | byte as margin to avoid the (false?) warning Coverity gives us |
597 | | otherwise */ |
598 | 311 | char *workend = &work[sizeof(work) - 2]; |
599 | | |
600 | | /* Do the actual %-code parsing */ |
601 | 311 | if(dprintf_Pass1(format, vto, endpos, ap_save)) |
602 | 0 | return 0; |
603 | | |
604 | 311 | end = &endpos[0]; /* the initial end-position from the list dprintf_Pass1() |
605 | | created for us */ |
606 | | |
607 | 311 | f = (char *)format; |
608 | 2.23k | while(*f != '\0') { |
609 | | /* Format spec modifiers. */ |
610 | 1.92k | int is_alt; |
611 | | |
612 | | /* Width of a field. */ |
613 | 1.92k | long width; |
614 | | |
615 | | /* Precision of a field. */ |
616 | 1.92k | long prec; |
617 | | |
618 | | /* Decimal integer is negative. */ |
619 | 1.92k | int is_neg; |
620 | | |
621 | | /* Base of a number to be written. */ |
622 | 1.92k | unsigned long base; |
623 | | |
624 | | /* Integral values to be written. */ |
625 | 1.92k | mp_uintmax_t num; |
626 | | |
627 | | /* Used to convert negative in positive. */ |
628 | 1.92k | mp_intmax_t signed_num; |
629 | | |
630 | 1.92k | char *w; |
631 | | |
632 | 1.92k | if(*f != '%') { |
633 | | /* This isn't a format spec, so write everything out until the next one |
634 | | OR end of string is reached. */ |
635 | 807 | do { |
636 | 807 | OUTCHAR(*f); |
637 | 807 | } while(*++f && ('%' != *f)); |
638 | 807 | continue; |
639 | 807 | } |
640 | | |
641 | 1.11k | ++f; |
642 | | |
643 | | /* Check for "%%". Note that although the ANSI standard lists |
644 | | '%' as a conversion specifier, it says "The complete format |
645 | | specification shall be `%%'," so we can avoid all the width |
646 | | and precision processing. */ |
647 | 1.11k | if(*f == '%') { |
648 | 0 | ++f; |
649 | 0 | OUTCHAR('%'); |
650 | 0 | continue; |
651 | 0 | } |
652 | | |
653 | | /* If this is a positional parameter, the position must follow immediately |
654 | | after the %, thus create a %<num>$ sequence */ |
655 | 1.11k | param = dprintf_DollarString(f, &f); |
656 | | |
657 | 1.11k | if(!param) |
658 | 1.11k | param = param_num; |
659 | 0 | else |
660 | 0 | --param; |
661 | | |
662 | 1.11k | param_num++; /* increase this always to allow "%2$s %1$s %s" and then the |
663 | | third %s will pick the 3rd argument */ |
664 | | |
665 | 1.11k | p = &vto[param]; |
666 | | |
667 | | /* pick up the specified width */ |
668 | 1.11k | if(p->flags & FLAGS_WIDTHPARAM) { |
669 | 0 | width = (long)vto[p->width].data.num.as_signed; |
670 | 0 | param_num++; /* since the width is extracted from a parameter, we |
671 | | must skip that to get to the next one properly */ |
672 | 0 | if(width < 0) { |
673 | | /* "A negative field width is taken as a '-' flag followed by a |
674 | | positive field width." */ |
675 | 0 | width = -width; |
676 | 0 | p->flags |= FLAGS_LEFT; |
677 | 0 | p->flags &= ~FLAGS_PAD_NIL; |
678 | 0 | } |
679 | 0 | } |
680 | 1.11k | else |
681 | 1.11k | width = p->width; |
682 | | |
683 | | /* pick up the specified precision */ |
684 | 1.11k | if(p->flags & FLAGS_PRECPARAM) { |
685 | 0 | prec = (long)vto[p->precision].data.num.as_signed; |
686 | 0 | param_num++; /* since the precision is extracted from a parameter, we |
687 | | must skip that to get to the next one properly */ |
688 | 0 | if(prec < 0) |
689 | | /* "A negative precision is taken as if the precision were |
690 | | omitted." */ |
691 | 0 | prec = -1; |
692 | 0 | } |
693 | 1.11k | else if(p->flags & FLAGS_PREC) |
694 | 0 | prec = p->precision; |
695 | 1.11k | else |
696 | 1.11k | prec = -1; |
697 | | |
698 | 1.11k | is_alt = (p->flags & FLAGS_ALT) ? 1 : 0; |
699 | | |
700 | 1.11k | switch(p->type) { |
701 | 1.11k | case FORMAT_INT: |
702 | 1.11k | num = p->data.num.as_unsigned; |
703 | 1.11k | if(p->flags & FLAGS_CHAR) { |
704 | | /* Character. */ |
705 | 0 | if(!(p->flags & FLAGS_LEFT)) |
706 | 0 | while(--width > 0) |
707 | 0 | OUTCHAR(' '); |
708 | 0 | OUTCHAR((char) num); |
709 | 0 | if(p->flags & FLAGS_LEFT) |
710 | 0 | while(--width > 0) |
711 | 0 | OUTCHAR(' '); |
712 | 0 | break; |
713 | 0 | } |
714 | 1.11k | if(p->flags & FLAGS_OCTAL) { |
715 | | /* Octal unsigned integer. */ |
716 | 0 | base = 8; |
717 | 0 | goto unsigned_number; |
718 | 0 | } |
719 | 1.11k | else if(p->flags & FLAGS_HEX) { |
720 | | /* Hexadecimal unsigned integer. */ |
721 | |
|
722 | 0 | digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits; |
723 | 0 | base = 16; |
724 | 0 | goto unsigned_number; |
725 | 0 | } |
726 | 1.11k | else if(p->flags & FLAGS_UNSIGNED) { |
727 | | /* Decimal unsigned integer. */ |
728 | 1.07k | base = 10; |
729 | 1.07k | goto unsigned_number; |
730 | 1.07k | } |
731 | | |
732 | | /* Decimal integer. */ |
733 | 42 | base = 10; |
734 | | |
735 | 42 | is_neg = (p->data.num.as_signed < (mp_intmax_t)0) ? 1 : 0; |
736 | 42 | if(is_neg) { |
737 | | /* signed_num might fail to hold absolute negative minimum by 1 */ |
738 | 0 | signed_num = p->data.num.as_signed + (mp_intmax_t)1; |
739 | 0 | signed_num = -signed_num; |
740 | 0 | num = (mp_uintmax_t)signed_num; |
741 | 0 | num += (mp_uintmax_t)1; |
742 | 0 | } |
743 | | |
744 | 42 | goto number; |
745 | | |
746 | 1.07k | unsigned_number: |
747 | | /* Unsigned number of base BASE. */ |
748 | 1.07k | is_neg = 0; |
749 | | |
750 | 1.11k | number: |
751 | | /* Number of base BASE. */ |
752 | | |
753 | | /* Supply a default precision if none was given. */ |
754 | 1.11k | if(prec == -1) |
755 | 1.11k | prec = 1; |
756 | | |
757 | | /* Put the number in WORK. */ |
758 | 1.11k | w = workend; |
759 | 2.71k | while(num > 0) { |
760 | 1.59k | *w-- = digits[num % base]; |
761 | 1.59k | num /= base; |
762 | 1.59k | } |
763 | 1.11k | width -= (long)(workend - w); |
764 | 1.11k | prec -= (long)(workend - w); |
765 | | |
766 | 1.11k | if(is_alt && base == 8 && prec <= 0) { |
767 | 0 | *w-- = '0'; |
768 | 0 | --width; |
769 | 0 | } |
770 | | |
771 | 1.11k | if(prec > 0) { |
772 | 377 | width -= prec; |
773 | 754 | while(prec-- > 0 && w >= work) |
774 | 377 | *w-- = '0'; |
775 | 377 | } |
776 | | |
777 | 1.11k | if(is_alt && base == 16) |
778 | 0 | width -= 2; |
779 | | |
780 | 1.11k | if(is_neg || (p->flags & FLAGS_SHOWSIGN) || (p->flags & FLAGS_SPACE)) |
781 | 0 | --width; |
782 | | |
783 | 1.11k | if(!(p->flags & FLAGS_LEFT) && !(p->flags & FLAGS_PAD_NIL)) |
784 | 1.11k | while(width-- > 0) |
785 | 0 | OUTCHAR(' '); |
786 | | |
787 | 1.11k | if(is_neg) |
788 | 0 | OUTCHAR('-'); |
789 | 1.11k | else if(p->flags & FLAGS_SHOWSIGN) |
790 | 0 | OUTCHAR('+'); |
791 | 1.11k | else if(p->flags & FLAGS_SPACE) |
792 | 0 | OUTCHAR(' '); |
793 | | |
794 | 1.11k | if(is_alt && base == 16) { |
795 | 0 | OUTCHAR('0'); |
796 | 0 | if(p->flags & FLAGS_UPPER) |
797 | 0 | OUTCHAR('X'); |
798 | 0 | else |
799 | 0 | OUTCHAR('x'); |
800 | 0 | } |
801 | | |
802 | 1.11k | if(!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL)) |
803 | 0 | while(width-- > 0) |
804 | 0 | OUTCHAR('0'); |
805 | | |
806 | | /* Write the number. */ |
807 | 3.09k | while(++w <= workend) { |
808 | 1.97k | OUTCHAR(*w); |
809 | 1.97k | } |
810 | | |
811 | 1.11k | if(p->flags & FLAGS_LEFT) |
812 | 0 | while(width-- > 0) |
813 | 0 | OUTCHAR(' '); |
814 | 1.11k | break; |
815 | | |
816 | 1.11k | case FORMAT_STRING: |
817 | | /* String. */ |
818 | 0 | { |
819 | 0 | static const char null[] = "(nil)"; |
820 | 0 | const char *str; |
821 | 0 | size_t len; |
822 | |
|
823 | 0 | str = (char *) p->data.str; |
824 | 0 | if(!str) { |
825 | | /* Write null[] if there's space. */ |
826 | 0 | if(prec == -1 || prec >= (long) sizeof(null) - 1) { |
827 | 0 | str = null; |
828 | 0 | len = sizeof(null) - 1; |
829 | | /* Disable quotes around (nil) */ |
830 | 0 | p->flags &= (~FLAGS_ALT); |
831 | 0 | } |
832 | 0 | else { |
833 | 0 | str = ""; |
834 | 0 | len = 0; |
835 | 0 | } |
836 | 0 | } |
837 | 0 | else if(prec != -1) |
838 | 0 | len = (size_t)prec; |
839 | 0 | else if(*str == '\0') |
840 | 0 | len = 0; |
841 | 0 | else |
842 | 0 | len = strlen(str); |
843 | |
|
844 | 0 | width -= (len > LONG_MAX) ? LONG_MAX : (long)len; |
845 | |
|
846 | 0 | if(p->flags & FLAGS_ALT) |
847 | 0 | OUTCHAR('"'); |
848 | | |
849 | 0 | if(!(p->flags&FLAGS_LEFT)) |
850 | 0 | while(width-- > 0) |
851 | 0 | OUTCHAR(' '); |
852 | | |
853 | 0 | for(; len && *str; len--) |
854 | 0 | OUTCHAR(*str++); |
855 | 0 | if(p->flags&FLAGS_LEFT) |
856 | 0 | while(width-- > 0) |
857 | 0 | OUTCHAR(' '); |
858 | | |
859 | 0 | if(p->flags & FLAGS_ALT) |
860 | 0 | OUTCHAR('"'); |
861 | 0 | } |
862 | 0 | break; |
863 | | |
864 | 0 | case FORMAT_PTR: |
865 | | /* Generic pointer. */ |
866 | 0 | { |
867 | 0 | void *ptr; |
868 | 0 | ptr = (void *) p->data.ptr; |
869 | 0 | if(ptr) { |
870 | | /* If the pointer is not NULL, write it as a %#x spec. */ |
871 | 0 | base = 16; |
872 | 0 | digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits; |
873 | 0 | is_alt = 1; |
874 | 0 | num = (size_t) ptr; |
875 | 0 | is_neg = 0; |
876 | 0 | goto number; |
877 | 0 | } |
878 | 0 | else { |
879 | | /* Write "(nil)" for a nil pointer. */ |
880 | 0 | static const char strnil[] = "(nil)"; |
881 | 0 | const char *point; |
882 | |
|
883 | 0 | width -= (long)(sizeof(strnil) - 1); |
884 | 0 | if(p->flags & FLAGS_LEFT) |
885 | 0 | while(width-- > 0) |
886 | 0 | OUTCHAR(' '); |
887 | 0 | for(point = strnil; *point != '\0'; ++point) |
888 | 0 | OUTCHAR(*point); |
889 | 0 | if(!(p->flags & FLAGS_LEFT)) |
890 | 0 | while(width-- > 0) |
891 | 0 | OUTCHAR(' '); |
892 | 0 | } |
893 | 0 | } |
894 | 0 | break; |
895 | | |
896 | 0 | case FORMAT_DOUBLE: |
897 | 0 | { |
898 | 0 | char formatbuf[32]="%"; |
899 | 0 | char *fptr = &formatbuf[1]; |
900 | 0 | size_t left = sizeof(formatbuf)-strlen(formatbuf); |
901 | 0 | int len; |
902 | |
|
903 | 0 | width = -1; |
904 | 0 | if(p->flags & FLAGS_WIDTH) |
905 | 0 | width = p->width; |
906 | 0 | else if(p->flags & FLAGS_WIDTHPARAM) |
907 | 0 | width = (long)vto[p->width].data.num.as_signed; |
908 | |
|
909 | 0 | prec = -1; |
910 | 0 | if(p->flags & FLAGS_PREC) |
911 | 0 | prec = p->precision; |
912 | 0 | else if(p->flags & FLAGS_PRECPARAM) |
913 | 0 | prec = (long)vto[p->precision].data.num.as_signed; |
914 | |
|
915 | 0 | if(p->flags & FLAGS_LEFT) |
916 | 0 | *fptr++ = '-'; |
917 | 0 | if(p->flags & FLAGS_SHOWSIGN) |
918 | 0 | *fptr++ = '+'; |
919 | 0 | if(p->flags & FLAGS_SPACE) |
920 | 0 | *fptr++ = ' '; |
921 | 0 | if(p->flags & FLAGS_ALT) |
922 | 0 | *fptr++ = '#'; |
923 | |
|
924 | 0 | *fptr = 0; |
925 | |
|
926 | 0 | if(width >= 0) { |
927 | 0 | if(width >= (long)sizeof(work)) |
928 | 0 | width = sizeof(work)-1; |
929 | | /* RECURSIVE USAGE */ |
930 | 0 | len = curl_msnprintf(fptr, left, "%ld", width); |
931 | 0 | fptr += len; |
932 | 0 | left -= len; |
933 | 0 | } |
934 | 0 | if(prec >= 0) { |
935 | | /* for each digit in the integer part, we can have one less |
936 | | precision */ |
937 | 0 | size_t maxprec = sizeof(work) - 2; |
938 | 0 | double val = p->data.dnum; |
939 | 0 | if(width > 0 && prec <= width) |
940 | 0 | maxprec -= width; |
941 | 0 | while(val >= 10.0) { |
942 | 0 | val /= 10; |
943 | 0 | maxprec--; |
944 | 0 | } |
945 | |
|
946 | 0 | if(prec > (long)maxprec) |
947 | 0 | prec = (long)maxprec-1; |
948 | 0 | if(prec < 0) |
949 | 0 | prec = 0; |
950 | | /* RECURSIVE USAGE */ |
951 | 0 | len = curl_msnprintf(fptr, left, ".%ld", prec); |
952 | 0 | fptr += len; |
953 | 0 | } |
954 | 0 | if(p->flags & FLAGS_LONG) |
955 | 0 | *fptr++ = 'l'; |
956 | |
|
957 | 0 | if(p->flags & FLAGS_FLOATE) |
958 | 0 | *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'E':'e'); |
959 | 0 | else if(p->flags & FLAGS_FLOATG) |
960 | 0 | *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'G' : 'g'); |
961 | 0 | else |
962 | 0 | *fptr++ = 'f'; |
963 | |
|
964 | 0 | *fptr = 0; /* and a final null-termination */ |
965 | |
|
966 | 0 | #ifdef __clang__ |
967 | 0 | #pragma clang diagnostic push |
968 | 0 | #pragma clang diagnostic ignored "-Wformat-nonliteral" |
969 | 0 | #endif |
970 | | /* NOTE NOTE NOTE!! Not all sprintf implementations return number of |
971 | | output characters */ |
972 | 0 | #ifdef HAVE_SNPRINTF |
973 | 0 | (snprintf)(work, sizeof(work), formatbuf, p->data.dnum); |
974 | | #else |
975 | | (sprintf)(work, formatbuf, p->data.dnum); |
976 | | #endif |
977 | 0 | #ifdef __clang__ |
978 | 0 | #pragma clang diagnostic pop |
979 | 0 | #endif |
980 | 0 | DEBUGASSERT(strlen(work) <= sizeof(work)); |
981 | 0 | for(fptr = work; *fptr; fptr++) |
982 | 0 | OUTCHAR(*fptr); |
983 | 0 | } |
984 | 0 | break; |
985 | | |
986 | 0 | case FORMAT_INTPTR: |
987 | | /* Answer the count of characters written. */ |
988 | 0 | #ifdef HAVE_LONG_LONG_TYPE |
989 | 0 | if(p->flags & FLAGS_LONGLONG) |
990 | 0 | *(LONG_LONG_TYPE *) p->data.ptr = (LONG_LONG_TYPE)done; |
991 | 0 | else |
992 | 0 | #endif |
993 | 0 | if(p->flags & FLAGS_LONG) |
994 | 0 | *(long *) p->data.ptr = (long)done; |
995 | 0 | else if(!(p->flags & FLAGS_SHORT)) |
996 | 0 | *(int *) p->data.ptr = (int)done; |
997 | 0 | else |
998 | 0 | *(short *) p->data.ptr = (short)done; |
999 | 0 | break; |
1000 | | |
1001 | 0 | default: |
1002 | 0 | break; |
1003 | 1.11k | } |
1004 | 1.11k | f = *end++; /* goto end of %-code */ |
1005 | | |
1006 | 1.11k | } |
1007 | 311 | return done; |
1008 | 311 | } |
1009 | | |
1010 | | /* fputc() look-alike */ |
1011 | | static int addbyter(int output, FILE *data) |
1012 | 0 | { |
1013 | 0 | struct nsprintf *infop = (struct nsprintf *)data; |
1014 | 0 | unsigned char outc = (unsigned char)output; |
1015 | |
|
1016 | 0 | if(infop->length < infop->max) { |
1017 | | /* only do this if we haven't reached max length yet */ |
1018 | 0 | infop->buffer[0] = outc; /* store */ |
1019 | 0 | infop->buffer++; /* increase pointer */ |
1020 | 0 | infop->length++; /* we are now one byte larger */ |
1021 | 0 | return outc; /* fputc() returns like this on success */ |
1022 | 0 | } |
1023 | 0 | return -1; |
1024 | 0 | } |
1025 | | |
1026 | | int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format, |
1027 | | va_list ap_save) |
1028 | 0 | { |
1029 | 0 | int retcode; |
1030 | 0 | struct nsprintf info; |
1031 | |
|
1032 | 0 | info.buffer = buffer; |
1033 | 0 | info.length = 0; |
1034 | 0 | info.max = maxlength; |
1035 | |
|
1036 | 0 | retcode = dprintf_formatf(&info, addbyter, format, ap_save); |
1037 | 0 | if(info.max) { |
1038 | | /* we terminate this with a zero byte */ |
1039 | 0 | if(info.max == info.length) { |
1040 | | /* we're at maximum, scrap the last letter */ |
1041 | 0 | info.buffer[-1] = 0; |
1042 | 0 | DEBUGASSERT(retcode); |
1043 | 0 | retcode--; /* don't count the nul byte */ |
1044 | 0 | } |
1045 | 0 | else |
1046 | 0 | info.buffer[0] = 0; |
1047 | 0 | } |
1048 | 0 | return retcode; |
1049 | 0 | } |
1050 | | |
1051 | | int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) |
1052 | 0 | { |
1053 | 0 | int retcode; |
1054 | 0 | va_list ap_save; /* argument pointer */ |
1055 | 0 | va_start(ap_save, format); |
1056 | 0 | retcode = curl_mvsnprintf(buffer, maxlength, format, ap_save); |
1057 | 0 | va_end(ap_save); |
1058 | 0 | return retcode; |
1059 | 0 | } |
1060 | | |
1061 | | /* fputc() look-alike */ |
1062 | | static int alloc_addbyter(int output, FILE *data) |
1063 | 2.78k | { |
1064 | 2.78k | struct asprintf *infop = (struct asprintf *)data; |
1065 | 2.78k | unsigned char outc = (unsigned char)output; |
1066 | | |
1067 | 2.78k | if(Curl_dyn_addn(infop->b, &outc, 1)) { |
1068 | 0 | infop->fail = 1; |
1069 | 0 | return -1; /* fail */ |
1070 | 0 | } |
1071 | 2.78k | return outc; /* fputc() returns like this on success */ |
1072 | 2.78k | } |
1073 | | |
1074 | | extern int Curl_dyn_vprintf(struct dynbuf *dyn, |
1075 | | const char *format, va_list ap_save); |
1076 | | |
1077 | | /* appends the formatted string, returns 0 on success, 1 on error */ |
1078 | | int Curl_dyn_vprintf(struct dynbuf *dyn, const char *format, va_list ap_save) |
1079 | 269 | { |
1080 | 269 | struct asprintf info; |
1081 | 269 | info.b = dyn; |
1082 | 269 | info.fail = 0; |
1083 | | |
1084 | 269 | (void)dprintf_formatf(&info, alloc_addbyter, format, ap_save); |
1085 | 269 | if(info.fail) { |
1086 | 0 | Curl_dyn_free(info.b); |
1087 | 0 | return 1; |
1088 | 0 | } |
1089 | 269 | return 0; |
1090 | 269 | } |
1091 | | |
1092 | | char *curl_mvaprintf(const char *format, va_list ap_save) |
1093 | 42 | { |
1094 | 42 | struct asprintf info; |
1095 | 42 | struct dynbuf dyn; |
1096 | 42 | info.b = &dyn; |
1097 | 42 | Curl_dyn_init(info.b, DYN_APRINTF); |
1098 | 42 | info.fail = 0; |
1099 | | |
1100 | 42 | (void)dprintf_formatf(&info, alloc_addbyter, format, ap_save); |
1101 | 42 | if(info.fail) { |
1102 | 0 | Curl_dyn_free(info.b); |
1103 | 0 | return NULL; |
1104 | 0 | } |
1105 | 42 | if(Curl_dyn_len(info.b)) |
1106 | 42 | return Curl_dyn_ptr(info.b); |
1107 | 0 | return strdup(""); |
1108 | 42 | } |
1109 | | |
1110 | | char *curl_maprintf(const char *format, ...) |
1111 | 42 | { |
1112 | 42 | va_list ap_save; |
1113 | 42 | char *s; |
1114 | 42 | va_start(ap_save, format); |
1115 | 42 | s = curl_mvaprintf(format, ap_save); |
1116 | 42 | va_end(ap_save); |
1117 | 42 | return s; |
1118 | 42 | } |
1119 | | |
1120 | | static int storebuffer(int output, FILE *data) |
1121 | 0 | { |
1122 | 0 | char **buffer = (char **)data; |
1123 | 0 | unsigned char outc = (unsigned char)output; |
1124 | 0 | **buffer = outc; |
1125 | 0 | (*buffer)++; |
1126 | 0 | return outc; /* act like fputc() ! */ |
1127 | 0 | } |
1128 | | |
1129 | | int curl_msprintf(char *buffer, const char *format, ...) |
1130 | 0 | { |
1131 | 0 | va_list ap_save; /* argument pointer */ |
1132 | 0 | int retcode; |
1133 | 0 | va_start(ap_save, format); |
1134 | 0 | retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save); |
1135 | 0 | va_end(ap_save); |
1136 | 0 | *buffer = 0; /* we terminate this with a zero byte */ |
1137 | 0 | return retcode; |
1138 | 0 | } |
1139 | | |
1140 | | int curl_mprintf(const char *format, ...) |
1141 | 0 | { |
1142 | 0 | int retcode; |
1143 | 0 | va_list ap_save; /* argument pointer */ |
1144 | 0 | va_start(ap_save, format); |
1145 | |
|
1146 | 0 | retcode = dprintf_formatf(stdout, fputc, format, ap_save); |
1147 | 0 | va_end(ap_save); |
1148 | 0 | return retcode; |
1149 | 0 | } |
1150 | | |
1151 | | int curl_mfprintf(FILE *whereto, const char *format, ...) |
1152 | 0 | { |
1153 | 0 | int retcode; |
1154 | 0 | va_list ap_save; /* argument pointer */ |
1155 | 0 | va_start(ap_save, format); |
1156 | 0 | retcode = dprintf_formatf(whereto, fputc, format, ap_save); |
1157 | 0 | va_end(ap_save); |
1158 | 0 | return retcode; |
1159 | 0 | } |
1160 | | |
1161 | | int curl_mvsprintf(char *buffer, const char *format, va_list ap_save) |
1162 | 0 | { |
1163 | 0 | int retcode; |
1164 | 0 | retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save); |
1165 | 0 | *buffer = 0; /* we terminate this with a zero byte */ |
1166 | 0 | return retcode; |
1167 | 0 | } |
1168 | | |
1169 | | int curl_mvprintf(const char *format, va_list ap_save) |
1170 | 0 | { |
1171 | 0 | return dprintf_formatf(stdout, fputc, format, ap_save); |
1172 | 0 | } |
1173 | | |
1174 | | int curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save) |
1175 | 0 | { |
1176 | 0 | return dprintf_formatf(whereto, fputc, format, ap_save); |
1177 | 0 | } |