You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

973 lines
25KB

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