Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavutil/avsscanf.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2005-2014 Rich Felker, et al.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 */
23
24
#include <errno.h>
25
#include <limits.h>
26
#include <math.h>
27
#include <stdarg.h>
28
#include <stddef.h>
29
#include <stdint.h>
30
#include <stdio.h>
31
#include <string.h>
32
#include <float.h>
33
34
#include "avstring.h"
35
#include "libm.h"
36
37
typedef struct FFFILE {
38
    size_t buf_size;
39
    unsigned char *buf;
40
    unsigned char *rpos, *rend;
41
    unsigned char *shend;
42
    ptrdiff_t shlim, shcnt;
43
    void *cookie;
44
    size_t (*read)(struct FFFILE *, unsigned char *, size_t);
45
} FFFILE;
46
47
0
#define SIZE_hh -2
48
0
#define SIZE_h  -1
49
30.8M
#define SIZE_def 0
50
2.99M
#define SIZE_l   1
51
0
#define SIZE_L   2
52
8.72M
#define SIZE_ll  3
53
54
66.3M
#define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
55
56
static int fftoread(FFFILE *f)
57
15.1M
{
58
15.1M
    f->rpos = f->rend = f->buf + f->buf_size;
59
15.1M
    return 0;
60
15.1M
}
61
62
static size_t ffstring_read(FFFILE *f, unsigned char *buf, size_t len)
63
15.1M
{
64
15.1M
    char *src = f->cookie;
65
15.1M
    size_t k = len+256;
66
15.1M
    char *end = memchr(src, 0, k);
67
68
15.1M
    if (end) k = end-src;
69
15.1M
    if (k < len) len = k;
70
15.1M
    memcpy(buf, src, len);
71
15.1M
    f->rpos = (void *)(src+len);
72
15.1M
    f->rend = (void *)(src+k);
73
15.1M
    f->cookie = src+k;
74
75
15.1M
    return len;
76
15.1M
}
77
78
static int ffuflow(FFFILE *f)
79
15.1M
{
80
15.1M
    unsigned char c;
81
15.1M
    if (!fftoread(f) && f->read(f, &c, 1)==1) return c;
82
6.40M
    return EOF;
83
15.1M
}
84
85
static void ffshlim(FFFILE *f, ptrdiff_t lim)
86
37.5M
{
87
37.5M
    f->shlim = lim;
88
37.5M
    f->shcnt = f->buf - f->rpos;
89
    /* If lim is nonzero, rend must be a valid pointer. */
90
37.5M
    if (lim && f->rend - f->rpos > lim)
91
504k
        f->shend = f->rpos + lim;
92
37.0M
    else
93
37.0M
        f->shend = f->rend;
94
37.5M
}
95
96
static int ffshgetc(FFFILE *f)
97
17.4M
{
98
17.4M
    int c;
99
17.4M
    ptrdiff_t cnt = shcnt(f);
100
17.4M
    if (f->shlim && cnt >= f->shlim || (c=ffuflow(f)) < 0) {
101
8.65M
        f->shcnt = f->buf - f->rpos + cnt;
102
8.65M
        f->shend = 0;
103
8.65M
        return EOF;
104
8.65M
    }
105
8.79M
    cnt++;
106
8.79M
    if (f->shlim && f->rend - f->rpos > f->shlim - cnt)
107
2.37M
        f->shend = f->rpos + (f->shlim - cnt);
108
6.41M
    else
109
6.41M
        f->shend = f->rend;
110
8.79M
    f->shcnt = f->buf - f->rpos + cnt;
111
8.79M
    if (f->rpos[-1] != c) f->rpos[-1] = c;
112
8.79M
    return c;
113
17.4M
}
114
115
37.5M
#define shlim(f, lim) ffshlim((f), (lim))
116
84.8M
#define shgetc(f) (((f)->rpos < (f)->shend) ? *(f)->rpos++ : ffshgetc(f))
117
49.2M
#define shunget(f) ((f)->shend ? (void)(f)->rpos-- : (void)0)
118
119
static const unsigned char table[] = { -1,
120
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
121
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
122
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
123
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
124
    -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
125
    25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
126
    -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
127
    25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
128
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
129
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
130
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
131
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
132
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
133
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
134
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
135
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
136
};
137
138
static unsigned long long ffintscan(FFFILE *f, unsigned base, int pok, unsigned long long lim)
139
8.43M
{
140
8.43M
    const unsigned char *val = table+1;
141
8.43M
    int c, neg=0;
142
8.43M
    unsigned x;
143
8.43M
    unsigned long long y;
144
8.43M
    if (base > 36 || base == 1) {
145
0
        errno = EINVAL;
146
0
        return 0;
147
0
    }
148
8.43M
    while (av_isspace((c=shgetc(f))));
149
8.43M
    if (c=='+' || c=='-') {
150
559k
        neg = -(c=='-');
151
559k
        c = shgetc(f);
152
559k
    }
153
8.43M
    if ((base == 0 || base == 16) && c=='0') {
154
0
        c = shgetc(f);
155
0
        if ((c|32)=='x') {
156
0
            c = shgetc(f);
157
0
            if (val[c]>=16) {
158
0
                shunget(f);
159
0
                if (pok) shunget(f);
160
0
                else shlim(f, 0);
161
0
                return 0;
162
0
            }
163
0
            base = 16;
164
0
        } else if (base == 0) {
165
0
            base = 8;
166
0
        }
167
8.43M
    } else {
168
8.43M
        if (base == 0) base = 10;
169
8.43M
        if (val[c] >= base) {
170
97.3k
            shunget(f);
171
97.3k
            shlim(f, 0);
172
97.3k
            errno = EINVAL;
173
97.3k
            return 0;
174
97.3k
        }
175
8.43M
    }
176
8.34M
    if (base == 10) {
177
17.6M
        for (x=0; c-'0'<10U && x<=UINT_MAX/10-1; c=shgetc(f))
178
9.31M
            x = x*10 + (c-'0');
179
8.40M
        for (y=x; c-'0'<10U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(c-'0'); c=shgetc(f))
180
60.1k
            y = y*10 + (c-'0');
181
8.34M
        if (c-'0'>=10U) goto done;
182
8.34M
    } else if (!(base & base-1)) {
183
0
        int bs = "\0\1\2\4\7\3\6\5"[(0x17*base)>>5&7];
184
0
        for (x=0; val[c]<base && x<=UINT_MAX/32; c=shgetc(f))
185
0
            x = x<<bs | val[c];
186
0
        for (y=x; val[c]<base && y<=ULLONG_MAX>>bs; c=shgetc(f))
187
0
            y = y<<bs | val[c];
188
0
    } else {
189
0
        for (x=0; val[c]<base && x<=UINT_MAX/36-1; c=shgetc(f))
190
0
            x = x*base + val[c];
191
0
        for (y=x; val[c]<base && y<=ULLONG_MAX/base && base*y<=ULLONG_MAX-val[c]; c=shgetc(f))
192
0
            y = y*base + val[c];
193
0
    }
194
2.95k
    if (val[c]<base) {
195
1.08M
        for (; val[c]<base; c=shgetc(f));
196
2.95k
        errno = ERANGE;
197
2.95k
        y = lim;
198
2.95k
        if (lim&1) neg = 0;
199
2.95k
    }
200
8.34M
done:
201
8.34M
    shunget(f);
202
8.34M
    if (y>=lim) {
203
2.95k
        if (!(lim&1) && !neg) {
204
0
            errno = ERANGE;
205
0
            return lim-1;
206
2.95k
        } else if (y>lim) {
207
0
            errno = ERANGE;
208
0
            return lim;
209
0
        }
210
2.95k
    }
211
8.34M
    return (y^neg)-neg;
212
8.34M
}
213
214
static long long scanexp(FFFILE *f, int pok)
215
261k
{
216
261k
    int c;
217
261k
    int x;
218
261k
    long long y;
219
261k
    int neg = 0;
220
221
261k
    c = shgetc(f);
222
261k
    if (c=='+' || c=='-') {
223
175k
        neg = (c=='-');
224
175k
        c = shgetc(f);
225
175k
        if (c-'0'>=10U && pok) shunget(f);
226
175k
    }
227
261k
    if (c-'0'>=10U) {
228
13.0k
        shunget(f);
229
13.0k
        return LLONG_MIN;
230
13.0k
    }
231
1.92M
    for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
232
1.67M
        x = 10*x + (c-'0');
233
676k
    for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f))
234
427k
        y = 10*y + (c-'0');
235
277k
    for (; c-'0'<10U; c = shgetc(f));
236
248k
    shunget(f);
237
248k
    return neg ? -y : y;
238
261k
}
239
240
53.2M
#define LD_B1B_DIG 2
241
2.83M
#define LD_B1B_MAX 9007199, 254740991
242
84.5M
#define KMAX 128
243
80.6M
#define MASK (KMAX-1)
244
245
static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok)
246
2.83M
{
247
2.83M
    uint32_t x[KMAX];
248
2.83M
    static const uint32_t th[] = { LD_B1B_MAX };
249
2.83M
    int i, j, k, a, z;
250
2.83M
    long long lrp=0, dc=0;
251
2.83M
    long long e10=0;
252
2.83M
    int lnz = 0;
253
2.83M
    int gotdig = 0, gotrad = 0;
254
2.83M
    int rp;
255
2.83M
    int e2;
256
2.83M
    int emax = -emin-bits+3;
257
2.83M
    int denormal = 0;
258
2.83M
    double y;
259
2.83M
    double frac=0;
260
2.83M
    double bias=0;
261
2.83M
    static const int p10s[] = { 10, 100, 1000, 10000,
262
2.83M
        100000, 1000000, 10000000, 100000000 };
263
264
2.83M
    j=0;
265
2.83M
    k=0;
266
267
    /* Don't let leading zeros consume buffer space */
268
3.84M
    for (; c=='0'; c = shgetc(f)) gotdig=1;
269
2.83M
    if (c=='.') {
270
33.6k
        gotrad = 1;
271
56.4k
        for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
272
33.6k
    }
273
274
2.83M
    x[0] = 0;
275
7.54M
    for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
276
4.71M
        if (c == '.') {
277
861k
            if (gotrad) break;
278
852k
            gotrad = 1;
279
852k
            lrp = dc;
280
3.85M
        } else if (k < KMAX-3) {
281
3.82M
            dc++;
282
3.82M
            if (c!='0') lnz = dc;
283
3.82M
            if (j) x[k] = x[k]*10 + c-'0';
284
2.24M
            else x[k] = c-'0';
285
3.82M
            if (++j==9) {
286
66.9k
                k++;
287
66.9k
                j=0;
288
66.9k
            }
289
3.82M
            gotdig=1;
290
3.82M
        } else {
291
30.2k
            dc++;
292
30.2k
            if (c!='0') {
293
255
                lnz = (KMAX-4)*9;
294
255
                x[KMAX-4] |= 1;
295
255
            }
296
30.2k
        }
297
4.71M
    }
298
2.83M
    if (!gotrad) lrp=dc;
299
300
2.83M
    if (gotdig && (c|32)=='e') {
301
227k
        e10 = scanexp(f, pok);
302
227k
        if (e10 == LLONG_MIN) {
303
8.86k
            if (pok) {
304
0
                shunget(f);
305
8.86k
            } else {
306
8.86k
                shlim(f, 0);
307
8.86k
                return 0;
308
8.86k
            }
309
0
            e10 = 0;
310
0
        }
311
218k
        lrp += e10;
312
2.60M
    } else if (c>=0) {
313
2.12M
        shunget(f);
314
2.12M
    }
315
2.82M
    if (!gotdig) {
316
64.6k
        errno = EINVAL;
317
64.6k
        shlim(f, 0);
318
64.6k
        return 0;
319
64.6k
    }
320
321
    /* Handle zero specially to avoid nasty special cases later */
322
2.76M
    if (!x[0]) return sign * 0.0;
323
324
    /* Optimize small integers (w/no exponent) and over/under-flow */
325
2.17M
    if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
326
1.07M
        return sign * (double)x[0];
327
1.10M
    if (lrp > -emin/2) {
328
55.8k
        errno = ERANGE;
329
55.8k
        return sign * DBL_MAX * DBL_MAX;
330
55.8k
    }
331
1.05M
    if (lrp < emin-2*DBL_MANT_DIG) {
332
108k
        errno = ERANGE;
333
108k
        return sign * DBL_MIN * DBL_MIN;
334
108k
    }
335
336
    /* Align incomplete final B1B digit */
337
944k
    if (j) {
338
7.45M
        for (; j<9; j++) x[k]*=10;
339
937k
        k++;
340
937k
        j=0;
341
937k
    }
342
343
944k
    a = 0;
344
944k
    z = k;
345
944k
    e2 = 0;
346
944k
    rp = lrp;
347
348
    /* Optimize small to mid-size integers (even in exp. notation) */
349
944k
    if (lnz<9 && lnz<=rp && rp < 18) {
350
17.6k
        int bitlim;
351
17.6k
        if (rp == 9) return sign * (double)x[0];
352
14.2k
        if (rp < 9) return sign * (double)x[0] / p10s[8-rp];
353
9.62k
        bitlim = bits-3*(int)(rp-9);
354
9.62k
        if (bitlim>30 || x[0]>>bitlim==0)
355
4.16k
            return sign * (double)x[0] * p10s[rp-10];
356
9.62k
    }
357
358
    /* Drop trailing zeros */
359
941k
    for (; !x[z-1]; z--);
360
361
    /* Align radix point to B1B digit boundary */
362
932k
    if (rp % 9) {
363
902k
        int rpm9 = rp>=0 ? rp%9 : rp%9+9;
364
902k
        int p10 = p10s[8-rpm9];
365
902k
        uint32_t carry = 0;
366
1.84M
        for (k=a; k!=z; k++) {
367
946k
            uint32_t tmp = x[k] % p10;
368
946k
            x[k] = x[k]/p10 + carry;
369
946k
            carry = 1000000000/p10 * tmp;
370
946k
            if (k==a && !x[k]) {
371
0
                a = (a+1 & MASK);
372
0
                rp -= 9;
373
0
            }
374
946k
        }
375
902k
        if (carry) x[z++] = carry;
376
902k
        rp += 9-rpm9;
377
902k
    }
378
379
    /* Upscale until desired number of bits are left of radix point */
380
2.97M
    while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
381
2.03M
        uint32_t carry = 0;
382
2.03M
        e2 -= 29;
383
10.4M
        for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
384
10.4M
            uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
385
10.4M
            if (tmp > 1000000000) {
386
10.4M
                carry = tmp / 1000000000;
387
10.4M
                x[k] = tmp % 1000000000;
388
10.4M
            } else {
389
36.4k
                carry = 0;
390
36.4k
                x[k] = tmp;
391
36.4k
            }
392
10.4M
            if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
393
10.4M
            if (k==a) break;
394
10.4M
        }
395
2.03M
        if (carry) {
396
2.02M
            rp += 9;
397
2.02M
            a = (a-1 & MASK);
398
2.02M
            if (a == z) {
399
2.01k
                z = (z-1 & MASK);
400
2.01k
                x[z-1 & MASK] |= x[z];
401
2.01k
            }
402
2.02M
            x[a] = carry;
403
2.02M
        }
404
2.03M
    }
405
406
    /* Downscale until exactly number of bits are left of radix point */
407
8.96M
    for (;;) {
408
8.96M
        uint32_t carry = 0;
409
8.96M
        int sh = 1;
410
8.97M
        for (i=0; i<LD_B1B_DIG; i++) {
411
8.97M
            k = (a+i & MASK);
412
8.97M
            if (k == z || x[k] < th[i]) {
413
2.98M
                i=LD_B1B_DIG;
414
2.98M
                break;
415
2.98M
            }
416
5.99M
            if (x[a+i & MASK] > th[i]) break;
417
5.99M
        }
418
8.96M
        if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
419
        /* FIXME: find a way to compute optimal sh */
420
8.02M
        if (rp > 9+9*LD_B1B_DIG) sh = 9;
421
8.02M
        e2 += sh;
422
43.8M
        for (k=a; k!=z; k=(k+1 & MASK)) {
423
35.8M
            uint32_t tmp = x[k] & (1<<sh)-1;
424
35.8M
            x[k] = (x[k]>>sh) + carry;
425
35.8M
            carry = (1000000000>>sh) * tmp;
426
35.8M
            if (k==a && !x[k]) {
427
937k
                a = (a+1 & MASK);
428
937k
                i--;
429
937k
                rp -= 9;
430
937k
            }
431
35.8M
        }
432
8.02M
        if (carry) {
433
209k
            if ((z+1 & MASK) != a) {
434
204k
                x[z] = carry;
435
204k
                z = (z+1 & MASK);
436
204k
            } else x[z-1 & MASK] |= 1;
437
209k
        }
438
8.02M
    }
439
440
    /* Assemble desired bits into floating point variable */
441
2.79M
    for (y=i=0; i<LD_B1B_DIG; i++) {
442
1.86M
        if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
443
1.86M
        y = 1000000000.0L * y + x[a+i & MASK];
444
1.86M
    }
445
446
932k
    y *= sign;
447
448
    /* Limit precision for denormal results */
449
932k
    if (bits > DBL_MANT_DIG+e2-emin) {
450
15.7k
        bits = DBL_MANT_DIG+e2-emin;
451
15.7k
        if (bits<0) bits=0;
452
15.7k
        denormal = 1;
453
15.7k
    }
454
455
    /* Calculate bias term to force rounding, move out lower bits */
456
932k
    if (bits < DBL_MANT_DIG) {
457
87.5k
        bias = copysign(scalbn(1, 2*DBL_MANT_DIG-bits-1), y);
458
87.5k
        frac = fmod(y, scalbn(1, DBL_MANT_DIG-bits));
459
87.5k
        y -= frac;
460
87.5k
        y += bias;
461
87.5k
    }
462
463
    /* Process tail of decimal input so it can affect rounding */
464
932k
    if ((a+i & MASK) != z) {
465
908k
        uint32_t t = x[a+i & MASK];
466
908k
        if (t < 500000000 && (t || (a+i+1 & MASK) != z))
467
434k
            frac += 0.25*sign;
468
473k
        else if (t > 500000000)
469
468k
            frac += 0.75*sign;
470
4.26k
        else if (t == 500000000) {
471
4.26k
            if ((a+i+1 & MASK) == z)
472
2.48k
                frac += 0.5*sign;
473
1.77k
            else
474
1.77k
                frac += 0.75*sign;
475
4.26k
        }
476
908k
        if (DBL_MANT_DIG-bits >= 2 && !fmod(frac, 1))
477
13.9k
            frac++;
478
908k
    }
479
480
932k
    y += frac;
481
932k
    y -= bias;
482
483
932k
    if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) {
484
54.8k
        if (fabs(y) >= pow(2, DBL_MANT_DIG)) {
485
17.1k
            if (denormal && bits==DBL_MANT_DIG+e2-emin)
486
9.07k
                denormal = 0;
487
17.1k
            y *= 0.5;
488
17.1k
            e2++;
489
17.1k
        }
490
54.8k
        if (e2+DBL_MANT_DIG>emax || (denormal && frac))
491
54.8k
            errno = ERANGE;
492
54.8k
    }
493
494
932k
    return scalbn(y, e2);
495
944k
}
496
497
static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
498
115k
{
499
115k
    uint32_t x = 0;
500
115k
    double y = 0;
501
115k
    double scale = 1;
502
115k
    double bias = 0;
503
115k
    int gottail = 0, gotrad = 0, gotdig = 0;
504
115k
    long long rp = 0;
505
115k
    long long dc = 0;
506
115k
    long long e2 = 0;
507
115k
    int d;
508
115k
    int c;
509
510
115k
    c = shgetc(f);
511
512
    /* Skip leading zeros */
513
215k
    for (; c=='0'; c = shgetc(f))
514
100k
        gotdig = 1;
515
516
115k
    if (c=='.') {
517
34.7k
        gotrad = 1;
518
34.7k
        c = shgetc(f);
519
        /* Count zeros after the radix point before significand */
520
177k
        for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
521
34.7k
    }
522
523
1.69M
    for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
524
1.58M
        if (c=='.') {
525
13.8k
            if (gotrad) break;
526
8.72k
            rp = dc;
527
8.72k
            gotrad = 1;
528
1.57M
        } else {
529
1.57M
            gotdig = 1;
530
1.57M
            if (c > '9') d = (c|32)+10-'a';
531
1.39M
            else d = c-'0';
532
1.57M
            if (dc<8) {
533
287k
                x = x*16 + d;
534
1.28M
            } else if (dc < DBL_MANT_DIG/4+1) {
535
63.3k
                y += d*(scale/=16);
536
1.22M
            } else if (d && !gottail) {
537
3.99k
                y += 0.5*scale;
538
3.99k
                gottail = 1;
539
3.99k
            }
540
1.57M
            dc++;
541
1.57M
        }
542
1.58M
    }
543
115k
    if (!gotdig) {
544
12.6k
        shunget(f);
545
12.6k
        if (pok) {
546
0
            shunget(f);
547
0
            if (gotrad) shunget(f);
548
12.6k
        } else {
549
12.6k
            shlim(f, 0);
550
12.6k
        }
551
12.6k
        return sign * 0.0;
552
12.6k
    }
553
102k
    if (!gotrad) rp = dc;
554
635k
    while (dc<8) x *= 16, dc++;
555
102k
    if ((c|32)=='p') {
556
34.2k
        e2 = scanexp(f, pok);
557
34.2k
        if (e2 == LLONG_MIN) {
558
4.20k
            if (pok) {
559
0
                shunget(f);
560
4.20k
            } else {
561
4.20k
                shlim(f, 0);
562
4.20k
                return 0;
563
4.20k
            }
564
0
            e2 = 0;
565
0
        }
566
68.2k
    } else {
567
68.2k
        shunget(f);
568
68.2k
    }
569
98.3k
    e2 += 4*rp - 32;
570
571
98.3k
    if (!x) return sign * 0.0;
572
69.1k
    if (e2 > -emin) {
573
4.85k
        errno = ERANGE;
574
4.85k
        return sign * DBL_MAX * DBL_MAX;
575
4.85k
    }
576
64.3k
    if (e2 < emin-2*DBL_MANT_DIG) {
577
3.54k
        errno = ERANGE;
578
3.54k
        return sign * DBL_MIN * DBL_MIN;
579
3.54k
    }
580
581
157k
    while (x < 0x80000000) {
582
96.2k
        if (y>=0.5) {
583
17.4k
            x += x + 1;
584
17.4k
            y += y - 1;
585
78.7k
        } else {
586
78.7k
            x += x;
587
78.7k
            y += y;
588
78.7k
        }
589
96.2k
        e2--;
590
96.2k
    }
591
592
60.7k
    if (bits > 32+e2-emin) {
593
10.2k
        bits = 32+e2-emin;
594
10.2k
        if (bits<0) bits=0;
595
10.2k
    }
596
597
60.7k
    if (bits < DBL_MANT_DIG)
598
53.8k
        bias = copysign(scalbn(1, 32+DBL_MANT_DIG-bits-1), sign);
599
600
60.7k
    if (bits<32 && y && !(x&1)) x++, y=0;
601
602
60.7k
    y = bias + sign*(double)x + sign*y;
603
60.7k
    y -= bias;
604
605
60.7k
    if (!y) errno = ERANGE;
606
607
60.7k
    return scalbn(y, e2);
608
64.3k
}
609
610
static double fffloatscan(FFFILE *f, int prec, int pok)
611
2.99M
{
612
2.99M
    int sign = 1;
613
2.99M
    size_t i;
614
2.99M
    int bits;
615
2.99M
    int emin;
616
2.99M
    int c;
617
618
2.99M
    switch (prec) {
619
1.48M
    case 0:
620
1.48M
        bits = FLT_MANT_DIG;
621
1.48M
        emin = FLT_MIN_EXP-bits;
622
1.48M
        break;
623
1.50M
    case 1:
624
1.50M
        bits = DBL_MANT_DIG;
625
1.50M
        emin = DBL_MIN_EXP-bits;
626
1.50M
        break;
627
0
    case 2:
628
0
        bits = DBL_MANT_DIG;
629
0
        emin = DBL_MIN_EXP-bits;
630
0
        break;
631
0
    default:
632
0
        return 0;
633
2.99M
    }
634
635
2.99M
    while (av_isspace((c = shgetc(f))));
636
637
2.99M
    if (c=='+' || c=='-') {
638
113k
        sign -= 2*(c=='-');
639
113k
        c = shgetc(f);
640
113k
    }
641
642
3.06M
    for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
643
74.0k
        if (i<7) c = shgetc(f);
644
2.99M
    if (i==3 || i==8 || (i>3 && pok)) {
645
9.99k
        if (i!=8) {
646
5.46k
            shunget(f);
647
5.46k
            if (pok) for (; i>3; i--) shunget(f);
648
5.46k
        }
649
9.99k
        return sign * INFINITY;
650
9.99k
    }
651
3.02M
    if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
652
53.1k
        if (i<2) c = shgetc(f);
653
2.98M
    if (i==3) {
654
15.8k
        if (shgetc(f) != '(') {
655
7.83k
            shunget(f);
656
7.83k
            return NAN;
657
7.83k
        }
658
42.9k
        for (i=1; ; i++) {
659
42.9k
            c = shgetc(f);
660
42.9k
            if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_')
661
34.9k
                continue;
662
7.99k
            if (c==')') return NAN;
663
5.93k
            shunget(f);
664
5.93k
            if (!pok) {
665
5.93k
                errno = EINVAL;
666
5.93k
                shlim(f, 0);
667
5.93k
                return 0;
668
5.93k
            }
669
0
            while (i--) shunget(f);
670
0
            return NAN;
671
5.93k
        }
672
7.99k
    }
673
674
2.96M
    if (i) {
675
15.8k
        shunget(f);
676
15.8k
        errno = EINVAL;
677
15.8k
        shlim(f, 0);
678
15.8k
        return 0;
679
15.8k
    }
680
681
2.95M
    if (c=='0') {
682
1.06M
        c = shgetc(f);
683
1.06M
        if ((c|32) == 'x')
684
115k
            return hexfloat(f, bits, emin, sign, pok);
685
952k
        shunget(f);
686
952k
        c = '0';
687
952k
    }
688
689
2.83M
    return decfloat(f, c, bits, emin, sign, pok);
690
2.95M
}
691
692
static void *arg_n(va_list ap, unsigned int n)
693
0
{
694
0
    void *p;
695
0
    unsigned int i;
696
0
    va_list ap2;
697
0
    va_copy(ap2, ap);
698
0
    for (i=n; i>1; i--) va_arg(ap2, void *);
699
0
    p = va_arg(ap2, void *);
700
0
    va_end(ap2);
701
0
    return p;
702
0
}
703
704
static void store_int(void *dest, int size, unsigned long long i)
705
12.4M
{
706
12.4M
    if (!dest) return;
707
12.4M
    switch (size) {
708
0
    case SIZE_hh:
709
0
        *(char *)dest = i;
710
0
        break;
711
0
    case SIZE_h:
712
0
        *(short *)dest = i;
713
0
        break;
714
8.34M
    case SIZE_def:
715
8.34M
        *(int *)dest = i;
716
8.34M
        break;
717
0
    case SIZE_l:
718
0
        *(long *)dest = i;
719
0
        break;
720
4.09M
    case SIZE_ll:
721
4.09M
        *(long long *)dest = i;
722
4.09M
        break;
723
12.4M
    }
724
12.4M
}
725
726
static int ff_vfscanf(FFFILE *f, const char *fmt, va_list ap)
727
9.24M
{
728
9.24M
    int width;
729
9.24M
    int size;
730
9.24M
    int base;
731
9.24M
    const unsigned char *p;
732
9.24M
    int c, t;
733
9.24M
    char *s;
734
9.24M
    void *dest=NULL;
735
9.24M
    int invert;
736
9.24M
    int matches=0;
737
9.24M
    unsigned long long x;
738
9.24M
    double y;
739
9.24M
    ptrdiff_t pos = 0;
740
9.24M
    unsigned char scanset[257];
741
9.24M
    size_t i;
742
743
32.2M
    for (p=(const unsigned char *)fmt; *p; p++) {
744
745
29.5M
        if (av_isspace(*p)) {
746
16.7k
            while (av_isspace(p[1])) p++;
747
16.7k
            shlim(f, 0);
748
16.7k
            while (av_isspace(shgetc(f)));
749
16.7k
            shunget(f);
750
16.7k
            pos += shcnt(f);
751
16.7k
            continue;
752
16.7k
        }
753
29.5M
        if (*p != '%' || p[1] == '%') {
754
8.43M
            shlim(f, 0);
755
8.43M
            if (*p == '%') {
756
0
                p++;
757
0
                while (av_isspace((c=shgetc(f))));
758
8.43M
            } else {
759
8.43M
                c = shgetc(f);
760
8.43M
            }
761
8.43M
            if (c!=*p) {
762
5.17M
                shunget(f);
763
5.17M
                if (c<0) goto input_fail;
764
3.12M
                goto match_fail;
765
5.17M
            }
766
3.26M
            pos += shcnt(f);
767
3.26M
            continue;
768
8.43M
        }
769
770
21.0M
        p++;
771
21.0M
        if (*p=='*') {
772
683k
            dest = 0; p++;
773
20.3M
        } else if (av_isdigit(*p) && p[1]=='$') {
774
0
            dest = arg_n(ap, *p-'0'); p+=2;
775
20.3M
        } else {
776
20.3M
            dest = va_arg(ap, void *);
777
20.3M
        }
778
779
22.5M
        for (width=0; av_isdigit(*p); p++) {
780
1.49M
            width = 10*width + *p - '0';
781
1.49M
        }
782
783
21.0M
        if (*p=='m') {
784
0
            s = 0;
785
0
            p++;
786
0
        }
787
788
21.0M
        size = SIZE_def;
789
21.0M
        switch (*p++) {
790
0
        case 'h':
791
0
            if (*p == 'h') p++, size = SIZE_hh;
792
0
            else size = SIZE_h;
793
0
            break;
794
6.13M
        case 'l':
795
6.13M
            if (*p == 'l') p++, size = SIZE_ll;
796
1.50M
            else size = SIZE_l;
797
6.13M
            break;
798
0
        case 'j':
799
0
            size = SIZE_ll;
800
0
            break;
801
0
        case 'z':
802
0
        case 't':
803
0
            size = SIZE_l;
804
0
            break;
805
0
        case 'L':
806
0
            size = SIZE_L;
807
0
            break;
808
4.26M
        case 'd': case 'i': case 'o': case 'u': case 'x':
809
5.76M
        case 'a': case 'e': case 'f': case 'g':
810
5.76M
        case 'A': case 'E': case 'F': case 'G': case 'X':
811
10.8M
        case 's': case 'c': case '[':
812
10.8M
        case 'S': case 'C':
813
14.9M
        case 'p': case 'n':
814
14.9M
            p--;
815
14.9M
            break;
816
0
        default:
817
0
            goto fmt_fail;
818
21.0M
        }
819
820
21.0M
        t = *p;
821
822
        /* C or S */
823
21.0M
        if ((t&0x2f) == 3) {
824
0
            t |= 32;
825
0
            size = SIZE_l;
826
0
        }
827
828
21.0M
        switch (t) {
829
2.90M
            case 'c':
830
2.90M
                if (width < 1) width = 1;
831
5.08M
            case '[':
832
5.08M
                break;
833
4.09M
            case 'n':
834
4.09M
                store_int(dest, size, pos);
835
                /* do not increment match count, etc! */
836
4.09M
                continue;
837
11.8M
            default:
838
11.8M
                shlim(f, 0);
839
13.4M
                while (av_isspace(shgetc(f)));
840
11.8M
                shunget(f);
841
11.8M
                pos += shcnt(f);
842
21.0M
        }
843
844
16.9M
        shlim(f, width);
845
16.9M
        if (shgetc(f) < 0) goto input_fail;
846
15.8M
        shunget(f);
847
848
15.8M
        switch (t) {
849
0
            case 's':
850
2.25M
            case 'c':
851
4.43M
            case '[':
852
4.43M
                if (t == 'c' || t == 's') {
853
2.25M
                    memset(scanset, -1, sizeof scanset);
854
2.25M
                    scanset[0] = 0;
855
2.25M
                    if (t == 's') {
856
0
                        scanset[1 + '\t'] = 0;
857
0
                        scanset[1 + '\n'] = 0;
858
0
                        scanset[1 + '\v'] = 0;
859
0
                        scanset[1 + '\f'] = 0;
860
0
                        scanset[1 + '\r'] = 0;
861
0
                        scanset[1 + ' ' ] = 0;
862
0
                    }
863
2.25M
                } else {
864
2.17M
                    if (*++p == '^') p++, invert = 1;
865
2.17M
                    else invert = 0;
866
2.17M
                    memset(scanset, invert, sizeof scanset);
867
2.17M
                    scanset[0] = 0;
868
2.17M
                    if (*p == '-') p++, scanset[1+'-'] = 1-invert;
869
2.17M
                    else if (*p == ']') p++, scanset[1+']'] = 1-invert;
870
6.53M
                    for (; *p != ']'; p++) {
871
4.35M
                        if (!*p) goto fmt_fail;
872
4.35M
                        if (*p=='-' && p[1] && p[1] != ']')
873
0
                            for (c=p++[-1]; c<*p; c++)
874
0
                                scanset[1+c] = 1-invert;
875
4.35M
                        scanset[1+*p] = 1-invert;
876
4.35M
                    }
877
2.17M
                }
878
4.43M
                s = 0;
879
4.43M
                i = 0;
880
4.43M
                if ((s = dest)) {
881
7.50M
                    while (scanset[(c=shgetc(f))+1])
882
3.75M
                        s[i++] = c;
883
3.74M
                } else {
884
1.36M
                    while (scanset[(c=shgetc(f))+1]);
885
683k
                }
886
4.43M
                shunget(f);
887
4.43M
                if (!shcnt(f)) goto match_fail;
888
4.43M
                if (t == 'c' && shcnt(f) != width) goto match_fail;
889
4.43M
                if (t != 'c') {
890
2.17M
                    if (s) s[i] = 0;
891
2.17M
                }
892
4.43M
                break;
893
0
            case 'p':
894
0
            case 'X':
895
0
            case 'x':
896
0
                base = 16;
897
0
                goto int_common;
898
0
            case 'o':
899
0
                base = 8;
900
0
                goto int_common;
901
6.94M
            case 'd':
902
8.43M
            case 'u':
903
8.43M
                base = 10;
904
8.43M
                goto int_common;
905
0
            case 'i':
906
0
                base = 0;
907
8.43M
int_common:
908
8.43M
                x = ffintscan(f, base, 0, ULLONG_MAX);
909
8.43M
                if (!shcnt(f))
910
97.3k
                    goto match_fail;
911
8.34M
                if (t=='p' && dest)
912
0
                    *(void **)dest = (void *)(uintptr_t)x;
913
8.34M
                else
914
8.34M
                    store_int(dest, size, x);
915
8.34M
                break;
916
0
            case 'a': case 'A':
917
0
            case 'e': case 'E':
918
2.99M
            case 'f': case 'F':
919
2.99M
            case 'g': case 'G':
920
2.99M
                y = fffloatscan(f, size, 0);
921
2.99M
                if (!shcnt(f))
922
112k
                    goto match_fail;
923
2.88M
                if (dest) {
924
2.88M
                    switch (size) {
925
1.38M
                    case SIZE_def:
926
1.38M
                        *(float *)dest = y;
927
1.38M
                        break;
928
1.49M
                    case SIZE_l:
929
1.49M
                        *(double *)dest = y;
930
1.49M
                        break;
931
0
                    case SIZE_L:
932
0
                        *(double *)dest = y;
933
0
                        break;
934
2.88M
                    }
935
2.88M
                }
936
2.88M
                break;
937
15.8M
        }
938
939
15.6M
        pos += shcnt(f);
940
15.6M
        if (dest) matches++;
941
15.6M
    }
942
2.74M
    if (0) {
943
0
fmt_fail:
944
3.16M
input_fail:
945
3.16M
        if (!matches) matches--;
946
3.16M
    }
947
9.24M
match_fail:
948
9.24M
    return matches;
949
2.74M
}
950
951
static int ff_vsscanf(const char *s, const char *fmt, va_list ap)
952
9.24M
{
953
9.24M
    FFFILE f = {
954
9.24M
        .buf = (void *)s, .cookie = (void *)s,
955
9.24M
        .read = ffstring_read,
956
9.24M
    };
957
958
9.24M
    return ff_vfscanf(&f, fmt, ap);
959
9.24M
}
960
961
int av_sscanf(const char *string, const char *format, ...)
962
9.24M
{
963
9.24M
    int ret;
964
9.24M
    va_list ap;
965
9.24M
    va_start(ap, format);
966
9.24M
    ret = ff_vsscanf(string, format, ap);
967
    va_end(ap);
968
9.24M
    return ret;
969
9.24M
}