Coverage Report

Created: 2026-11-15 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nspr/pr/src/io/prscanf.c
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
/*
6
 * Scan functions for NSPR types
7
 *
8
 * Author: Wan-Teh Chang
9
 *
10
 * Acknowledgment: The implementation is inspired by the source code
11
 * in P.J. Plauger's "The Standard C Library," Prentice-Hall, 1992.
12
 */
13
14
#include <limits.h>
15
#include <ctype.h>
16
#include <string.h>
17
#include <stdlib.h>
18
#include "prprf.h"
19
#include "prdtoa.h"
20
#include "prlog.h"
21
#include "prerror.h"
22
23
/*
24
 * A function that reads a character from 'stream'.
25
 * Returns the character read, or EOF if end of stream is reached.
26
 */
27
typedef int (*_PRGetCharFN)(void* stream);
28
29
/*
30
 * A function that pushes the character 'ch' back to 'stream'.
31
 */
32
typedef void (*_PRUngetCharFN)(void* stream, int ch);
33
34
/*
35
 * The size specifier for the integer and floating point number
36
 * conversions in format control strings.
37
 */
38
typedef enum {
39
  _PR_size_none, /* No size specifier is given */
40
  _PR_size_h,    /* The 'h' specifier, suggesting "short" */
41
  _PR_size_l,    /* The 'l' specifier, suggesting "long" */
42
  _PR_size_L,    /* The 'L' specifier, meaning a 'long double' */
43
  _PR_size_ll    /* The 'll' specifier, suggesting "long long" */
44
} _PRSizeSpec;
45
46
/*
47
 * The collection of data that is passed between the scan function
48
 * and its subordinate functions.  The fields of this structure
49
 * serve as the input or output arguments for these functions.
50
 */
51
typedef struct {
52
  _PRGetCharFN get;     /* get a character from input stream */
53
  _PRUngetCharFN unget; /* unget (push back) a character */
54
  void* stream;         /* argument for get and unget */
55
  va_list ap;           /* the variable argument list */
56
  int nChar;            /* number of characters read from 'stream' */
57
58
  PRBool assign;        /* assign, or suppress assignment? */
59
  int width;            /* field width */
60
  _PRSizeSpec sizeSpec; /* 'h', 'l', 'L', or 'll' */
61
62
  PRBool converted; /* is the value actually converted? */
63
} ScanfState;
64
65
0
#define GET(state) ((state)->nChar++, (state)->get((state)->stream))
66
0
#define UNGET(state, ch) ((state)->nChar--, (state)->unget((state)->stream, ch))
67
68
/*
69
 * The following two macros, GET_IF_WITHIN_WIDTH and WITHIN_WIDTH,
70
 * are always used together.
71
 *
72
 * GET_IF_WITHIN_WIDTH calls the GET macro and assigns its return
73
 * value to 'ch' only if we have not exceeded the field width of
74
 * 'state'.  Therefore, after GET_IF_WITHIN_WIDTH, the value of
75
 * 'ch' is valid only if the macro WITHIN_WIDTH evaluates to true.
76
 */
77
78
#define GET_IF_WITHIN_WIDTH(state, ch) \
79
0
  if (--(state)->width >= 0) {         \
80
0
    (ch) = GET(state);                 \
81
0
  }
82
0
#define WITHIN_WIDTH(state) ((state)->width >= 0)
83
84
/*
85
 * _pr_strtoull:
86
 *     Convert a string to an unsigned 64-bit integer.  The string
87
 *     'str' is assumed to be a representation of the integer in
88
 *     base 'base'.
89
 *
90
 * Warning:
91
 *     - Only handle base 8, 10, and 16.
92
 *     - No overflow checking.
93
 */
94
95
0
static PRUint64 _pr_strtoull(const char* str, char** endptr, int base) {
96
0
  static const int BASE_MAX = 16;
97
0
  static const char digits[] = "0123456789abcdef";
98
0
  char* digitPtr;
99
0
  PRUint64 x; /* return value */
100
0
  PRInt64 base64;
101
0
  const char* cPtr;
102
0
  PRBool negative;
103
0
  const char* digitStart;
104
105
0
  PR_ASSERT(base == 0 || base == 8 || base == 10 || base == 16);
106
0
  if (base < 0 || base == 1 || base > BASE_MAX) {
107
0
    if (endptr) {
108
0
      *endptr = (char*)str;
109
0
      return LL_ZERO;
110
0
    }
111
0
  }
112
113
0
  cPtr = str;
114
0
  while (isspace(*cPtr)) {
115
0
    ++cPtr;
116
0
  }
117
118
0
  negative = PR_FALSE;
119
0
  if (*cPtr == '-') {
120
0
    negative = PR_TRUE;
121
0
    cPtr++;
122
0
  } else if (*cPtr == '+') {
123
0
    cPtr++;
124
0
  }
125
126
0
  if (base == 16) {
127
0
    if (*cPtr == '0' && (cPtr[1] == 'x' || cPtr[1] == 'X')) {
128
0
      cPtr += 2;
129
0
    }
130
0
  } else if (base == 0) {
131
0
    if (*cPtr != '0') {
132
0
      base = 10;
133
0
    } else if (cPtr[1] == 'x' || cPtr[1] == 'X') {
134
0
      base = 16;
135
0
      cPtr += 2;
136
0
    } else {
137
0
      base = 8;
138
0
    }
139
0
  }
140
0
  PR_ASSERT(base != 0);
141
0
  LL_I2L(base64, base);
142
0
  digitStart = cPtr;
143
144
  /* Skip leading zeros */
145
0
  while (*cPtr == '0') {
146
0
    cPtr++;
147
0
  }
148
149
0
  LL_I2L(x, 0);
150
0
  while ((digitPtr = (char*)memchr(digits, tolower(*cPtr), base)) != NULL) {
151
0
    PRUint64 d;
152
153
0
    LL_I2L(d, (digitPtr - digits));
154
0
    LL_MUL(x, x, base64);
155
0
    LL_ADD(x, x, d);
156
0
    cPtr++;
157
0
  }
158
159
0
  if (cPtr == digitStart) {
160
0
    if (endptr) {
161
0
      *endptr = (char*)str;
162
0
    }
163
0
    return LL_ZERO;
164
0
  }
165
166
0
  if (negative) {
167
0
#ifdef HAVE_LONG_LONG
168
    /* The cast to a signed type is to avoid a compiler warning */
169
0
    x = -(PRInt64)x;
170
#else
171
    LL_NEG(x, x);
172
#endif
173
0
  }
174
175
0
  if (endptr) {
176
0
    *endptr = (char*)cPtr;
177
0
  }
178
0
  return x;
179
0
}
180
181
/*
182
 * The maximum field width (in number of characters) that is enough
183
 * (may be more than necessary) to represent a 64-bit integer or
184
 * floating point number.
185
 */
186
0
#define FMAX 31
187
0
#define DECIMAL_POINT '.'
188
189
0
static PRStatus GetInt(ScanfState* state, int code) {
190
0
  char buf[FMAX + 1], *p;
191
0
  int ch = 0;
192
0
  static const char digits[] = "0123456789abcdefABCDEF";
193
0
  PRBool seenDigit = PR_FALSE;
194
0
  int base;
195
0
  int dlen;
196
197
0
  switch (code) {
198
0
    case 'd':
199
0
    case 'u':
200
0
      base = 10;
201
0
      break;
202
0
    case 'i':
203
0
      base = 0;
204
0
      break;
205
0
    case 'x':
206
0
    case 'X':
207
0
    case 'p':
208
0
      base = 16;
209
0
      break;
210
0
    case 'o':
211
0
      base = 8;
212
0
      break;
213
0
    default:
214
0
      return PR_FAILURE;
215
0
  }
216
0
  if (state->width == 0 || state->width > FMAX) {
217
0
    state->width = FMAX;
218
0
  }
219
0
  p = buf;
220
0
  GET_IF_WITHIN_WIDTH(state, ch);
221
0
  if (WITHIN_WIDTH(state) && (ch == '+' || ch == '-')) {
222
0
    *p++ = ch;
223
0
    GET_IF_WITHIN_WIDTH(state, ch);
224
0
  }
225
0
  if (WITHIN_WIDTH(state) && ch == '0') {
226
0
    seenDigit = PR_TRUE;
227
0
    *p++ = ch;
228
0
    GET_IF_WITHIN_WIDTH(state, ch);
229
0
    if (WITHIN_WIDTH(state) && (ch == 'x' || ch == 'X') &&
230
0
        (base == 0 || base == 16)) {
231
0
      base = 16;
232
0
      *p++ = ch;
233
0
      GET_IF_WITHIN_WIDTH(state, ch);
234
0
    } else if (base == 0) {
235
0
      base = 8;
236
0
    }
237
0
  }
238
0
  if (base == 0 || base == 10) {
239
0
    dlen = 10;
240
0
  } else if (base == 8) {
241
0
    dlen = 8;
242
0
  } else {
243
0
    PR_ASSERT(base == 16);
244
0
    dlen = 16 + 6; /* 16 digits, plus 6 in uppercase */
245
0
  }
246
0
  while (WITHIN_WIDTH(state) && memchr(digits, ch, dlen)) {
247
0
    *p++ = ch;
248
0
    GET_IF_WITHIN_WIDTH(state, ch);
249
0
    seenDigit = PR_TRUE;
250
0
  }
251
0
  if (WITHIN_WIDTH(state)) {
252
0
    UNGET(state, ch);
253
0
  }
254
0
  if (!seenDigit) {
255
0
    return PR_FAILURE;
256
0
  }
257
0
  *p = '\0';
258
0
  if (state->assign) {
259
0
    if (code == 'd' || code == 'i') {
260
0
      if (state->sizeSpec == _PR_size_ll) {
261
0
        PRInt64 llval = _pr_strtoull(buf, NULL, base);
262
0
        *va_arg(state->ap, PRInt64*) = llval;
263
0
      } else {
264
0
        long lval = strtol(buf, NULL, base);
265
266
0
        if (state->sizeSpec == _PR_size_none) {
267
0
          *va_arg(state->ap, PRIntn*) = lval;
268
0
        } else if (state->sizeSpec == _PR_size_h) {
269
0
          *va_arg(state->ap, PRInt16*) = (PRInt16)lval;
270
0
        } else if (state->sizeSpec == _PR_size_l) {
271
0
          *va_arg(state->ap, PRInt32*) = lval;
272
0
        } else {
273
0
          return PR_FAILURE;
274
0
        }
275
0
      }
276
0
    } else {
277
0
      if (state->sizeSpec == _PR_size_ll) {
278
0
        PRUint64 llval = _pr_strtoull(buf, NULL, base);
279
0
        *va_arg(state->ap, PRUint64*) = llval;
280
0
      } else {
281
0
        unsigned long lval = strtoul(buf, NULL, base);
282
283
0
        if (state->sizeSpec == _PR_size_none) {
284
0
          *va_arg(state->ap, PRUintn*) = lval;
285
0
        } else if (state->sizeSpec == _PR_size_h) {
286
0
          *va_arg(state->ap, PRUint16*) = (PRUint16)lval;
287
0
        } else if (state->sizeSpec == _PR_size_l) {
288
0
          *va_arg(state->ap, PRUint32*) = lval;
289
0
        } else {
290
0
          return PR_FAILURE;
291
0
        }
292
0
      }
293
0
    }
294
0
    state->converted = PR_TRUE;
295
0
  }
296
0
  return PR_SUCCESS;
297
0
}
298
299
0
static PRStatus GetFloat(ScanfState* state) {
300
0
  char buf[FMAX + 1], *p;
301
0
  int ch = 0;
302
0
  PRBool seenDigit = PR_FALSE;
303
304
0
  if (state->width == 0 || state->width > FMAX) {
305
0
    state->width = FMAX;
306
0
  }
307
0
  p = buf;
308
0
  GET_IF_WITHIN_WIDTH(state, ch);
309
0
  if (WITHIN_WIDTH(state) && (ch == '+' || ch == '-')) {
310
0
    *p++ = ch;
311
0
    GET_IF_WITHIN_WIDTH(state, ch);
312
0
  }
313
0
  while (WITHIN_WIDTH(state) && isdigit(ch)) {
314
0
    *p++ = ch;
315
0
    GET_IF_WITHIN_WIDTH(state, ch);
316
0
    seenDigit = PR_TRUE;
317
0
  }
318
0
  if (WITHIN_WIDTH(state) && ch == DECIMAL_POINT) {
319
0
    *p++ = ch;
320
0
    GET_IF_WITHIN_WIDTH(state, ch);
321
0
    while (WITHIN_WIDTH(state) && isdigit(ch)) {
322
0
      *p++ = ch;
323
0
      GET_IF_WITHIN_WIDTH(state, ch);
324
0
      seenDigit = PR_TRUE;
325
0
    }
326
0
  }
327
328
  /*
329
   * This is not robust.  For example, "1.2e+" would confuse
330
   * the code below to read 'e' and '+', only to realize that
331
   * it should have stopped at "1.2".  But we can't push back
332
   * more than one character, so there is nothing I can do.
333
   */
334
335
  /* Parse exponent */
336
0
  if (WITHIN_WIDTH(state) && (ch == 'e' || ch == 'E') && seenDigit) {
337
0
    *p++ = ch;
338
0
    GET_IF_WITHIN_WIDTH(state, ch);
339
0
    if (WITHIN_WIDTH(state) && (ch == '+' || ch == '-')) {
340
0
      *p++ = ch;
341
0
      GET_IF_WITHIN_WIDTH(state, ch);
342
0
    }
343
0
    while (WITHIN_WIDTH(state) && isdigit(ch)) {
344
0
      *p++ = ch;
345
0
      GET_IF_WITHIN_WIDTH(state, ch);
346
0
    }
347
0
  }
348
0
  if (WITHIN_WIDTH(state)) {
349
0
    UNGET(state, ch);
350
0
  }
351
0
  if (!seenDigit) {
352
0
    return PR_FAILURE;
353
0
  }
354
0
  *p = '\0';
355
0
  if (state->assign) {
356
0
    PRFloat64 dval = PR_strtod(buf, NULL);
357
358
0
    state->converted = PR_TRUE;
359
0
    if (state->sizeSpec == _PR_size_l) {
360
0
      *va_arg(state->ap, PRFloat64*) = dval;
361
0
    } else if (state->sizeSpec == _PR_size_L) {
362
0
      *va_arg(state->ap, long double*) = dval;
363
0
    } else {
364
0
      *va_arg(state->ap, float*) = (float)dval;
365
0
    }
366
0
  }
367
0
  return PR_SUCCESS;
368
0
}
369
370
/*
371
 * Convert, and return the end of the conversion spec.
372
 * Return NULL on error.
373
 */
374
375
0
static const char* Convert(ScanfState* state, const char* fmt) {
376
0
  const char* cPtr;
377
0
  int ch;
378
0
  char* cArg = NULL;
379
380
0
  state->converted = PR_FALSE;
381
0
  cPtr = fmt;
382
0
  if (*cPtr != 'c' && *cPtr != 'n' && *cPtr != '[') {
383
0
    do {
384
0
      ch = GET(state);
385
0
    } while (isspace(ch));
386
0
    UNGET(state, ch);
387
0
  }
388
0
  switch (*cPtr) {
389
0
    case 'c':
390
0
      if (state->assign) {
391
0
        cArg = va_arg(state->ap, char*);
392
0
      }
393
0
      if (state->width == 0) {
394
0
        state->width = 1;
395
0
      }
396
0
      for (; state->width > 0; state->width--) {
397
0
        ch = GET(state);
398
0
        if (ch == EOF) {
399
0
          return NULL;
400
0
        }
401
0
        if (state->assign) {
402
0
          *cArg++ = ch;
403
0
        }
404
0
      }
405
0
      if (state->assign) {
406
0
        state->converted = PR_TRUE;
407
0
      }
408
0
      break;
409
0
    case 'p':
410
0
    case 'd':
411
0
    case 'i':
412
0
    case 'o':
413
0
    case 'u':
414
0
    case 'x':
415
0
    case 'X':
416
0
      if (GetInt(state, *cPtr) == PR_FAILURE) {
417
0
        return NULL;
418
0
      }
419
0
      break;
420
0
    case 'e':
421
0
    case 'E':
422
0
    case 'f':
423
0
    case 'g':
424
0
    case 'G':
425
0
      if (GetFloat(state) == PR_FAILURE) {
426
0
        return NULL;
427
0
      }
428
0
      break;
429
0
    case 'n':
430
      /* do not consume any input */
431
0
      if (state->assign) {
432
0
        switch (state->sizeSpec) {
433
0
          case _PR_size_none:
434
0
            *va_arg(state->ap, PRIntn*) = state->nChar;
435
0
            break;
436
0
          case _PR_size_h:
437
0
            *va_arg(state->ap, PRInt16*) = state->nChar;
438
0
            break;
439
0
          case _PR_size_l:
440
0
            *va_arg(state->ap, PRInt32*) = state->nChar;
441
0
            break;
442
0
          case _PR_size_ll:
443
0
            LL_I2L(*va_arg(state->ap, PRInt64*), state->nChar);
444
0
            break;
445
0
          default:
446
0
            PR_ASSERT(0);
447
0
        }
448
0
      }
449
0
      break;
450
0
    case 's':
451
0
      if (state->width == 0) {
452
0
        state->width = INT_MAX;
453
0
      }
454
0
      if (state->assign) {
455
0
        cArg = va_arg(state->ap, char*);
456
0
      }
457
0
      for (; state->width > 0; state->width--) {
458
0
        ch = GET(state);
459
0
        if ((ch == EOF) || isspace(ch)) {
460
0
          UNGET(state, ch);
461
0
          break;
462
0
        }
463
0
        if (state->assign) {
464
0
          *cArg++ = ch;
465
0
        }
466
0
      }
467
0
      if (state->assign) {
468
0
        *cArg = '\0';
469
0
        state->converted = PR_TRUE;
470
0
      }
471
0
      break;
472
0
    case '%':
473
0
      ch = GET(state);
474
0
      if (ch != '%') {
475
0
        UNGET(state, ch);
476
0
        return NULL;
477
0
      }
478
0
      break;
479
0
    case '[': {
480
0
      PRBool complement = PR_FALSE;
481
0
      const char* closeBracket;
482
0
      size_t n;
483
484
0
      if (*++cPtr == '^') {
485
0
        complement = PR_TRUE;
486
0
        cPtr++;
487
0
      }
488
0
      closeBracket = strchr(*cPtr == ']' ? cPtr + 1 : cPtr, ']');
489
0
      if (closeBracket == NULL) {
490
0
        return NULL;
491
0
      }
492
0
      n = closeBracket - cPtr;
493
0
      if (state->width == 0) {
494
0
        state->width = INT_MAX;
495
0
      }
496
0
      if (state->assign) {
497
0
        cArg = va_arg(state->ap, char*);
498
0
      }
499
0
      for (; state->width > 0; state->width--) {
500
0
        ch = GET(state);
501
0
        if ((ch == EOF) || (!complement && !memchr(cPtr, ch, n)) ||
502
0
            (complement && memchr(cPtr, ch, n))) {
503
0
          UNGET(state, ch);
504
0
          break;
505
0
        }
506
0
        if (state->assign) {
507
0
          *cArg++ = ch;
508
0
        }
509
0
      }
510
0
      if (state->assign) {
511
0
        *cArg = '\0';
512
0
        state->converted = PR_TRUE;
513
0
      }
514
0
      cPtr = closeBracket;
515
0
    } break;
516
0
    default:
517
0
      return NULL;
518
0
  }
519
0
  return cPtr;
520
0
}
521
522
0
static PRInt32 DoScanf(ScanfState* state, const char* fmt) {
523
0
  PRInt32 nConverted = 0;
524
0
  const char* cPtr;
525
0
  int ch;
526
527
0
  state->nChar = 0;
528
0
  cPtr = fmt;
529
0
  while (1) {
530
0
    if (isspace(*cPtr)) {
531
      /* white space: skip */
532
0
      do {
533
0
        cPtr++;
534
0
      } while (isspace(*cPtr));
535
0
      do {
536
0
        ch = GET(state);
537
0
      } while (isspace(ch));
538
0
      UNGET(state, ch);
539
0
    } else if (*cPtr == '%') {
540
      /* format spec: convert */
541
0
      cPtr++;
542
0
      state->assign = PR_TRUE;
543
0
      if (*cPtr == '*') {
544
0
        cPtr++;
545
0
        state->assign = PR_FALSE;
546
0
      }
547
0
      for (state->width = 0; isdigit(*cPtr); cPtr++) {
548
0
        state->width = state->width * 10 + *cPtr - '0';
549
0
      }
550
0
      state->sizeSpec = _PR_size_none;
551
0
      if (*cPtr == 'h') {
552
0
        cPtr++;
553
0
        state->sizeSpec = _PR_size_h;
554
0
      } else if (*cPtr == 'l') {
555
0
        cPtr++;
556
0
        if (*cPtr == 'l') {
557
0
          cPtr++;
558
0
          state->sizeSpec = _PR_size_ll;
559
0
        } else {
560
0
          state->sizeSpec = _PR_size_l;
561
0
        }
562
0
      } else if (*cPtr == 'L') {
563
0
        cPtr++;
564
0
        state->sizeSpec = _PR_size_L;
565
0
      }
566
0
      cPtr = Convert(state, cPtr);
567
0
      if (cPtr == NULL) {
568
0
        return (nConverted > 0 ? nConverted : EOF);
569
0
      }
570
0
      if (state->converted) {
571
0
        nConverted++;
572
0
      }
573
0
      cPtr++;
574
0
    } else {
575
      /* others: must match */
576
0
      if (*cPtr == '\0') {
577
0
        return nConverted;
578
0
      }
579
0
      ch = GET(state);
580
0
      if (ch != *cPtr) {
581
0
        UNGET(state, ch);
582
0
        return nConverted;
583
0
      }
584
0
      cPtr++;
585
0
    }
586
0
  }
587
0
}
588
589
0
static int StringGetChar(void* stream) {
590
0
  char* cPtr = *((char**)stream);
591
592
0
  if (*cPtr == '\0') {
593
0
    return EOF;
594
0
  }
595
0
  *((char**)stream) = cPtr + 1;
596
0
  return (unsigned char)*cPtr;
597
0
}
598
599
0
static void StringUngetChar(void* stream, int ch) {
600
0
  char* cPtr = *((char**)stream);
601
602
0
  if (ch != EOF) {
603
0
    *((char**)stream) = cPtr - 1;
604
0
  }
605
0
}
606
607
PR_IMPLEMENT(PRInt32)
608
0
PR_sscanf(const char* buf, const char* fmt, ...) {
609
0
  PRInt32 rv;
610
0
  ScanfState state;
611
612
0
  state.get = &StringGetChar;
613
0
  state.unget = &StringUngetChar;
614
0
  state.stream = (void*)&buf;
615
0
  va_start(state.ap, fmt);
616
0
  rv = DoScanf(&state, fmt);
617
  va_end(state.ap);
618
0
  return rv;
619
0
}