Coverage Report

Created: 2026-04-01 07:42

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
34.9M
#define SIZE_def 0
50
4.26M
#define SIZE_l   1
51
0
#define SIZE_L   2
52
11.2M
#define SIZE_ll  3
53
54
74.1M
#define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
55
56
static int fftoread(FFFILE *f)
57
16.6M
{
58
16.6M
    f->rpos = f->rend = f->buf + f->buf_size;
59
16.6M
    return 0;
60
16.6M
}
61
62
static size_t ffstring_read(FFFILE *f, unsigned char *buf, size_t len)
63
16.6M
{
64
16.6M
    char *src = f->cookie;
65
16.6M
    size_t k = len+256;
66
16.6M
    char *end = memchr(src, 0, k);
67
68
16.6M
    if (end) k = end-src;
69
16.6M
    if (k < len) len = k;
70
16.6M
    memcpy(buf, src, len);
71
16.6M
    f->rpos = (void *)(src+len);
72
16.6M
    f->rend = (void *)(src+k);
73
16.6M
    f->cookie = src+k;
74
75
16.6M
    return len;
76
16.6M
}
77
78
static int ffuflow(FFFILE *f)
79
16.6M
{
80
16.6M
    unsigned char c;
81
16.6M
    if (!fftoread(f) && f->read(f, &c, 1)==1) return c;
82
6.79M
    return EOF;
83
16.6M
}
84
85
static void ffshlim(FFFILE *f, ptrdiff_t lim)
86
43.2M
{
87
43.2M
    f->shlim = lim;
88
43.2M
    f->shcnt = f->buf - f->rpos;
89
    /* If lim is nonzero, rend must be a valid pointer. */
90
43.2M
    if (lim && f->rend - f->rpos > lim)
91
605k
        f->shend = f->rpos + lim;
92
42.6M
    else
93
42.6M
        f->shend = f->rend;
94
43.2M
}
95
96
static int ffshgetc(FFFILE *f)
97
18.9M
{
98
18.9M
    int c;
99
18.9M
    ptrdiff_t cnt = shcnt(f);
100
18.9M
    if (f->shlim && cnt >= f->shlim || (c=ffuflow(f)) < 0) {
101
9.08M
        f->shcnt = f->buf - f->rpos + cnt;
102
9.08M
        f->shend = 0;
103
9.08M
        return EOF;
104
9.08M
    }
105
9.86M
    cnt++;
106
9.86M
    if (f->shlim && f->rend - f->rpos > f->shlim - cnt)
107
2.97M
        f->shend = f->rpos + (f->shlim - cnt);
108
6.89M
    else
109
6.89M
        f->shend = f->rend;
110
9.86M
    f->shcnt = f->buf - f->rpos + cnt;
111
9.86M
    if (f->rpos[-1] != c) f->rpos[-1] = c;
112
9.86M
    return c;
113
18.9M
}
114
115
43.2M
#define shlim(f, lim) ffshlim((f), (lim))
116
96.3M
#define shgetc(f) (((f)->rpos < (f)->shend) ? *(f)->rpos++ : ffshgetc(f))
117
57.5M
#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
10.2M
{
140
10.2M
    const unsigned char *val = table+1;
141
10.2M
    int c, neg=0;
142
10.2M
    unsigned x;
143
10.2M
    unsigned long long y;
144
10.2M
    if (base > 36 || base == 1) {
145
0
        errno = EINVAL;
146
0
        return 0;
147
0
    }
148
10.2M
    while (av_isspace((c=shgetc(f))));
149
10.2M
    if (c=='+' || c=='-') {
150
546k
        neg = -(c=='-');
151
546k
        c = shgetc(f);
152
546k
    }
153
10.2M
    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
10.2M
    } else {
168
10.2M
        if (base == 0) base = 10;
169
10.2M
        if (val[c] >= base) {
170
102k
            shunget(f);
171
102k
            shlim(f, 0);
172
102k
            errno = EINVAL;
173
102k
            return 0;
174
102k
        }
175
10.2M
    }
176
10.1M
    if (base == 10) {
177
20.9M
        for (x=0; c-'0'<10U && x<=UINT_MAX/10-1; c=shgetc(f))
178
10.8M
            x = x*10 + (c-'0');
179
10.1M
        for (y=x; c-'0'<10U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(c-'0'); c=shgetc(f))
180
72.7k
            y = y*10 + (c-'0');
181
10.1M
        if (c-'0'>=10U) goto done;
182
10.1M
    } 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
3.57k
    if (val[c]<base) {
195
2.20M
        for (; val[c]<base; c=shgetc(f));
196
3.57k
        errno = ERANGE;
197
3.57k
        y = lim;
198
3.57k
        if (lim&1) neg = 0;
199
3.57k
    }
200
10.1M
done:
201
10.1M
    shunget(f);
202
10.1M
    if (y>=lim) {
203
3.57k
        if (!(lim&1) && !neg) {
204
0
            errno = ERANGE;
205
0
            return lim-1;
206
3.57k
        } else if (y>lim) {
207
0
            errno = ERANGE;
208
0
            return lim;
209
0
        }
210
3.57k
    }
211
10.1M
    return (y^neg)-neg;
212
10.1M
}
213
214
static long long scanexp(FFFILE *f, int pok)
215
81.9k
{
216
81.9k
    int c;
217
81.9k
    int x;
218
81.9k
    long long y;
219
81.9k
    int neg = 0;
220
221
81.9k
    c = shgetc(f);
222
81.9k
    if (c=='+' || c=='-') {
223
45.2k
        neg = (c=='-');
224
45.2k
        c = shgetc(f);
225
45.2k
        if (c-'0'>=10U && pok) shunget(f);
226
45.2k
    }
227
81.9k
    if (c-'0'>=10U) {
228
12.9k
        shunget(f);
229
12.9k
        return LLONG_MIN;
230
12.9k
    }
231
396k
    for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
232
327k
        x = 10*x + (c-'0');
233
142k
    for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f))
234
73.8k
        y = 10*y + (c-'0');
235
92.2k
    for (; c-'0'<10U; c = shgetc(f));
236
68.9k
    shunget(f);
237
68.9k
    return neg ? -y : y;
238
81.9k
}
239
240
53.7M
#define LD_B1B_DIG 2
241
2.64M
#define LD_B1B_MAX 9007199, 254740991
242
83.8M
#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.64M
{
247
2.64M
    uint32_t x[KMAX];
248
2.64M
    static const uint32_t th[] = { LD_B1B_MAX };
249
2.64M
    int i, j, k, a, z;
250
2.64M
    long long lrp=0, dc=0;
251
2.64M
    long long e10=0;
252
2.64M
    int lnz = 0;
253
2.64M
    int gotdig = 0, gotrad = 0;
254
2.64M
    int rp;
255
2.64M
    int e2;
256
2.64M
    int emax = -emin-bits+3;
257
2.64M
    int denormal = 0;
258
2.64M
    double y;
259
2.64M
    double frac=0;
260
2.64M
    double bias=0;
261
2.64M
    static const int p10s[] = { 10, 100, 1000, 10000,
262
2.64M
        100000, 1000000, 10000000, 100000000 };
263
264
2.64M
    j=0;
265
2.64M
    k=0;
266
267
    /* Don't let leading zeros consume buffer space */
268
4.04M
    for (; c=='0'; c = shgetc(f)) gotdig=1;
269
2.64M
    if (c=='.') {
270
32.1k
        gotrad = 1;
271
54.9k
        for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
272
32.1k
    }
273
274
2.64M
    x[0] = 0;
275
6.72M
    for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
276
4.08M
        if (c == '.') {
277
901k
            if (gotrad) break;
278
892k
            gotrad = 1;
279
892k
            lrp = dc;
280
3.18M
        } else if (k < KMAX-3) {
281
3.17M
            dc++;
282
3.17M
            if (c!='0') lnz = dc;
283
3.17M
            if (j) x[k] = x[k]*10 + c-'0';
284
1.51M
            else x[k] = c-'0';
285
3.17M
            if (++j==9) {
286
76.0k
                k++;
287
76.0k
                j=0;
288
76.0k
            }
289
3.17M
            gotdig=1;
290
3.17M
        } else {
291
6.25k
            dc++;
292
6.25k
            if (c!='0') {
293
242
                lnz = (KMAX-4)*9;
294
242
                x[KMAX-4] |= 1;
295
242
            }
296
6.25k
        }
297
4.08M
    }
298
2.64M
    if (!gotrad) lrp=dc;
299
300
2.64M
    if (gotdig && (c|32)=='e') {
301
54.1k
        e10 = scanexp(f, pok);
302
54.1k
        if (e10 == LLONG_MIN) {
303
8.89k
            if (pok) {
304
0
                shunget(f);
305
8.89k
            } else {
306
8.89k
                shlim(f, 0);
307
8.89k
                return 0;
308
8.89k
            }
309
0
            e10 = 0;
310
0
        }
311
45.3k
        lrp += e10;
312
2.59M
    } else if (c>=0) {
313
2.45M
        shunget(f);
314
2.45M
    }
315
2.64M
    if (!gotdig) {
316
57.5k
        errno = EINVAL;
317
57.5k
        shlim(f, 0);
318
57.5k
        return 0;
319
57.5k
    }
320
321
    /* Handle zero specially to avoid nasty special cases later */
322
2.58M
    if (!x[0]) return sign * 0.0;
323
324
    /* Optimize small integers (w/no exponent) and over/under-flow */
325
1.43M
    if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
326
455k
        return sign * (double)x[0];
327
980k
    if (lrp > -emin/2) {
328
9.36k
        errno = ERANGE;
329
9.36k
        return sign * DBL_MAX * DBL_MAX;
330
9.36k
    }
331
971k
    if (lrp < emin-2*DBL_MANT_DIG) {
332
5.16k
        errno = ERANGE;
333
5.16k
        return sign * DBL_MIN * DBL_MIN;
334
5.16k
    }
335
336
    /* Align incomplete final B1B digit */
337
966k
    if (j) {
338
7.61M
        for (; j<9; j++) x[k]*=10;
339
958k
        k++;
340
958k
        j=0;
341
958k
    }
342
343
966k
    a = 0;
344
966k
    z = k;
345
966k
    e2 = 0;
346
966k
    rp = lrp;
347
348
    /* Optimize small to mid-size integers (even in exp. notation) */
349
966k
    if (lnz<9 && lnz<=rp && rp < 18) {
350
16.9k
        int bitlim;
351
16.9k
        if (rp == 9) return sign * (double)x[0];
352
13.7k
        if (rp < 9) return sign * (double)x[0] / p10s[8-rp];
353
9.50k
        bitlim = bits-3*(int)(rp-9);
354
9.50k
        if (bitlim>30 || x[0]>>bitlim==0)
355
3.42k
            return sign * (double)x[0] * p10s[rp-10];
356
9.50k
    }
357
358
    /* Drop trailing zeros */
359
965k
    for (; !x[z-1]; z--);
360
361
    /* Align radix point to B1B digit boundary */
362
955k
    if (rp % 9) {
363
929k
        int rpm9 = rp>=0 ? rp%9 : rp%9+9;
364
929k
        int p10 = p10s[8-rpm9];
365
929k
        uint32_t carry = 0;
366
1.90M
        for (k=a; k!=z; k++) {
367
977k
            uint32_t tmp = x[k] % p10;
368
977k
            x[k] = x[k]/p10 + carry;
369
977k
            carry = 1000000000/p10 * tmp;
370
977k
            if (k==a && !x[k]) {
371
0
                a = (a+1 & MASK);
372
0
                rp -= 9;
373
0
            }
374
977k
        }
375
929k
        if (carry) x[z++] = carry;
376
929k
        rp += 9-rpm9;
377
929k
    }
378
379
    /* Upscale until desired number of bits are left of radix point */
380
3.01M
    while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
381
2.06M
        uint32_t carry = 0;
382
2.06M
        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.3M
                carry = tmp / 1000000000;
387
10.3M
                x[k] = tmp % 1000000000;
388
10.3M
            } 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.06M
        if (carry) {
396
2.04M
            rp += 9;
397
2.04M
            a = (a-1 & MASK);
398
2.04M
            if (a == z) {
399
1.71k
                z = (z-1 & MASK);
400
1.71k
                x[z-1 & MASK] |= x[z];
401
1.71k
            }
402
2.04M
            x[a] = carry;
403
2.04M
        }
404
2.06M
    }
405
406
    /* Downscale until exactly number of bits are left of radix point */
407
9.04M
    for (;;) {
408
9.04M
        uint32_t carry = 0;
409
9.04M
        int sh = 1;
410
9.05M
        for (i=0; i<LD_B1B_DIG; i++) {
411
9.05M
            k = (a+i & MASK);
412
9.05M
            if (k == z || x[k] < th[i]) {
413
2.93M
                i=LD_B1B_DIG;
414
2.93M
                break;
415
2.93M
            }
416
6.11M
            if (x[a+i & MASK] > th[i]) break;
417
6.11M
        }
418
9.04M
        if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
419
        /* FIXME: find a way to compute optimal sh */
420
8.08M
        if (rp > 9+9*LD_B1B_DIG) sh = 9;
421
8.08M
        e2 += sh;
422
43.6M
        for (k=a; k!=z; k=(k+1 & MASK)) {
423
35.5M
            uint32_t tmp = x[k] & (1<<sh)-1;
424
35.5M
            x[k] = (x[k]>>sh) + carry;
425
35.5M
            carry = (1000000000>>sh) * tmp;
426
35.5M
            if (k==a && !x[k]) {
427
960k
                a = (a+1 & MASK);
428
960k
                i--;
429
960k
                rp -= 9;
430
960k
            }
431
35.5M
        }
432
8.08M
        if (carry) {
433
187k
            if ((z+1 & MASK) != a) {
434
182k
                x[z] = carry;
435
182k
                z = (z+1 & MASK);
436
182k
            } else x[z-1 & MASK] |= 1;
437
187k
        }
438
8.08M
    }
439
440
    /* Assemble desired bits into floating point variable */
441
2.86M
    for (y=i=0; i<LD_B1B_DIG; i++) {
442
1.91M
        if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
443
1.91M
        y = 1000000000.0L * y + x[a+i & MASK];
444
1.91M
    }
445
446
955k
    y *= sign;
447
448
    /* Limit precision for denormal results */
449
955k
    if (bits > DBL_MANT_DIG+e2-emin) {
450
13.3k
        bits = DBL_MANT_DIG+e2-emin;
451
13.3k
        if (bits<0) bits=0;
452
13.3k
        denormal = 1;
453
13.3k
    }
454
455
    /* Calculate bias term to force rounding, move out lower bits */
456
955k
    if (bits < DBL_MANT_DIG) {
457
69.1k
        bias = copysign(scalbn(1, 2*DBL_MANT_DIG-bits-1), y);
458
69.1k
        frac = fmod(y, scalbn(1, DBL_MANT_DIG-bits));
459
69.1k
        y -= frac;
460
69.1k
        y += bias;
461
69.1k
    }
462
463
    /* Process tail of decimal input so it can affect rounding */
464
955k
    if ((a+i & MASK) != z) {
465
927k
        uint32_t t = x[a+i & MASK];
466
927k
        if (t < 500000000 && (t || (a+i+1 & MASK) != z))
467
416k
            frac += 0.25*sign;
468
510k
        else if (t > 500000000)
469
505k
            frac += 0.75*sign;
470
5.59k
        else if (t == 500000000) {
471
5.59k
            if ((a+i+1 & MASK) == z)
472
3.32k
                frac += 0.5*sign;
473
2.27k
            else
474
2.27k
                frac += 0.75*sign;
475
5.59k
        }
476
927k
        if (DBL_MANT_DIG-bits >= 2 && !fmod(frac, 1))
477
11.5k
            frac++;
478
927k
    }
479
480
955k
    y += frac;
481
955k
    y -= bias;
482
483
955k
    if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) {
484
31.6k
        if (fabs(y) >= pow(2, DBL_MANT_DIG)) {
485
14.6k
            if (denormal && bits==DBL_MANT_DIG+e2-emin)
486
7.17k
                denormal = 0;
487
14.6k
            y *= 0.5;
488
14.6k
            e2++;
489
14.6k
        }
490
31.6k
        if (e2+DBL_MANT_DIG>emax || (denormal && frac))
491
31.6k
            errno = ERANGE;
492
31.6k
    }
493
494
955k
    return scalbn(y, e2);
495
966k
}
496
497
static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
498
103k
{
499
103k
    uint32_t x = 0;
500
103k
    double y = 0;
501
103k
    double scale = 1;
502
103k
    double bias = 0;
503
103k
    int gottail = 0, gotrad = 0, gotdig = 0;
504
103k
    long long rp = 0;
505
103k
    long long dc = 0;
506
103k
    long long e2 = 0;
507
103k
    int d;
508
103k
    int c;
509
510
103k
    c = shgetc(f);
511
512
    /* Skip leading zeros */
513
200k
    for (; c=='0'; c = shgetc(f))
514
97.0k
        gotdig = 1;
515
516
103k
    if (c=='.') {
517
32.5k
        gotrad = 1;
518
32.5k
        c = shgetc(f);
519
        /* Count zeros after the radix point before significand */
520
172k
        for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
521
32.5k
    }
522
523
3.66M
    for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
524
3.56M
        if (c=='.') {
525
10.7k
            if (gotrad) break;
526
6.92k
            rp = dc;
527
6.92k
            gotrad = 1;
528
3.55M
        } else {
529
3.55M
            gotdig = 1;
530
3.55M
            if (c > '9') d = (c|32)+10-'a';
531
3.38M
            else d = c-'0';
532
3.55M
            if (dc<8) {
533
237k
                x = x*16 + d;
534
3.31M
            } else if (dc < DBL_MANT_DIG/4+1) {
535
55.4k
                y += d*(scale/=16);
536
3.25M
            } else if (d && !gottail) {
537
3.92k
                y += 0.5*scale;
538
3.92k
                gottail = 1;
539
3.92k
            }
540
3.55M
            dc++;
541
3.55M
        }
542
3.56M
    }
543
103k
    if (!gotdig) {
544
10.9k
        shunget(f);
545
10.9k
        if (pok) {
546
0
            shunget(f);
547
0
            if (gotrad) shunget(f);
548
10.9k
        } else {
549
10.9k
            shlim(f, 0);
550
10.9k
        }
551
10.9k
        return sign * 0.0;
552
10.9k
    }
553
92.0k
    if (!gotrad) rp = dc;
554
590k
    while (dc<8) x *= 16, dc++;
555
92.0k
    if ((c|32)=='p') {
556
27.7k
        e2 = scanexp(f, pok);
557
27.7k
        if (e2 == LLONG_MIN) {
558
4.09k
            if (pok) {
559
0
                shunget(f);
560
4.09k
            } else {
561
4.09k
                shlim(f, 0);
562
4.09k
                return 0;
563
4.09k
            }
564
0
            e2 = 0;
565
0
        }
566
64.2k
    } else {
567
64.2k
        shunget(f);
568
64.2k
    }
569
87.9k
    e2 += 4*rp - 32;
570
571
87.9k
    if (!x) return sign * 0.0;
572
60.2k
    if (e2 > -emin) {
573
5.57k
        errno = ERANGE;
574
5.57k
        return sign * DBL_MAX * DBL_MAX;
575
5.57k
    }
576
54.6k
    if (e2 < emin-2*DBL_MANT_DIG) {
577
3.00k
        errno = ERANGE;
578
3.00k
        return sign * DBL_MIN * DBL_MIN;
579
3.00k
    }
580
581
128k
    while (x < 0x80000000) {
582
76.9k
        if (y>=0.5) {
583
14.7k
            x += x + 1;
584
14.7k
            y += y - 1;
585
62.2k
        } else {
586
62.2k
            x += x;
587
62.2k
            y += y;
588
62.2k
        }
589
76.9k
        e2--;
590
76.9k
    }
591
592
51.6k
    if (bits > 32+e2-emin) {
593
8.55k
        bits = 32+e2-emin;
594
8.55k
        if (bits<0) bits=0;
595
8.55k
    }
596
597
51.6k
    if (bits < DBL_MANT_DIG)
598
44.6k
        bias = copysign(scalbn(1, 32+DBL_MANT_DIG-bits-1), sign);
599
600
51.6k
    if (bits<32 && y && !(x&1)) x++, y=0;
601
602
51.6k
    y = bias + sign*(double)x + sign*y;
603
51.6k
    y -= bias;
604
605
51.6k
    if (!y) errno = ERANGE;
606
607
51.6k
    return scalbn(y, e2);
608
54.6k
}
609
610
static double fffloatscan(FFFILE *f, int prec, int pok)
611
2.78M
{
612
2.78M
    int sign = 1;
613
2.78M
    size_t i;
614
2.78M
    int bits;
615
2.78M
    int emin;
616
2.78M
    int c;
617
618
2.78M
    switch (prec) {
619
651k
    case 0:
620
651k
        bits = FLT_MANT_DIG;
621
651k
        emin = FLT_MIN_EXP-bits;
622
651k
        break;
623
2.13M
    case 1:
624
2.13M
        bits = DBL_MANT_DIG;
625
2.13M
        emin = DBL_MIN_EXP-bits;
626
2.13M
        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.78M
    }
634
635
2.78M
    while (av_isspace((c = shgetc(f))));
636
637
2.78M
    if (c=='+' || c=='-') {
638
85.0k
        sign -= 2*(c=='-');
639
85.0k
        c = shgetc(f);
640
85.0k
    }
641
642
2.83M
    for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
643
49.8k
        if (i<7) c = shgetc(f);
644
2.78M
    if (i==3 || i==8 || (i>3 && pok)) {
645
7.75k
        if (i!=8) {
646
5.18k
            shunget(f);
647
5.18k
            if (pok) for (; i>3; i--) shunget(f);
648
5.18k
        }
649
7.75k
        return sign * INFINITY;
650
7.75k
    }
651
2.82M
    if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
652
49.8k
        if (i<2) c = shgetc(f);
653
2.77M
    if (i==3) {
654
15.0k
        if (shgetc(f) != '(') {
655
7.25k
            shunget(f);
656
7.25k
            return NAN;
657
7.25k
        }
658
41.0k
        for (i=1; ; i++) {
659
41.0k
            c = shgetc(f);
660
41.0k
            if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_')
661
33.2k
                continue;
662
7.80k
            if (c==')') return NAN;
663
5.43k
            shunget(f);
664
5.43k
            if (!pok) {
665
5.43k
                errno = EINVAL;
666
5.43k
                shlim(f, 0);
667
5.43k
                return 0;
668
5.43k
            }
669
0
            while (i--) shunget(f);
670
0
            return NAN;
671
5.43k
        }
672
7.80k
    }
673
674
2.76M
    if (i) {
675
10.6k
        shunget(f);
676
10.6k
        errno = EINVAL;
677
10.6k
        shlim(f, 0);
678
10.6k
        return 0;
679
10.6k
    }
680
681
2.75M
    if (c=='0') {
682
1.28M
        c = shgetc(f);
683
1.28M
        if ((c|32) == 'x')
684
103k
            return hexfloat(f, bits, emin, sign, pok);
685
1.18M
        shunget(f);
686
1.18M
        c = '0';
687
1.18M
    }
688
689
2.64M
    return decfloat(f, c, bits, emin, sign, pok);
690
2.75M
}
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
15.5M
{
706
15.5M
    if (!dest) return;
707
15.5M
    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
10.1M
    case SIZE_def:
715
10.1M
        *(int *)dest = i;
716
10.1M
        break;
717
0
    case SIZE_l:
718
0
        *(long *)dest = i;
719
0
        break;
720
5.44M
    case SIZE_ll:
721
5.44M
        *(long long *)dest = i;
722
5.44M
        break;
723
15.5M
    }
724
15.5M
}
725
726
static int ff_vfscanf(FFFILE *f, const char *fmt, va_list ap)
727
10.1M
{
728
10.1M
    int width;
729
10.1M
    int size;
730
10.1M
    int base;
731
10.1M
    const unsigned char *p;
732
10.1M
    int c, t;
733
10.1M
    char *s;
734
10.1M
    void *dest=NULL;
735
10.1M
    int invert;
736
10.1M
    int matches=0;
737
10.1M
    unsigned long long x;
738
10.1M
    double y;
739
10.1M
    ptrdiff_t pos = 0;
740
10.1M
    unsigned char scanset[257];
741
10.1M
    size_t i;
742
743
37.1M
    for (p=(const unsigned char *)fmt; *p; p++) {
744
745
35.2M
        if (av_isspace(*p)) {
746
15.7k
            while (av_isspace(p[1])) p++;
747
15.7k
            shlim(f, 0);
748
15.7k
            while (av_isspace(shgetc(f)));
749
15.7k
            shunget(f);
750
15.7k
            pos += shcnt(f);
751
15.7k
            continue;
752
15.7k
        }
753
35.2M
        if (*p != '%' || p[1] == '%') {
754
10.9M
            shlim(f, 0);
755
10.9M
            if (*p == '%') {
756
0
                p++;
757
0
                while (av_isspace((c=shgetc(f))));
758
10.9M
            } else {
759
10.9M
                c = shgetc(f);
760
10.9M
            }
761
10.9M
            if (c!=*p) {
762
7.14M
                shunget(f);
763
7.14M
                if (c<0) goto input_fail;
764
4.42M
                goto match_fail;
765
7.14M
            }
766
3.78M
            pos += shcnt(f);
767
3.78M
            continue;
768
10.9M
        }
769
770
24.3M
        p++;
771
24.3M
        if (*p=='*') {
772
629k
            dest = 0; p++;
773
23.6M
        } else if (av_isdigit(*p) && p[1]=='$') {
774
0
            dest = arg_n(ap, *p-'0'); p+=2;
775
23.6M
        } else {
776
23.6M
            dest = va_arg(ap, void *);
777
23.6M
        }
778
779
26.4M
        for (width=0; av_isdigit(*p); p++) {
780
2.12M
            width = 10*width + *p - '0';
781
2.12M
        }
782
783
24.3M
        if (*p=='m') {
784
0
            s = 0;
785
0
            p++;
786
0
        }
787
788
24.3M
        size = SIZE_def;
789
24.3M
        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
7.92M
        case 'l':
795
7.92M
            if (*p == 'l') p++, size = SIZE_ll;
796
2.13M
            else size = SIZE_l;
797
7.92M
            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.67M
        case 'd': case 'i': case 'o': case 'u': case 'x':
809
5.33M
        case 'a': case 'e': case 'f': case 'g':
810
5.33M
        case 'A': case 'E': case 'F': case 'G': case 'X':
811
10.9M
        case 's': case 'c': case '[':
812
10.9M
        case 'S': case 'C':
813
16.4M
        case 'p': case 'n':
814
16.4M
            p--;
815
16.4M
            break;
816
0
        default:
817
0
            goto fmt_fail;
818
24.3M
        }
819
820
24.3M
        t = *p;
821
822
        /* C or S */
823
24.3M
        if ((t&0x2f) == 3) {
824
0
            t |= 32;
825
0
            size = SIZE_l;
826
0
        }
827
828
24.3M
        switch (t) {
829
2.86M
            case 'c':
830
2.86M
                if (width < 1) width = 1;
831
5.61M
            case '[':
832
5.61M
                break;
833
5.44M
            case 'n':
834
5.44M
                store_int(dest, size, pos);
835
                /* do not increment match count, etc! */
836
5.44M
                continue;
837
13.2M
            default:
838
13.2M
                shlim(f, 0);
839
15.4M
                while (av_isspace(shgetc(f)));
840
13.2M
                shunget(f);
841
13.2M
                pos += shcnt(f);
842
24.3M
        }
843
844
18.8M
        shlim(f, width);
845
18.8M
        if (shgetc(f) < 0) goto input_fail;
846
18.0M
        shunget(f);
847
848
18.0M
        switch (t) {
849
0
            case 's':
850
2.29M
            case 'c':
851
5.04M
            case '[':
852
5.04M
                if (t == 'c' || t == 's') {
853
2.29M
                    memset(scanset, -1, sizeof scanset);
854
2.29M
                    scanset[0] = 0;
855
2.29M
                    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.75M
                } else {
864
2.75M
                    if (*++p == '^') p++, invert = 1;
865
2.75M
                    else invert = 0;
866
2.75M
                    memset(scanset, invert, sizeof scanset);
867
2.75M
                    scanset[0] = 0;
868
2.75M
                    if (*p == '-') p++, scanset[1+'-'] = 1-invert;
869
2.75M
                    else if (*p == ']') p++, scanset[1+']'] = 1-invert;
870
8.26M
                    for (; *p != ']'; p++) {
871
5.50M
                        if (!*p) goto fmt_fail;
872
5.50M
                        if (*p=='-' && p[1] && p[1] != ']')
873
0
                            for (c=p++[-1]; c<*p; c++)
874
0
                                scanset[1+c] = 1-invert;
875
5.50M
                        scanset[1+*p] = 1-invert;
876
5.50M
                    }
877
2.75M
                }
878
5.04M
                s = 0;
879
5.04M
                i = 0;
880
5.04M
                if ((s = dest)) {
881
8.84M
                    while (scanset[(c=shgetc(f))+1])
882
4.42M
                        s[i++] = c;
883
4.41M
                } else {
884
1.25M
                    while (scanset[(c=shgetc(f))+1]);
885
629k
                }
886
5.04M
                shunget(f);
887
5.04M
                if (!shcnt(f)) goto match_fail;
888
5.04M
                if (t == 'c' && shcnt(f) != width) goto match_fail;
889
5.04M
                if (t != 'c') {
890
2.75M
                    if (s) s[i] = 0;
891
2.75M
                }
892
5.04M
                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
8.08M
            case 'd':
902
10.2M
            case 'u':
903
10.2M
                base = 10;
904
10.2M
                goto int_common;
905
0
            case 'i':
906
0
                base = 0;
907
10.2M
int_common:
908
10.2M
                x = ffintscan(f, base, 0, ULLONG_MAX);
909
10.2M
                if (!shcnt(f))
910
102k
                    goto match_fail;
911
10.1M
                if (t=='p' && dest)
912
0
                    *(void **)dest = (void *)(uintptr_t)x;
913
10.1M
                else
914
10.1M
                    store_int(dest, size, x);
915
10.1M
                break;
916
0
            case 'a': case 'A':
917
0
            case 'e': case 'E':
918
2.78M
            case 'f': case 'F':
919
2.78M
            case 'g': case 'G':
920
2.78M
                y = fffloatscan(f, size, 0);
921
2.78M
                if (!shcnt(f))
922
97.6k
                    goto match_fail;
923
2.68M
                if (dest) {
924
2.68M
                    switch (size) {
925
562k
                    case SIZE_def:
926
562k
                        *(float *)dest = y;
927
562k
                        break;
928
2.12M
                    case SIZE_l:
929
2.12M
                        *(double *)dest = y;
930
2.12M
                        break;
931
0
                    case SIZE_L:
932
0
                        *(double *)dest = y;
933
0
                        break;
934
2.68M
                    }
935
2.68M
                }
936
2.68M
                break;
937
18.0M
        }
938
939
17.8M
        pos += shcnt(f);
940
17.8M
        if (dest) matches++;
941
17.8M
    }
942
1.91M
    if (0) {
943
0
fmt_fail:
944
3.56M
input_fail:
945
3.56M
        if (!matches) matches--;
946
3.56M
    }
947
10.1M
match_fail:
948
10.1M
    return matches;
949
1.91M
}
950
951
static int ff_vsscanf(const char *s, const char *fmt, va_list ap)
952
10.1M
{
953
10.1M
    FFFILE f = {
954
10.1M
        .buf = (void *)s, .cookie = (void *)s,
955
10.1M
        .read = ffstring_read,
956
10.1M
    };
957
958
10.1M
    return ff_vfscanf(&f, fmt, ap);
959
10.1M
}
960
961
int av_sscanf(const char *string, const char *format, ...)
962
10.1M
{
963
10.1M
    int ret;
964
10.1M
    va_list ap;
965
10.1M
    va_start(ap, format);
966
10.1M
    ret = ff_vsscanf(string, format, ap);
967
    va_end(ap);
968
10.1M
    return ret;
969
10.1M
}