/src/PROJ/curl/lib/mprintf.c
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 | | |
25 | | #include "curl_setup.h" |
26 | | #include "curlx/dynbuf.h" |
27 | | #include "curl_printf.h" |
28 | | #include "curlx/strparse.h" |
29 | | |
30 | | #include "curl_memory.h" |
31 | | /* The last #include file should be: */ |
32 | | #include "memdebug.h" |
33 | | |
34 | | #ifdef HAVE_LONGLONG |
35 | 0 | # define LONG_LONG_TYPE long long |
36 | | # define HAVE_LONG_LONG_TYPE |
37 | | #elif defined(_MSC_VER) |
38 | | # define LONG_LONG_TYPE __int64 |
39 | | # define HAVE_LONG_LONG_TYPE |
40 | | #else |
41 | | # undef LONG_LONG_TYPE |
42 | | # undef HAVE_LONG_LONG_TYPE |
43 | | #endif |
44 | | |
45 | | /* |
46 | | * Max integer data types that mprintf.c is capable |
47 | | */ |
48 | | |
49 | | #ifdef HAVE_LONG_LONG_TYPE |
50 | 0 | # define mp_intmax_t LONG_LONG_TYPE |
51 | | # define mp_uintmax_t unsigned LONG_LONG_TYPE |
52 | | #else |
53 | | # define mp_intmax_t long |
54 | | # define mp_uintmax_t unsigned long |
55 | | #endif |
56 | | |
57 | 0 | #define BUFFSIZE 326 /* buffer for long-to-str and float-to-str calcs, should |
58 | | fit negative DBL_MAX (317 letters) */ |
59 | 0 | #define MAX_PARAMETERS 128 /* number of input arguments */ |
60 | 0 | #define MAX_SEGMENTS 128 /* number of output segments */ |
61 | | |
62 | | #ifdef __AMIGA__ |
63 | | # undef FORMAT_INT |
64 | | #endif |
65 | | |
66 | | /* Lower-case digits. */ |
67 | | const unsigned char Curl_ldigits[] = "0123456789abcdef"; |
68 | | |
69 | | /* Upper-case digits. */ |
70 | | const unsigned char Curl_udigits[] = "0123456789ABCDEF"; |
71 | | |
72 | | #define OUTCHAR(x) \ |
73 | 0 | do { \ |
74 | 0 | if(stream((unsigned char)x, userp)) \ |
75 | 0 | return TRUE; \ |
76 | 0 | (*donep)++; \ |
77 | 0 | } while(0) |
78 | | |
79 | | /* Data type to read from the arglist */ |
80 | | typedef enum { |
81 | | FORMAT_STRING, |
82 | | FORMAT_PTR, |
83 | | FORMAT_INTPTR, |
84 | | FORMAT_INT, |
85 | | FORMAT_LONG, |
86 | | FORMAT_LONGLONG, |
87 | | FORMAT_INTU, |
88 | | FORMAT_LONGU, |
89 | | FORMAT_LONGLONGU, |
90 | | FORMAT_DOUBLE, |
91 | | FORMAT_LONGDOUBLE, |
92 | | FORMAT_WIDTH, |
93 | | FORMAT_PRECISION |
94 | | } FormatType; |
95 | | |
96 | | /* conversion and display flags */ |
97 | | enum { |
98 | | FLAGS_SPACE = 1 << 0, |
99 | | FLAGS_SHOWSIGN = 1 << 1, |
100 | | FLAGS_LEFT = 1 << 2, |
101 | | FLAGS_ALT = 1 << 3, |
102 | | FLAGS_SHORT = 1 << 4, |
103 | | FLAGS_LONG = 1 << 5, |
104 | | FLAGS_LONGLONG = 1 << 6, |
105 | | FLAGS_LONGDOUBLE = 1 << 7, |
106 | | FLAGS_PAD_NIL = 1 << 8, |
107 | | FLAGS_UNSIGNED = 1 << 9, |
108 | | FLAGS_OCTAL = 1 << 10, |
109 | | FLAGS_HEX = 1 << 11, |
110 | | FLAGS_UPPER = 1 << 12, |
111 | | FLAGS_WIDTH = 1 << 13, /* '*' or '*<num>$' used */ |
112 | | FLAGS_WIDTHPARAM = 1 << 14, /* width PARAMETER was specified */ |
113 | | FLAGS_PREC = 1 << 15, /* precision was specified */ |
114 | | FLAGS_PRECPARAM = 1 << 16, /* precision PARAMETER was specified */ |
115 | | FLAGS_CHAR = 1 << 17, /* %c story */ |
116 | | FLAGS_FLOATE = 1 << 18, /* %e or %E */ |
117 | | FLAGS_FLOATG = 1 << 19, /* %g or %G */ |
118 | | FLAGS_SUBSTR = 1 << 20 /* no input, only substring */ |
119 | | }; |
120 | | |
121 | | enum { |
122 | | DOLLAR_UNKNOWN, |
123 | | DOLLAR_NOPE, |
124 | | DOLLAR_USE |
125 | | }; |
126 | | |
127 | | /* |
128 | | * Describes an input va_arg type and hold its value. |
129 | | */ |
130 | | struct va_input { |
131 | | FormatType type; /* FormatType */ |
132 | | union { |
133 | | const char *str; |
134 | | void *ptr; |
135 | | mp_intmax_t nums; /* signed */ |
136 | | mp_uintmax_t numu; /* unsigned */ |
137 | | double dnum; |
138 | | } val; |
139 | | }; |
140 | | |
141 | | /* |
142 | | * Describes an output segment. |
143 | | */ |
144 | | struct outsegment { |
145 | | int width; /* width OR width parameter number */ |
146 | | int precision; /* precision OR precision parameter number */ |
147 | | unsigned int flags; |
148 | | unsigned int input; /* input argument array index */ |
149 | | const char *start; /* format string start to output */ |
150 | | size_t outlen; /* number of bytes from the format string to output */ |
151 | | }; |
152 | | |
153 | | struct nsprintf { |
154 | | char *buffer; |
155 | | size_t length; |
156 | | size_t max; |
157 | | }; |
158 | | |
159 | | struct asprintf { |
160 | | struct dynbuf *b; |
161 | | char merr; |
162 | | }; |
163 | | |
164 | | /* the provided input number is 1-based but this returns the number 0-based. |
165 | | |
166 | | returns -1 if no valid number was provided. |
167 | | */ |
168 | | static int dollarstring(const char *p, const char **end) |
169 | 0 | { |
170 | 0 | curl_off_t num; |
171 | 0 | if(curlx_str_number(&p, &num, MAX_PARAMETERS) || |
172 | 0 | curlx_str_single(&p, '$') || !num) |
173 | 0 | return -1; |
174 | 0 | *end = p; |
175 | 0 | return (int)num - 1; |
176 | 0 | } |
177 | | |
178 | 0 | #define is_arg_used(x,y) ((x)[(y)/8] & (1 << ((y)&7))) |
179 | 0 | #define mark_arg_used(x,y) ((x)[y/8] |= (unsigned char)(1 << ((y)&7))) |
180 | | |
181 | | /* |
182 | | * Parse the format string. |
183 | | * |
184 | | * Create two arrays. One describes the inputs, one describes the outputs. |
185 | | * |
186 | | * Returns zero on success. |
187 | | */ |
188 | | |
189 | 0 | #define PFMT_OK 0 |
190 | 0 | #define PFMT_DOLLAR 1 /* bad dollar for main param */ |
191 | 0 | #define PFMT_DOLLARWIDTH 2 /* bad dollar use for width */ |
192 | 0 | #define PFMT_DOLLARPREC 3 /* bad dollar use for precision */ |
193 | 0 | #define PFMT_MANYARGS 4 /* too many input arguments used */ |
194 | 0 | #define PFMT_PREC 5 /* precision overflow */ |
195 | 0 | #define PFMT_PRECMIX 6 /* bad mix of precision specifiers */ |
196 | 0 | #define PFMT_WIDTH 7 /* width overflow */ |
197 | 0 | #define PFMT_INPUTGAP 8 /* gap in arguments */ |
198 | 0 | #define PFMT_WIDTHARG 9 /* attempted to use same arg twice, for width */ |
199 | 0 | #define PFMT_PRECARG 10 /* attempted to use same arg twice, for prec */ |
200 | 0 | #define PFMT_MANYSEGS 11 /* maxed out output segments */ |
201 | | |
202 | | static int parsefmt(const char *format, |
203 | | struct outsegment *out, |
204 | | struct va_input *in, |
205 | | int *opieces, |
206 | | int *ipieces, va_list arglist) |
207 | 0 | { |
208 | 0 | const char *fmt = format; |
209 | 0 | int param_num = 0; |
210 | 0 | int max_param = -1; |
211 | 0 | int i; |
212 | 0 | int ocount = 0; |
213 | 0 | unsigned char usedinput[MAX_PARAMETERS/8]; |
214 | 0 | size_t outlen = 0; |
215 | 0 | struct outsegment *optr; |
216 | 0 | int use_dollar = DOLLAR_UNKNOWN; |
217 | 0 | const char *start = fmt; |
218 | | |
219 | | /* clear, set a bit for each used input */ |
220 | 0 | memset(usedinput, 0, sizeof(usedinput)); |
221 | |
|
222 | 0 | while(*fmt) { |
223 | 0 | if(*fmt == '%') { |
224 | 0 | struct va_input *iptr; |
225 | 0 | bool loopit = TRUE; |
226 | 0 | FormatType type; |
227 | 0 | unsigned int flags = 0; |
228 | 0 | int width = 0; |
229 | 0 | int precision = 0; |
230 | 0 | int param = -1; |
231 | 0 | fmt++; |
232 | 0 | outlen = (size_t)(fmt - start - 1); |
233 | 0 | if(*fmt == '%') { |
234 | | /* this means a %% that should be output only as %. Create an output |
235 | | segment. */ |
236 | 0 | if(outlen) { |
237 | 0 | optr = &out[ocount++]; |
238 | 0 | if(ocount > MAX_SEGMENTS) |
239 | 0 | return PFMT_MANYSEGS; |
240 | 0 | optr->input = 0; |
241 | 0 | optr->flags = FLAGS_SUBSTR; |
242 | 0 | optr->start = start; |
243 | 0 | optr->outlen = outlen; |
244 | 0 | } |
245 | 0 | start = fmt; |
246 | 0 | fmt++; |
247 | 0 | continue; /* while */ |
248 | 0 | } |
249 | | |
250 | 0 | if(use_dollar != DOLLAR_NOPE) { |
251 | 0 | param = dollarstring(fmt, &fmt); |
252 | 0 | if(param < 0) { |
253 | 0 | if(use_dollar == DOLLAR_USE) |
254 | | /* illegal combo */ |
255 | 0 | return PFMT_DOLLAR; |
256 | | |
257 | | /* we got no positional, just get the next arg */ |
258 | 0 | param = -1; |
259 | 0 | use_dollar = DOLLAR_NOPE; |
260 | 0 | } |
261 | 0 | else |
262 | 0 | use_dollar = DOLLAR_USE; |
263 | 0 | } |
264 | | |
265 | | /* Handle the flags */ |
266 | 0 | while(loopit) { |
267 | 0 | switch(*fmt++) { |
268 | 0 | case ' ': |
269 | 0 | flags |= FLAGS_SPACE; |
270 | 0 | break; |
271 | 0 | case '+': |
272 | 0 | flags |= FLAGS_SHOWSIGN; |
273 | 0 | break; |
274 | 0 | case '-': |
275 | 0 | flags |= FLAGS_LEFT; |
276 | 0 | flags &= ~(unsigned int)FLAGS_PAD_NIL; |
277 | 0 | break; |
278 | 0 | case '#': |
279 | 0 | flags |= FLAGS_ALT; |
280 | 0 | break; |
281 | 0 | case '.': |
282 | 0 | if('*' == *fmt) { |
283 | | /* The precision is picked from a specified parameter */ |
284 | 0 | flags |= FLAGS_PRECPARAM; |
285 | 0 | fmt++; |
286 | |
|
287 | 0 | if(use_dollar == DOLLAR_USE) { |
288 | 0 | precision = dollarstring(fmt, &fmt); |
289 | 0 | if(precision < 0) |
290 | | /* illegal combo */ |
291 | 0 | return PFMT_DOLLARPREC; |
292 | 0 | } |
293 | 0 | else |
294 | | /* get it from the next argument */ |
295 | 0 | precision = -1; |
296 | 0 | } |
297 | 0 | else { |
298 | 0 | bool is_neg; |
299 | 0 | curl_off_t num; |
300 | 0 | flags |= FLAGS_PREC; |
301 | 0 | is_neg = ('-' == *fmt); |
302 | 0 | if(is_neg) |
303 | 0 | fmt++; |
304 | 0 | if(curlx_str_number(&fmt, &num, INT_MAX)) |
305 | 0 | return PFMT_PREC; |
306 | 0 | precision = (int)num; |
307 | 0 | if(is_neg) |
308 | 0 | precision = -precision; |
309 | 0 | } |
310 | 0 | if((flags & (FLAGS_PREC | FLAGS_PRECPARAM)) == |
311 | 0 | (FLAGS_PREC | FLAGS_PRECPARAM)) |
312 | | /* it is not permitted to use both kinds of precision for the same |
313 | | argument */ |
314 | 0 | return PFMT_PRECMIX; |
315 | 0 | break; |
316 | 0 | case 'h': |
317 | 0 | flags |= FLAGS_SHORT; |
318 | 0 | break; |
319 | | #ifdef _WIN32 |
320 | | case 'I': |
321 | | /* Non-ANSI integer extensions I32 I64 */ |
322 | | if((fmt[0] == '3') && (fmt[1] == '2')) { |
323 | | flags |= FLAGS_LONG; |
324 | | fmt += 2; |
325 | | } |
326 | | else if((fmt[0] == '6') && (fmt[1] == '4')) { |
327 | | flags |= FLAGS_LONGLONG; |
328 | | fmt += 2; |
329 | | } |
330 | | else { |
331 | | #if (SIZEOF_CURL_OFF_T > SIZEOF_LONG) |
332 | | flags |= FLAGS_LONGLONG; |
333 | | #else |
334 | | flags |= FLAGS_LONG; |
335 | | #endif |
336 | | } |
337 | | break; |
338 | | #endif /* _WIN32 */ |
339 | 0 | case 'l': |
340 | 0 | if(flags & FLAGS_LONG) |
341 | 0 | flags |= FLAGS_LONGLONG; |
342 | 0 | else |
343 | 0 | flags |= FLAGS_LONG; |
344 | 0 | 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 | 0 | 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 | curl_off_t num; |
374 | 0 | flags |= FLAGS_WIDTH; |
375 | 0 | fmt--; |
376 | 0 | if(curlx_str_number(&fmt, &num, INT_MAX)) |
377 | 0 | return PFMT_WIDTH; |
378 | 0 | width = (int)num; |
379 | 0 | break; |
380 | 0 | } |
381 | 0 | case '*': /* read width from argument list */ |
382 | 0 | flags |= FLAGS_WIDTHPARAM; |
383 | 0 | if(use_dollar == DOLLAR_USE) { |
384 | 0 | width = dollarstring(fmt, &fmt); |
385 | 0 | if(width < 0) |
386 | | /* illegal combo */ |
387 | 0 | return PFMT_DOLLARWIDTH; |
388 | 0 | } |
389 | 0 | else |
390 | | /* pick from the next argument */ |
391 | 0 | width = -1; |
392 | 0 | break; |
393 | 0 | default: |
394 | 0 | loopit = FALSE; |
395 | 0 | fmt--; |
396 | 0 | break; |
397 | 0 | } /* switch */ |
398 | 0 | } /* while */ |
399 | | |
400 | 0 | switch(*fmt) { |
401 | 0 | case 'S': |
402 | 0 | flags |= FLAGS_ALT; |
403 | 0 | FALLTHROUGH(); |
404 | 0 | case 's': |
405 | 0 | type = FORMAT_STRING; |
406 | 0 | break; |
407 | 0 | case 'n': |
408 | 0 | type = FORMAT_INTPTR; |
409 | 0 | break; |
410 | 0 | case 'p': |
411 | 0 | type = FORMAT_PTR; |
412 | 0 | break; |
413 | 0 | case 'd': |
414 | 0 | case 'i': |
415 | 0 | if(flags & FLAGS_LONGLONG) |
416 | 0 | type = FORMAT_LONGLONG; |
417 | 0 | else if(flags & FLAGS_LONG) |
418 | 0 | type = FORMAT_LONG; |
419 | 0 | else |
420 | 0 | type = FORMAT_INT; |
421 | 0 | break; |
422 | 0 | case 'u': |
423 | 0 | if(flags & FLAGS_LONGLONG) |
424 | 0 | type = FORMAT_LONGLONGU; |
425 | 0 | else if(flags & FLAGS_LONG) |
426 | 0 | type = FORMAT_LONGU; |
427 | 0 | else |
428 | 0 | type = FORMAT_INTU; |
429 | 0 | flags |= FLAGS_UNSIGNED; |
430 | 0 | break; |
431 | 0 | case 'o': |
432 | 0 | if(flags & FLAGS_LONGLONG) |
433 | 0 | type = FORMAT_LONGLONGU; |
434 | 0 | else if(flags & FLAGS_LONG) |
435 | 0 | type = FORMAT_LONGU; |
436 | 0 | else |
437 | 0 | type = FORMAT_INTU; |
438 | 0 | flags |= FLAGS_OCTAL|FLAGS_UNSIGNED; |
439 | 0 | break; |
440 | 0 | case 'x': |
441 | 0 | if(flags & FLAGS_LONGLONG) |
442 | 0 | type = FORMAT_LONGLONGU; |
443 | 0 | else if(flags & FLAGS_LONG) |
444 | 0 | type = FORMAT_LONGU; |
445 | 0 | else |
446 | 0 | type = FORMAT_INTU; |
447 | 0 | flags |= FLAGS_HEX|FLAGS_UNSIGNED; |
448 | 0 | break; |
449 | 0 | case 'X': |
450 | 0 | if(flags & FLAGS_LONGLONG) |
451 | 0 | type = FORMAT_LONGLONGU; |
452 | 0 | else if(flags & FLAGS_LONG) |
453 | 0 | type = FORMAT_LONGU; |
454 | 0 | else |
455 | 0 | type = FORMAT_INTU; |
456 | 0 | flags |= FLAGS_HEX|FLAGS_UPPER|FLAGS_UNSIGNED; |
457 | 0 | break; |
458 | 0 | case 'c': |
459 | 0 | type = FORMAT_INT; |
460 | 0 | flags |= FLAGS_CHAR; |
461 | 0 | break; |
462 | 0 | case 'f': |
463 | 0 | type = FORMAT_DOUBLE; |
464 | 0 | break; |
465 | 0 | case 'e': |
466 | 0 | type = FORMAT_DOUBLE; |
467 | 0 | flags |= FLAGS_FLOATE; |
468 | 0 | break; |
469 | 0 | case 'E': |
470 | 0 | type = FORMAT_DOUBLE; |
471 | 0 | flags |= FLAGS_FLOATE|FLAGS_UPPER; |
472 | 0 | break; |
473 | 0 | case 'g': |
474 | 0 | type = FORMAT_DOUBLE; |
475 | 0 | flags |= FLAGS_FLOATG; |
476 | 0 | break; |
477 | 0 | case 'G': |
478 | 0 | type = FORMAT_DOUBLE; |
479 | 0 | flags |= FLAGS_FLOATG|FLAGS_UPPER; |
480 | 0 | break; |
481 | 0 | default: |
482 | | /* invalid instruction, disregard and continue */ |
483 | 0 | continue; |
484 | 0 | } /* switch */ |
485 | | |
486 | 0 | if(flags & FLAGS_WIDTHPARAM) { |
487 | 0 | if(width < 0) |
488 | 0 | width = param_num++; |
489 | 0 | else { |
490 | | /* if this identifies a parameter already used, this is illegal */ |
491 | 0 | if(is_arg_used(usedinput, width)) |
492 | 0 | return PFMT_WIDTHARG; |
493 | 0 | } |
494 | 0 | if(width >= MAX_PARAMETERS) |
495 | 0 | return PFMT_MANYARGS; |
496 | 0 | if(width >= max_param) |
497 | 0 | max_param = width; |
498 | |
|
499 | 0 | in[width].type = FORMAT_WIDTH; |
500 | | /* mark as used */ |
501 | 0 | mark_arg_used(usedinput, width); |
502 | 0 | } |
503 | | |
504 | 0 | if(flags & FLAGS_PRECPARAM) { |
505 | 0 | if(precision < 0) |
506 | 0 | precision = param_num++; |
507 | 0 | else { |
508 | | /* if this identifies a parameter already used, this is illegal */ |
509 | 0 | if(is_arg_used(usedinput, precision)) |
510 | 0 | return PFMT_PRECARG; |
511 | 0 | } |
512 | 0 | if(precision >= MAX_PARAMETERS) |
513 | 0 | return PFMT_MANYARGS; |
514 | 0 | if(precision >= max_param) |
515 | 0 | max_param = precision; |
516 | |
|
517 | 0 | in[precision].type = FORMAT_PRECISION; |
518 | 0 | mark_arg_used(usedinput, precision); |
519 | 0 | } |
520 | | |
521 | | /* Handle the specifier */ |
522 | 0 | if(param < 0) |
523 | 0 | param = param_num++; |
524 | 0 | if(param >= MAX_PARAMETERS) |
525 | 0 | return PFMT_MANYARGS; |
526 | 0 | if(param >= max_param) |
527 | 0 | max_param = param; |
528 | |
|
529 | 0 | iptr = &in[param]; |
530 | 0 | iptr->type = type; |
531 | | |
532 | | /* mark this input as used */ |
533 | 0 | mark_arg_used(usedinput, param); |
534 | |
|
535 | 0 | fmt++; |
536 | 0 | optr = &out[ocount++]; |
537 | 0 | if(ocount > MAX_SEGMENTS) |
538 | 0 | return PFMT_MANYSEGS; |
539 | 0 | optr->input = (unsigned int)param; |
540 | 0 | optr->flags = flags; |
541 | 0 | optr->width = width; |
542 | 0 | optr->precision = precision; |
543 | 0 | optr->start = start; |
544 | 0 | optr->outlen = outlen; |
545 | 0 | start = fmt; |
546 | 0 | } |
547 | 0 | else |
548 | 0 | fmt++; |
549 | 0 | } |
550 | | |
551 | | /* is there a trailing piece */ |
552 | 0 | outlen = (size_t)(fmt - start); |
553 | 0 | if(outlen) { |
554 | 0 | optr = &out[ocount++]; |
555 | 0 | if(ocount > MAX_SEGMENTS) |
556 | 0 | return PFMT_MANYSEGS; |
557 | 0 | optr->input = 0; |
558 | 0 | optr->flags = FLAGS_SUBSTR; |
559 | 0 | optr->start = start; |
560 | 0 | optr->outlen = outlen; |
561 | 0 | } |
562 | | |
563 | | /* Read the arg list parameters into our data list */ |
564 | 0 | for(i = 0; i < max_param + 1; i++) { |
565 | 0 | struct va_input *iptr = &in[i]; |
566 | 0 | if(!is_arg_used(usedinput, i)) |
567 | | /* bad input */ |
568 | 0 | return PFMT_INPUTGAP; |
569 | | |
570 | | /* based on the type, read the correct argument */ |
571 | 0 | switch(iptr->type) { |
572 | 0 | case FORMAT_STRING: |
573 | 0 | iptr->val.str = va_arg(arglist, const char *); |
574 | 0 | break; |
575 | | |
576 | 0 | case FORMAT_INTPTR: |
577 | 0 | case FORMAT_PTR: |
578 | 0 | iptr->val.ptr = va_arg(arglist, void *); |
579 | 0 | break; |
580 | | |
581 | 0 | case FORMAT_LONGLONGU: |
582 | 0 | iptr->val.numu = (mp_uintmax_t)va_arg(arglist, mp_uintmax_t); |
583 | 0 | break; |
584 | | |
585 | 0 | case FORMAT_LONGLONG: |
586 | 0 | iptr->val.nums = (mp_intmax_t)va_arg(arglist, mp_intmax_t); |
587 | 0 | break; |
588 | | |
589 | 0 | case FORMAT_LONGU: |
590 | 0 | iptr->val.numu = (mp_uintmax_t)va_arg(arglist, unsigned long); |
591 | 0 | break; |
592 | | |
593 | 0 | case FORMAT_LONG: |
594 | 0 | iptr->val.nums = (mp_intmax_t)va_arg(arglist, long); |
595 | 0 | break; |
596 | | |
597 | 0 | case FORMAT_INTU: |
598 | 0 | iptr->val.numu = (mp_uintmax_t)va_arg(arglist, unsigned int); |
599 | 0 | break; |
600 | | |
601 | 0 | case FORMAT_INT: |
602 | 0 | case FORMAT_WIDTH: |
603 | 0 | case FORMAT_PRECISION: |
604 | 0 | iptr->val.nums = (mp_intmax_t)va_arg(arglist, int); |
605 | 0 | break; |
606 | | |
607 | 0 | case FORMAT_DOUBLE: |
608 | 0 | iptr->val.dnum = va_arg(arglist, double); |
609 | 0 | break; |
610 | | |
611 | 0 | default: |
612 | 0 | DEBUGASSERT(NULL); /* unexpected */ |
613 | 0 | break; |
614 | 0 | } |
615 | 0 | } |
616 | 0 | *ipieces = max_param + 1; |
617 | 0 | *opieces = ocount; |
618 | |
|
619 | 0 | return PFMT_OK; |
620 | 0 | } |
621 | | |
622 | | struct mproperty { |
623 | | int width; /* Width of a field. */ |
624 | | int prec; /* Precision of a field. */ |
625 | | unsigned int flags; |
626 | | }; |
627 | | |
628 | | static bool out_double(void *userp, |
629 | | int (*stream)(unsigned char, void *), |
630 | | struct mproperty *p, |
631 | | double dnum, |
632 | | char *work, int *donep) |
633 | 0 | { |
634 | 0 | char formatbuf[32]="%"; |
635 | 0 | char *fptr = &formatbuf[1]; |
636 | 0 | size_t left = sizeof(formatbuf)-strlen(formatbuf); |
637 | 0 | int flags = p->flags; |
638 | 0 | int width = p->width; |
639 | 0 | int prec = p->prec; |
640 | |
|
641 | 0 | if(flags & FLAGS_LEFT) |
642 | 0 | *fptr++ = '-'; |
643 | 0 | if(flags & FLAGS_SHOWSIGN) |
644 | 0 | *fptr++ = '+'; |
645 | 0 | if(flags & FLAGS_SPACE) |
646 | 0 | *fptr++ = ' '; |
647 | 0 | if(flags & FLAGS_ALT) |
648 | 0 | *fptr++ = '#'; |
649 | |
|
650 | 0 | *fptr = 0; |
651 | |
|
652 | 0 | if(width >= 0) { |
653 | 0 | size_t dlen; |
654 | 0 | if(width >= BUFFSIZE) |
655 | 0 | width = BUFFSIZE - 1; |
656 | | /* RECURSIVE USAGE */ |
657 | 0 | dlen = (size_t)curl_msnprintf(fptr, left, "%d", width); |
658 | 0 | fptr += dlen; |
659 | 0 | left -= dlen; |
660 | 0 | } |
661 | 0 | if(prec >= 0) { |
662 | | /* for each digit in the integer part, we can have one less |
663 | | precision */ |
664 | 0 | int maxprec = BUFFSIZE - 1; |
665 | 0 | double val = dnum; |
666 | 0 | int len; |
667 | 0 | if(prec > maxprec) |
668 | 0 | prec = maxprec - 1; |
669 | 0 | if(width > 0 && prec <= width) |
670 | 0 | maxprec -= width; |
671 | 0 | while(val >= 10.0) { |
672 | 0 | val /= 10; |
673 | 0 | maxprec--; |
674 | 0 | } |
675 | |
|
676 | 0 | if(prec > maxprec) |
677 | 0 | prec = maxprec - 1; |
678 | 0 | if(prec < 0) |
679 | 0 | prec = 0; |
680 | | /* RECURSIVE USAGE */ |
681 | 0 | len = curl_msnprintf(fptr, left, ".%d", prec); |
682 | 0 | fptr += len; |
683 | 0 | } |
684 | 0 | if(flags & FLAGS_LONG) |
685 | 0 | *fptr++ = 'l'; |
686 | |
|
687 | 0 | if(flags & FLAGS_FLOATE) |
688 | 0 | *fptr++ = (char)((flags & FLAGS_UPPER) ? 'E' : 'e'); |
689 | 0 | else if(flags & FLAGS_FLOATG) |
690 | 0 | *fptr++ = (char)((flags & FLAGS_UPPER) ? 'G' : 'g'); |
691 | 0 | else |
692 | 0 | *fptr++ = 'f'; |
693 | |
|
694 | 0 | *fptr = 0; /* and a final null-termination */ |
695 | |
|
696 | 0 | #ifdef __clang__ |
697 | 0 | #pragma clang diagnostic push |
698 | 0 | #pragma clang diagnostic ignored "-Wformat-nonliteral" |
699 | 0 | #endif |
700 | | /* NOTE NOTE NOTE!! Not all sprintf implementations return number of |
701 | | output characters */ |
702 | 0 | #ifdef HAVE_SNPRINTF |
703 | | /* !checksrc! disable LONGLINE */ |
704 | | /* NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) */ |
705 | 0 | (snprintf)(work, BUFFSIZE, formatbuf, dnum); |
706 | | #ifdef _WIN32 |
707 | | /* Old versions of the Windows CRT do not terminate the snprintf output |
708 | | buffer if it reaches the max size so we do that here. */ |
709 | | work[BUFFSIZE - 1] = 0; |
710 | | #endif |
711 | | #else |
712 | | (sprintf)(work, formatbuf, dnum); |
713 | | #endif |
714 | 0 | #ifdef __clang__ |
715 | 0 | #pragma clang diagnostic pop |
716 | 0 | #endif |
717 | 0 | DEBUGASSERT(strlen(work) < BUFFSIZE); |
718 | 0 | while(*work) { |
719 | 0 | if(stream(*work++, userp)) |
720 | 0 | return TRUE; |
721 | 0 | (*donep)++; |
722 | 0 | } |
723 | 0 | return 0; |
724 | 0 | } |
725 | | |
726 | | static bool out_number(void *userp, |
727 | | int (*stream)(unsigned char, void *), |
728 | | struct mproperty *p, |
729 | | mp_uintmax_t num, |
730 | | mp_intmax_t nums, |
731 | | char *work, int *donep) |
732 | 0 | { |
733 | 0 | const unsigned char *digits = Curl_ldigits; |
734 | 0 | int flags = p->flags; |
735 | 0 | int width = p->width; |
736 | 0 | int prec = p->prec; |
737 | 0 | bool is_alt = flags & FLAGS_ALT; |
738 | 0 | bool is_neg = FALSE; |
739 | 0 | int base = 10; |
740 | | |
741 | | /* 'workend' points to the final buffer byte position, but with an extra |
742 | | byte as margin to avoid the (FALSE?) warning Coverity gives us |
743 | | otherwise */ |
744 | 0 | char *workend = &work[BUFFSIZE - 2]; |
745 | 0 | char *w; |
746 | |
|
747 | 0 | if(flags & FLAGS_CHAR) { |
748 | | /* Character. */ |
749 | 0 | if(!(flags & FLAGS_LEFT)) |
750 | 0 | while(--width > 0) |
751 | 0 | OUTCHAR(' '); |
752 | 0 | OUTCHAR((char) num); |
753 | 0 | if(flags & FLAGS_LEFT) |
754 | 0 | while(--width > 0) |
755 | 0 | OUTCHAR(' '); |
756 | 0 | return FALSE; |
757 | 0 | } |
758 | 0 | if(flags & FLAGS_OCTAL) |
759 | | /* Octal unsigned integer */ |
760 | 0 | base = 8; |
761 | | |
762 | 0 | else if(flags & FLAGS_HEX) { |
763 | | /* Hexadecimal unsigned integer */ |
764 | 0 | digits = (flags & FLAGS_UPPER) ? Curl_udigits : Curl_ldigits; |
765 | 0 | base = 16; |
766 | 0 | } |
767 | 0 | else if(flags & FLAGS_UNSIGNED) |
768 | | /* Decimal unsigned integer */ |
769 | 0 | ; |
770 | | |
771 | 0 | else { |
772 | | /* Decimal integer. */ |
773 | 0 | is_neg = (nums < 0); |
774 | 0 | if(is_neg) { |
775 | | /* signed_num might fail to hold absolute negative minimum by 1 */ |
776 | 0 | mp_intmax_t signed_num; /* Used to convert negative in positive. */ |
777 | 0 | signed_num = nums + (mp_intmax_t)1; |
778 | 0 | signed_num = -signed_num; |
779 | 0 | num = (mp_uintmax_t)signed_num; |
780 | 0 | num += (mp_uintmax_t)1; |
781 | 0 | } |
782 | 0 | } |
783 | | |
784 | | /* Supply a default precision if none was given. */ |
785 | 0 | if(prec == -1) |
786 | 0 | prec = 1; |
787 | | |
788 | | /* Put the number in WORK. */ |
789 | 0 | w = workend; |
790 | 0 | DEBUGASSERT(base <= 16); |
791 | 0 | switch(base) { |
792 | 0 | case 10: |
793 | 0 | while(num > 0) { |
794 | 0 | *w-- = (char)('0' + (num % 10)); |
795 | 0 | num /= 10; |
796 | 0 | } |
797 | 0 | break; |
798 | 0 | default: |
799 | 0 | while(num > 0) { |
800 | 0 | *w-- = digits[num % base]; |
801 | 0 | num /= base; |
802 | 0 | } |
803 | 0 | break; |
804 | 0 | } |
805 | 0 | width -= (int)(workend - w); |
806 | 0 | prec -= (int)(workend - w); |
807 | |
|
808 | 0 | if(is_alt && base == 8 && prec <= 0) { |
809 | 0 | *w-- = '0'; |
810 | 0 | --width; |
811 | 0 | } |
812 | |
|
813 | 0 | if(prec > 0) { |
814 | 0 | width -= prec; |
815 | 0 | while(prec-- > 0 && w >= work) |
816 | 0 | *w-- = '0'; |
817 | 0 | } |
818 | |
|
819 | 0 | if(is_alt && base == 16) |
820 | 0 | width -= 2; |
821 | |
|
822 | 0 | if(is_neg || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE)) |
823 | 0 | --width; |
824 | |
|
825 | 0 | if(!(flags & FLAGS_LEFT) && !(flags & FLAGS_PAD_NIL)) |
826 | 0 | while(width-- > 0) |
827 | 0 | OUTCHAR(' '); |
828 | | |
829 | 0 | if(is_neg) |
830 | 0 | OUTCHAR('-'); |
831 | 0 | else if(flags & FLAGS_SHOWSIGN) |
832 | 0 | OUTCHAR('+'); |
833 | 0 | else if(flags & FLAGS_SPACE) |
834 | 0 | OUTCHAR(' '); |
835 | | |
836 | 0 | if(is_alt && base == 16) { |
837 | 0 | OUTCHAR('0'); |
838 | 0 | if(flags & FLAGS_UPPER) |
839 | 0 | OUTCHAR('X'); |
840 | 0 | else |
841 | 0 | OUTCHAR('x'); |
842 | 0 | } |
843 | | |
844 | 0 | if(!(flags & FLAGS_LEFT) && (flags & FLAGS_PAD_NIL)) |
845 | 0 | while(width-- > 0) |
846 | 0 | OUTCHAR('0'); |
847 | | |
848 | | /* Write the number. */ |
849 | 0 | while(++w <= workend) { |
850 | 0 | OUTCHAR(*w); |
851 | 0 | } |
852 | | |
853 | 0 | if(flags & FLAGS_LEFT) |
854 | 0 | while(width-- > 0) |
855 | 0 | OUTCHAR(' '); |
856 | | |
857 | 0 | return FALSE; |
858 | 0 | } |
859 | | |
860 | | static const char nilstr[] = "(nil)"; |
861 | | |
862 | | static bool out_string(void *userp, |
863 | | int (*stream)(unsigned char, void *), |
864 | | struct mproperty *p, |
865 | | const char *str, |
866 | | int *donep) |
867 | 0 | { |
868 | 0 | int flags = p->flags; |
869 | 0 | int width = p->width; |
870 | 0 | int prec = p->prec; |
871 | 0 | size_t len; |
872 | |
|
873 | 0 | if(!str) { |
874 | | /* Write null string if there is space. */ |
875 | 0 | if(prec == -1 || prec >= (int) sizeof(nilstr) - 1) { |
876 | 0 | str = nilstr; |
877 | 0 | len = sizeof(nilstr) - 1; |
878 | | /* Disable quotes around (nil) */ |
879 | 0 | flags &= ~(unsigned int)FLAGS_ALT; |
880 | 0 | } |
881 | 0 | else { |
882 | 0 | str = ""; |
883 | 0 | len = 0; |
884 | 0 | } |
885 | 0 | } |
886 | 0 | else if(prec != -1) |
887 | 0 | len = (size_t)prec; |
888 | 0 | else if(*str == '\0') |
889 | 0 | len = 0; |
890 | 0 | else |
891 | 0 | len = strlen(str); |
892 | |
|
893 | 0 | width -= (len > INT_MAX) ? INT_MAX : (int)len; |
894 | |
|
895 | 0 | if(flags & FLAGS_ALT) |
896 | 0 | OUTCHAR('"'); |
897 | | |
898 | 0 | if(!(flags & FLAGS_LEFT)) |
899 | 0 | while(width-- > 0) |
900 | 0 | OUTCHAR(' '); |
901 | | |
902 | 0 | for(; len && *str; len--) |
903 | 0 | OUTCHAR(*str++); |
904 | 0 | if(flags & FLAGS_LEFT) |
905 | 0 | while(width-- > 0) |
906 | 0 | OUTCHAR(' '); |
907 | | |
908 | 0 | if(flags & FLAGS_ALT) |
909 | 0 | OUTCHAR('"'); |
910 | | |
911 | 0 | return FALSE; |
912 | 0 | } |
913 | | |
914 | | static bool out_pointer(void *userp, |
915 | | int (*stream)(unsigned char, void *), |
916 | | struct mproperty *p, |
917 | | const char *ptr, |
918 | | char *work, |
919 | | int *donep) |
920 | 0 | { |
921 | | /* Generic pointer. */ |
922 | 0 | if(ptr) { |
923 | 0 | size_t num = (size_t) ptr; |
924 | | |
925 | | /* If the pointer is not NULL, write it as a %#x spec. */ |
926 | 0 | p->flags |= FLAGS_HEX|FLAGS_ALT; |
927 | 0 | if(out_number(userp, stream, p, num, 0, work, donep)) |
928 | 0 | return TRUE; |
929 | 0 | } |
930 | 0 | else { |
931 | | /* Write "(nil)" for a nil pointer. */ |
932 | 0 | const char *point; |
933 | 0 | int width = p->width; |
934 | 0 | int flags = p->flags; |
935 | |
|
936 | 0 | width -= (int)(sizeof(nilstr) - 1); |
937 | 0 | if(flags & FLAGS_LEFT) |
938 | 0 | while(width-- > 0) |
939 | 0 | OUTCHAR(' '); |
940 | 0 | for(point = nilstr; *point; ++point) |
941 | 0 | OUTCHAR(*point); |
942 | 0 | if(!(flags & FLAGS_LEFT)) |
943 | 0 | while(width-- > 0) |
944 | 0 | OUTCHAR(' '); |
945 | 0 | } |
946 | 0 | return FALSE; |
947 | 0 | } |
948 | | |
949 | | /* |
950 | | * formatf() - the general printf function. |
951 | | * |
952 | | * It calls parsefmt() to parse the format string. It populates two arrays; |
953 | | * one that describes the input arguments and one that describes a number of |
954 | | * output segments. |
955 | | * |
956 | | * On success, the input array describes the type of all arguments and their |
957 | | * values. |
958 | | * |
959 | | * The function then iterates over the output segments and outputs them one |
960 | | * by one until done. Using the appropriate input arguments (if any). |
961 | | * |
962 | | * All output is sent to the 'stream()' callback, one byte at a time. |
963 | | */ |
964 | | |
965 | | static int formatf( |
966 | | void *userp, /* untouched by format(), just sent to the stream() function in |
967 | | the second argument */ |
968 | | /* function pointer called for each output character */ |
969 | | int (*stream)(unsigned char, void *), |
970 | | const char *format, /* %-formatted string */ |
971 | | va_list ap_save) /* list of parameters */ |
972 | 0 | { |
973 | 0 | int done = 0; /* number of characters written */ |
974 | 0 | int i; |
975 | 0 | int ocount = 0; /* number of output segments */ |
976 | 0 | int icount = 0; /* number of input arguments */ |
977 | |
|
978 | 0 | struct outsegment output[MAX_SEGMENTS]; |
979 | 0 | struct va_input input[MAX_PARAMETERS]; |
980 | 0 | char work[BUFFSIZE + 2]; |
981 | | |
982 | | /* Parse the format string */ |
983 | 0 | if(parsefmt(format, output, input, &ocount, &icount, ap_save)) |
984 | 0 | return 0; |
985 | | |
986 | 0 | for(i = 0; i < ocount; i++) { |
987 | 0 | struct outsegment *optr = &output[i]; |
988 | 0 | struct va_input *iptr = &input[optr->input]; |
989 | 0 | struct mproperty p; |
990 | 0 | size_t outlen = optr->outlen; |
991 | |
|
992 | 0 | if(outlen) { |
993 | 0 | const char *str = optr->start; |
994 | 0 | for(; outlen && *str; outlen--) { |
995 | 0 | if(stream(*str++, userp)) |
996 | 0 | return done; |
997 | 0 | done++; |
998 | 0 | } |
999 | 0 | if(optr->flags & FLAGS_SUBSTR) |
1000 | | /* this is just a substring */ |
1001 | 0 | continue; |
1002 | 0 | } |
1003 | | |
1004 | 0 | p.flags = optr->flags; |
1005 | | |
1006 | | /* pick up the specified width */ |
1007 | 0 | if(p.flags & FLAGS_WIDTHPARAM) { |
1008 | 0 | p.width = (int)input[optr->width].val.nums; |
1009 | 0 | if(p.width < 0) { |
1010 | | /* "A negative field width is taken as a '-' flag followed by a |
1011 | | positive field width." */ |
1012 | 0 | if(p.width == INT_MIN) |
1013 | 0 | p.width = INT_MAX; |
1014 | 0 | else |
1015 | 0 | p.width = -p.width; |
1016 | 0 | p.flags |= FLAGS_LEFT; |
1017 | 0 | p.flags &= ~(unsigned int)FLAGS_PAD_NIL; |
1018 | 0 | } |
1019 | 0 | } |
1020 | 0 | else |
1021 | 0 | p.width = optr->width; |
1022 | | |
1023 | | /* pick up the specified precision */ |
1024 | 0 | if(p.flags & FLAGS_PRECPARAM) { |
1025 | 0 | p.prec = (int)input[optr->precision].val.nums; |
1026 | 0 | if(p.prec < 0) |
1027 | | /* "A negative precision is taken as if the precision were |
1028 | | omitted." */ |
1029 | 0 | p.prec = -1; |
1030 | 0 | } |
1031 | 0 | else if(p.flags & FLAGS_PREC) |
1032 | 0 | p.prec = optr->precision; |
1033 | 0 | else |
1034 | 0 | p.prec = -1; |
1035 | |
|
1036 | 0 | switch(iptr->type) { |
1037 | 0 | case FORMAT_INTU: |
1038 | 0 | case FORMAT_LONGU: |
1039 | 0 | case FORMAT_LONGLONGU: |
1040 | 0 | p.flags |= FLAGS_UNSIGNED; |
1041 | 0 | if(out_number(userp, stream, &p, iptr->val.numu, 0, work, &done)) |
1042 | 0 | return done; |
1043 | 0 | break; |
1044 | | |
1045 | 0 | case FORMAT_INT: |
1046 | 0 | case FORMAT_LONG: |
1047 | 0 | case FORMAT_LONGLONG: |
1048 | 0 | if(out_number(userp, stream, &p, iptr->val.numu, |
1049 | 0 | iptr->val.nums, work, &done)) |
1050 | 0 | return done; |
1051 | 0 | break; |
1052 | | |
1053 | 0 | case FORMAT_STRING: |
1054 | 0 | if(out_string(userp, stream, &p, iptr->val.str, &done)) |
1055 | 0 | return done; |
1056 | 0 | break; |
1057 | | |
1058 | 0 | case FORMAT_PTR: |
1059 | 0 | if(out_pointer(userp, stream, &p, iptr->val.ptr, work, &done)) |
1060 | 0 | return done; |
1061 | 0 | break; |
1062 | | |
1063 | 0 | case FORMAT_DOUBLE: |
1064 | 0 | if(out_double(userp, stream, &p, iptr->val.dnum, work, &done)) |
1065 | 0 | return done; |
1066 | 0 | break; |
1067 | | |
1068 | 0 | case FORMAT_INTPTR: |
1069 | | /* Answer the count of characters written. */ |
1070 | 0 | #ifdef HAVE_LONG_LONG_TYPE |
1071 | 0 | if(p.flags & FLAGS_LONGLONG) |
1072 | 0 | *(LONG_LONG_TYPE *) iptr->val.ptr = (LONG_LONG_TYPE)done; |
1073 | 0 | else |
1074 | 0 | #endif |
1075 | 0 | if(p.flags & FLAGS_LONG) |
1076 | 0 | *(long *) iptr->val.ptr = (long)done; |
1077 | 0 | else if(!(p.flags & FLAGS_SHORT)) |
1078 | 0 | *(int *) iptr->val.ptr = (int)done; |
1079 | 0 | else |
1080 | 0 | *(short *) iptr->val.ptr = (short)done; |
1081 | 0 | break; |
1082 | | |
1083 | 0 | default: |
1084 | 0 | break; |
1085 | 0 | } |
1086 | 0 | } |
1087 | 0 | return done; |
1088 | 0 | } |
1089 | | |
1090 | | /* fputc() look-alike */ |
1091 | | static int addbyter(unsigned char outc, void *f) |
1092 | 0 | { |
1093 | 0 | struct nsprintf *infop = f; |
1094 | 0 | if(infop->length < infop->max) { |
1095 | | /* only do this if we have not reached max length yet */ |
1096 | 0 | *infop->buffer++ = (char)outc; /* store */ |
1097 | 0 | infop->length++; /* we are now one byte larger */ |
1098 | 0 | return 0; /* fputc() returns like this on success */ |
1099 | 0 | } |
1100 | 0 | return 1; |
1101 | 0 | } |
1102 | | |
1103 | | int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format, |
1104 | | va_list ap_save) |
1105 | 0 | { |
1106 | 0 | int retcode; |
1107 | 0 | struct nsprintf info; |
1108 | |
|
1109 | 0 | info.buffer = buffer; |
1110 | 0 | info.length = 0; |
1111 | 0 | info.max = maxlength; |
1112 | |
|
1113 | 0 | retcode = formatf(&info, addbyter, format, ap_save); |
1114 | 0 | if(info.max) { |
1115 | | /* we terminate this with a zero byte */ |
1116 | 0 | if(info.max == info.length) { |
1117 | | /* we are at maximum, scrap the last letter */ |
1118 | 0 | info.buffer[-1] = 0; |
1119 | 0 | DEBUGASSERT(retcode); |
1120 | 0 | retcode--; /* do not count the nul byte */ |
1121 | 0 | } |
1122 | 0 | else |
1123 | 0 | info.buffer[0] = 0; |
1124 | 0 | } |
1125 | 0 | return retcode; |
1126 | 0 | } |
1127 | | |
1128 | | int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...) |
1129 | 0 | { |
1130 | 0 | int retcode; |
1131 | 0 | va_list ap_save; /* argument pointer */ |
1132 | 0 | va_start(ap_save, format); |
1133 | 0 | retcode = curl_mvsnprintf(buffer, maxlength, format, ap_save); |
1134 | 0 | va_end(ap_save); |
1135 | 0 | return retcode; |
1136 | 0 | } |
1137 | | |
1138 | | /* fputc() look-alike */ |
1139 | | static int alloc_addbyter(unsigned char outc, void *f) |
1140 | 0 | { |
1141 | 0 | struct asprintf *infop = f; |
1142 | 0 | CURLcode result = curlx_dyn_addn(infop->b, &outc, 1); |
1143 | 0 | if(result) { |
1144 | 0 | infop->merr = result == CURLE_TOO_LARGE ? MERR_TOO_LARGE : MERR_MEM; |
1145 | 0 | return 1 ; /* fail */ |
1146 | 0 | } |
1147 | 0 | return 0; |
1148 | 0 | } |
1149 | | |
1150 | | /* appends the formatted string, returns MERR error code */ |
1151 | | int curlx_dyn_vprintf(struct dynbuf *dyn, const char *format, va_list ap_save) |
1152 | 0 | { |
1153 | 0 | struct asprintf info; |
1154 | 0 | info.b = dyn; |
1155 | 0 | info.merr = MERR_OK; |
1156 | |
|
1157 | 0 | (void)formatf(&info, alloc_addbyter, format, ap_save); |
1158 | 0 | if(info.merr) { |
1159 | 0 | curlx_dyn_free(info.b); |
1160 | 0 | return info.merr; |
1161 | 0 | } |
1162 | 0 | return 0; |
1163 | 0 | } |
1164 | | |
1165 | | char *curl_mvaprintf(const char *format, va_list ap_save) |
1166 | 0 | { |
1167 | 0 | struct asprintf info; |
1168 | 0 | struct dynbuf dyn; |
1169 | 0 | info.b = &dyn; |
1170 | 0 | curlx_dyn_init(info.b, DYN_APRINTF); |
1171 | 0 | info.merr = MERR_OK; |
1172 | |
|
1173 | 0 | (void)formatf(&info, alloc_addbyter, format, ap_save); |
1174 | 0 | if(info.merr) { |
1175 | 0 | curlx_dyn_free(info.b); |
1176 | 0 | return NULL; |
1177 | 0 | } |
1178 | 0 | if(curlx_dyn_len(info.b)) |
1179 | 0 | return curlx_dyn_ptr(info.b); |
1180 | 0 | return strdup(""); |
1181 | 0 | } |
1182 | | |
1183 | | char *curl_maprintf(const char *format, ...) |
1184 | 0 | { |
1185 | 0 | va_list ap_save; |
1186 | 0 | char *s; |
1187 | 0 | va_start(ap_save, format); |
1188 | 0 | s = curl_mvaprintf(format, ap_save); |
1189 | 0 | va_end(ap_save); |
1190 | 0 | return s; |
1191 | 0 | } |
1192 | | |
1193 | | static int storebuffer(unsigned char outc, void *f) |
1194 | 0 | { |
1195 | 0 | char **buffer = f; |
1196 | 0 | **buffer = (char)outc; |
1197 | 0 | (*buffer)++; |
1198 | 0 | return 0; |
1199 | 0 | } |
1200 | | |
1201 | | int curl_msprintf(char *buffer, const char *format, ...) |
1202 | 0 | { |
1203 | 0 | va_list ap_save; /* argument pointer */ |
1204 | 0 | int retcode; |
1205 | 0 | va_start(ap_save, format); |
1206 | 0 | retcode = formatf(&buffer, storebuffer, format, ap_save); |
1207 | 0 | va_end(ap_save); |
1208 | 0 | *buffer = 0; /* we terminate this with a zero byte */ |
1209 | 0 | return retcode; |
1210 | 0 | } |
1211 | | |
1212 | | static int fputc_wrapper(unsigned char outc, void *f) |
1213 | 0 | { |
1214 | 0 | int out = outc; |
1215 | 0 | FILE *s = f; |
1216 | 0 | int rc = fputc(out, s); |
1217 | 0 | return rc == EOF; |
1218 | 0 | } |
1219 | | |
1220 | | int curl_mprintf(const char *format, ...) |
1221 | 0 | { |
1222 | 0 | int retcode; |
1223 | 0 | va_list ap_save; /* argument pointer */ |
1224 | 0 | va_start(ap_save, format); |
1225 | 0 | retcode = formatf(stdout, fputc_wrapper, format, ap_save); |
1226 | 0 | va_end(ap_save); |
1227 | 0 | return retcode; |
1228 | 0 | } |
1229 | | |
1230 | | int curl_mfprintf(FILE *whereto, const char *format, ...) |
1231 | 0 | { |
1232 | 0 | int retcode; |
1233 | 0 | va_list ap_save; /* argument pointer */ |
1234 | 0 | va_start(ap_save, format); |
1235 | 0 | retcode = formatf(whereto, fputc_wrapper, format, ap_save); |
1236 | 0 | va_end(ap_save); |
1237 | 0 | return retcode; |
1238 | 0 | } |
1239 | | |
1240 | | int curl_mvsprintf(char *buffer, const char *format, va_list ap_save) |
1241 | 0 | { |
1242 | 0 | int retcode = formatf(&buffer, storebuffer, format, ap_save); |
1243 | 0 | *buffer = 0; /* we terminate this with a zero byte */ |
1244 | 0 | return retcode; |
1245 | 0 | } |
1246 | | |
1247 | | int curl_mvprintf(const char *format, va_list ap_save) |
1248 | 0 | { |
1249 | 0 | return formatf(stdout, fputc_wrapper, format, ap_save); |
1250 | 0 | } |
1251 | | |
1252 | | int curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save) |
1253 | 0 | { |
1254 | 0 | return formatf(whereto, fputc_wrapper, format, ap_save); |
1255 | 0 | } |