libc_sscanf.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright (c) 2019 Actions Semi Co., Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /* poriting sscanf from musl-1.2.3 */
  7. #ifndef __UVISION_VERSION
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdarg.h>
  11. #include <limits.h>
  12. #include <errno.h>
  13. #include <ctype.h>
  14. #include <sys/types.h>
  15. typedef struct _SBUF {
  16. unsigned char *rpos, *rend;
  17. unsigned char *buf;
  18. void *cookie;
  19. unsigned char *shend;
  20. off_t shlim, shcnt;
  21. } _BUF;
  22. #define F_PERM 1
  23. #define F_NORD 4
  24. #define F_NOWR 8
  25. #define F_EOF 16
  26. #define F_ERR 32
  27. #define F_SVB 64
  28. #define F_APP 128
  29. #define SIZE_hh -2
  30. #define SIZE_h -1
  31. #define SIZE_def 0
  32. #define SIZE_l 1
  33. #define SIZE_L 2
  34. #define SIZE_ll 3
  35. #define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
  36. #define shlim(f, lim) __shlim((f), (lim))
  37. #define shgetc(f) (((f)->rpos != (f)->shend) ? *(f)->rpos++ : __shgetc(f))
  38. #define shunget(f) ((f)->shlim>=0 ? (void)(f)->rpos-- : (void)0)
  39. /* Lookup table for digit values. -1==255>=36 -> invalid */
  40. static const unsigned char table[] = { -1,
  41. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  42. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  43. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  44. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
  45. -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
  46. 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
  47. -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
  48. 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
  49. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  50. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  51. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  52. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  53. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  54. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  55. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  56. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  57. };
  58. static int __toread(_BUF *f)
  59. {
  60. f->rpos = f->rend = f->buf;
  61. return 0;
  62. }
  63. static size_t __read(_BUF *f, unsigned char *buf, size_t len)
  64. {
  65. char *src = f->cookie;
  66. size_t k = len+256;
  67. char *end = memchr(src, 0, k);
  68. if (end) k = end-src;
  69. if (k < len) len = k;
  70. memcpy(buf, src, len);
  71. f->rpos = (void *)(src+len);
  72. f->rend = (void *)(src+k);
  73. f->cookie = src+k;
  74. return len;
  75. }
  76. static int __uflow(_BUF *f)
  77. {
  78. unsigned char c;
  79. if (!__toread(f) && __read(f, &c, 1)==1) return c;
  80. return EOF;
  81. }
  82. static void __shlim(_BUF *f, off_t lim)
  83. {
  84. f->shlim = lim;
  85. f->shcnt = f->buf - f->rpos;
  86. /* If lim is nonzero, rend must be a valid pointer. */
  87. if (lim && f->rend - f->rpos > lim)
  88. f->shend = f->rpos + lim;
  89. else
  90. f->shend = f->rend;
  91. }
  92. static int __shgetc(_BUF *f)
  93. {
  94. int c;
  95. off_t cnt = shcnt(f);
  96. if ((f->shlim && cnt >= f->shlim) || (c=__uflow(f)) < 0) {
  97. f->shcnt = f->buf - f->rpos + cnt;
  98. f->shend = f->rpos;
  99. f->shlim = -1;
  100. return EOF;
  101. }
  102. cnt++;
  103. if (f->shlim && f->rend - f->rpos > f->shlim - cnt)
  104. f->shend = f->rpos + (f->shlim - cnt);
  105. else
  106. f->shend = f->rend;
  107. f->shcnt = f->buf - f->rpos + cnt;
  108. if (f->rpos <= f->buf) f->rpos[-1] = c;
  109. return c;
  110. }
  111. static unsigned long long __intscan(_BUF *f, unsigned base, int pok, unsigned long long lim)
  112. {
  113. const unsigned char *val = table+1;
  114. int c, neg=0;
  115. unsigned x;
  116. unsigned long long y;
  117. if (base > 36 || base == 1) {
  118. errno = EINVAL;
  119. return 0;
  120. }
  121. while (isspace((c=shgetc(f))));
  122. if (c=='+' || c=='-') {
  123. neg = -(c=='-');
  124. c = shgetc(f);
  125. }
  126. if ((base == 0 || base == 16) && c=='0') {
  127. c = shgetc(f);
  128. if ((c|32)=='x') {
  129. c = shgetc(f);
  130. if (val[c]>=16) {
  131. shunget(f);
  132. if (pok) shunget(f);
  133. else shlim(f, 0);
  134. return 0;
  135. }
  136. base = 16;
  137. } else if (base == 0) {
  138. base = 8;
  139. }
  140. } else {
  141. if (base == 0) base = 10;
  142. if (val[c] >= base) {
  143. shunget(f);
  144. shlim(f, 0);
  145. errno = EINVAL;
  146. return 0;
  147. }
  148. }
  149. if (base == 10) {
  150. for (x=0; c-'0'<10U && x<=UINT_MAX/10-1; c=shgetc(f))
  151. x = x*10 + (c-'0');
  152. for (y=x; c-'0'<10U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(c-'0'); c=shgetc(f))
  153. y = y*10 + (c-'0');
  154. if (c-'0'>=10U) goto done;
  155. } else if (!(base & (base-1))) {
  156. int bs = "\0\1\2\4\7\3\6\5"[(0x17*base)>>5&7];
  157. for (x=0; val[c]<base && x<=UINT_MAX/32; c=shgetc(f))
  158. x = x<<bs | val[c];
  159. for (y=x; val[c]<base && y<=ULLONG_MAX>>bs; c=shgetc(f))
  160. y = y<<bs | val[c];
  161. } else {
  162. for (x=0; val[c]<base && x<=UINT_MAX/36-1; c=shgetc(f))
  163. x = x*base + val[c];
  164. for (y=x; val[c]<base && y<=ULLONG_MAX/base && base*y<=ULLONG_MAX-val[c]; c=shgetc(f))
  165. y = y*base + val[c];
  166. }
  167. if (val[c]<base) {
  168. for (; val[c]<base; c=shgetc(f));
  169. errno = ERANGE;
  170. y = lim;
  171. if (lim&1) neg = 0;
  172. }
  173. done:
  174. shunget(f);
  175. if (y>=lim) {
  176. if (!(lim&1) && !neg) {
  177. errno = ERANGE;
  178. return lim-1;
  179. } else if (y>lim) {
  180. errno = ERANGE;
  181. return lim;
  182. }
  183. }
  184. return (y^neg)-neg;
  185. }
  186. static void store_int(void *dest, int size, unsigned long long i)
  187. {
  188. if (!dest) return;
  189. switch (size) {
  190. case SIZE_hh:
  191. *(char *)dest = i;
  192. break;
  193. case SIZE_h:
  194. *(short *)dest = i;
  195. break;
  196. case SIZE_def:
  197. *(int *)dest = i;
  198. break;
  199. case SIZE_l:
  200. *(long *)dest = i;
  201. break;
  202. case SIZE_ll:
  203. *(long long *)dest = i;
  204. break;
  205. }
  206. }
  207. static void *arg_n(va_list ap, unsigned int n)
  208. {
  209. void *p;
  210. unsigned int i;
  211. va_list ap2;
  212. va_copy(ap2, ap);
  213. for (i=n; i>1; i--) va_arg(ap2, void *);
  214. p = va_arg(ap2, void *);
  215. va_end(ap2);
  216. return p;
  217. }
  218. static int _vfscanf(_BUF *f, const char *fmt, va_list ap)
  219. {
  220. int width;
  221. int size;
  222. int base;
  223. const unsigned char *p;
  224. int c, t;
  225. char *s;
  226. void *dest=NULL;
  227. int invert;
  228. int matches=0;
  229. unsigned long long x;
  230. off_t pos = 0;
  231. unsigned char scanset[257];
  232. size_t i;
  233. if (!f->rpos) __toread(f);
  234. if (!f->rpos) goto input_fail;
  235. for (p=(const unsigned char *)fmt; *p; p++) {
  236. if (isspace(*p)) {
  237. while (isspace(p[1])) p++;
  238. shlim(f, 0);
  239. while (isspace(shgetc(f)));
  240. shunget(f);
  241. pos += shcnt(f);
  242. continue;
  243. }
  244. if (*p != '%' || p[1] == '%') {
  245. shlim(f, 0);
  246. if (*p == '%') {
  247. p++;
  248. while (isspace((c=shgetc(f))));
  249. } else {
  250. c = shgetc(f);
  251. }
  252. if (c!=*p) {
  253. shunget(f);
  254. if (c<0) goto input_fail;
  255. goto match_fail;
  256. }
  257. pos += shcnt(f);
  258. continue;
  259. }
  260. p++;
  261. if (*p=='*') {
  262. dest = 0; p++;
  263. } else if (isdigit(*p) && p[1]=='$') {
  264. dest = arg_n(ap, *p-'0'); p+=2;
  265. } else {
  266. dest = va_arg(ap, void *);
  267. }
  268. for (width=0; isdigit(*p); p++) {
  269. width = 10*width + *p - '0';
  270. }
  271. size = SIZE_def;
  272. switch (*p++) {
  273. case 'h':
  274. if (*p == 'h') p++, size = SIZE_hh;
  275. else size = SIZE_h;
  276. break;
  277. case 'l':
  278. if (*p == 'l') p++, size = SIZE_ll;
  279. else size = SIZE_l;
  280. break;
  281. case 'j':
  282. size = SIZE_ll;
  283. break;
  284. case 'z':
  285. case 't':
  286. size = SIZE_l;
  287. break;
  288. case 'L':
  289. size = SIZE_L;
  290. break;
  291. case 'd': case 'i': case 'o': case 'u': case 'x':
  292. case 'a': case 'e': case 'f': case 'g':
  293. case 'A': case 'E': case 'F': case 'G': case 'X':
  294. case 's': case 'c': case '[':
  295. case 'S': case 'C':
  296. case 'p': case 'n':
  297. p--;
  298. break;
  299. default:
  300. goto fmt_fail;
  301. }
  302. t = *p;
  303. /* C or S */
  304. if ((t&0x2f) == 3) {
  305. t |= 32;
  306. size = SIZE_l;
  307. }
  308. switch (t) {
  309. case 'c':
  310. if (width < 1) width = 1;
  311. case '[':
  312. break;
  313. case 'n':
  314. store_int(dest, size, pos);
  315. /* do not increment match count, etc! */
  316. continue;
  317. default:
  318. shlim(f, 0);
  319. while (isspace(shgetc(f)));
  320. shunget(f);
  321. pos += shcnt(f);
  322. }
  323. shlim(f, width);
  324. if (shgetc(f) < 0) goto input_fail;
  325. shunget(f);
  326. switch (t) {
  327. case 's':
  328. case 'c':
  329. case '[':
  330. if (t == 'c' || t == 's') {
  331. memset(scanset, -1, sizeof scanset);
  332. scanset[0] = 0;
  333. if (t == 's') {
  334. scanset[1+'\t'] = 0;
  335. scanset[1+'\n'] = 0;
  336. scanset[1+'\v'] = 0;
  337. scanset[1+'\f'] = 0;
  338. scanset[1+'\r'] = 0;
  339. scanset[1+' '] = 0;
  340. }
  341. } else {
  342. if (*++p == '^') p++, invert = 1;
  343. else invert = 0;
  344. memset(scanset, invert, sizeof scanset);
  345. scanset[0] = 0;
  346. if (*p == '-') p++, scanset[1+'-'] = 1-invert;
  347. else if (*p == ']') p++, scanset[1+']'] = 1-invert;
  348. for (; *p != ']'; p++) {
  349. if (!*p) goto fmt_fail;
  350. if (*p=='-' && p[1] && p[1] != ']')
  351. for (c=p++[-1]; c<*p; c++)
  352. scanset[1+c] = 1-invert;
  353. scanset[1+*p] = 1-invert;
  354. }
  355. }
  356. s = 0;
  357. i = 0;
  358. if ((s = dest)) {
  359. while (scanset[(c=shgetc(f))+1])
  360. s[i++] = c;
  361. } else {
  362. while (scanset[(c=shgetc(f))+1]);
  363. }
  364. shunget(f);
  365. if (!shcnt(f)) goto match_fail;
  366. if (t == 'c' && shcnt(f) != width) goto match_fail;
  367. if (t != 'c') {
  368. if (s) s[i] = 0;
  369. }
  370. break;
  371. case 'p':
  372. case 'X':
  373. case 'x':
  374. base = 16;
  375. goto int_common;
  376. case 'o':
  377. base = 8;
  378. goto int_common;
  379. case 'd':
  380. case 'u':
  381. base = 10;
  382. goto int_common;
  383. case 'i':
  384. base = 0;
  385. int_common:
  386. x = __intscan(f, base, 0, ULLONG_MAX);
  387. if (!shcnt(f)) goto match_fail;
  388. if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x;
  389. else store_int(dest, size, x);
  390. break;
  391. }
  392. pos += shcnt(f);
  393. if (dest) matches++;
  394. }
  395. if (0) {
  396. fmt_fail:
  397. input_fail:
  398. if (!matches) matches--;
  399. }
  400. match_fail:
  401. return matches;
  402. }
  403. int vsscanf(const char *s, const char *fmt, va_list ap)
  404. {
  405. _BUF f = {
  406. .buf = (void *)s, .cookie = (void *)s,
  407. };
  408. return _vfscanf(&f, fmt, ap);
  409. }
  410. int sscanf(const char *s, const char *fmt, ...)
  411. {
  412. int ret;
  413. va_list ap;
  414. va_start(ap, fmt);
  415. ret = vsscanf(s, fmt, ap);
  416. va_end(ap);
  417. return ret;
  418. }
  419. #endif