Coverage Report

Created: 2026-05-16 07:49

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
43.5M
#define SIZE_def 0
50
5.70M
#define SIZE_l   1
51
0
#define SIZE_L   2
52
11.8M
#define SIZE_ll  3
53
54
93.8M
#define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
55
56
static int fftoread(FFFILE *f)
57
19.3M
{
58
19.3M
    f->rpos = f->rend = f->buf + f->buf_size;
59
19.3M
    return 0;
60
19.3M
}
61
62
static size_t ffstring_read(FFFILE *f, unsigned char *buf, size_t len)
63
19.3M
{
64
19.3M
    char *src = f->cookie;
65
19.3M
    size_t k = len+256;
66
19.3M
    char *end = memchr(src, 0, k);
67
68
19.3M
    if (end) k = end-src;
69
19.3M
    if (k < len) len = k;
70
19.3M
    memcpy(buf, src, len);
71
19.3M
    f->rpos = (void *)(src+len);
72
19.3M
    f->rend = (void *)(src+k);
73
19.3M
    f->cookie = src+k;
74
75
19.3M
    return len;
76
19.3M
}
77
78
static int ffuflow(FFFILE *f)
79
19.3M
{
80
19.3M
    unsigned char c;
81
19.3M
    if (!fftoread(f) && f->read(f, &c, 1)==1) return c;
82
7.80M
    return EOF;
83
19.3M
}
84
85
static void ffshlim(FFFILE *f, ptrdiff_t lim)
86
55.1M
{
87
55.1M
    f->shlim = lim;
88
55.1M
    f->shcnt = f->buf - f->rpos;
89
    /* If lim is nonzero, rend must be a valid pointer. */
90
55.1M
    if (lim && f->rend - f->rpos > lim)
91
641k
        f->shend = f->rpos + lim;
92
54.4M
    else
93
54.4M
        f->shend = f->rend;
94
55.1M
}
95
96
static int ffshgetc(FFFILE *f)
97
22.4M
{
98
22.4M
    int c;
99
22.4M
    ptrdiff_t cnt = shcnt(f);
100
22.4M
    if (f->shlim && cnt >= f->shlim || (c=ffuflow(f)) < 0) {
101
10.9M
        f->shcnt = f->buf - f->rpos + cnt;
102
10.9M
        f->shend = 0;
103
10.9M
        return EOF;
104
10.9M
    }
105
11.5M
    cnt++;
106
11.5M
    if (f->shlim && f->rend - f->rpos > f->shlim - cnt)
107
4.10M
        f->shend = f->rpos + (f->shlim - cnt);
108
7.43M
    else
109
7.43M
        f->shend = f->rend;
110
11.5M
    f->shcnt = f->buf - f->rpos + cnt;
111
11.5M
    if (f->rpos[-1] != c) f->rpos[-1] = c;
112
11.5M
    return c;
113
22.4M
}
114
115
55.1M
#define shlim(f, lim) ffshlim((f), (lim))
116
118M
#define shgetc(f) (((f)->rpos < (f)->shend) ? *(f)->rpos++ : ffshgetc(f))
117
73.6M
#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
12.8M
{
140
12.8M
    const unsigned char *val = table+1;
141
12.8M
    int c, neg=0;
142
12.8M
    unsigned x;
143
12.8M
    unsigned long long y;
144
12.8M
    if (base > 36 || base == 1) {
145
0
        errno = EINVAL;
146
0
        return 0;
147
0
    }
148
12.8M
    while (av_isspace((c=shgetc(f))));
149
12.8M
    if (c=='+' || c=='-') {
150
784k
        neg = -(c=='-');
151
784k
        c = shgetc(f);
152
784k
    }
153
12.8M
    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
12.8M
    } else {
168
12.8M
        if (base == 0) base = 10;
169
12.8M
        if (val[c] >= base) {
170
113k
            shunget(f);
171
113k
            shlim(f, 0);
172
113k
            errno = EINVAL;
173
113k
            return 0;
174
113k
        }
175
12.8M
    }
176
12.7M
    if (base == 10) {
177
26.5M
        for (x=0; c-'0'<10U && x<=UINT_MAX/10-1; c=shgetc(f))
178
13.8M
            x = x*10 + (c-'0');
179
12.8M
        for (y=x; c-'0'<10U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(c-'0'); c=shgetc(f))
180
77.2k
            y = y*10 + (c-'0');
181
12.7M
        if (c-'0'>=10U) goto done;
182
12.7M
    } 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.89k
    if (val[c]<base) {
195
1.72M
        for (; val[c]<base; c=shgetc(f));
196
3.89k
        errno = ERANGE;
197
3.89k
        y = lim;
198
3.89k
        if (lim&1) neg = 0;
199
3.89k
    }
200
12.7M
done:
201
12.7M
    shunget(f);
202
12.7M
    if (y>=lim) {
203
3.90k
        if (!(lim&1) && !neg) {
204
0
            errno = ERANGE;
205
0
            return lim-1;
206
3.90k
        } else if (y>lim) {
207
0
            errno = ERANGE;
208
0
            return lim;
209
0
        }
210
3.90k
    }
211
12.7M
    return (y^neg)-neg;
212
12.7M
}
213
214
static long long scanexp(FFFILE *f, int pok)
215
84.7k
{
216
84.7k
    int c;
217
84.7k
    int x;
218
84.7k
    long long y;
219
84.7k
    int neg = 0;
220
221
84.7k
    c = shgetc(f);
222
84.7k
    if (c=='+' || c=='-') {
223
46.3k
        neg = (c=='-');
224
46.3k
        c = shgetc(f);
225
46.3k
        if (c-'0'>=10U && pok) shunget(f);
226
46.3k
    }
227
84.7k
    if (c-'0'>=10U) {
228
12.6k
        shunget(f);
229
12.6k
        return LLONG_MIN;
230
12.6k
    }
231
418k
    for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
232
346k
        x = 10*x + (c-'0');
233
140k
    for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f))
234
68.2k
        y = 10*y + (c-'0');
235
96.0k
    for (; c-'0'<10U; c = shgetc(f));
236
72.1k
    shunget(f);
237
72.1k
    return neg ? -y : y;
238
84.7k
}
239
240
34.9M
#define LD_B1B_DIG 2
241
3.28M
#define LD_B1B_MAX 9007199, 254740991
242
69.2M
#define KMAX 128
243
66.6M
#define MASK (KMAX-1)
244
245
static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok)
246
3.28M
{
247
3.28M
    uint32_t x[KMAX];
248
3.28M
    static const uint32_t th[] = { LD_B1B_MAX };
249
3.28M
    int i, j, k, a, z;
250
3.28M
    long long lrp=0, dc=0;
251
3.28M
    long long e10=0;
252
3.28M
    int lnz = 0;
253
3.28M
    int gotdig = 0, gotrad = 0;
254
3.28M
    int rp;
255
3.28M
    int e2;
256
3.28M
    int emax = -emin-bits+3;
257
3.28M
    int denormal = 0;
258
3.28M
    double y;
259
3.28M
    double frac=0;
260
3.28M
    double bias=0;
261
3.28M
    static const int p10s[] = { 10, 100, 1000, 10000,
262
3.28M
        100000, 1000000, 10000000, 100000000 };
263
264
3.28M
    j=0;
265
3.28M
    k=0;
266
267
    /* Don't let leading zeros consume buffer space */
268
5.57M
    for (; c=='0'; c = shgetc(f)) gotdig=1;
269
3.28M
    if (c=='.') {
270
59.2k
        gotrad = 1;
271
112k
        for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
272
59.2k
    }
273
274
3.28M
    x[0] = 0;
275
6.40M
    for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
276
3.13M
        if (c == '.') {
277
554k
            if (gotrad) break;
278
543k
            gotrad = 1;
279
543k
            lrp = dc;
280
2.58M
        } else if (k < KMAX-3) {
281
2.51M
            dc++;
282
2.51M
            if (c!='0') lnz = dc;
283
2.51M
            if (j) x[k] = x[k]*10 + c-'0';
284
1.13M
            else x[k] = c-'0';
285
2.51M
            if (++j==9) {
286
85.4k
                k++;
287
85.4k
                j=0;
288
85.4k
            }
289
2.51M
            gotdig=1;
290
2.51M
        } else {
291
68.5k
            dc++;
292
68.5k
            if (c!='0') {
293
448
                lnz = (KMAX-4)*9;
294
448
                x[KMAX-4] |= 1;
295
448
            }
296
68.5k
        }
297
3.13M
    }
298
3.28M
    if (!gotrad) lrp=dc;
299
300
3.28M
    if (gotdig && (c|32)=='e') {
301
53.3k
        e10 = scanexp(f, pok);
302
53.3k
        if (e10 == LLONG_MIN) {
303
8.49k
            if (pok) {
304
0
                shunget(f);
305
8.49k
            } else {
306
8.49k
                shlim(f, 0);
307
8.49k
                return 0;
308
8.49k
            }
309
0
            e10 = 0;
310
0
        }
311
44.8k
        lrp += e10;
312
3.23M
    } else if (c>=0) {
313
3.08M
        shunget(f);
314
3.08M
    }
315
3.27M
    if (!gotdig) {
316
59.2k
        errno = EINVAL;
317
59.2k
        shlim(f, 0);
318
59.2k
        return 0;
319
59.2k
    }
320
321
    /* Handle zero specially to avoid nasty special cases later */
322
3.21M
    if (!x[0]) return sign * 0.0;
323
324
    /* Optimize small integers (w/no exponent) and over/under-flow */
325
1.04M
    if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
326
420k
        return sign * (double)x[0];
327
625k
    if (lrp > -emin/2) {
328
8.89k
        errno = ERANGE;
329
8.89k
        return sign * DBL_MAX * DBL_MAX;
330
8.89k
    }
331
616k
    if (lrp < emin-2*DBL_MANT_DIG) {
332
7.03k
        errno = ERANGE;
333
7.03k
        return sign * DBL_MIN * DBL_MIN;
334
7.03k
    }
335
336
    /* Align incomplete final B1B digit */
337
609k
    if (j) {
338
4.77M
        for (; j<9; j++) x[k]*=10;
339
602k
        k++;
340
602k
        j=0;
341
602k
    }
342
343
609k
    a = 0;
344
609k
    z = k;
345
609k
    e2 = 0;
346
609k
    rp = lrp;
347
348
    /* Optimize small to mid-size integers (even in exp. notation) */
349
609k
    if (lnz<9 && lnz<=rp && rp < 18) {
350
20.8k
        int bitlim;
351
20.8k
        if (rp == 9) return sign * (double)x[0];
352
17.5k
        if (rp < 9) return sign * (double)x[0] / p10s[8-rp];
353
13.2k
        bitlim = bits-3*(int)(rp-9);
354
13.2k
        if (bitlim>30 || x[0]>>bitlim==0)
355
6.53k
            return sign * (double)x[0] * p10s[rp-10];
356
13.2k
    }
357
358
    /* Drop trailing zeros */
359
606k
    for (; !x[z-1]; z--);
360
361
    /* Align radix point to B1B digit boundary */
362
595k
    if (rp % 9) {
363
569k
        int rpm9 = rp>=0 ? rp%9 : rp%9+9;
364
569k
        int p10 = p10s[8-rpm9];
365
569k
        uint32_t carry = 0;
366
1.19M
        for (k=a; k!=z; k++) {
367
620k
            uint32_t tmp = x[k] % p10;
368
620k
            x[k] = x[k]/p10 + carry;
369
620k
            carry = 1000000000/p10 * tmp;
370
620k
            if (k==a && !x[k]) {
371
0
                a = (a+1 & MASK);
372
0
                rp -= 9;
373
0
            }
374
620k
        }
375
569k
        if (carry) x[z++] = carry;
376
569k
        rp += 9-rpm9;
377
569k
    }
378
379
    /* Upscale until desired number of bits are left of radix point */
380
2.00M
    while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
381
1.41M
        uint32_t carry = 0;
382
1.41M
        e2 -= 29;
383
11.2M
        for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
384
11.2M
            uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
385
11.2M
            if (tmp > 1000000000) {
386
11.2M
                carry = tmp / 1000000000;
387
11.2M
                x[k] = tmp % 1000000000;
388
11.2M
            } else {
389
52.4k
                carry = 0;
390
52.4k
                x[k] = tmp;
391
52.4k
            }
392
11.2M
            if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
393
11.2M
            if (k==a) break;
394
11.2M
        }
395
1.41M
        if (carry) {
396
1.39M
            rp += 9;
397
1.39M
            a = (a-1 & MASK);
398
1.39M
            if (a == z) {
399
2.81k
                z = (z-1 & MASK);
400
2.81k
                x[z-1 & MASK] |= x[z];
401
2.81k
            }
402
1.39M
            x[a] = carry;
403
1.39M
        }
404
1.41M
    }
405
406
    /* Downscale until exactly number of bits are left of radix point */
407
5.81M
    for (;;) {
408
5.81M
        uint32_t carry = 0;
409
5.81M
        int sh = 1;
410
5.82M
        for (i=0; i<LD_B1B_DIG; i++) {
411
5.82M
            k = (a+i & MASK);
412
5.82M
            if (k == z || x[k] < th[i]) {
413
2.09M
                i=LD_B1B_DIG;
414
2.09M
                break;
415
2.09M
            }
416
3.73M
            if (x[a+i & MASK] > th[i]) break;
417
3.73M
        }
418
5.81M
        if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
419
        /* FIXME: find a way to compute optimal sh */
420
5.21M
        if (rp > 9+9*LD_B1B_DIG) sh = 9;
421
5.21M
        e2 += sh;
422
33.7M
        for (k=a; k!=z; k=(k+1 & MASK)) {
423
28.5M
            uint32_t tmp = x[k] & (1<<sh)-1;
424
28.5M
            x[k] = (x[k]>>sh) + carry;
425
28.5M
            carry = (1000000000>>sh) * tmp;
426
28.5M
            if (k==a && !x[k]) {
427
602k
                a = (a+1 & MASK);
428
602k
                i--;
429
602k
                rp -= 9;
430
602k
            }
431
28.5M
        }
432
5.21M
        if (carry) {
433
195k
            if ((z+1 & MASK) != a) {
434
188k
                x[z] = carry;
435
188k
                z = (z+1 & MASK);
436
188k
            } else x[z-1 & MASK] |= 1;
437
195k
        }
438
5.21M
    }
439
440
    /* Assemble desired bits into floating point variable */
441
1.78M
    for (y=i=0; i<LD_B1B_DIG; i++) {
442
1.19M
        if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
443
1.19M
        y = 1000000000.0L * y + x[a+i & MASK];
444
1.19M
    }
445
446
595k
    y *= sign;
447
448
    /* Limit precision for denormal results */
449
595k
    if (bits > DBL_MANT_DIG+e2-emin) {
450
12.0k
        bits = DBL_MANT_DIG+e2-emin;
451
12.0k
        if (bits<0) bits=0;
452
12.0k
        denormal = 1;
453
12.0k
    }
454
455
    /* Calculate bias term to force rounding, move out lower bits */
456
595k
    if (bits < DBL_MANT_DIG) {
457
58.3k
        bias = copysign(scalbn(1, 2*DBL_MANT_DIG-bits-1), y);
458
58.3k
        frac = fmod(y, scalbn(1, DBL_MANT_DIG-bits));
459
58.3k
        y -= frac;
460
58.3k
        y += bias;
461
58.3k
    }
462
463
    /* Process tail of decimal input so it can affect rounding */
464
595k
    if ((a+i & MASK) != z) {
465
574k
        uint32_t t = x[a+i & MASK];
466
574k
        if (t < 500000000 && (t || (a+i+1 & MASK) != z))
467
170k
            frac += 0.25*sign;
468
403k
        else if (t > 500000000)
469
398k
            frac += 0.75*sign;
470
4.50k
        else if (t == 500000000) {
471
4.50k
            if ((a+i+1 & MASK) == z)
472
2.48k
                frac += 0.5*sign;
473
2.02k
            else
474
2.02k
                frac += 0.75*sign;
475
4.50k
        }
476
574k
        if (DBL_MANT_DIG-bits >= 2 && !fmod(frac, 1))
477
10.0k
            frac++;
478
574k
    }
479
480
595k
    y += frac;
481
595k
    y -= bias;
482
483
595k
    if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) {
484
31.0k
        if (fabs(y) >= pow(2, DBL_MANT_DIG)) {
485
12.8k
            if (denormal && bits==DBL_MANT_DIG+e2-emin)
486
4.46k
                denormal = 0;
487
12.8k
            y *= 0.5;
488
12.8k
            e2++;
489
12.8k
        }
490
31.0k
        if (e2+DBL_MANT_DIG>emax || (denormal && frac))
491
31.0k
            errno = ERANGE;
492
31.0k
    }
493
494
595k
    return scalbn(y, e2);
495
609k
}
496
497
static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
498
108k
{
499
108k
    uint32_t x = 0;
500
108k
    double y = 0;
501
108k
    double scale = 1;
502
108k
    double bias = 0;
503
108k
    int gottail = 0, gotrad = 0, gotdig = 0;
504
108k
    long long rp = 0;
505
108k
    long long dc = 0;
506
108k
    long long e2 = 0;
507
108k
    int d;
508
108k
    int c;
509
510
108k
    c = shgetc(f);
511
512
    /* Skip leading zeros */
513
195k
    for (; c=='0'; c = shgetc(f))
514
86.8k
        gotdig = 1;
515
516
108k
    if (c=='.') {
517
35.5k
        gotrad = 1;
518
35.5k
        c = shgetc(f);
519
        /* Count zeros after the radix point before significand */
520
653k
        for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
521
35.5k
    }
522
523
1.95M
    for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
524
1.85M
        if (c=='.') {
525
9.68k
            if (gotrad) break;
526
6.02k
            rp = dc;
527
6.02k
            gotrad = 1;
528
1.84M
        } else {
529
1.84M
            gotdig = 1;
530
1.84M
            if (c > '9') d = (c|32)+10-'a';
531
1.67M
            else d = c-'0';
532
1.84M
            if (dc<8) {
533
242k
                x = x*16 + d;
534
1.60M
            } else if (dc < DBL_MANT_DIG/4+1) {
535
48.3k
                y += d*(scale/=16);
536
1.55M
            } else if (d && !gottail) {
537
4.10k
                y += 0.5*scale;
538
4.10k
                gottail = 1;
539
4.10k
            }
540
1.84M
            dc++;
541
1.84M
        }
542
1.85M
    }
543
108k
    if (!gotdig) {
544
11.2k
        shunget(f);
545
11.2k
        if (pok) {
546
0
            shunget(f);
547
0
            if (gotrad) shunget(f);
548
11.2k
        } else {
549
11.2k
            shlim(f, 0);
550
11.2k
        }
551
11.2k
        return sign * 0.0;
552
11.2k
    }
553
97.2k
    if (!gotrad) rp = dc;
554
631k
    while (dc<8) x *= 16, dc++;
555
97.2k
    if ((c|32)=='p') {
556
31.3k
        e2 = scanexp(f, pok);
557
31.3k
        if (e2 == LLONG_MIN) {
558
4.18k
            if (pok) {
559
0
                shunget(f);
560
4.18k
            } else {
561
4.18k
                shlim(f, 0);
562
4.18k
                return 0;
563
4.18k
            }
564
0
            e2 = 0;
565
0
        }
566
65.8k
    } else {
567
65.8k
        shunget(f);
568
65.8k
    }
569
93.0k
    e2 += 4*rp - 32;
570
571
93.0k
    if (!x) return sign * 0.0;
572
62.9k
    if (e2 > -emin) {
573
7.30k
        errno = ERANGE;
574
7.30k
        return sign * DBL_MAX * DBL_MAX;
575
7.30k
    }
576
55.6k
    if (e2 < emin-2*DBL_MANT_DIG) {
577
2.92k
        errno = ERANGE;
578
2.92k
        return sign * DBL_MIN * DBL_MIN;
579
2.92k
    }
580
581
129k
    while (x < 0x80000000) {
582
77.2k
        if (y>=0.5) {
583
10.6k
            x += x + 1;
584
10.6k
            y += y - 1;
585
66.6k
        } else {
586
66.6k
            x += x;
587
66.6k
            y += y;
588
66.6k
        }
589
77.2k
        e2--;
590
77.2k
    }
591
592
52.6k
    if (bits > 32+e2-emin) {
593
9.17k
        bits = 32+e2-emin;
594
9.17k
        if (bits<0) bits=0;
595
9.17k
    }
596
597
52.6k
    if (bits < DBL_MANT_DIG)
598
45.3k
        bias = copysign(scalbn(1, 32+DBL_MANT_DIG-bits-1), sign);
599
600
52.6k
    if (bits<32 && y && !(x&1)) x++, y=0;
601
602
52.6k
    y = bias + sign*(double)x + sign*y;
603
52.6k
    y -= bias;
604
605
52.6k
    if (!y) errno = ERANGE;
606
607
52.6k
    return scalbn(y, e2);
608
55.6k
}
609
610
static double fffloatscan(FFFILE *f, int prec, int pok)
611
3.42M
{
612
3.42M
    int sign = 1;
613
3.42M
    size_t i;
614
3.42M
    int bits;
615
3.42M
    int emin;
616
3.42M
    int c;
617
618
3.42M
    switch (prec) {
619
569k
    case 0:
620
569k
        bits = FLT_MANT_DIG;
621
569k
        emin = FLT_MIN_EXP-bits;
622
569k
        break;
623
2.85M
    case 1:
624
2.85M
        bits = DBL_MANT_DIG;
625
2.85M
        emin = DBL_MIN_EXP-bits;
626
2.85M
        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
3.42M
    }
634
635
3.42M
    while (av_isspace((c = shgetc(f))));
636
637
3.42M
    if (c=='+' || c=='-') {
638
79.4k
        sign -= 2*(c=='-');
639
79.4k
        c = shgetc(f);
640
79.4k
    }
641
642
3.46M
    for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
643
41.1k
        if (i<7) c = shgetc(f);
644
3.42M
    if (i==3 || i==8 || (i>3 && pok)) {
645
6.38k
        if (i!=8) {
646
4.41k
            shunget(f);
647
4.41k
            if (pok) for (; i>3; i--) shunget(f);
648
4.41k
        }
649
6.38k
        return sign * INFINITY;
650
6.38k
    }
651
3.47M
    if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
652
63.3k
        if (i<2) c = shgetc(f);
653
3.42M
    if (i==3) {
654
19.3k
        if (shgetc(f) != '(') {
655
6.44k
            shunget(f);
656
6.44k
            return NAN;
657
6.44k
        }
658
43.9k
        for (i=1; ; i++) {
659
43.9k
            c = shgetc(f);
660
43.9k
            if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_')
661
31.0k
                continue;
662
12.9k
            if (c==')') return NAN;
663
5.69k
            shunget(f);
664
5.69k
            if (!pok) {
665
5.69k
                errno = EINVAL;
666
5.69k
                shlim(f, 0);
667
5.69k
                return 0;
668
5.69k
            }
669
0
            while (i--) shunget(f);
670
0
            return NAN;
671
5.69k
        }
672
12.9k
    }
673
674
3.40M
    if (i) {
675
10.0k
        shunget(f);
676
10.0k
        errno = EINVAL;
677
10.0k
        shlim(f, 0);
678
10.0k
        return 0;
679
10.0k
    }
680
681
3.39M
    if (c=='0') {
682
2.29M
        c = shgetc(f);
683
2.29M
        if ((c|32) == 'x')
684
108k
            return hexfloat(f, bits, emin, sign, pok);
685
2.18M
        shunget(f);
686
2.18M
        c = '0';
687
2.18M
    }
688
689
3.28M
    return decfloat(f, c, bits, emin, sign, pok);
690
3.39M
}
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
18.3M
{
706
18.3M
    if (!dest) return;
707
18.3M
    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
12.7M
    case SIZE_def:
715
12.7M
        *(int *)dest = i;
716
12.7M
        break;
717
0
    case SIZE_l:
718
0
        *(long *)dest = i;
719
0
        break;
720
5.64M
    case SIZE_ll:
721
5.64M
        *(long long *)dest = i;
722
5.64M
        break;
723
18.3M
    }
724
18.3M
}
725
726
static int ff_vfscanf(FFFILE *f, const char *fmt, va_list ap)
727
11.9M
{
728
11.9M
    int width;
729
11.9M
    int size;
730
11.9M
    int base;
731
11.9M
    const unsigned char *p;
732
11.9M
    int c, t;
733
11.9M
    char *s;
734
11.9M
    void *dest=NULL;
735
11.9M
    int invert;
736
11.9M
    int matches=0;
737
11.9M
    unsigned long long x;
738
11.9M
    double y;
739
11.9M
    ptrdiff_t pos = 0;
740
11.9M
    unsigned char scanset[257];
741
11.9M
    size_t i;
742
743
45.8M
    for (p=(const unsigned char *)fmt; *p; p++) {
744
745
43.8M
        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
43.8M
        if (*p != '%' || p[1] == '%') {
754
13.4M
            shlim(f, 0);
755
13.4M
            if (*p == '%') {
756
0
                p++;
757
0
                while (av_isspace((c=shgetc(f))));
758
13.4M
            } else {
759
13.4M
                c = shgetc(f);
760
13.4M
            }
761
13.4M
            if (c!=*p) {
762
8.30M
                shunget(f);
763
8.30M
                if (c<0) goto input_fail;
764
5.48M
                goto match_fail;
765
8.30M
            }
766
5.11M
            pos += shcnt(f);
767
5.11M
            continue;
768
13.4M
        }
769
770
30.3M
        p++;
771
30.3M
        if (*p=='*') {
772
1.05M
            dest = 0; p++;
773
29.3M
        } else if (av_isdigit(*p) && p[1]=='$') {
774
0
            dest = arg_n(ap, *p-'0'); p+=2;
775
29.3M
        } else {
776
29.3M
            dest = va_arg(ap, void *);
777
29.3M
        }
778
779
33.2M
        for (width=0; av_isdigit(*p); p++) {
780
2.85M
            width = 10*width + *p - '0';
781
2.85M
        }
782
783
30.3M
        if (*p=='m') {
784
0
            s = 0;
785
0
            p++;
786
0
        }
787
788
30.3M
        size = SIZE_def;
789
30.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
9.06M
        case 'l':
795
9.06M
            if (*p == 'l') p++, size = SIZE_ll;
796
2.85M
            else size = SIZE_l;
797
9.06M
            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
7.11M
        case 'd': case 'i': case 'o': case 'u': case 'x':
809
7.68M
        case 'a': case 'e': case 'f': case 'g':
810
7.68M
        case 'A': case 'E': case 'F': case 'G': case 'X':
811
15.6M
        case 's': case 'c': case '[':
812
15.6M
        case 'S': case 'C':
813
21.3M
        case 'p': case 'n':
814
21.3M
            p--;
815
21.3M
            break;
816
0
        default:
817
0
            goto fmt_fail;
818
30.3M
        }
819
820
30.3M
        t = *p;
821
822
        /* C or S */
823
30.3M
        if ((t&0x2f) == 3) {
824
0
            t |= 32;
825
0
            size = SIZE_l;
826
0
        }
827
828
30.3M
        switch (t) {
829
4.08M
            case 'c':
830
4.08M
                if (width < 1) width = 1;
831
4.08M
                break;
832
3.90M
            case '[':
833
3.90M
                break;
834
5.64M
            case 'n':
835
5.64M
                store_int(dest, size, pos);
836
                /* do not increment match count, etc! */
837
5.64M
                continue;
838
16.7M
            default:
839
16.7M
                shlim(f, 0);
840
18.8M
                while (av_isspace(shgetc(f)));
841
16.7M
                shunget(f);
842
16.7M
                pos += shcnt(f);
843
30.3M
        }
844
845
24.7M
        shlim(f, width);
846
24.7M
        if (shgetc(f) < 0) goto input_fail;
847
23.2M
        shunget(f);
848
849
23.2M
        switch (t) {
850
0
            case 's':
851
3.12M
            case 'c':
852
7.03M
            case '[':
853
7.03M
                if (t == 'c' || t == 's') {
854
3.12M
                    memset(scanset, -1, sizeof scanset);
855
3.12M
                    scanset[0] = 0;
856
3.12M
                    if (t == 's') {
857
0
                        scanset[1 + '\t'] = 0;
858
0
                        scanset[1 + '\n'] = 0;
859
0
                        scanset[1 + '\v'] = 0;
860
0
                        scanset[1 + '\f'] = 0;
861
0
                        scanset[1 + '\r'] = 0;
862
0
                        scanset[1 + ' ' ] = 0;
863
0
                    }
864
3.90M
                } else {
865
3.90M
                    if (*++p == '^') p++, invert = 1;
866
3.90M
                    else invert = 0;
867
3.90M
                    memset(scanset, invert, sizeof scanset);
868
3.90M
                    scanset[0] = 0;
869
3.90M
                    if (*p == '-') p++, scanset[1+'-'] = 1-invert;
870
3.90M
                    else if (*p == ']') p++, scanset[1+']'] = 1-invert;
871
11.7M
                    for (; *p != ']'; p++) {
872
7.81M
                        if (!*p) goto fmt_fail;
873
7.81M
                        if (*p=='-' && p[1] && p[1] != ']')
874
0
                            for (c=p++[-1]; c<*p; c++)
875
0
                                scanset[1+c] = 1-invert;
876
7.81M
                        scanset[1+*p] = 1-invert;
877
7.81M
                    }
878
3.90M
                }
879
7.03M
                s = 0;
880
7.03M
                i = 0;
881
7.03M
                if ((s = dest)) {
882
11.9M
                    while (scanset[(c=shgetc(f))+1])
883
5.98M
                        s[i++] = c;
884
5.97M
                } else {
885
2.10M
                    while (scanset[(c=shgetc(f))+1]);
886
1.05M
                }
887
7.03M
                shunget(f);
888
7.03M
                if (!shcnt(f)) goto match_fail;
889
7.03M
                if (t == 'c' && shcnt(f) != width) goto match_fail;
890
7.03M
                if (t != 'c') {
891
3.90M
                    if (s) s[i] = 0;
892
3.90M
                }
893
7.03M
                break;
894
0
            case 'p':
895
0
            case 'X':
896
0
            case 'x':
897
0
                base = 16;
898
0
                goto int_common;
899
0
            case 'o':
900
0
                base = 8;
901
0
                goto int_common;
902
9.99M
            case 'd':
903
12.8M
            case 'u':
904
12.8M
                base = 10;
905
12.8M
                goto int_common;
906
0
            case 'i':
907
0
                base = 0;
908
12.8M
int_common:
909
12.8M
                x = ffintscan(f, base, 0, ULLONG_MAX);
910
12.8M
                if (!shcnt(f))
911
113k
                    goto match_fail;
912
12.7M
                if (t=='p' && dest)
913
0
                    *(void **)dest = (void *)(uintptr_t)x;
914
12.7M
                else
915
12.7M
                    store_int(dest, size, x);
916
12.7M
                break;
917
0
            case 'a': case 'A':
918
0
            case 'e': case 'E':
919
3.42M
            case 'f': case 'F':
920
3.42M
            case 'g': case 'G':
921
3.42M
                y = fffloatscan(f, size, 0);
922
3.42M
                if (!shcnt(f))
923
98.8k
                    goto match_fail;
924
3.32M
                if (dest) {
925
3.32M
                    switch (size) {
926
482k
                    case SIZE_def:
927
482k
                        *(float *)dest = y;
928
482k
                        break;
929
2.84M
                    case SIZE_l:
930
2.84M
                        *(double *)dest = y;
931
2.84M
                        break;
932
0
                    case SIZE_L:
933
0
                        *(double *)dest = y;
934
0
                        break;
935
3.32M
                    }
936
3.32M
                }
937
3.32M
                break;
938
23.2M
        }
939
940
23.0M
        pos += shcnt(f);
941
23.0M
        if (dest) matches++;
942
23.0M
    }
943
2.03M
    if (0) {
944
0
fmt_fail:
945
4.25M
input_fail:
946
4.25M
        if (!matches) matches--;
947
4.25M
    }
948
11.9M
match_fail:
949
11.9M
    return matches;
950
2.03M
}
951
952
static int ff_vsscanf(const char *s, const char *fmt, va_list ap)
953
11.9M
{
954
11.9M
    FFFILE f = {
955
11.9M
        .buf = (void *)s, .cookie = (void *)s,
956
11.9M
        .read = ffstring_read,
957
11.9M
    };
958
959
11.9M
    return ff_vfscanf(&f, fmt, ap);
960
11.9M
}
961
962
int av_sscanf(const char *string, const char *format, ...)
963
11.9M
{
964
11.9M
    int ret;
965
11.9M
    va_list ap;
966
11.9M
    va_start(ap, format);
967
11.9M
    ret = ff_vsscanf(string, format, ap);
968
    va_end(ap);
969
11.9M
    return ret;
970
11.9M
}