| Line | Branch | Exec | 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 | |||
| 36 | typedef struct FFFILE { | ||
| 37 | size_t buf_size; | ||
| 38 | unsigned char *buf; | ||
| 39 | unsigned char *rpos, *rend; | ||
| 40 | unsigned char *shend; | ||
| 41 | ptrdiff_t shlim, shcnt; | ||
| 42 | void *cookie; | ||
| 43 | size_t (*read)(struct FFFILE *, unsigned char *, size_t); | ||
| 44 | } FFFILE; | ||
| 45 | |||
| 46 | #define SIZE_hh -2 | ||
| 47 | #define SIZE_h -1 | ||
| 48 | #define SIZE_def 0 | ||
| 49 | #define SIZE_l 1 | ||
| 50 | #define SIZE_L 2 | ||
| 51 | #define SIZE_ll 3 | ||
| 52 | |||
| 53 | #define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf)) | ||
| 54 | |||
| 55 | 9614 | static int fftoread(FFFILE *f) | |
| 56 | { | ||
| 57 | 9614 | f->rpos = f->rend = f->buf + f->buf_size; | |
| 58 | 9614 | return 0; | |
| 59 | } | ||
| 60 | |||
| 61 | 9614 | static size_t ffstring_read(FFFILE *f, unsigned char *buf, size_t len) | |
| 62 | { | ||
| 63 | 9614 | char *src = f->cookie; | |
| 64 | 9614 | size_t k = len+256; | |
| 65 | 9614 | char *end = memchr(src, 0, k); | |
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 9608 times.
✓ Branch 1 taken 6 times.
|
9614 | if (end) k = end-src; |
| 68 |
2/2✓ Branch 0 taken 145 times.
✓ Branch 1 taken 9469 times.
|
9614 | if (k < len) len = k; |
| 69 | 9614 | memcpy(buf, src, len); | |
| 70 | 9614 | f->rpos = (void *)(src+len); | |
| 71 | 9614 | f->rend = (void *)(src+k); | |
| 72 | 9614 | f->cookie = src+k; | |
| 73 | |||
| 74 | 9614 | return len; | |
| 75 | } | ||
| 76 | |||
| 77 | 9614 | static int ffuflow(FFFILE *f) | |
| 78 | { | ||
| 79 | unsigned char c; | ||
| 80 |
3/4✓ Branch 1 taken 9614 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9469 times.
✓ Branch 5 taken 145 times.
|
9614 | if (!fftoread(f) && f->read(f, &c, 1)==1) return c; |
| 81 | 145 | return EOF; | |
| 82 | } | ||
| 83 | |||
| 84 | 41866 | static void ffshlim(FFFILE *f, ptrdiff_t lim) | |
| 85 | { | ||
| 86 | 41866 | f->shlim = lim; | |
| 87 | 41866 | f->shcnt = f->buf - f->rpos; | |
| 88 | /* If lim is nonzero, rend must be a valid pointer. */ | ||
| 89 |
4/4✓ Branch 0 taken 34305 times.
✓ Branch 1 taken 7561 times.
✓ Branch 2 taken 17088 times.
✓ Branch 3 taken 17217 times.
|
41866 | if (lim && f->rend - f->rpos > lim) |
| 90 | 17088 | f->shend = f->rpos + lim; | |
| 91 | else | ||
| 92 | 24778 | f->shend = f->rend; | |
| 93 | 41866 | } | |
| 94 | |||
| 95 | 43793 | static int ffshgetc(FFFILE *f) | |
| 96 | { | ||
| 97 | int c; | ||
| 98 | 43793 | ptrdiff_t cnt = shcnt(f); | |
| 99 |
6/6✓ Branch 0 taken 42852 times.
✓ Branch 1 taken 941 times.
✓ Branch 2 taken 8673 times.
✓ Branch 3 taken 34179 times.
✓ Branch 5 taken 145 times.
✓ Branch 6 taken 9469 times.
|
43793 | if (f->shlim && cnt >= f->shlim || (c=ffuflow(f)) < 0) { |
| 100 | 34324 | f->shcnt = f->buf - f->rpos + cnt; | |
| 101 | 34324 | f->shend = 0; | |
| 102 | 34324 | return EOF; | |
| 103 | } | ||
| 104 | 9469 | cnt++; | |
| 105 |
3/4✓ Branch 0 taken 8673 times.
✓ Branch 1 taken 796 times.
✓ Branch 2 taken 8673 times.
✗ Branch 3 not taken.
|
9469 | if (f->shlim && f->rend - f->rpos > f->shlim - cnt) |
| 106 | 8673 | f->shend = f->rpos + (f->shlim - cnt); | |
| 107 | else | ||
| 108 | 796 | f->shend = f->rend; | |
| 109 | 9469 | f->shcnt = f->buf - f->rpos + cnt; | |
| 110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9469 times.
|
9469 | if (f->rpos[-1] != c) f->rpos[-1] = c; |
| 111 | 9469 | return c; | |
| 112 | } | ||
| 113 | |||
| 114 | #define shlim(f, lim) ffshlim((f), (lim)) | ||
| 115 | #define shgetc(f) (((f)->rpos < (f)->shend) ? *(f)->rpos++ : ffshgetc(f)) | ||
| 116 | #define shunget(f) ((f)->shend ? (void)(f)->rpos-- : (void)0) | ||
| 117 | |||
| 118 | static const unsigned char table[] = { -1, | ||
| 119 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, | ||
| 123 | -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, | ||
| 124 | 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, | ||
| 125 | -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, | ||
| 126 | 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, | ||
| 127 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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 | }; | ||
| 136 | |||
| 137 | 2288 | static unsigned long long ffintscan(FFFILE *f, unsigned base, int pok, unsigned long long lim) | |
| 138 | { | ||
| 139 | 2288 | const unsigned char *val = table+1; | |
| 140 | 2288 | int c, neg=0; | |
| 141 | unsigned x; | ||
| 142 | unsigned long long y; | ||
| 143 |
2/4✓ Branch 0 taken 2288 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2288 times.
|
2288 | if (base > 36 || base == 1) { |
| 144 | ✗ | errno = EINVAL; | |
| 145 | ✗ | return 0; | |
| 146 | } | ||
| 147 |
2/4✓ Branch 0 taken 2288 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2288 times.
|
2288 | while (av_isspace((c=shgetc(f)))); |
| 148 |
3/4✓ Branch 0 taken 2288 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2285 times.
|
2288 | if (c=='+' || c=='-') { |
| 149 | 3 | neg = -(c=='-'); | |
| 150 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | c = shgetc(f); |
| 151 | } | ||
| 152 |
2/6✓ Branch 0 taken 2288 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2288 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2288 | if ((base == 0 || base == 16) && c=='0') { |
| 153 | ✗ | c = shgetc(f); | |
| 154 | ✗ | if ((c|32)=='x') { | |
| 155 | ✗ | c = shgetc(f); | |
| 156 | ✗ | if (val[c]>=16) { | |
| 157 | ✗ | shunget(f); | |
| 158 | ✗ | if (pok) shunget(f); | |
| 159 | ✗ | else shlim(f, 0); | |
| 160 | ✗ | return 0; | |
| 161 | } | ||
| 162 | ✗ | base = 16; | |
| 163 | ✗ | } else if (base == 0) { | |
| 164 | ✗ | base = 8; | |
| 165 | } | ||
| 166 | } else { | ||
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2288 times.
|
2288 | if (base == 0) base = 10; |
| 168 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 2210 times.
|
2288 | if (val[c] >= base) { |
| 169 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | shunget(f); |
| 170 | 78 | shlim(f, 0); | |
| 171 | 78 | errno = EINVAL; | |
| 172 | 78 | return 0; | |
| 173 | } | ||
| 174 | } | ||
| 175 |
1/2✓ Branch 0 taken 2210 times.
✗ Branch 1 not taken.
|
2210 | if (base == 10) { |
| 176 |
3/4✓ Branch 1 taken 4014 times.
✓ Branch 2 taken 2210 times.
✓ Branch 3 taken 4014 times.
✗ Branch 4 not taken.
|
6224 | for (x=0; c-'0'<10U && x<=UINT_MAX/10-1; c=shgetc(f)) |
| 177 |
2/2✓ Branch 0 taken 3985 times.
✓ Branch 1 taken 29 times.
|
4014 | x = x*10 + (c-'0'); |
| 178 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 2210 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
2210 | for (y=x; c-'0'<10U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(c-'0'); c=shgetc(f)) |
| 179 | ✗ | y = y*10 + (c-'0'); | |
| 180 |
1/2✓ Branch 0 taken 2210 times.
✗ Branch 1 not taken.
|
2210 | if (c-'0'>=10U) goto done; |
| 181 | ✗ | } else if (!(base & base-1)) { | |
| 182 | ✗ | int bs = "\0\1\2\4\7\3\6\5"[(0x17*base)>>5&7]; | |
| 183 | ✗ | for (x=0; val[c]<base && x<=UINT_MAX/32; c=shgetc(f)) | |
| 184 | ✗ | x = x<<bs | val[c]; | |
| 185 | ✗ | for (y=x; val[c]<base && y<=ULLONG_MAX>>bs; c=shgetc(f)) | |
| 186 | ✗ | y = y<<bs | val[c]; | |
| 187 | } else { | ||
| 188 | ✗ | for (x=0; val[c]<base && x<=UINT_MAX/36-1; c=shgetc(f)) | |
| 189 | ✗ | x = x*base + val[c]; | |
| 190 | ✗ | for (y=x; val[c]<base && y<=ULLONG_MAX/base && base*y<=ULLONG_MAX-val[c]; c=shgetc(f)) | |
| 191 | ✗ | y = y*base + val[c]; | |
| 192 | } | ||
| 193 | ✗ | if (val[c]<base) { | |
| 194 | ✗ | for (; val[c]<base; c=shgetc(f)); | |
| 195 | ✗ | errno = ERANGE; | |
| 196 | ✗ | y = lim; | |
| 197 | ✗ | if (lim&1) neg = 0; | |
| 198 | } | ||
| 199 | ✗ | done: | |
| 200 |
2/2✓ Branch 0 taken 2181 times.
✓ Branch 1 taken 29 times.
|
2210 | shunget(f); |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2210 times.
|
2210 | if (y>=lim) { |
| 202 | ✗ | if (!(lim&1) && !neg) { | |
| 203 | ✗ | errno = ERANGE; | |
| 204 | ✗ | return lim-1; | |
| 205 | ✗ | } else if (y>lim) { | |
| 206 | ✗ | errno = ERANGE; | |
| 207 | ✗ | return lim; | |
| 208 | } | ||
| 209 | } | ||
| 210 | 2210 | return (y^neg)-neg; | |
| 211 | } | ||
| 212 | |||
| 213 | ✗ | static long long scanexp(FFFILE *f, int pok) | |
| 214 | { | ||
| 215 | int c; | ||
| 216 | int x; | ||
| 217 | long long y; | ||
| 218 | ✗ | int neg = 0; | |
| 219 | |||
| 220 | ✗ | c = shgetc(f); | |
| 221 | ✗ | if (c=='+' || c=='-') { | |
| 222 | ✗ | neg = (c=='-'); | |
| 223 | ✗ | c = shgetc(f); | |
| 224 | ✗ | if (c-'0'>=10U && pok) shunget(f); | |
| 225 | } | ||
| 226 | ✗ | if (c-'0'>=10U) { | |
| 227 | ✗ | shunget(f); | |
| 228 | ✗ | return LLONG_MIN; | |
| 229 | } | ||
| 230 | ✗ | for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f)) | |
| 231 | ✗ | x = 10*x + (c-'0'); | |
| 232 | ✗ | for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f)) | |
| 233 | ✗ | y = 10*y + (c-'0'); | |
| 234 | ✗ | for (; c-'0'<10U; c = shgetc(f)); | |
| 235 | ✗ | shunget(f); | |
| 236 | ✗ | return neg ? -y : y; | |
| 237 | } | ||
| 238 | |||
| 239 | #define LD_B1B_DIG 2 | ||
| 240 | #define LD_B1B_MAX 9007199, 254740991 | ||
| 241 | #define KMAX 128 | ||
| 242 | #define MASK (KMAX-1) | ||
| 243 | |||
| 244 | 190 | static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok) | |
| 245 | { | ||
| 246 | uint32_t x[KMAX]; | ||
| 247 | static const uint32_t th[] = { LD_B1B_MAX }; | ||
| 248 | int i, j, k, a, z; | ||
| 249 | 190 | long long lrp=0, dc=0; | |
| 250 | 190 | long long e10=0; | |
| 251 | 190 | int lnz = 0; | |
| 252 | 190 | int gotdig = 0, gotrad = 0; | |
| 253 | int rp; | ||
| 254 | int e2; | ||
| 255 | 190 | int emax = -emin-bits+3; | |
| 256 | 190 | int denormal = 0; | |
| 257 | double y; | ||
| 258 | 190 | double frac=0; | |
| 259 | 190 | double bias=0; | |
| 260 | static const int p10s[] = { 10, 100, 1000, 10000, | ||
| 261 | 100000, 1000000, 10000000, 100000000 }; | ||
| 262 | |||
| 263 | 190 | j=0; | |
| 264 | 190 | k=0; | |
| 265 | |||
| 266 | /* Don't let leading zeros consume buffer space */ | ||
| 267 |
3/4✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 190 times.
|
207 | for (; c=='0'; c = shgetc(f)) gotdig=1; |
| 268 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 188 times.
|
190 | if (c=='.') { |
| 269 | 2 | gotrad = 1; | |
| 270 |
2/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
|
2 | for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--; |
| 271 | } | ||
| 272 | |||
| 273 | 190 | x[0] = 0; | |
| 274 |
6/6✓ Branch 0 taken 1111 times.
✓ Branch 1 taken 61 times.
✓ Branch 3 taken 987 times.
✓ Branch 4 taken 375 times.
✓ Branch 5 taken 185 times.
✓ Branch 6 taken 190 times.
|
1362 | for (; c-'0'<10U || c=='.'; c = shgetc(f)) { |
| 275 |
2/2✓ Branch 0 taken 185 times.
✓ Branch 1 taken 987 times.
|
1172 | if (c == '.') { |
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (gotrad) break; |
| 277 | 185 | gotrad = 1; | |
| 278 | 185 | lrp = dc; | |
| 279 |
1/2✓ Branch 0 taken 987 times.
✗ Branch 1 not taken.
|
987 | } else if (k < KMAX-3) { |
| 280 | 987 | dc++; | |
| 281 |
2/2✓ Branch 0 taken 605 times.
✓ Branch 1 taken 382 times.
|
987 | if (c!='0') lnz = dc; |
| 282 |
2/2✓ Branch 0 taken 797 times.
✓ Branch 1 taken 190 times.
|
987 | if (j) x[k] = x[k]*10 + c-'0'; |
| 283 | 190 | else x[k] = c-'0'; | |
| 284 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 987 times.
|
987 | if (++j==9) { |
| 285 | ✗ | k++; | |
| 286 | ✗ | j=0; | |
| 287 | } | ||
| 288 | 987 | gotdig=1; | |
| 289 | } else { | ||
| 290 | ✗ | dc++; | |
| 291 | ✗ | if (c!='0') { | |
| 292 | ✗ | lnz = (KMAX-4)*9; | |
| 293 | ✗ | x[KMAX-4] |= 1; | |
| 294 | } | ||
| 295 | } | ||
| 296 | } | ||
| 297 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 187 times.
|
190 | if (!gotrad) lrp=dc; |
| 298 | |||
| 299 |
2/4✓ Branch 0 taken 190 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 190 times.
|
190 | if (gotdig && (c|32)=='e') { |
| 300 | ✗ | e10 = scanexp(f, pok); | |
| 301 | ✗ | if (e10 == LLONG_MIN) { | |
| 302 | ✗ | if (pok) { | |
| 303 | ✗ | shunget(f); | |
| 304 | } else { | ||
| 305 | ✗ | shlim(f, 0); | |
| 306 | ✗ | return 0; | |
| 307 | } | ||
| 308 | ✗ | e10 = 0; | |
| 309 | } | ||
| 310 | ✗ | lrp += e10; | |
| 311 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 61 times.
|
190 | } else if (c>=0) { |
| 312 |
1/2✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
|
129 | shunget(f); |
| 313 | } | ||
| 314 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
|
190 | if (!gotdig) { |
| 315 | ✗ | errno = EINVAL; | |
| 316 | ✗ | shlim(f, 0); | |
| 317 | ✗ | return 0; | |
| 318 | } | ||
| 319 | |||
| 320 | /* Handle zero specially to avoid nasty special cases later */ | ||
| 321 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
|
190 | if (!x[0]) return sign * 0.0; |
| 322 | |||
| 323 | /* Optimize small integers (w/no exponent) and over/under-flow */ | ||
| 324 |
6/8✓ Branch 0 taken 3 times.
✓ Branch 1 taken 187 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
|
190 | if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0)) |
| 325 | 3 | return sign * (double)x[0]; | |
| 326 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 187 times.
|
187 | if (lrp > -emin/2) { |
| 327 | ✗ | errno = ERANGE; | |
| 328 | ✗ | return sign * DBL_MAX * DBL_MAX; | |
| 329 | } | ||
| 330 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 187 times.
|
187 | if (lrp < emin-2*DBL_MANT_DIG) { |
| 331 | ✗ | errno = ERANGE; | |
| 332 | ✗ | return sign * DBL_MIN * DBL_MIN; | |
| 333 | } | ||
| 334 | |||
| 335 | /* Align incomplete final B1B digit */ | ||
| 336 |
1/2✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
|
187 | if (j) { |
| 337 |
2/2✓ Branch 0 taken 702 times.
✓ Branch 1 taken 187 times.
|
889 | for (; j<9; j++) x[k]*=10; |
| 338 | 187 | k++; | |
| 339 | 187 | j=0; | |
| 340 | } | ||
| 341 | |||
| 342 | 187 | a = 0; | |
| 343 | 187 | z = k; | |
| 344 | 187 | e2 = 0; | |
| 345 | 187 | rp = lrp; | |
| 346 | |||
| 347 | /* Optimize small to mid-size integers (even in exp. notation) */ | ||
| 348 |
4/6✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 131 times.
✓ Branch 4 taken 56 times.
✗ Branch 5 not taken.
|
187 | if (lnz<9 && lnz<=rp && rp < 18) { |
| 349 | int bitlim; | ||
| 350 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | if (rp == 9) return sign * (double)x[0]; |
| 351 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | if (rp < 9) return sign * (double)x[0] / p10s[8-rp]; |
| 352 | ✗ | bitlim = bits-3*(int)(rp-9); | |
| 353 | ✗ | if (bitlim>30 || x[0]>>bitlim==0) | |
| 354 | ✗ | return sign * (double)x[0] * p10s[rp-10]; | |
| 355 | } | ||
| 356 | |||
| 357 | /* Drop trailing zeros */ | ||
| 358 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
|
131 | for (; !x[z-1]; z--); |
| 359 | |||
| 360 | /* Align radix point to B1B digit boundary */ | ||
| 361 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 2 times.
|
131 | if (rp % 9) { |
| 362 |
1/2✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
|
129 | int rpm9 = rp>=0 ? rp%9 : rp%9+9; |
| 363 | 129 | int p10 = p10s[8-rpm9]; | |
| 364 | 129 | uint32_t carry = 0; | |
| 365 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 129 times.
|
258 | for (k=a; k!=z; k++) { |
| 366 | 129 | uint32_t tmp = x[k] % p10; | |
| 367 | 129 | x[k] = x[k]/p10 + carry; | |
| 368 | 129 | carry = 1000000000/p10 * tmp; | |
| 369 |
2/4✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 129 times.
|
129 | if (k==a && !x[k]) { |
| 370 | ✗ | a = (a+1 & MASK); | |
| 371 | ✗ | rp -= 9; | |
| 372 | } | ||
| 373 | } | ||
| 374 |
1/2✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
|
129 | if (carry) x[z++] = carry; |
| 375 | 129 | rp += 9-rpm9; | |
| 376 | } | ||
| 377 | |||
| 378 | /* Upscale until desired number of bits are left of radix point */ | ||
| 379 |
6/6✓ Branch 0 taken 139 times.
✓ Branch 1 taken 254 times.
✓ Branch 2 taken 123 times.
✓ Branch 3 taken 131 times.
✓ Branch 4 taken 123 times.
✓ Branch 5 taken 8 times.
|
393 | while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) { |
| 380 | 262 | uint32_t carry = 0; | |
| 381 | 262 | e2 -= 29; | |
| 382 | 642 | for (k=(z-1 & MASK); ; k=(k-1 & MASK)) { | |
| 383 | 642 | uint64_t tmp = ((uint64_t)x[k] << 29) + carry; | |
| 384 |
2/2✓ Branch 0 taken 634 times.
✓ Branch 1 taken 8 times.
|
642 | if (tmp > 1000000000) { |
| 385 | 634 | carry = tmp / 1000000000; | |
| 386 | 634 | x[k] = tmp % 1000000000; | |
| 387 | } else { | ||
| 388 | 8 | carry = 0; | |
| 389 | 8 | x[k] = tmp; | |
| 390 | } | ||
| 391 |
6/6✓ Branch 0 taken 267 times.
✓ Branch 1 taken 375 times.
✓ Branch 2 taken 260 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 255 times.
|
642 | if (k==(z-1 & MASK) && k!=a && !x[k]) z = k; |
| 392 |
2/2✓ Branch 0 taken 262 times.
✓ Branch 1 taken 380 times.
|
642 | if (k==a) break; |
| 393 | } | ||
| 394 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 6 times.
|
262 | if (carry) { |
| 395 | 256 | rp += 9; | |
| 396 | 256 | a = (a-1 & MASK); | |
| 397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 256 times.
|
256 | if (a == z) { |
| 398 | ✗ | z = (z-1 & MASK); | |
| 399 | ✗ | x[z-1 & MASK] |= x[z]; | |
| 400 | } | ||
| 401 | 256 | x[a] = carry; | |
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | /* Downscale until exactly number of bits are left of radix point */ | ||
| 406 | 1315 | for (;;) { | |
| 407 | 1446 | uint32_t carry = 0; | |
| 408 | 1446 | int sh = 1; | |
| 409 |
1/2✓ Branch 0 taken 1448 times.
✗ Branch 1 not taken.
|
1448 | for (i=0; i<LD_B1B_DIG; i++) { |
| 410 | 1448 | k = (a+i & MASK); | |
| 411 |
3/4✓ Branch 0 taken 1448 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 566 times.
✓ Branch 3 taken 882 times.
|
1448 | if (k == z || x[k] < th[i]) { |
| 412 | 566 | i=LD_B1B_DIG; | |
| 413 | 566 | break; | |
| 414 | } | ||
| 415 |
2/2✓ Branch 0 taken 880 times.
✓ Branch 1 taken 2 times.
|
882 | if (x[a+i & MASK] > th[i]) break; |
| 416 | } | ||
| 417 |
4/4✓ Branch 0 taken 566 times.
✓ Branch 1 taken 880 times.
✓ Branch 2 taken 131 times.
✓ Branch 3 taken 435 times.
|
1446 | if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break; |
| 418 | /* FIXME: find a way to compute optimal sh */ | ||
| 419 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1315 times.
|
1315 | if (rp > 9+9*LD_B1B_DIG) sh = 9; |
| 420 | 1315 | e2 += sh; | |
| 421 |
2/2✓ Branch 0 taken 4343 times.
✓ Branch 1 taken 1315 times.
|
5658 | for (k=a; k!=z; k=(k+1 & MASK)) { |
| 422 | 4343 | uint32_t tmp = x[k] & (1<<sh)-1; | |
| 423 | 4343 | x[k] = (x[k]>>sh) + carry; | |
| 424 | 4343 | carry = (1000000000>>sh) * tmp; | |
| 425 |
4/4✓ Branch 0 taken 1438 times.
✓ Branch 1 taken 2905 times.
✓ Branch 2 taken 123 times.
✓ Branch 3 taken 1315 times.
|
4343 | if (k==a && !x[k]) { |
| 426 | 123 | a = (a+1 & MASK); | |
| 427 | 123 | i--; | |
| 428 | 123 | rp -= 9; | |
| 429 | } | ||
| 430 | } | ||
| 431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1315 times.
|
1315 | if (carry) { |
| 432 | ✗ | if ((z+1 & MASK) != a) { | |
| 433 | ✗ | x[z] = carry; | |
| 434 | ✗ | z = (z+1 & MASK); | |
| 435 | ✗ | } else x[z-1 & MASK] |= 1; | |
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | /* Assemble desired bits into floating point variable */ | ||
| 440 |
2/2✓ Branch 0 taken 262 times.
✓ Branch 1 taken 131 times.
|
393 | for (y=i=0; i<LD_B1B_DIG; i++) { |
| 441 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
|
262 | if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0; |
| 442 | 262 | y = 1000000000.0L * y + x[a+i & MASK]; | |
| 443 | } | ||
| 444 | |||
| 445 | 131 | y *= sign; | |
| 446 | |||
| 447 | /* Limit precision for denormal results */ | ||
| 448 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
|
131 | if (bits > DBL_MANT_DIG+e2-emin) { |
| 449 | ✗ | bits = DBL_MANT_DIG+e2-emin; | |
| 450 | ✗ | if (bits<0) bits=0; | |
| 451 | ✗ | denormal = 1; | |
| 452 | } | ||
| 453 | |||
| 454 | /* Calculate bias term to force rounding, move out lower bits */ | ||
| 455 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 129 times.
|
131 | if (bits < DBL_MANT_DIG) { |
| 456 | 2 | bias = copysign(scalbn(1, 2*DBL_MANT_DIG-bits-1), y); | |
| 457 | 2 | frac = fmod(y, scalbn(1, DBL_MANT_DIG-bits)); | |
| 458 | 2 | y -= frac; | |
| 459 | 2 | y += bias; | |
| 460 | } | ||
| 461 | |||
| 462 | /* Process tail of decimal input so it can affect rounding */ | ||
| 463 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 5 times.
|
131 | if ((a+i & MASK) != z) { |
| 464 | 126 | uint32_t t = x[a+i & MASK]; | |
| 465 |
3/6✓ Branch 0 taken 69 times.
✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 69 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
126 | if (t < 500000000 && (t || (a+i+1 & MASK) != z)) |
| 466 | 69 | frac += 0.25*sign; | |
| 467 |
1/2✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
|
57 | else if (t > 500000000) |
| 468 | 57 | frac += 0.75*sign; | |
| 469 | ✗ | else if (t == 500000000) { | |
| 470 | ✗ | if ((a+i+1 & MASK) == z) | |
| 471 | ✗ | frac += 0.5*sign; | |
| 472 | else | ||
| 473 | ✗ | frac += 0.75*sign; | |
| 474 | } | ||
| 475 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
126 | if (DBL_MANT_DIG-bits >= 2 && !fmod(frac, 1)) |
| 476 | ✗ | frac++; | |
| 477 | } | ||
| 478 | |||
| 479 | 131 | y += frac; | |
| 480 | 131 | y -= bias; | |
| 481 | |||
| 482 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
|
131 | if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) { |
| 483 | ✗ | if (fabs(y) >= pow(2, DBL_MANT_DIG)) { | |
| 484 | ✗ | if (denormal && bits==DBL_MANT_DIG+e2-emin) | |
| 485 | ✗ | denormal = 0; | |
| 486 | ✗ | y *= 0.5; | |
| 487 | ✗ | e2++; | |
| 488 | } | ||
| 489 | ✗ | if (e2+DBL_MANT_DIG>emax || (denormal && frac)) | |
| 490 | ✗ | errno = ERANGE; | |
| 491 | } | ||
| 492 | |||
| 493 | 131 | return scalbn(y, e2); | |
| 494 | } | ||
| 495 | |||
| 496 | ✗ | static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok) | |
| 497 | { | ||
| 498 | ✗ | uint32_t x = 0; | |
| 499 | ✗ | double y = 0; | |
| 500 | ✗ | double scale = 1; | |
| 501 | ✗ | double bias = 0; | |
| 502 | ✗ | int gottail = 0, gotrad = 0, gotdig = 0; | |
| 503 | ✗ | long long rp = 0; | |
| 504 | ✗ | long long dc = 0; | |
| 505 | ✗ | long long e2 = 0; | |
| 506 | int d; | ||
| 507 | int c; | ||
| 508 | |||
| 509 | ✗ | c = shgetc(f); | |
| 510 | |||
| 511 | /* Skip leading zeros */ | ||
| 512 | ✗ | for (; c=='0'; c = shgetc(f)) | |
| 513 | ✗ | gotdig = 1; | |
| 514 | |||
| 515 | ✗ | if (c=='.') { | |
| 516 | ✗ | gotrad = 1; | |
| 517 | ✗ | c = shgetc(f); | |
| 518 | /* Count zeros after the radix point before significand */ | ||
| 519 | ✗ | for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1; | |
| 520 | } | ||
| 521 | |||
| 522 | ✗ | for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) { | |
| 523 | ✗ | if (c=='.') { | |
| 524 | ✗ | if (gotrad) break; | |
| 525 | ✗ | rp = dc; | |
| 526 | ✗ | gotrad = 1; | |
| 527 | } else { | ||
| 528 | ✗ | gotdig = 1; | |
| 529 | ✗ | if (c > '9') d = (c|32)+10-'a'; | |
| 530 | ✗ | else d = c-'0'; | |
| 531 | ✗ | if (dc<8) { | |
| 532 | ✗ | x = x*16 + d; | |
| 533 | ✗ | } else if (dc < DBL_MANT_DIG/4+1) { | |
| 534 | ✗ | y += d*(scale/=16); | |
| 535 | ✗ | } else if (d && !gottail) { | |
| 536 | ✗ | y += 0.5*scale; | |
| 537 | ✗ | gottail = 1; | |
| 538 | } | ||
| 539 | ✗ | dc++; | |
| 540 | } | ||
| 541 | } | ||
| 542 | ✗ | if (!gotdig) { | |
| 543 | ✗ | shunget(f); | |
| 544 | ✗ | if (pok) { | |
| 545 | ✗ | shunget(f); | |
| 546 | ✗ | if (gotrad) shunget(f); | |
| 547 | } else { | ||
| 548 | ✗ | shlim(f, 0); | |
| 549 | } | ||
| 550 | ✗ | return sign * 0.0; | |
| 551 | } | ||
| 552 | ✗ | if (!gotrad) rp = dc; | |
| 553 | ✗ | while (dc<8) x *= 16, dc++; | |
| 554 | ✗ | if ((c|32)=='p') { | |
| 555 | ✗ | e2 = scanexp(f, pok); | |
| 556 | ✗ | if (e2 == LLONG_MIN) { | |
| 557 | ✗ | if (pok) { | |
| 558 | ✗ | shunget(f); | |
| 559 | } else { | ||
| 560 | ✗ | shlim(f, 0); | |
| 561 | ✗ | return 0; | |
| 562 | } | ||
| 563 | ✗ | e2 = 0; | |
| 564 | } | ||
| 565 | } else { | ||
| 566 | ✗ | shunget(f); | |
| 567 | } | ||
| 568 | ✗ | e2 += 4*rp - 32; | |
| 569 | |||
| 570 | ✗ | if (!x) return sign * 0.0; | |
| 571 | ✗ | if (e2 > -emin) { | |
| 572 | ✗ | errno = ERANGE; | |
| 573 | ✗ | return sign * DBL_MAX * DBL_MAX; | |
| 574 | } | ||
| 575 | ✗ | if (e2 < emin-2*DBL_MANT_DIG) { | |
| 576 | ✗ | errno = ERANGE; | |
| 577 | ✗ | return sign * DBL_MIN * DBL_MIN; | |
| 578 | } | ||
| 579 | |||
| 580 | ✗ | while (x < 0x80000000) { | |
| 581 | ✗ | if (y>=0.5) { | |
| 582 | ✗ | x += x + 1; | |
| 583 | ✗ | y += y - 1; | |
| 584 | } else { | ||
| 585 | ✗ | x += x; | |
| 586 | ✗ | y += y; | |
| 587 | } | ||
| 588 | ✗ | e2--; | |
| 589 | } | ||
| 590 | |||
| 591 | ✗ | if (bits > 32+e2-emin) { | |
| 592 | ✗ | bits = 32+e2-emin; | |
| 593 | ✗ | if (bits<0) bits=0; | |
| 594 | } | ||
| 595 | |||
| 596 | ✗ | if (bits < DBL_MANT_DIG) | |
| 597 | ✗ | bias = copysign(scalbn(1, 32+DBL_MANT_DIG-bits-1), sign); | |
| 598 | |||
| 599 | ✗ | if (bits<32 && y && !(x&1)) x++, y=0; | |
| 600 | |||
| 601 | ✗ | y = bias + sign*(double)x + sign*y; | |
| 602 | ✗ | y -= bias; | |
| 603 | |||
| 604 | ✗ | if (!y) errno = ERANGE; | |
| 605 | |||
| 606 | ✗ | return scalbn(y, e2); | |
| 607 | } | ||
| 608 | |||
| 609 | 190 | static double fffloatscan(FFFILE *f, int prec, int pok) | |
| 610 | { | ||
| 611 | 190 | int sign = 1; | |
| 612 | size_t i; | ||
| 613 | int bits; | ||
| 614 | int emin; | ||
| 615 | int c; | ||
| 616 | |||
| 617 |
2/4✓ Branch 0 taken 60 times.
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
190 | switch (prec) { |
| 618 | 60 | case 0: | |
| 619 | 60 | bits = FLT_MANT_DIG; | |
| 620 | 60 | emin = FLT_MIN_EXP-bits; | |
| 621 | 60 | break; | |
| 622 | 130 | case 1: | |
| 623 | 130 | bits = DBL_MANT_DIG; | |
| 624 | 130 | emin = DBL_MIN_EXP-bits; | |
| 625 | 130 | break; | |
| 626 | ✗ | case 2: | |
| 627 | ✗ | bits = DBL_MANT_DIG; | |
| 628 | ✗ | emin = DBL_MIN_EXP-bits; | |
| 629 | ✗ | break; | |
| 630 | ✗ | default: | |
| 631 | ✗ | return 0; | |
| 632 | } | ||
| 633 | |||
| 634 |
2/4✓ Branch 0 taken 190 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 190 times.
|
190 | while (av_isspace((c = shgetc(f)))); |
| 635 | |||
| 636 |
3/4✓ Branch 0 taken 190 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 162 times.
|
190 | if (c=='+' || c=='-') { |
| 637 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | sign -= 2*(c=='-'); |
| 638 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | c = shgetc(f); |
| 639 | } | ||
| 640 | |||
| 641 |
2/4✓ Branch 0 taken 190 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 190 times.
|
190 | for (i=0; i<8 && (c|32)=="infinity"[i]; i++) |
| 642 | ✗ | if (i<7) c = shgetc(f); | |
| 643 |
3/8✓ Branch 0 taken 190 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 190 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 190 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
190 | if (i==3 || i==8 || (i>3 && pok)) { |
| 644 | ✗ | if (i!=8) { | |
| 645 | ✗ | shunget(f); | |
| 646 | ✗ | if (pok) for (; i>3; i--) shunget(f); | |
| 647 | } | ||
| 648 | ✗ | return sign * INFINITY; | |
| 649 | } | ||
| 650 |
3/6✓ Branch 0 taken 190 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 190 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 190 times.
|
190 | if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++) |
| 651 | ✗ | if (i<2) c = shgetc(f); | |
| 652 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
|
190 | if (i==3) { |
| 653 | ✗ | if (shgetc(f) != '(') { | |
| 654 | ✗ | shunget(f); | |
| 655 | ✗ | return NAN; | |
| 656 | } | ||
| 657 | ✗ | for (i=1; ; i++) { | |
| 658 | ✗ | c = shgetc(f); | |
| 659 | ✗ | if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_') | |
| 660 | ✗ | continue; | |
| 661 | ✗ | if (c==')') return NAN; | |
| 662 | ✗ | shunget(f); | |
| 663 | ✗ | if (!pok) { | |
| 664 | ✗ | errno = EINVAL; | |
| 665 | ✗ | shlim(f, 0); | |
| 666 | ✗ | return 0; | |
| 667 | } | ||
| 668 | ✗ | while (i--) shunget(f); | |
| 669 | ✗ | return NAN; | |
| 670 | } | ||
| 671 | } | ||
| 672 | |||
| 673 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
|
190 | if (i) { |
| 674 | ✗ | shunget(f); | |
| 675 | ✗ | errno = EINVAL; | |
| 676 | ✗ | shlim(f, 0); | |
| 677 | ✗ | return 0; | |
| 678 | } | ||
| 679 | |||
| 680 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 173 times.
|
190 | if (c=='0') { |
| 681 |
1/2✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
17 | c = shgetc(f); |
| 682 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if ((c|32) == 'x') |
| 683 | ✗ | return hexfloat(f, bits, emin, sign, pok); | |
| 684 |
1/2✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
17 | shunget(f); |
| 685 | 17 | c = '0'; | |
| 686 | } | ||
| 687 | |||
| 688 | 190 | return decfloat(f, c, bits, emin, sign, pok); | |
| 689 | } | ||
| 690 | |||
| 691 | ✗ | static void *arg_n(va_list ap, unsigned int n) | |
| 692 | { | ||
| 693 | void *p; | ||
| 694 | unsigned int i; | ||
| 695 | va_list ap2; | ||
| 696 | ✗ | va_copy(ap2, ap); | |
| 697 | ✗ | for (i=n; i>1; i--) va_arg(ap2, void *); | |
| 698 | ✗ | p = va_arg(ap2, void *); | |
| 699 | ✗ | va_end(ap2); | |
| 700 | ✗ | return p; | |
| 701 | } | ||
| 702 | |||
| 703 | 2261 | static void store_int(void *dest, int size, unsigned long long i) | |
| 704 | { | ||
| 705 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2261 times.
|
2261 | if (!dest) return; |
| 706 |
3/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1984 times.
✓ Branch 3 taken 226 times.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
|
2261 | switch (size) { |
| 707 | ✗ | case SIZE_hh: | |
| 708 | ✗ | *(char *)dest = i; | |
| 709 | ✗ | break; | |
| 710 | ✗ | case SIZE_h: | |
| 711 | ✗ | *(short *)dest = i; | |
| 712 | ✗ | break; | |
| 713 | 1984 | case SIZE_def: | |
| 714 | 1984 | *(int *)dest = i; | |
| 715 | 1984 | break; | |
| 716 | 226 | case SIZE_l: | |
| 717 | 226 | *(long *)dest = i; | |
| 718 | 226 | break; | |
| 719 | 51 | case SIZE_ll: | |
| 720 | 51 | *(long long *)dest = i; | |
| 721 | 51 | break; | |
| 722 | } | ||
| 723 | } | ||
| 724 | |||
| 725 | 9474 | static int ff_vfscanf(FFFILE *f, const char *fmt, va_list ap) | |
| 726 | { | ||
| 727 | int width; | ||
| 728 | int size; | ||
| 729 | int base; | ||
| 730 | const unsigned char *p; | ||
| 731 | int c, t; | ||
| 732 | char *s; | ||
| 733 | 9474 | void *dest=NULL; | |
| 734 | int invert; | ||
| 735 | 9474 | int matches=0; | |
| 736 | unsigned long long x; | ||
| 737 | double y; | ||
| 738 | 9474 | ptrdiff_t pos = 0; | |
| 739 | unsigned char scanset[257]; | ||
| 740 | size_t i; | ||
| 741 | |||
| 742 |
2/2✓ Branch 0 taken 39356 times.
✓ Branch 1 taken 9261 times.
|
48617 | for (p=(const unsigned char *)fmt; *p; p++) { |
| 743 | |||
| 744 |
2/2✓ Branch 0 taken 412 times.
✓ Branch 1 taken 38944 times.
|
39356 | if (av_isspace(*p)) { |
| 745 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
|
412 | while (av_isspace(p[1])) p++; |
| 746 | 412 | shlim(f, 0); | |
| 747 |
4/4✓ Branch 0 taken 732 times.
✓ Branch 1 taken 7 times.
✓ Branch 3 taken 327 times.
✓ Branch 4 taken 412 times.
|
739 | while (av_isspace(shgetc(f))); |
| 748 |
2/2✓ Branch 0 taken 405 times.
✓ Branch 1 taken 7 times.
|
412 | shunget(f); |
| 749 | 412 | pos += shcnt(f); | |
| 750 | 412 | continue; | |
| 751 | } | ||
| 752 |
3/4✓ Branch 0 taken 37249 times.
✓ Branch 1 taken 1695 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37249 times.
|
38944 | if (*p != '%' || p[1] == '%') { |
| 753 | 1695 | shlim(f, 0); | |
| 754 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1695 times.
|
1695 | if (*p == '%') { |
| 755 | ✗ | p++; | |
| 756 | ✗ | while (av_isspace((c=shgetc(f)))); | |
| 757 | } else { | ||
| 758 |
2/2✓ Branch 0 taken 1658 times.
✓ Branch 1 taken 37 times.
|
1695 | c = shgetc(f); |
| 759 | } | ||
| 760 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 1566 times.
|
1695 | if (c!=*p) { |
| 761 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 37 times.
|
129 | shunget(f); |
| 762 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 92 times.
|
129 | if (c<0) goto input_fail; |
| 763 | 92 | goto match_fail; | |
| 764 | } | ||
| 765 | 1566 | pos += shcnt(f); | |
| 766 | 1566 | continue; | |
| 767 | } | ||
| 768 | |||
| 769 | 37249 | p++; | |
| 770 |
2/2✓ Branch 0 taken 360 times.
✓ Branch 1 taken 36889 times.
|
37249 | if (*p=='*') { |
| 771 | 360 | dest = 0; p++; | |
| 772 |
3/4✓ Branch 0 taken 129 times.
✓ Branch 1 taken 36760 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 129 times.
|
36889 | } else if (av_isdigit(*p) && p[1]=='$') { |
| 773 | ✗ | dest = arg_n(ap, *p-'0'); p+=2; | |
| 774 | } else { | ||
| 775 | 36889 | dest = va_arg(ap, void *); | |
| 776 | } | ||
| 777 | |||
| 778 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 37249 times.
|
37378 | for (width=0; av_isdigit(*p); p++) { |
| 779 | 129 | width = 10*width + *p - '0'; | |
| 780 | } | ||
| 781 | |||
| 782 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37249 times.
|
37249 | if (*p=='m') { |
| 783 | ✗ | s = 0; | |
| 784 | ✗ | p++; | |
| 785 | } | ||
| 786 | |||
| 787 | 37249 | size = SIZE_def; | |
| 788 |
2/7✗ Branch 0 not taken.
✓ Branch 1 taken 426 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 36823 times.
✗ Branch 6 not taken.
|
37249 | switch (*p++) { |
| 789 | ✗ | case 'h': | |
| 790 | ✗ | if (*p == 'h') p++, size = SIZE_hh; | |
| 791 | ✗ | else size = SIZE_h; | |
| 792 | ✗ | break; | |
| 793 | 426 | case 'l': | |
| 794 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 356 times.
|
426 | if (*p == 'l') p++, size = SIZE_ll; |
| 795 | 356 | else size = SIZE_l; | |
| 796 | 426 | break; | |
| 797 | ✗ | case 'j': | |
| 798 | ✗ | size = SIZE_ll; | |
| 799 | ✗ | break; | |
| 800 | ✗ | case 'z': | |
| 801 | case 't': | ||
| 802 | ✗ | size = SIZE_l; | |
| 803 | ✗ | break; | |
| 804 | ✗ | case 'L': | |
| 805 | ✗ | size = SIZE_L; | |
| 806 | ✗ | break; | |
| 807 | 36823 | case 'd': case 'i': case 'o': case 'u': case 'x': | |
| 808 | case 'a': case 'e': case 'f': case 'g': | ||
| 809 | case 'A': case 'E': case 'F': case 'G': case 'X': | ||
| 810 | case 's': case 'c': case '[': | ||
| 811 | case 'S': case 'C': | ||
| 812 | case 'p': case 'n': | ||
| 813 | 36823 | p--; | |
| 814 | 36823 | break; | |
| 815 | ✗ | default: | |
| 816 | ✗ | goto fmt_fail; | |
| 817 | } | ||
| 818 | |||
| 819 | 37249 | t = *p; | |
| 820 | |||
| 821 | /* C or S */ | ||
| 822 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37249 times.
|
37249 | if ((t&0x2f) == 3) { |
| 823 | ✗ | t |= 32; | |
| 824 | ✗ | size = SIZE_l; | |
| 825 | } | ||
| 826 | |||
| 827 |
4/4✓ Branch 0 taken 34176 times.
✓ Branch 1 taken 539 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 2483 times.
|
37249 | switch (t) { |
| 828 | 34176 | case 'c': | |
| 829 |
1/2✓ Branch 0 taken 34176 times.
✗ Branch 1 not taken.
|
34176 | if (width < 1) width = 1; |
| 830 | 34176 | break; | |
| 831 | 539 | case '[': | |
| 832 | 539 | break; | |
| 833 | 51 | case 'n': | |
| 834 | 51 | store_int(dest, size, pos); | |
| 835 | /* do not increment match count, etc! */ | ||
| 836 | 51 | continue; | |
| 837 | 2483 | default: | |
| 838 | 2483 | shlim(f, 0); | |
| 839 |
4/4✓ Branch 0 taken 1865 times.
✓ Branch 1 taken 801 times.
✓ Branch 3 taken 183 times.
✓ Branch 4 taken 2483 times.
|
2666 | while (av_isspace(shgetc(f))); |
| 840 |
2/2✓ Branch 0 taken 2478 times.
✓ Branch 1 taken 5 times.
|
2483 | shunget(f); |
| 841 | 2483 | pos += shcnt(f); | |
| 842 | } | ||
| 843 | |||
| 844 | 37198 | shlim(f, width); | |
| 845 |
4/4✓ Branch 0 taken 28520 times.
✓ Branch 1 taken 8678 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 37193 times.
|
37198 | if (shgetc(f) < 0) goto input_fail; |
| 846 |
1/2✓ Branch 0 taken 37193 times.
✗ Branch 1 not taken.
|
37193 | shunget(f); |
| 847 | |||
| 848 |
3/7✓ Branch 0 taken 34715 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2288 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 190 times.
✗ Branch 6 not taken.
|
37193 | switch (t) { |
| 849 | 34715 | case 's': | |
| 850 | case 'c': | ||
| 851 | case '[': | ||
| 852 |
3/4✓ Branch 0 taken 539 times.
✓ Branch 1 taken 34176 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 539 times.
|
34715 | if (t == 'c' || t == 's') { |
| 853 | 34176 | memset(scanset, -1, sizeof scanset); | |
| 854 | 34176 | scanset[0] = 0; | |
| 855 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34176 times.
|
34176 | if (t == 's') { |
| 856 | ✗ | scanset[1 + '\t'] = 0; | |
| 857 | ✗ | scanset[1 + '\n'] = 0; | |
| 858 | ✗ | scanset[1 + '\v'] = 0; | |
| 859 | ✗ | scanset[1 + '\f'] = 0; | |
| 860 | ✗ | scanset[1 + '\r'] = 0; | |
| 861 | ✗ | scanset[1 + ' ' ] = 0; | |
| 862 | } | ||
| 863 | } else { | ||
| 864 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 489 times.
|
539 | if (*++p == '^') p++, invert = 1; |
| 865 | 489 | else invert = 0; | |
| 866 | 539 | memset(scanset, invert, sizeof scanset); | |
| 867 | 539 | scanset[0] = 0; | |
| 868 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 539 times.
|
539 | if (*p == '-') p++, scanset[1+'-'] = 1-invert; |
| 869 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 539 times.
|
539 | else if (*p == ']') p++, scanset[1+']'] = 1-invert; |
| 870 |
2/2✓ Branch 0 taken 1028 times.
✓ Branch 1 taken 539 times.
|
1567 | for (; *p != ']'; p++) { |
| 871 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1028 times.
|
1028 | if (!*p) goto fmt_fail; |
| 872 |
4/6✓ Branch 0 taken 129 times.
✓ Branch 1 taken 899 times.
✓ Branch 2 taken 129 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 129 times.
|
1028 | if (*p=='-' && p[1] && p[1] != ']') |
| 873 | ✗ | for (c=p++[-1]; c<*p; c++) | |
| 874 | ✗ | scanset[1+c] = 1-invert; | |
| 875 | 1028 | scanset[1+*p] = 1-invert; | |
| 876 | } | ||
| 877 | } | ||
| 878 | 34715 | s = 0; | |
| 879 | 34715 | i = 0; | |
| 880 |
2/2✓ Branch 0 taken 34355 times.
✓ Branch 1 taken 360 times.
|
34715 | if ((s = dest)) { |
| 881 |
4/4✓ Branch 0 taken 35001 times.
✓ Branch 1 taken 34180 times.
✓ Branch 3 taken 34826 times.
✓ Branch 4 taken 34355 times.
|
69181 | while (scanset[(c=shgetc(f))+1]) |
| 882 | 34826 | s[i++] = c; | |
| 883 | } else { | ||
| 884 |
3/4✓ Branch 0 taken 720 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 360 times.
✓ Branch 4 taken 360 times.
|
720 | while (scanset[(c=shgetc(f))+1]); |
| 885 | } | ||
| 886 |
2/2✓ Branch 0 taken 535 times.
✓ Branch 1 taken 34180 times.
|
34715 | shunget(f); |
| 887 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34714 times.
|
34715 | if (!shcnt(f)) goto match_fail; |
| 888 |
3/4✓ Branch 0 taken 34176 times.
✓ Branch 1 taken 538 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34176 times.
|
34714 | if (t == 'c' && shcnt(f) != width) goto match_fail; |
| 889 |
2/2✓ Branch 0 taken 538 times.
✓ Branch 1 taken 34176 times.
|
34714 | if (t != 'c') { |
| 890 |
2/2✓ Branch 0 taken 178 times.
✓ Branch 1 taken 360 times.
|
538 | if (s) s[i] = 0; |
| 891 | } | ||
| 892 | 34714 | break; | |
| 893 | ✗ | case 'p': | |
| 894 | case 'X': | ||
| 895 | case 'x': | ||
| 896 | ✗ | base = 16; | |
| 897 | ✗ | goto int_common; | |
| 898 | ✗ | case 'o': | |
| 899 | ✗ | base = 8; | |
| 900 | ✗ | goto int_common; | |
| 901 | 2288 | case 'd': | |
| 902 | case 'u': | ||
| 903 | 2288 | base = 10; | |
| 904 | 2288 | goto int_common; | |
| 905 | ✗ | case 'i': | |
| 906 | ✗ | base = 0; | |
| 907 | 2288 | int_common: | |
| 908 | 2288 | x = ffintscan(f, base, 0, ULLONG_MAX); | |
| 909 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 2210 times.
|
2288 | if (!shcnt(f)) |
| 910 | 78 | goto match_fail; | |
| 911 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2210 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2210 | if (t=='p' && dest) |
| 912 | ✗ | *(void **)dest = (void *)(uintptr_t)x; | |
| 913 | else | ||
| 914 | 2210 | store_int(dest, size, x); | |
| 915 | 2210 | break; | |
| 916 | 190 | case 'a': case 'A': | |
| 917 | case 'e': case 'E': | ||
| 918 | case 'f': case 'F': | ||
| 919 | case 'g': case 'G': | ||
| 920 | 190 | y = fffloatscan(f, size, 0); | |
| 921 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
|
190 | if (!shcnt(f)) |
| 922 | ✗ | goto match_fail; | |
| 923 |
1/2✓ Branch 0 taken 190 times.
✗ Branch 1 not taken.
|
190 | if (dest) { |
| 924 |
2/4✓ Branch 0 taken 60 times.
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
190 | switch (size) { |
| 925 | 60 | case SIZE_def: | |
| 926 | 60 | *(float *)dest = y; | |
| 927 | 60 | break; | |
| 928 | 130 | case SIZE_l: | |
| 929 | 130 | *(double *)dest = y; | |
| 930 | 130 | break; | |
| 931 | ✗ | case SIZE_L: | |
| 932 | ✗ | *(double *)dest = y; | |
| 933 | ✗ | break; | |
| 934 | } | ||
| 935 | } | ||
| 936 | 190 | break; | |
| 937 | } | ||
| 938 | |||
| 939 | 37114 | pos += shcnt(f); | |
| 940 |
2/2✓ Branch 0 taken 36754 times.
✓ Branch 1 taken 360 times.
|
37114 | if (dest) matches++; |
| 941 | } | ||
| 942 | if (0) { | ||
| 943 | ✗ | fmt_fail: | |
| 944 | 42 | input_fail: | |
| 945 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 5 times.
|
42 | if (!matches) matches--; |
| 946 | } | ||
| 947 | 9298 | match_fail: | |
| 948 | 9474 | return matches; | |
| 949 | } | ||
| 950 | |||
| 951 | 9474 | static int ff_vsscanf(const char *s, const char *fmt, va_list ap) | |
| 952 | { | ||
| 953 | 9474 | FFFILE f = { | |
| 954 | .buf = (void *)s, .cookie = (void *)s, | ||
| 955 | .read = ffstring_read, | ||
| 956 | }; | ||
| 957 | |||
| 958 | 9474 | return ff_vfscanf(&f, fmt, ap); | |
| 959 | } | ||
| 960 | |||
| 961 | 9474 | int av_sscanf(const char *string, const char *format, ...) | |
| 962 | { | ||
| 963 | int ret; | ||
| 964 | va_list ap; | ||
| 965 | 9474 | va_start(ap, format); | |
| 966 | 9474 | ret = ff_vsscanf(string, format, ap); | |
| 967 | 9474 | va_end(ap); | |
| 968 | 9474 | return ret; | |
| 969 | } | ||
| 970 |