Coverage Report

Created: 2026-06-15 07:03

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