2
0

avsscanf.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. ptrdiff_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, ptrdiff_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. ptrdiff_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. static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok)
  227. {
  228. uint32_t x[KMAX];
  229. static const uint32_t th[] = { LD_B1B_MAX };
  230. int i, j, k, a, z;
  231. long long lrp=0, dc=0;
  232. long long e10=0;
  233. int lnz = 0;
  234. int gotdig = 0, gotrad = 0;
  235. int rp;
  236. int e2;
  237. int emax = -emin-bits+3;
  238. int denormal = 0;
  239. double y;
  240. double frac=0;
  241. double bias=0;
  242. static const int p10s[] = { 10, 100, 1000, 10000,
  243. 100000, 1000000, 10000000, 100000000 };
  244. j=0;
  245. k=0;
  246. /* Don't let leading zeros consume buffer space */
  247. for (; c=='0'; c = shgetc(f)) gotdig=1;
  248. if (c=='.') {
  249. gotrad = 1;
  250. for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
  251. }
  252. x[0] = 0;
  253. for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
  254. if (c == '.') {
  255. if (gotrad) break;
  256. gotrad = 1;
  257. lrp = dc;
  258. } else if (k < KMAX-3) {
  259. dc++;
  260. if (c!='0') lnz = dc;
  261. if (j) x[k] = x[k]*10 + c-'0';
  262. else x[k] = c-'0';
  263. if (++j==9) {
  264. k++;
  265. j=0;
  266. }
  267. gotdig=1;
  268. } else {
  269. dc++;
  270. if (c!='0') {
  271. lnz = (KMAX-4)*9;
  272. x[KMAX-4] |= 1;
  273. }
  274. }
  275. }
  276. if (!gotrad) lrp=dc;
  277. if (gotdig && (c|32)=='e') {
  278. e10 = scanexp(f, pok);
  279. if (e10 == LLONG_MIN) {
  280. if (pok) {
  281. shunget(f);
  282. } else {
  283. shlim(f, 0);
  284. return 0;
  285. }
  286. e10 = 0;
  287. }
  288. lrp += e10;
  289. } else if (c>=0) {
  290. shunget(f);
  291. }
  292. if (!gotdig) {
  293. errno = EINVAL;
  294. shlim(f, 0);
  295. return 0;
  296. }
  297. /* Handle zero specially to avoid nasty special cases later */
  298. if (!x[0]) return sign * 0.0;
  299. /* Optimize small integers (w/no exponent) and over/under-flow */
  300. if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0))
  301. return sign * (double)x[0];
  302. if (lrp > -emin/2) {
  303. errno = ERANGE;
  304. return sign * DBL_MAX * DBL_MAX;
  305. }
  306. if (lrp < emin-2*DBL_MANT_DIG) {
  307. errno = ERANGE;
  308. return sign * DBL_MIN * DBL_MIN;
  309. }
  310. /* Align incomplete final B1B digit */
  311. if (j) {
  312. for (; j<9; j++) x[k]*=10;
  313. k++;
  314. j=0;
  315. }
  316. a = 0;
  317. z = k;
  318. e2 = 0;
  319. rp = lrp;
  320. /* Optimize small to mid-size integers (even in exp. notation) */
  321. if (lnz<9 && lnz<=rp && rp < 18) {
  322. int bitlim;
  323. if (rp == 9) return sign * (double)x[0];
  324. if (rp < 9) return sign * (double)x[0] / p10s[8-rp];
  325. bitlim = bits-3*(int)(rp-9);
  326. if (bitlim>30 || x[0]>>bitlim==0)
  327. return sign * (double)x[0] * p10s[rp-10];
  328. }
  329. /* Drop trailing zeros */
  330. for (; !x[z-1]; z--);
  331. /* Align radix point to B1B digit boundary */
  332. if (rp % 9) {
  333. int rpm9 = rp>=0 ? rp%9 : rp%9+9;
  334. int p10 = p10s[8-rpm9];
  335. uint32_t carry = 0;
  336. for (k=a; k!=z; k++) {
  337. uint32_t tmp = x[k] % p10;
  338. x[k] = x[k]/p10 + carry;
  339. carry = 1000000000/p10 * tmp;
  340. if (k==a && !x[k]) {
  341. a = (a+1 & MASK);
  342. rp -= 9;
  343. }
  344. }
  345. if (carry) x[z++] = carry;
  346. rp += 9-rpm9;
  347. }
  348. /* Upscale until desired number of bits are left of radix point */
  349. while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) {
  350. uint32_t carry = 0;
  351. e2 -= 29;
  352. for (k=(z-1 & MASK); ; k=(k-1 & MASK)) {
  353. uint64_t tmp = ((uint64_t)x[k] << 29) + carry;
  354. if (tmp > 1000000000) {
  355. carry = tmp / 1000000000;
  356. x[k] = tmp % 1000000000;
  357. } else {
  358. carry = 0;
  359. x[k] = tmp;
  360. }
  361. if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
  362. if (k==a) break;
  363. }
  364. if (carry) {
  365. rp += 9;
  366. a = (a-1 & MASK);
  367. if (a == z) {
  368. z = (z-1 & MASK);
  369. x[z-1 & MASK] |= x[z];
  370. }
  371. x[a] = carry;
  372. }
  373. }
  374. /* Downscale until exactly number of bits are left of radix point */
  375. for (;;) {
  376. uint32_t carry = 0;
  377. int sh = 1;
  378. for (i=0; i<LD_B1B_DIG; i++) {
  379. k = (a+i & MASK);
  380. if (k == z || x[k] < th[i]) {
  381. i=LD_B1B_DIG;
  382. break;
  383. }
  384. if (x[a+i & MASK] > th[i]) break;
  385. }
  386. if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break;
  387. /* FIXME: find a way to compute optimal sh */
  388. if (rp > 9+9*LD_B1B_DIG) sh = 9;
  389. e2 += sh;
  390. for (k=a; k!=z; k=(k+1 & MASK)) {
  391. uint32_t tmp = x[k] & (1<<sh)-1;
  392. x[k] = (x[k]>>sh) + carry;
  393. carry = (1000000000>>sh) * tmp;
  394. if (k==a && !x[k]) {
  395. a = (a+1 & MASK);
  396. i--;
  397. rp -= 9;
  398. }
  399. }
  400. if (carry) {
  401. if ((z+1 & MASK) != a) {
  402. x[z] = carry;
  403. z = (z+1 & MASK);
  404. } else x[z-1 & MASK] |= 1;
  405. }
  406. }
  407. /* Assemble desired bits into floating point variable */
  408. for (y=i=0; i<LD_B1B_DIG; i++) {
  409. if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
  410. y = 1000000000.0L * y + x[a+i & MASK];
  411. }
  412. y *= sign;
  413. /* Limit precision for denormal results */
  414. if (bits > DBL_MANT_DIG+e2-emin) {
  415. bits = DBL_MANT_DIG+e2-emin;
  416. if (bits<0) bits=0;
  417. denormal = 1;
  418. }
  419. /* Calculate bias term to force rounding, move out lower bits */
  420. if (bits < DBL_MANT_DIG) {
  421. bias = copysign(scalbn(1, 2*DBL_MANT_DIG-bits-1), y);
  422. frac = fmod(y, scalbn(1, DBL_MANT_DIG-bits));
  423. y -= frac;
  424. y += bias;
  425. }
  426. /* Process tail of decimal input so it can affect rounding */
  427. if ((a+i & MASK) != z) {
  428. uint32_t t = x[a+i & MASK];
  429. if (t < 500000000 && (t || (a+i+1 & MASK) != z))
  430. frac += 0.25*sign;
  431. else if (t > 500000000)
  432. frac += 0.75*sign;
  433. else if (t == 500000000) {
  434. if ((a+i+1 & MASK) == z)
  435. frac += 0.5*sign;
  436. else
  437. frac += 0.75*sign;
  438. }
  439. if (DBL_MANT_DIG-bits >= 2 && !fmod(frac, 1))
  440. frac++;
  441. }
  442. y += frac;
  443. y -= bias;
  444. if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) {
  445. if (fabs(y) >= pow(2, DBL_MANT_DIG)) {
  446. if (denormal && bits==DBL_MANT_DIG+e2-emin)
  447. denormal = 0;
  448. y *= 0.5;
  449. e2++;
  450. }
  451. if (e2+DBL_MANT_DIG>emax || (denormal && frac))
  452. errno = ERANGE;
  453. }
  454. return scalbn(y, e2);
  455. }
  456. static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
  457. {
  458. uint32_t x = 0;
  459. double y = 0;
  460. double scale = 1;
  461. double bias = 0;
  462. int gottail = 0, gotrad = 0, gotdig = 0;
  463. long long rp = 0;
  464. long long dc = 0;
  465. long long e2 = 0;
  466. int d;
  467. int c;
  468. c = shgetc(f);
  469. /* Skip leading zeros */
  470. for (; c=='0'; c = shgetc(f))
  471. gotdig = 1;
  472. if (c=='.') {
  473. gotrad = 1;
  474. c = shgetc(f);
  475. /* Count zeros after the radix point before significand */
  476. for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
  477. }
  478. for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
  479. if (c=='.') {
  480. if (gotrad) break;
  481. rp = dc;
  482. gotrad = 1;
  483. } else {
  484. gotdig = 1;
  485. if (c > '9') d = (c|32)+10-'a';
  486. else d = c-'0';
  487. if (dc<8) {
  488. x = x*16 + d;
  489. } else if (dc < DBL_MANT_DIG/4+1) {
  490. y += d*(scale/=16);
  491. } else if (d && !gottail) {
  492. y += 0.5*scale;
  493. gottail = 1;
  494. }
  495. dc++;
  496. }
  497. }
  498. if (!gotdig) {
  499. shunget(f);
  500. if (pok) {
  501. shunget(f);
  502. if (gotrad) shunget(f);
  503. } else {
  504. shlim(f, 0);
  505. }
  506. return sign * 0.0;
  507. }
  508. if (!gotrad) rp = dc;
  509. while (dc<8) x *= 16, dc++;
  510. if ((c|32)=='p') {
  511. e2 = scanexp(f, pok);
  512. if (e2 == LLONG_MIN) {
  513. if (pok) {
  514. shunget(f);
  515. } else {
  516. shlim(f, 0);
  517. return 0;
  518. }
  519. e2 = 0;
  520. }
  521. } else {
  522. shunget(f);
  523. }
  524. e2 += 4*rp - 32;
  525. if (!x) return sign * 0.0;
  526. if (e2 > -emin) {
  527. errno = ERANGE;
  528. return sign * DBL_MAX * DBL_MAX;
  529. }
  530. if (e2 < emin-2*DBL_MANT_DIG) {
  531. errno = ERANGE;
  532. return sign * DBL_MIN * DBL_MIN;
  533. }
  534. while (x < 0x80000000) {
  535. if (y>=0.5) {
  536. x += x + 1;
  537. y += y - 1;
  538. } else {
  539. x += x;
  540. y += y;
  541. }
  542. e2--;
  543. }
  544. if (bits > 32+e2-emin) {
  545. bits = 32+e2-emin;
  546. if (bits<0) bits=0;
  547. }
  548. if (bits < DBL_MANT_DIG)
  549. bias = copysign(scalbn(1, 32+DBL_MANT_DIG-bits-1), sign);
  550. if (bits<32 && y && !(x&1)) x++, y=0;
  551. y = bias + sign*(double)x + sign*y;
  552. y -= bias;
  553. if (!y) errno = ERANGE;
  554. return scalbn(y, e2);
  555. }
  556. static double fffloatscan(FFFILE *f, int prec, int pok)
  557. {
  558. int sign = 1;
  559. size_t i;
  560. int bits;
  561. int emin;
  562. int c;
  563. switch (prec) {
  564. case 0:
  565. bits = FLT_MANT_DIG;
  566. emin = FLT_MIN_EXP-bits;
  567. break;
  568. case 1:
  569. bits = DBL_MANT_DIG;
  570. emin = DBL_MIN_EXP-bits;
  571. break;
  572. case 2:
  573. bits = DBL_MANT_DIG;
  574. emin = DBL_MIN_EXP-bits;
  575. break;
  576. default:
  577. return 0;
  578. }
  579. while (av_isspace((c = shgetc(f))));
  580. if (c=='+' || c=='-') {
  581. sign -= 2*(c=='-');
  582. c = shgetc(f);
  583. }
  584. for (i=0; i<8 && (c|32)=="infinity"[i]; i++)
  585. if (i<7) c = shgetc(f);
  586. if (i==3 || i==8 || (i>3 && pok)) {
  587. if (i!=8) {
  588. shunget(f);
  589. if (pok) for (; i>3; i--) shunget(f);
  590. }
  591. return sign * INFINITY;
  592. }
  593. if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
  594. if (i<2) c = shgetc(f);
  595. if (i==3) {
  596. if (shgetc(f) != '(') {
  597. shunget(f);
  598. return NAN;
  599. }
  600. for (i=1; ; i++) {
  601. c = shgetc(f);
  602. if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_')
  603. continue;
  604. if (c==')') return NAN;
  605. shunget(f);
  606. if (!pok) {
  607. errno = EINVAL;
  608. shlim(f, 0);
  609. return 0;
  610. }
  611. while (i--) shunget(f);
  612. return NAN;
  613. }
  614. return NAN;
  615. }
  616. if (i) {
  617. shunget(f);
  618. errno = EINVAL;
  619. shlim(f, 0);
  620. return 0;
  621. }
  622. if (c=='0') {
  623. c = shgetc(f);
  624. if ((c|32) == 'x')
  625. return hexfloat(f, bits, emin, sign, pok);
  626. shunget(f);
  627. c = '0';
  628. }
  629. return decfloat(f, c, bits, emin, sign, pok);
  630. }
  631. static void *arg_n(va_list ap, unsigned int n)
  632. {
  633. void *p;
  634. unsigned int i;
  635. va_list ap2;
  636. va_copy(ap2, ap);
  637. for (i=n; i>1; i--) va_arg(ap2, void *);
  638. p = va_arg(ap2, void *);
  639. va_end(ap2);
  640. return p;
  641. }
  642. static void store_int(void *dest, int size, unsigned long long i)
  643. {
  644. if (!dest) return;
  645. switch (size) {
  646. case SIZE_hh:
  647. *(char *)dest = i;
  648. break;
  649. case SIZE_h:
  650. *(short *)dest = i;
  651. break;
  652. case SIZE_def:
  653. *(int *)dest = i;
  654. break;
  655. case SIZE_l:
  656. *(long *)dest = i;
  657. break;
  658. case SIZE_ll:
  659. *(long long *)dest = i;
  660. break;
  661. }
  662. }
  663. static int ff_vfscanf(FFFILE *f, const char *fmt, va_list ap)
  664. {
  665. int width;
  666. int size;
  667. int base;
  668. const unsigned char *p;
  669. int c, t;
  670. char *s;
  671. void *dest=NULL;
  672. int invert;
  673. int matches=0;
  674. unsigned long long x;
  675. double y;
  676. ptrdiff_t pos = 0;
  677. unsigned char scanset[257];
  678. size_t i;
  679. for (p=(const unsigned char *)fmt; *p; p++) {
  680. if (av_isspace(*p)) {
  681. while (av_isspace(p[1])) p++;
  682. shlim(f, 0);
  683. while (av_isspace(shgetc(f)));
  684. shunget(f);
  685. pos += shcnt(f);
  686. continue;
  687. }
  688. if (*p != '%' || p[1] == '%') {
  689. shlim(f, 0);
  690. if (*p == '%') {
  691. p++;
  692. while (av_isspace((c=shgetc(f))));
  693. } else {
  694. c = shgetc(f);
  695. }
  696. if (c!=*p) {
  697. shunget(f);
  698. if (c<0) goto input_fail;
  699. goto match_fail;
  700. }
  701. pos += shcnt(f);
  702. continue;
  703. }
  704. p++;
  705. if (*p=='*') {
  706. dest = 0; p++;
  707. } else if (av_isdigit(*p) && p[1]=='$') {
  708. dest = arg_n(ap, *p-'0'); p+=2;
  709. } else {
  710. dest = va_arg(ap, void *);
  711. }
  712. for (width=0; av_isdigit(*p); p++) {
  713. width = 10*width + *p - '0';
  714. }
  715. if (*p=='m') {
  716. s = 0;
  717. p++;
  718. }
  719. size = SIZE_def;
  720. switch (*p++) {
  721. case 'h':
  722. if (*p == 'h') p++, size = SIZE_hh;
  723. else size = SIZE_h;
  724. break;
  725. case 'l':
  726. if (*p == 'l') p++, size = SIZE_ll;
  727. else size = SIZE_l;
  728. break;
  729. case 'j':
  730. size = SIZE_ll;
  731. break;
  732. case 'z':
  733. case 't':
  734. size = SIZE_l;
  735. break;
  736. case 'L':
  737. size = SIZE_L;
  738. break;
  739. case 'd': case 'i': case 'o': case 'u': case 'x':
  740. case 'a': case 'e': case 'f': case 'g':
  741. case 'A': case 'E': case 'F': case 'G': case 'X':
  742. case 's': case 'c': case '[':
  743. case 'S': case 'C':
  744. case 'p': case 'n':
  745. p--;
  746. break;
  747. default:
  748. goto fmt_fail;
  749. }
  750. t = *p;
  751. /* C or S */
  752. if ((t&0x2f) == 3) {
  753. t |= 32;
  754. size = SIZE_l;
  755. }
  756. switch (t) {
  757. case 'c':
  758. if (width < 1) width = 1;
  759. case '[':
  760. break;
  761. case 'n':
  762. store_int(dest, size, pos);
  763. /* do not increment match count, etc! */
  764. continue;
  765. default:
  766. shlim(f, 0);
  767. while (av_isspace(shgetc(f)));
  768. shunget(f);
  769. pos += shcnt(f);
  770. }
  771. shlim(f, width);
  772. if (shgetc(f) < 0) goto input_fail;
  773. shunget(f);
  774. switch (t) {
  775. case 's':
  776. case 'c':
  777. case '[':
  778. if (t == 'c' || t == 's') {
  779. memset(scanset, -1, sizeof scanset);
  780. scanset[0] = 0;
  781. if (t == 's') {
  782. scanset[1 + '\t'] = 0;
  783. scanset[1 + '\n'] = 0;
  784. scanset[1 + '\v'] = 0;
  785. scanset[1 + '\f'] = 0;
  786. scanset[1 + '\r'] = 0;
  787. scanset[1 + ' ' ] = 0;
  788. }
  789. } else {
  790. if (*++p == '^') p++, invert = 1;
  791. else invert = 0;
  792. memset(scanset, invert, sizeof scanset);
  793. scanset[0] = 0;
  794. if (*p == '-') p++, scanset[1+'-'] = 1-invert;
  795. else if (*p == ']') p++, scanset[1+']'] = 1-invert;
  796. for (; *p != ']'; p++) {
  797. if (!*p) goto fmt_fail;
  798. if (*p=='-' && p[1] && p[1] != ']')
  799. for (c=p++[-1]; c<*p; c++)
  800. scanset[1+c] = 1-invert;
  801. scanset[1+*p] = 1-invert;
  802. }
  803. }
  804. s = 0;
  805. i = 0;
  806. if ((s = dest)) {
  807. while (scanset[(c=shgetc(f))+1])
  808. s[i++] = c;
  809. } else {
  810. while (scanset[(c=shgetc(f))+1]);
  811. }
  812. shunget(f);
  813. if (!shcnt(f)) goto match_fail;
  814. if (t == 'c' && shcnt(f) != width) goto match_fail;
  815. if (t != 'c') {
  816. if (s) s[i] = 0;
  817. }
  818. break;
  819. case 'p':
  820. case 'X':
  821. case 'x':
  822. base = 16;
  823. goto int_common;
  824. case 'o':
  825. base = 8;
  826. goto int_common;
  827. case 'd':
  828. case 'u':
  829. base = 10;
  830. goto int_common;
  831. case 'i':
  832. base = 0;
  833. int_common:
  834. x = ffintscan(f, base, 0, ULLONG_MAX);
  835. if (!shcnt(f))
  836. goto match_fail;
  837. if (t=='p' && dest)
  838. *(void **)dest = (void *)(uintptr_t)x;
  839. else
  840. store_int(dest, size, x);
  841. break;
  842. case 'a': case 'A':
  843. case 'e': case 'E':
  844. case 'f': case 'F':
  845. case 'g': case 'G':
  846. y = fffloatscan(f, size, 0);
  847. if (!shcnt(f))
  848. goto match_fail;
  849. if (dest) {
  850. switch (size) {
  851. case SIZE_def:
  852. *(float *)dest = y;
  853. break;
  854. case SIZE_l:
  855. *(double *)dest = y;
  856. break;
  857. case SIZE_L:
  858. *(double *)dest = y;
  859. break;
  860. }
  861. }
  862. break;
  863. }
  864. pos += shcnt(f);
  865. if (dest) matches++;
  866. }
  867. if (0) {
  868. fmt_fail:
  869. input_fail:
  870. if (!matches) matches--;
  871. }
  872. match_fail:
  873. return matches;
  874. }
  875. static int ff_vsscanf(const char *s, const char *fmt, va_list ap)
  876. {
  877. FFFILE f = {
  878. .buf = (void *)s, .cookie = (void *)s,
  879. .read = ffstring_read,
  880. };
  881. return ff_vfscanf(&f, fmt, ap);
  882. }
  883. int av_sscanf(const char *string, const char *format, ...)
  884. {
  885. int ret;
  886. va_list ap;
  887. va_start(ap, format);
  888. ret = ff_vsscanf(string, format, ap);
  889. va_end(ap);
  890. return ret;
  891. }