lstrlib.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  1. /*
  2. ** $Id: lstrlib.c,v 1.254 2016/12/22 13:08:50 roberto Exp $
  3. ** Standard library for string operations and pattern-matching
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lstrlib_c
  7. #define LUA_LIB
  8. #include "lprefix.h"
  9. #include <ctype.h>
  10. #include <float.h>
  11. #include <limits.h>
  12. #include <locale.h>
  13. #include <stddef.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "lua.h"
  18. #include "lauxlib.h"
  19. #include "lualib.h"
  20. /*
  21. ** maximum number of captures that a pattern can do during
  22. ** pattern-matching. This limit is arbitrary, but must fit in
  23. ** an unsigned char.
  24. */
  25. #if !defined(LUA_MAXCAPTURES)
  26. #define LUA_MAXCAPTURES 32
  27. #endif
  28. /* macro to 'unsign' a character */
  29. #define uchar(c) ((unsigned char)(c))
  30. /*
  31. ** Some sizes are better limited to fit in 'int', but must also fit in
  32. ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
  33. */
  34. #define MAX_SIZET ((size_t)(~(size_t)0))
  35. #define MAXSIZE \
  36. (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
  37. static int str_len(lua_State *L)
  38. {
  39. size_t l;
  40. luaL_checklstring(L, 1, &l);
  41. lua_pushinteger(L, (lua_Integer)l);
  42. return 1;
  43. }
  44. /* translate a relative string position: negative means back from end */
  45. static lua_Integer posrelat(lua_Integer pos, size_t len)
  46. {
  47. if (pos >= 0) return pos;
  48. else if (0u - (size_t)pos > len) return 0;
  49. else return (lua_Integer)len + pos + 1;
  50. }
  51. static int str_sub(lua_State *L)
  52. {
  53. size_t l;
  54. const char *s = luaL_checklstring(L, 1, &l);
  55. lua_Integer start = posrelat(luaL_checkinteger(L, 2), l);
  56. lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l);
  57. if (start < 1) start = 1;
  58. if (end > (lua_Integer)l) end = l;
  59. if (start <= end)
  60. lua_pushlstring(L, s + start - 1, (size_t)(end - start) + 1);
  61. else lua_pushliteral(L, "");
  62. return 1;
  63. }
  64. static int str_reverse(lua_State *L)
  65. {
  66. size_t l, i;
  67. luaL_Buffer b;
  68. const char *s = luaL_checklstring(L, 1, &l);
  69. char *p = luaL_buffinitsize(L, &b, l);
  70. for (i = 0; i < l; i++)
  71. p[i] = s[l - i - 1];
  72. luaL_pushresultsize(&b, l);
  73. return 1;
  74. }
  75. static int str_lower(lua_State *L)
  76. {
  77. size_t l;
  78. size_t i;
  79. luaL_Buffer b;
  80. const char *s = luaL_checklstring(L, 1, &l);
  81. char *p = luaL_buffinitsize(L, &b, l);
  82. for (i = 0; i < l; i++)
  83. p[i] = tolower(uchar(s[i]));
  84. luaL_pushresultsize(&b, l);
  85. return 1;
  86. }
  87. static int str_upper(lua_State *L)
  88. {
  89. size_t l;
  90. size_t i;
  91. luaL_Buffer b;
  92. const char *s = luaL_checklstring(L, 1, &l);
  93. char *p = luaL_buffinitsize(L, &b, l);
  94. for (i = 0; i < l; i++)
  95. p[i] = toupper(uchar(s[i]));
  96. luaL_pushresultsize(&b, l);
  97. return 1;
  98. }
  99. static int str_rep(lua_State *L)
  100. {
  101. size_t l, lsep;
  102. const char *s = luaL_checklstring(L, 1, &l);
  103. lua_Integer n = luaL_checkinteger(L, 2);
  104. const char *sep = luaL_optlstring(L, 3, "", &lsep);
  105. if (n <= 0) lua_pushliteral(L, "");
  106. else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */
  107. return luaL_error(L, "resulting string too large");
  108. else
  109. {
  110. size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
  111. luaL_Buffer b;
  112. char *p = luaL_buffinitsize(L, &b, totallen);
  113. while (n-- > 1) /* first n-1 copies (followed by separator) */
  114. {
  115. memcpy(p, s, l * sizeof(char));
  116. p += l;
  117. if (lsep > 0) /* empty 'memcpy' is not that cheap */
  118. {
  119. memcpy(p, sep, lsep * sizeof(char));
  120. p += lsep;
  121. }
  122. }
  123. memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
  124. luaL_pushresultsize(&b, totallen);
  125. }
  126. return 1;
  127. }
  128. static int str_byte(lua_State *L)
  129. {
  130. size_t l;
  131. const char *s = luaL_checklstring(L, 1, &l);
  132. lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l);
  133. lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l);
  134. int n, i;
  135. if (posi < 1) posi = 1;
  136. if (pose > (lua_Integer)l) pose = l;
  137. if (posi > pose) return 0; /* empty interval; return no values */
  138. if (pose - posi >= INT_MAX) /* arithmetic overflow? */
  139. return luaL_error(L, "string slice too long");
  140. n = (int)(pose - posi) + 1;
  141. luaL_checkstack(L, n, "string slice too long");
  142. for (i = 0; i < n; i++)
  143. lua_pushinteger(L, uchar(s[posi + i - 1]));
  144. return n;
  145. }
  146. static int str_char(lua_State *L)
  147. {
  148. int n = lua_gettop(L); /* number of arguments */
  149. int i;
  150. luaL_Buffer b;
  151. char *p = luaL_buffinitsize(L, &b, n);
  152. for (i = 1; i <= n; i++)
  153. {
  154. lua_Integer c = luaL_checkinteger(L, i);
  155. luaL_argcheck(L, uchar(c) == c, i, "value out of range");
  156. p[i - 1] = uchar(c);
  157. }
  158. luaL_pushresultsize(&b, n);
  159. return 1;
  160. }
  161. static int writer(lua_State *L, const void *b, size_t size, void *B)
  162. {
  163. (void)L;
  164. luaL_addlstring((luaL_Buffer *) B, (const char *)b, size);
  165. return 0;
  166. }
  167. static int str_dump(lua_State *L)
  168. {
  169. luaL_Buffer b;
  170. int strip = lua_toboolean(L, 2);
  171. luaL_checktype(L, 1, LUA_TFUNCTION);
  172. lua_settop(L, 1);
  173. luaL_buffinit(L, &b);
  174. if (lua_dump(L, writer, &b, strip) != 0)
  175. return luaL_error(L, "unable to dump given function");
  176. luaL_pushresult(&b);
  177. return 1;
  178. }
  179. /*
  180. ** {======================================================
  181. ** PATTERN MATCHING
  182. ** =======================================================
  183. */
  184. #define CAP_UNFINISHED (-1)
  185. #define CAP_POSITION (-2)
  186. typedef struct MatchState
  187. {
  188. const char *src_init; /* init of source string */
  189. const char *src_end; /* end ('\0') of source string */
  190. const char *p_end; /* end ('\0') of pattern */
  191. lua_State *L;
  192. int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
  193. unsigned char level; /* total number of captures (finished or unfinished) */
  194. struct
  195. {
  196. const char *init;
  197. ptrdiff_t len;
  198. } capture[LUA_MAXCAPTURES];
  199. } MatchState;
  200. /* recursive function */
  201. static const char *match(MatchState *ms, const char *s, const char *p);
  202. /* maximum recursion depth for 'match' */
  203. #if !defined(MAXCCALLS)
  204. #define MAXCCALLS 200
  205. #endif
  206. #define L_ESC '%'
  207. #define SPECIALS "^$*+?.([%-"
  208. static int check_capture(MatchState *ms, int l)
  209. {
  210. l -= '1';
  211. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
  212. return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
  213. return l;
  214. }
  215. static int capture_to_close(MatchState *ms)
  216. {
  217. int level = ms->level;
  218. for (level--; level >= 0; level--)
  219. if (ms->capture[level].len == CAP_UNFINISHED) return level;
  220. return luaL_error(ms->L, "invalid pattern capture");
  221. }
  222. static const char *classend(MatchState *ms, const char *p)
  223. {
  224. switch (*p++)
  225. {
  226. case L_ESC:
  227. {
  228. if (p == ms->p_end)
  229. luaL_error(ms->L, "malformed pattern (ends with '%%')");
  230. return p + 1;
  231. }
  232. case '[':
  233. {
  234. if (*p == '^') p++;
  235. do /* look for a ']' */
  236. {
  237. if (p == ms->p_end)
  238. luaL_error(ms->L, "malformed pattern (missing ']')");
  239. if (*(p++) == L_ESC && p < ms->p_end)
  240. p++; /* skip escapes (e.g. '%]') */
  241. }
  242. while (*p != ']');
  243. return p + 1;
  244. }
  245. default:
  246. {
  247. return p;
  248. }
  249. }
  250. }
  251. static int match_class(int c, int cl)
  252. {
  253. int res;
  254. switch (tolower(cl))
  255. {
  256. case 'a' :
  257. res = isalpha(c);
  258. break;
  259. case 'c' :
  260. res = iscntrl(c);
  261. break;
  262. case 'd' :
  263. res = isdigit(c);
  264. break;
  265. case 'g' :
  266. res = isgraph(c);
  267. break;
  268. case 'l' :
  269. res = islower(c);
  270. break;
  271. case 'p' :
  272. res = ispunct(c);
  273. break;
  274. case 's' :
  275. res = isspace(c);
  276. break;
  277. case 'u' :
  278. res = isupper(c);
  279. break;
  280. case 'w' :
  281. res = isalnum(c);
  282. break;
  283. case 'x' :
  284. res = isxdigit(c);
  285. break;
  286. case 'z' :
  287. res = (c == 0);
  288. break; /* deprecated option */
  289. default:
  290. return (cl == c);
  291. }
  292. return (islower(cl) ? res : !res);
  293. }
  294. static int matchbracketclass(int c, const char *p, const char *ec)
  295. {
  296. int sig = 1;
  297. if (*(p + 1) == '^')
  298. {
  299. sig = 0;
  300. p++; /* skip the '^' */
  301. }
  302. while (++p < ec)
  303. {
  304. if (*p == L_ESC)
  305. {
  306. p++;
  307. if (match_class(c, uchar(*p)))
  308. return sig;
  309. }
  310. else if ((*(p + 1) == '-') && (p + 2 < ec))
  311. {
  312. p += 2;
  313. if (uchar(*(p - 2)) <= c && c <= uchar(*p))
  314. return sig;
  315. }
  316. else if (uchar(*p) == c) return sig;
  317. }
  318. return !sig;
  319. }
  320. static int singlematch(MatchState *ms, const char *s, const char *p,
  321. const char *ep)
  322. {
  323. if (s >= ms->src_end)
  324. return 0;
  325. else
  326. {
  327. int c = uchar(*s);
  328. switch (*p)
  329. {
  330. case '.':
  331. return 1; /* matches any char */
  332. case L_ESC:
  333. return match_class(c, uchar(*(p + 1)));
  334. case '[':
  335. return matchbracketclass(c, p, ep - 1);
  336. default:
  337. return (uchar(*p) == c);
  338. }
  339. }
  340. }
  341. static const char *matchbalance(MatchState *ms, const char *s,
  342. const char *p)
  343. {
  344. if (p >= ms->p_end - 1)
  345. luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')");
  346. if (*s != *p) return NULL;
  347. else
  348. {
  349. int b = *p;
  350. int e = *(p + 1);
  351. int cont = 1;
  352. while (++s < ms->src_end)
  353. {
  354. if (*s == e)
  355. {
  356. if (--cont == 0) return s + 1;
  357. }
  358. else if (*s == b) cont++;
  359. }
  360. }
  361. return NULL; /* string ends out of balance */
  362. }
  363. static const char *max_expand(MatchState *ms, const char *s,
  364. const char *p, const char *ep)
  365. {
  366. ptrdiff_t i = 0; /* counts maximum expand for item */
  367. while (singlematch(ms, s + i, p, ep))
  368. i++;
  369. /* keeps trying to match with the maximum repetitions */
  370. while (i >= 0)
  371. {
  372. const char *res = match(ms, (s + i), ep + 1);
  373. if (res) return res;
  374. i--; /* else didn't match; reduce 1 repetition to try again */
  375. }
  376. return NULL;
  377. }
  378. static const char *min_expand(MatchState *ms, const char *s,
  379. const char *p, const char *ep)
  380. {
  381. for (;;)
  382. {
  383. const char *res = match(ms, s, ep + 1);
  384. if (res != NULL)
  385. return res;
  386. else if (singlematch(ms, s, p, ep))
  387. s++; /* try with one more repetition */
  388. else return NULL;
  389. }
  390. }
  391. static const char *start_capture(MatchState *ms, const char *s,
  392. const char *p, int what)
  393. {
  394. const char *res;
  395. int level = ms->level;
  396. if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
  397. ms->capture[level].init = s;
  398. ms->capture[level].len = what;
  399. ms->level = level + 1;
  400. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  401. ms->level--; /* undo capture */
  402. return res;
  403. }
  404. static const char *end_capture(MatchState *ms, const char *s,
  405. const char *p)
  406. {
  407. int l = capture_to_close(ms);
  408. const char *res;
  409. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  410. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  411. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  412. return res;
  413. }
  414. static const char *match_capture(MatchState *ms, const char *s, int l)
  415. {
  416. size_t len;
  417. l = check_capture(ms, l);
  418. len = ms->capture[l].len;
  419. if ((size_t)(ms->src_end - s) >= len &&
  420. memcmp(ms->capture[l].init, s, len) == 0)
  421. return s + len;
  422. else return NULL;
  423. }
  424. static const char *match(MatchState *ms, const char *s, const char *p)
  425. {
  426. if (ms->matchdepth-- == 0)
  427. luaL_error(ms->L, "pattern too complex");
  428. init: /* using goto's to optimize tail recursion */
  429. if (p != ms->p_end) /* end of pattern? */
  430. {
  431. switch (*p)
  432. {
  433. case '(': /* start capture */
  434. {
  435. if (*(p + 1) == ')') /* position capture? */
  436. s = start_capture(ms, s, p + 2, CAP_POSITION);
  437. else
  438. s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
  439. break;
  440. }
  441. case ')': /* end capture */
  442. {
  443. s = end_capture(ms, s, p + 1);
  444. break;
  445. }
  446. case '$':
  447. {
  448. if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */
  449. goto dflt; /* no; go to default */
  450. s = (s == ms->src_end) ? s : NULL; /* check end of string */
  451. break;
  452. }
  453. case L_ESC: /* escaped sequences not in the format class[*+?-]? */
  454. {
  455. switch (*(p + 1))
  456. {
  457. case 'b': /* balanced string? */
  458. {
  459. s = matchbalance(ms, s, p + 2);
  460. if (s != NULL)
  461. {
  462. p += 4;
  463. goto init; /* return match(ms, s, p + 4); */
  464. } /* else fail (s == NULL) */
  465. break;
  466. }
  467. case 'f': /* frontier? */
  468. {
  469. const char *ep;
  470. char previous;
  471. p += 2;
  472. if (*p != '[')
  473. luaL_error(ms->L, "missing '[' after '%%f' in pattern");
  474. ep = classend(ms, p); /* points to what is next */
  475. previous = (s == ms->src_init) ? '\0' : *(s - 1);
  476. if (!matchbracketclass(uchar(previous), p, ep - 1) &&
  477. matchbracketclass(uchar(*s), p, ep - 1))
  478. {
  479. p = ep;
  480. goto init; /* return match(ms, s, ep); */
  481. }
  482. s = NULL; /* match failed */
  483. break;
  484. }
  485. case '0':
  486. case '1':
  487. case '2':
  488. case '3':
  489. case '4':
  490. case '5':
  491. case '6':
  492. case '7':
  493. case '8':
  494. case '9': /* capture results (%0-%9)? */
  495. {
  496. s = match_capture(ms, s, uchar(*(p + 1)));
  497. if (s != NULL)
  498. {
  499. p += 2;
  500. goto init; /* return match(ms, s, p + 2) */
  501. }
  502. break;
  503. }
  504. default:
  505. goto dflt;
  506. }
  507. break;
  508. }
  509. default:
  510. dflt: /* pattern class plus optional suffix */
  511. {
  512. const char *ep = classend(ms, p); /* points to optional suffix */
  513. /* does not match at least once? */
  514. if (!singlematch(ms, s, p, ep))
  515. {
  516. if (*ep == '*' || *ep == '?' || *ep == '-') /* accept empty? */
  517. {
  518. p = ep + 1;
  519. goto init; /* return match(ms, s, ep + 1); */
  520. }
  521. else /* '+' or no suffix */
  522. s = NULL; /* fail */
  523. }
  524. else /* matched once */
  525. {
  526. switch (*ep) /* handle optional suffix */
  527. {
  528. case '?': /* optional */
  529. {
  530. const char *res;
  531. if ((res = match(ms, s + 1, ep + 1)) != NULL)
  532. s = res;
  533. else
  534. {
  535. p = ep + 1;
  536. goto init; /* else return match(ms, s, ep + 1); */
  537. }
  538. break;
  539. }
  540. case '+': /* 1 or more repetitions */
  541. s++; /* 1 match already done */
  542. /* FALLTHROUGH */
  543. case '*': /* 0 or more repetitions */
  544. s = max_expand(ms, s, p, ep);
  545. break;
  546. case '-': /* 0 or more repetitions (minimum) */
  547. s = min_expand(ms, s, p, ep);
  548. break;
  549. default: /* no suffix */
  550. s++;
  551. p = ep;
  552. goto init; /* return match(ms, s + 1, ep); */
  553. }
  554. }
  555. break;
  556. }
  557. }
  558. }
  559. ms->matchdepth++;
  560. return s;
  561. }
  562. static const char *lmemfind(const char *s1, size_t l1,
  563. const char *s2, size_t l2)
  564. {
  565. if (l2 == 0) return s1; /* empty strings are everywhere */
  566. else if (l2 > l1) return NULL; /* avoids a negative 'l1' */
  567. else
  568. {
  569. const char *init; /* to search for a '*s2' inside 's1' */
  570. l2--; /* 1st char will be checked by 'memchr' */
  571. l1 = l1 - l2; /* 's2' cannot be found after that */
  572. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL)
  573. {
  574. init++; /* 1st char is already checked */
  575. if (memcmp(init, s2 + 1, l2) == 0)
  576. return init - 1;
  577. else /* correct 'l1' and 's1' to try again */
  578. {
  579. l1 -= init - s1;
  580. s1 = init;
  581. }
  582. }
  583. return NULL; /* not found */
  584. }
  585. }
  586. static void push_onecapture(MatchState *ms, int i, const char *s,
  587. const char *e)
  588. {
  589. if (i >= ms->level)
  590. {
  591. if (i == 0) /* ms->level == 0, too */
  592. lua_pushlstring(ms->L, s, e - s); /* add whole match */
  593. else
  594. luaL_error(ms->L, "invalid capture index %%%d", i + 1);
  595. }
  596. else
  597. {
  598. ptrdiff_t l = ms->capture[i].len;
  599. if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
  600. if (l == CAP_POSITION)
  601. lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1);
  602. else
  603. lua_pushlstring(ms->L, ms->capture[i].init, l);
  604. }
  605. }
  606. static int push_captures(MatchState *ms, const char *s, const char *e)
  607. {
  608. int i;
  609. int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
  610. luaL_checkstack(ms->L, nlevels, "too many captures");
  611. for (i = 0; i < nlevels; i++)
  612. push_onecapture(ms, i, s, e);
  613. return nlevels; /* number of strings pushed */
  614. }
  615. /* check whether pattern has no special characters */
  616. static int nospecials(const char *p, size_t l)
  617. {
  618. size_t upto = 0;
  619. do
  620. {
  621. if (strpbrk(p + upto, SPECIALS))
  622. return 0; /* pattern has a special character */
  623. upto += strlen(p + upto) + 1; /* may have more after \0 */
  624. }
  625. while (upto <= l);
  626. return 1; /* no special chars found */
  627. }
  628. static void prepstate(MatchState *ms, lua_State *L,
  629. const char *s, size_t ls, const char *p, size_t lp)
  630. {
  631. ms->L = L;
  632. ms->matchdepth = MAXCCALLS;
  633. ms->src_init = s;
  634. ms->src_end = s + ls;
  635. ms->p_end = p + lp;
  636. }
  637. static void reprepstate(MatchState *ms)
  638. {
  639. ms->level = 0;
  640. lua_assert(ms->matchdepth == MAXCCALLS);
  641. }
  642. static int str_find_aux(lua_State *L, int find)
  643. {
  644. size_t ls, lp;
  645. const char *s = luaL_checklstring(L, 1, &ls);
  646. const char *p = luaL_checklstring(L, 2, &lp);
  647. lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls);
  648. if (init < 1) init = 1;
  649. else if (init > (lua_Integer)ls + 1) /* start after string's end? */
  650. {
  651. lua_pushnil(L); /* cannot find anything */
  652. return 1;
  653. }
  654. /* explicit request or no special characters? */
  655. if (find && (lua_toboolean(L, 4) || nospecials(p, lp)))
  656. {
  657. /* do a plain search */
  658. const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
  659. if (s2)
  660. {
  661. lua_pushinteger(L, (s2 - s) + 1);
  662. lua_pushinteger(L, (s2 - s) + lp);
  663. return 2;
  664. }
  665. }
  666. else
  667. {
  668. MatchState ms;
  669. const char *s1 = s + init - 1;
  670. int anchor = (*p == '^');
  671. if (anchor)
  672. {
  673. p++;
  674. lp--; /* skip anchor character */
  675. }
  676. prepstate(&ms, L, s, ls, p, lp);
  677. do
  678. {
  679. const char *res;
  680. reprepstate(&ms);
  681. if ((res = match(&ms, s1, p)) != NULL)
  682. {
  683. if (find)
  684. {
  685. lua_pushinteger(L, (s1 - s) + 1); /* start */
  686. lua_pushinteger(L, res - s); /* end */
  687. return push_captures(&ms, NULL, 0) + 2;
  688. }
  689. else
  690. return push_captures(&ms, s1, res);
  691. }
  692. }
  693. while (s1++ < ms.src_end && !anchor);
  694. }
  695. lua_pushnil(L); /* not found */
  696. return 1;
  697. }
  698. static int str_find(lua_State *L)
  699. {
  700. return str_find_aux(L, 1);
  701. }
  702. static int str_match(lua_State *L)
  703. {
  704. return str_find_aux(L, 0);
  705. }
  706. /* state for 'gmatch' */
  707. typedef struct GMatchState
  708. {
  709. const char *src; /* current position */
  710. const char *p; /* pattern */
  711. const char *lastmatch; /* end of last match */
  712. MatchState ms; /* match state */
  713. } GMatchState;
  714. static int gmatch_aux(lua_State *L)
  715. {
  716. GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3));
  717. const char *src;
  718. gm->ms.L = L;
  719. for (src = gm->src; src <= gm->ms.src_end; src++)
  720. {
  721. const char *e;
  722. reprepstate(&gm->ms);
  723. if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch)
  724. {
  725. gm->src = gm->lastmatch = e;
  726. return push_captures(&gm->ms, src, e);
  727. }
  728. }
  729. return 0; /* not found */
  730. }
  731. static int gmatch(lua_State *L)
  732. {
  733. size_t ls, lp;
  734. const char *s = luaL_checklstring(L, 1, &ls);
  735. const char *p = luaL_checklstring(L, 2, &lp);
  736. GMatchState *gm;
  737. lua_settop(L, 2); /* keep them on closure to avoid being collected */
  738. gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState));
  739. prepstate(&gm->ms, L, s, ls, p, lp);
  740. gm->src = s;
  741. gm->p = p;
  742. gm->lastmatch = NULL;
  743. lua_pushcclosure(L, gmatch_aux, 3);
  744. return 1;
  745. }
  746. static void add_s(MatchState *ms, luaL_Buffer *b, const char *s,
  747. const char *e)
  748. {
  749. size_t l, i;
  750. lua_State *L = ms->L;
  751. const char *news = lua_tolstring(L, 3, &l);
  752. for (i = 0; i < l; i++)
  753. {
  754. if (news[i] != L_ESC)
  755. luaL_addchar(b, news[i]);
  756. else
  757. {
  758. i++; /* skip ESC */
  759. if (!isdigit(uchar(news[i])))
  760. {
  761. if (news[i] != L_ESC)
  762. luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
  763. luaL_addchar(b, news[i]);
  764. }
  765. else if (news[i] == '0')
  766. luaL_addlstring(b, s, e - s);
  767. else
  768. {
  769. push_onecapture(ms, news[i] - '1', s, e);
  770. luaL_tolstring(L, -1, NULL); /* if number, convert it to string */
  771. lua_remove(L, -2); /* remove original value */
  772. luaL_addvalue(b); /* add capture to accumulated result */
  773. }
  774. }
  775. }
  776. }
  777. static void add_value(MatchState *ms, luaL_Buffer *b, const char *s,
  778. const char *e, int tr)
  779. {
  780. lua_State *L = ms->L;
  781. switch (tr)
  782. {
  783. case LUA_TFUNCTION:
  784. {
  785. int n;
  786. lua_pushvalue(L, 3);
  787. n = push_captures(ms, s, e);
  788. lua_call(L, n, 1);
  789. break;
  790. }
  791. case LUA_TTABLE:
  792. {
  793. push_onecapture(ms, 0, s, e);
  794. lua_gettable(L, 3);
  795. break;
  796. }
  797. default: /* LUA_TNUMBER or LUA_TSTRING */
  798. {
  799. add_s(ms, b, s, e);
  800. return;
  801. }
  802. }
  803. if (!lua_toboolean(L, -1)) /* nil or false? */
  804. {
  805. lua_pop(L, 1);
  806. lua_pushlstring(L, s, e - s); /* keep original text */
  807. }
  808. else if (!lua_isstring(L, -1))
  809. luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
  810. luaL_addvalue(b); /* add result to accumulator */
  811. }
  812. static int str_gsub(lua_State *L)
  813. {
  814. size_t srcl, lp;
  815. const char *src = luaL_checklstring(L, 1, &srcl); /* subject */
  816. const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
  817. const char *lastmatch = NULL; /* end of last match */
  818. int tr = lua_type(L, 3); /* replacement type */
  819. lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */
  820. int anchor = (*p == '^');
  821. lua_Integer n = 0; /* replacement count */
  822. MatchState ms;
  823. luaL_Buffer b;
  824. luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
  825. tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
  826. "string/function/table expected");
  827. luaL_buffinit(L, &b);
  828. if (anchor)
  829. {
  830. p++;
  831. lp--; /* skip anchor character */
  832. }
  833. prepstate(&ms, L, src, srcl, p, lp);
  834. while (n < max_s)
  835. {
  836. const char *e;
  837. reprepstate(&ms); /* (re)prepare state for new match */
  838. if ((e = match(&ms, src, p)) != NULL && e != lastmatch) /* match? */
  839. {
  840. n++;
  841. add_value(&ms, &b, src, e, tr); /* add replacement to buffer */
  842. src = lastmatch = e;
  843. }
  844. else if (src < ms.src_end) /* otherwise, skip one character */
  845. luaL_addchar(&b, *src++);
  846. else break; /* end of subject */
  847. if (anchor) break;
  848. }
  849. luaL_addlstring(&b, src, ms.src_end - src);
  850. luaL_pushresult(&b);
  851. lua_pushinteger(L, n); /* number of substitutions */
  852. return 2;
  853. }
  854. /* }====================================================== */
  855. /*
  856. ** {======================================================
  857. ** STRING FORMAT
  858. ** =======================================================
  859. */
  860. #if !defined(lua_number2strx) /* { */
  861. /*
  862. ** Hexadecimal floating-point formatter
  863. */
  864. #include <math.h>
  865. #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
  866. /*
  867. ** Number of bits that goes into the first digit. It can be any value
  868. ** between 1 and 4; the following definition tries to align the number
  869. ** to nibble boundaries by making what is left after that first digit a
  870. ** multiple of 4.
  871. */
  872. #define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1)
  873. /*
  874. ** Add integer part of 'x' to buffer and return new 'x'
  875. */
  876. static lua_Number adddigit(char *buff, int n, lua_Number x)
  877. {
  878. lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */
  879. int d = (int)dd;
  880. buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
  881. return x - dd; /* return what is left */
  882. }
  883. static int num2straux(char *buff, int sz, lua_Number x)
  884. {
  885. /* if 'inf' or 'NaN', format it like '%g' */
  886. if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL)
  887. return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x);
  888. else if (x == 0) /* can be -0... */
  889. {
  890. /* create "0" or "-0" followed by exponent */
  891. return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x);
  892. }
  893. else
  894. {
  895. int e;
  896. lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */
  897. int n = 0; /* character count */
  898. if (m < 0) /* is number negative? */
  899. {
  900. buff[n++] = '-'; /* add signal */
  901. m = -m; /* make it positive */
  902. }
  903. buff[n++] = '0';
  904. buff[n++] = 'x'; /* add "0x" */
  905. m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */
  906. e -= L_NBFD; /* this digit goes before the radix point */
  907. if (m > 0) /* more digits? */
  908. {
  909. buff[n++] = lua_getlocaledecpoint(); /* add radix point */
  910. do /* add as many digits as needed */
  911. {
  912. m = adddigit(buff, n++, m * 16);
  913. }
  914. while (m > 0);
  915. }
  916. n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */
  917. lua_assert(n < sz);
  918. return n;
  919. }
  920. }
  921. static int lua_number2strx(lua_State *L, char *buff, int sz,
  922. const char *fmt, lua_Number x)
  923. {
  924. int n = num2straux(buff, sz, x);
  925. if (fmt[SIZELENMOD] == 'A')
  926. {
  927. int i;
  928. for (i = 0; i < n; i++)
  929. buff[i] = toupper(uchar(buff[i]));
  930. }
  931. else if (fmt[SIZELENMOD] != 'a')
  932. luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
  933. return n;
  934. }
  935. #endif /* } */
  936. /*
  937. ** Maximum size of each formatted item. This maximum size is produced
  938. ** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.',
  939. ** and '\0') + number of decimal digits to represent maxfloat (which
  940. ** is maximum exponent + 1). (99+3+1 then rounded to 120 for "extra
  941. ** expenses", such as locale-dependent stuff)
  942. */
  943. #define MAX_ITEM (120 + l_mathlim(MAX_10_EXP))
  944. /* valid flags in a format specification */
  945. #define FLAGS "-+ #0"
  946. /*
  947. ** maximum size of each format specification (such as "%-099.99d")
  948. */
  949. #define MAX_FORMAT 32
  950. static void addquoted(luaL_Buffer *b, const char *s, size_t len)
  951. {
  952. luaL_addchar(b, '"');
  953. while (len--)
  954. {
  955. if (*s == '"' || *s == '\\' || *s == '\n')
  956. {
  957. luaL_addchar(b, '\\');
  958. luaL_addchar(b, *s);
  959. }
  960. else if (iscntrl(uchar(*s)))
  961. {
  962. char buff[10];
  963. if (!isdigit(uchar(*(s + 1))))
  964. l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s));
  965. else
  966. l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s));
  967. luaL_addstring(b, buff);
  968. }
  969. else
  970. luaL_addchar(b, *s);
  971. s++;
  972. }
  973. luaL_addchar(b, '"');
  974. }
  975. /*
  976. ** Ensures the 'buff' string uses a dot as the radix character.
  977. */
  978. static void checkdp(char *buff, int nb)
  979. {
  980. if (memchr(buff, '.', nb) == NULL) /* no dot? */
  981. {
  982. char point = lua_getlocaledecpoint(); /* try locale point */
  983. char *ppoint = (char *)memchr(buff, point, nb);
  984. if (ppoint) *ppoint = '.'; /* change it to a dot */
  985. }
  986. }
  987. static void addliteral(lua_State *L, luaL_Buffer *b, int arg)
  988. {
  989. switch (lua_type(L, arg))
  990. {
  991. case LUA_TSTRING:
  992. {
  993. size_t len;
  994. const char *s = lua_tolstring(L, arg, &len);
  995. addquoted(b, s, len);
  996. break;
  997. }
  998. case LUA_TNUMBER:
  999. {
  1000. char *buff = luaL_prepbuffsize(b, MAX_ITEM);
  1001. int nb;
  1002. if (!lua_isinteger(L, arg)) /* float? */
  1003. {
  1004. lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */
  1005. nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n);
  1006. checkdp(buff, nb); /* ensure it uses a dot */
  1007. }
  1008. else /* integers */
  1009. {
  1010. lua_Integer n = lua_tointeger(L, arg);
  1011. const char *format = (n == LUA_MININTEGER) /* corner case? */
  1012. ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */
  1013. : LUA_INTEGER_FMT; /* else use default format */
  1014. nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n);
  1015. }
  1016. luaL_addsize(b, nb);
  1017. break;
  1018. }
  1019. case LUA_TNIL:
  1020. case LUA_TBOOLEAN:
  1021. {
  1022. luaL_tolstring(L, arg, NULL);
  1023. luaL_addvalue(b);
  1024. break;
  1025. }
  1026. default:
  1027. {
  1028. luaL_argerror(L, arg, "value has no literal form");
  1029. }
  1030. }
  1031. }
  1032. static const char *scanformat(lua_State *L, const char *strfrmt, char *form)
  1033. {
  1034. const char *p = strfrmt;
  1035. while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
  1036. if ((size_t)(p - strfrmt) >= sizeof(FLAGS) / sizeof(char))
  1037. luaL_error(L, "invalid format (repeated flags)");
  1038. if (isdigit(uchar(*p))) p++; /* skip width */
  1039. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  1040. if (*p == '.')
  1041. {
  1042. p++;
  1043. if (isdigit(uchar(*p))) p++; /* skip precision */
  1044. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  1045. }
  1046. if (isdigit(uchar(*p)))
  1047. luaL_error(L, "invalid format (width or precision too long)");
  1048. *(form++) = '%';
  1049. memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char));
  1050. form += (p - strfrmt) + 1;
  1051. *form = '\0';
  1052. return p;
  1053. }
  1054. /*
  1055. ** add length modifier into formats
  1056. */
  1057. static void addlenmod(char *form, const char *lenmod)
  1058. {
  1059. size_t l = strlen(form);
  1060. size_t lm = strlen(lenmod);
  1061. char spec = form[l - 1];
  1062. strcpy(form + l - 1, lenmod);
  1063. form[l + lm - 1] = spec;
  1064. form[l + lm] = '\0';
  1065. }
  1066. static int str_format(lua_State *L)
  1067. {
  1068. int top = lua_gettop(L);
  1069. int arg = 1;
  1070. size_t sfl;
  1071. const char *strfrmt = luaL_checklstring(L, arg, &sfl);
  1072. const char *strfrmt_end = strfrmt + sfl;
  1073. luaL_Buffer b;
  1074. luaL_buffinit(L, &b);
  1075. while (strfrmt < strfrmt_end)
  1076. {
  1077. if (*strfrmt != L_ESC)
  1078. luaL_addchar(&b, *strfrmt++);
  1079. else if (*++strfrmt == L_ESC)
  1080. luaL_addchar(&b, *strfrmt++); /* %% */
  1081. else /* format item */
  1082. {
  1083. char form[MAX_FORMAT]; /* to store the format ('%...') */
  1084. char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
  1085. int nb = 0; /* number of bytes in added item */
  1086. if (++arg > top)
  1087. luaL_argerror(L, arg, "no value");
  1088. strfrmt = scanformat(L, strfrmt, form);
  1089. switch (*strfrmt++)
  1090. {
  1091. case 'c':
  1092. {
  1093. nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg));
  1094. break;
  1095. }
  1096. case 'd':
  1097. case 'i':
  1098. case 'o':
  1099. case 'u':
  1100. case 'x':
  1101. case 'X':
  1102. {
  1103. lua_Integer n = luaL_checkinteger(L, arg);
  1104. addlenmod(form, LUA_INTEGER_FRMLEN);
  1105. nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACINT)n);
  1106. break;
  1107. }
  1108. case 'a':
  1109. case 'A':
  1110. addlenmod(form, LUA_NUMBER_FRMLEN);
  1111. nb = lua_number2strx(L, buff, MAX_ITEM, form,
  1112. luaL_checknumber(L, arg));
  1113. break;
  1114. case 'e':
  1115. case 'E':
  1116. case 'f':
  1117. case 'g':
  1118. case 'G':
  1119. {
  1120. lua_Number n = luaL_checknumber(L, arg);
  1121. addlenmod(form, LUA_NUMBER_FRMLEN);
  1122. nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n);
  1123. break;
  1124. }
  1125. case 'q':
  1126. {
  1127. addliteral(L, &b, arg);
  1128. break;
  1129. }
  1130. case 's':
  1131. {
  1132. size_t l;
  1133. const char *s = luaL_tolstring(L, arg, &l);
  1134. if (form[2] == '\0') /* no modifiers? */
  1135. luaL_addvalue(&b); /* keep entire string */
  1136. else
  1137. {
  1138. luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
  1139. if (!strchr(form, '.') && l >= 100)
  1140. {
  1141. /* no precision and string is too long to be formatted */
  1142. luaL_addvalue(&b); /* keep entire string */
  1143. }
  1144. else /* format the string into 'buff' */
  1145. {
  1146. nb = l_sprintf(buff, MAX_ITEM, form, s);
  1147. lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
  1148. }
  1149. }
  1150. break;
  1151. }
  1152. default: /* also treat cases 'pnLlh' */
  1153. {
  1154. return luaL_error(L, "invalid option '%%%c' to 'format'",
  1155. *(strfrmt - 1));
  1156. }
  1157. }
  1158. lua_assert(nb < MAX_ITEM);
  1159. luaL_addsize(&b, nb);
  1160. }
  1161. }
  1162. luaL_pushresult(&b);
  1163. return 1;
  1164. }
  1165. /* }====================================================== */
  1166. /*
  1167. ** {======================================================
  1168. ** PACK/UNPACK
  1169. ** =======================================================
  1170. */
  1171. /* value used for padding */
  1172. #if !defined(LUAL_PACKPADBYTE)
  1173. #define LUAL_PACKPADBYTE 0x00
  1174. #endif
  1175. /* maximum size for the binary representation of an integer */
  1176. #define MAXINTSIZE 16
  1177. /* number of bits in a character */
  1178. #define NB CHAR_BIT
  1179. /* mask for one character (NB 1's) */
  1180. #define MC ((1 << NB) - 1)
  1181. /* size of a lua_Integer */
  1182. #define SZINT ((int)sizeof(lua_Integer))
  1183. /* dummy union to get native endianness */
  1184. static const union
  1185. {
  1186. int dummy;
  1187. char little; /* true iff machine is little endian */
  1188. } nativeendian = {1};
  1189. /* dummy structure to get native alignment requirements */
  1190. struct cD
  1191. {
  1192. char c;
  1193. union
  1194. {
  1195. double d;
  1196. void *p;
  1197. lua_Integer i;
  1198. lua_Number n;
  1199. } u;
  1200. };
  1201. #define MAXALIGN (offsetof(struct cD, u))
  1202. /*
  1203. ** Union for serializing floats
  1204. */
  1205. typedef union Ftypes
  1206. {
  1207. float f;
  1208. double d;
  1209. lua_Number n;
  1210. char buff[5 * sizeof(lua_Number)]; /* enough for any float type */
  1211. } Ftypes;
  1212. /*
  1213. ** information to pack/unpack stuff
  1214. */
  1215. typedef struct Header
  1216. {
  1217. lua_State *L;
  1218. int islittle;
  1219. int maxalign;
  1220. } Header;
  1221. /*
  1222. ** options for pack/unpack
  1223. */
  1224. typedef enum KOption
  1225. {
  1226. Kint, /* signed integers */
  1227. Kuint, /* unsigned integers */
  1228. Kfloat, /* floating-point numbers */
  1229. Kchar, /* fixed-length strings */
  1230. Kstring, /* strings with prefixed length */
  1231. Kzstr, /* zero-terminated strings */
  1232. Kpadding, /* padding */
  1233. Kpaddalign, /* padding for alignment */
  1234. Knop /* no-op (configuration or spaces) */
  1235. } KOption;
  1236. /*
  1237. ** Read an integer numeral from string 'fmt' or return 'df' if
  1238. ** there is no numeral
  1239. */
  1240. static int digit(int c)
  1241. {
  1242. return '0' <= c && c <= '9';
  1243. }
  1244. static int getnum(const char **fmt, int df)
  1245. {
  1246. if (!digit(**fmt)) /* no number? */
  1247. return df; /* return default value */
  1248. else
  1249. {
  1250. int a = 0;
  1251. do
  1252. {
  1253. a = a * 10 + (*((*fmt)++) - '0');
  1254. }
  1255. while (digit(**fmt) && a <= ((int)MAXSIZE - 9) / 10);
  1256. return a;
  1257. }
  1258. }
  1259. /*
  1260. ** Read an integer numeral and raises an error if it is larger
  1261. ** than the maximum size for integers.
  1262. */
  1263. static int getnumlimit(Header *h, const char **fmt, int df)
  1264. {
  1265. int sz = getnum(fmt, df);
  1266. if (sz > MAXINTSIZE || sz <= 0)
  1267. luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
  1268. sz, MAXINTSIZE);
  1269. return sz;
  1270. }
  1271. /*
  1272. ** Initialize Header
  1273. */
  1274. static void initheader(lua_State *L, Header *h)
  1275. {
  1276. h->L = L;
  1277. h->islittle = nativeendian.little;
  1278. h->maxalign = 1;
  1279. }
  1280. /*
  1281. ** Read and classify next option. 'size' is filled with option's size.
  1282. */
  1283. static KOption getoption(Header *h, const char **fmt, int *size)
  1284. {
  1285. int opt = *((*fmt)++);
  1286. *size = 0; /* default */
  1287. switch (opt)
  1288. {
  1289. case 'b':
  1290. *size = sizeof(char);
  1291. return Kint;
  1292. case 'B':
  1293. *size = sizeof(char);
  1294. return Kuint;
  1295. case 'h':
  1296. *size = sizeof(short);
  1297. return Kint;
  1298. case 'H':
  1299. *size = sizeof(short);
  1300. return Kuint;
  1301. case 'l':
  1302. *size = sizeof(long);
  1303. return Kint;
  1304. case 'L':
  1305. *size = sizeof(long);
  1306. return Kuint;
  1307. case 'j':
  1308. *size = sizeof(lua_Integer);
  1309. return Kint;
  1310. case 'J':
  1311. *size = sizeof(lua_Integer);
  1312. return Kuint;
  1313. case 'T':
  1314. *size = sizeof(size_t);
  1315. return Kuint;
  1316. case 'f':
  1317. *size = sizeof(float);
  1318. return Kfloat;
  1319. case 'd':
  1320. *size = sizeof(double);
  1321. return Kfloat;
  1322. case 'n':
  1323. *size = sizeof(lua_Number);
  1324. return Kfloat;
  1325. case 'i':
  1326. *size = getnumlimit(h, fmt, sizeof(int));
  1327. return Kint;
  1328. case 'I':
  1329. *size = getnumlimit(h, fmt, sizeof(int));
  1330. return Kuint;
  1331. case 's':
  1332. *size = getnumlimit(h, fmt, sizeof(size_t));
  1333. return Kstring;
  1334. case 'c':
  1335. *size = getnum(fmt, -1);
  1336. if (*size == -1)
  1337. luaL_error(h->L, "missing size for format option 'c'");
  1338. return Kchar;
  1339. case 'z':
  1340. return Kzstr;
  1341. case 'x':
  1342. *size = 1;
  1343. return Kpadding;
  1344. case 'X':
  1345. return Kpaddalign;
  1346. case ' ':
  1347. break;
  1348. case '<':
  1349. h->islittle = 1;
  1350. break;
  1351. case '>':
  1352. h->islittle = 0;
  1353. break;
  1354. case '=':
  1355. h->islittle = nativeendian.little;
  1356. break;
  1357. case '!':
  1358. h->maxalign = getnumlimit(h, fmt, MAXALIGN);
  1359. break;
  1360. default:
  1361. luaL_error(h->L, "invalid format option '%c'", opt);
  1362. }
  1363. return Knop;
  1364. }
  1365. /*
  1366. ** Read, classify, and fill other details about the next option.
  1367. ** 'psize' is filled with option's size, 'notoalign' with its
  1368. ** alignment requirements.
  1369. ** Local variable 'size' gets the size to be aligned. (Kpadal option
  1370. ** always gets its full alignment, other options are limited by
  1371. ** the maximum alignment ('maxalign'). Kchar option needs no alignment
  1372. ** despite its size.
  1373. */
  1374. static KOption getdetails(Header *h, size_t totalsize,
  1375. const char **fmt, int *psize, int *ntoalign)
  1376. {
  1377. KOption opt = getoption(h, fmt, psize);
  1378. int align = *psize; /* usually, alignment follows size */
  1379. if (opt == Kpaddalign) /* 'X' gets alignment from following option */
  1380. {
  1381. if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0)
  1382. luaL_argerror(h->L, 1, "invalid next option for option 'X'");
  1383. }
  1384. if (align <= 1 || opt == Kchar) /* need no alignment? */
  1385. *ntoalign = 0;
  1386. else
  1387. {
  1388. if (align > h->maxalign) /* enforce maximum alignment */
  1389. align = h->maxalign;
  1390. if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */
  1391. luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
  1392. *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
  1393. }
  1394. return opt;
  1395. }
  1396. /*
  1397. ** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
  1398. ** The final 'if' handles the case when 'size' is larger than
  1399. ** the size of a Lua integer, correcting the extra sign-extension
  1400. ** bytes if necessary (by default they would be zeros).
  1401. */
  1402. static void packint(luaL_Buffer *b, lua_Unsigned n,
  1403. int islittle, int size, int neg)
  1404. {
  1405. char *buff = luaL_prepbuffsize(b, size);
  1406. int i;
  1407. buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */
  1408. for (i = 1; i < size; i++)
  1409. {
  1410. n >>= NB;
  1411. buff[islittle ? i : size - 1 - i] = (char)(n & MC);
  1412. }
  1413. if (neg && size > SZINT) /* negative number need sign extension? */
  1414. {
  1415. for (i = SZINT; i < size; i++) /* correct extra bytes */
  1416. buff[islittle ? i : size - 1 - i] = (char)MC;
  1417. }
  1418. luaL_addsize(b, size); /* add result to buffer */
  1419. }
  1420. /*
  1421. ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
  1422. ** given 'islittle' is different from native endianness.
  1423. */
  1424. static void copywithendian(volatile char *dest, volatile const char *src,
  1425. int size, int islittle)
  1426. {
  1427. if (islittle == nativeendian.little)
  1428. {
  1429. while (size-- != 0)
  1430. *(dest++) = *(src++);
  1431. }
  1432. else
  1433. {
  1434. dest += size - 1;
  1435. while (size-- != 0)
  1436. *(dest--) = *(src++);
  1437. }
  1438. }
  1439. static int str_pack(lua_State *L)
  1440. {
  1441. luaL_Buffer b;
  1442. Header h;
  1443. const char *fmt = luaL_checkstring(L, 1); /* format string */
  1444. int arg = 1; /* current argument to pack */
  1445. size_t totalsize = 0; /* accumulate total size of result */
  1446. initheader(L, &h);
  1447. lua_pushnil(L); /* mark to separate arguments from string buffer */
  1448. luaL_buffinit(L, &b);
  1449. while (*fmt != '\0')
  1450. {
  1451. int size, ntoalign;
  1452. KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
  1453. totalsize += ntoalign + size;
  1454. while (ntoalign-- > 0)
  1455. luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */
  1456. arg++;
  1457. switch (opt)
  1458. {
  1459. case Kint: /* signed integers */
  1460. {
  1461. lua_Integer n = luaL_checkinteger(L, arg);
  1462. if (size < SZINT) /* need overflow check? */
  1463. {
  1464. lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
  1465. luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
  1466. }
  1467. packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0));
  1468. break;
  1469. }
  1470. case Kuint: /* unsigned integers */
  1471. {
  1472. lua_Integer n = luaL_checkinteger(L, arg);
  1473. if (size < SZINT) /* need overflow check? */
  1474. luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
  1475. arg, "unsigned overflow");
  1476. packint(&b, (lua_Unsigned)n, h.islittle, size, 0);
  1477. break;
  1478. }
  1479. case Kfloat: /* floating-point options */
  1480. {
  1481. volatile Ftypes u;
  1482. char *buff = luaL_prepbuffsize(&b, size);
  1483. lua_Number n = luaL_checknumber(L, arg); /* get argument */
  1484. if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */
  1485. else if (size == sizeof(u.d)) u.d = (double)n;
  1486. else u.n = n;
  1487. /* move 'u' to final result, correcting endianness if needed */
  1488. copywithendian(buff, u.buff, size, h.islittle);
  1489. luaL_addsize(&b, size);
  1490. break;
  1491. }
  1492. case Kchar: /* fixed-size string */
  1493. {
  1494. size_t len;
  1495. const char *s = luaL_checklstring(L, arg, &len);
  1496. luaL_argcheck(L, len <= (size_t)size, arg,
  1497. "string longer than given size");
  1498. luaL_addlstring(&b, s, len); /* add string */
  1499. while (len++ < (size_t)size) /* pad extra space */
  1500. luaL_addchar(&b, LUAL_PACKPADBYTE);
  1501. break;
  1502. }
  1503. case Kstring: /* strings with length count */
  1504. {
  1505. size_t len;
  1506. const char *s = luaL_checklstring(L, arg, &len);
  1507. luaL_argcheck(L, size >= (int)sizeof(size_t) ||
  1508. len < ((size_t)1 << (size * NB)),
  1509. arg, "string length does not fit in given size");
  1510. packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */
  1511. luaL_addlstring(&b, s, len);
  1512. totalsize += len;
  1513. break;
  1514. }
  1515. case Kzstr: /* zero-terminated string */
  1516. {
  1517. size_t len;
  1518. const char *s = luaL_checklstring(L, arg, &len);
  1519. luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros");
  1520. luaL_addlstring(&b, s, len);
  1521. luaL_addchar(&b, '\0'); /* add zero at the end */
  1522. totalsize += len + 1;
  1523. break;
  1524. }
  1525. case Kpadding:
  1526. luaL_addchar(&b, LUAL_PACKPADBYTE); /* FALLTHROUGH */
  1527. case Kpaddalign:
  1528. case Knop:
  1529. arg--; /* undo increment */
  1530. break;
  1531. }
  1532. }
  1533. luaL_pushresult(&b);
  1534. return 1;
  1535. }
  1536. static int str_packsize(lua_State *L)
  1537. {
  1538. Header h;
  1539. const char *fmt = luaL_checkstring(L, 1); /* format string */
  1540. size_t totalsize = 0; /* accumulate total size of result */
  1541. initheader(L, &h);
  1542. while (*fmt != '\0')
  1543. {
  1544. int size, ntoalign;
  1545. KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
  1546. size += ntoalign; /* total space used by option */
  1547. luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
  1548. "format result too large");
  1549. totalsize += size;
  1550. switch (opt)
  1551. {
  1552. case Kstring: /* strings with length count */
  1553. case Kzstr: /* zero-terminated string */
  1554. luaL_argerror(L, 1, "variable-length format");
  1555. /* call never return, but to avoid warnings: *//* FALLTHROUGH */
  1556. default:
  1557. break;
  1558. }
  1559. }
  1560. lua_pushinteger(L, (lua_Integer)totalsize);
  1561. return 1;
  1562. }
  1563. /*
  1564. ** Unpack an integer with 'size' bytes and 'islittle' endianness.
  1565. ** If size is smaller than the size of a Lua integer and integer
  1566. ** is signed, must do sign extension (propagating the sign to the
  1567. ** higher bits); if size is larger than the size of a Lua integer,
  1568. ** it must check the unread bytes to see whether they do not cause an
  1569. ** overflow.
  1570. */
  1571. static lua_Integer unpackint(lua_State *L, const char *str,
  1572. int islittle, int size, int issigned)
  1573. {
  1574. lua_Unsigned res = 0;
  1575. int i;
  1576. int limit = (size <= SZINT) ? size : SZINT;
  1577. for (i = limit - 1; i >= 0; i--)
  1578. {
  1579. res <<= NB;
  1580. res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i];
  1581. }
  1582. if (size < SZINT) /* real size smaller than lua_Integer? */
  1583. {
  1584. if (issigned) /* needs sign extension? */
  1585. {
  1586. lua_Unsigned mask = (lua_Unsigned)1 << (size * NB - 1);
  1587. res = ((res ^ mask) - mask); /* do sign extension */
  1588. }
  1589. }
  1590. else if (size > SZINT) /* must check unread bytes */
  1591. {
  1592. int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
  1593. for (i = limit; i < size; i++)
  1594. {
  1595. if ((unsigned char)str[islittle ? i : size - 1 - i] != mask)
  1596. luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
  1597. }
  1598. }
  1599. return (lua_Integer)res;
  1600. }
  1601. static int str_unpack(lua_State *L)
  1602. {
  1603. Header h;
  1604. const char *fmt = luaL_checkstring(L, 1);
  1605. size_t ld;
  1606. const char *data = luaL_checklstring(L, 2, &ld);
  1607. size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1;
  1608. int n = 0; /* number of results */
  1609. luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
  1610. initheader(L, &h);
  1611. while (*fmt != '\0')
  1612. {
  1613. int size, ntoalign;
  1614. KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
  1615. if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
  1616. luaL_argerror(L, 2, "data string too short");
  1617. pos += ntoalign; /* skip alignment */
  1618. /* stack space for item + next position */
  1619. luaL_checkstack(L, 2, "too many results");
  1620. n++;
  1621. switch (opt)
  1622. {
  1623. case Kint:
  1624. case Kuint:
  1625. {
  1626. lua_Integer res = unpackint(L, data + pos, h.islittle, size,
  1627. (opt == Kint));
  1628. lua_pushinteger(L, res);
  1629. break;
  1630. }
  1631. case Kfloat:
  1632. {
  1633. volatile Ftypes u;
  1634. lua_Number num;
  1635. copywithendian(u.buff, data + pos, size, h.islittle);
  1636. if (size == sizeof(u.f)) num = (lua_Number)u.f;
  1637. else if (size == sizeof(u.d)) num = (lua_Number)u.d;
  1638. else num = u.n;
  1639. lua_pushnumber(L, num);
  1640. break;
  1641. }
  1642. case Kchar:
  1643. {
  1644. lua_pushlstring(L, data + pos, size);
  1645. break;
  1646. }
  1647. case Kstring:
  1648. {
  1649. size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
  1650. luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short");
  1651. lua_pushlstring(L, data + pos + size, len);
  1652. pos += len; /* skip string */
  1653. break;
  1654. }
  1655. case Kzstr:
  1656. {
  1657. size_t len = (int)strlen(data + pos);
  1658. lua_pushlstring(L, data + pos, len);
  1659. pos += len + 1; /* skip string plus final '\0' */
  1660. break;
  1661. }
  1662. case Kpaddalign:
  1663. case Kpadding:
  1664. case Knop:
  1665. n--; /* undo increment */
  1666. break;
  1667. }
  1668. pos += size;
  1669. }
  1670. lua_pushinteger(L, pos + 1); /* next position */
  1671. return n + 1;
  1672. }
  1673. /* }====================================================== */
  1674. static const luaL_Reg strlib[] =
  1675. {
  1676. {"byte", str_byte},
  1677. {"char", str_char},
  1678. {"dump", str_dump},
  1679. {"find", str_find},
  1680. {"format", str_format},
  1681. {"gmatch", gmatch},
  1682. {"gsub", str_gsub},
  1683. {"len", str_len},
  1684. {"lower", str_lower},
  1685. {"match", str_match},
  1686. {"rep", str_rep},
  1687. {"reverse", str_reverse},
  1688. {"sub", str_sub},
  1689. {"upper", str_upper},
  1690. {"pack", str_pack},
  1691. {"packsize", str_packsize},
  1692. {"unpack", str_unpack},
  1693. {NULL, NULL}
  1694. };
  1695. static void createmetatable(lua_State *L)
  1696. {
  1697. lua_createtable(L, 0, 1); /* table to be metatable for strings */
  1698. lua_pushliteral(L, ""); /* dummy string */
  1699. lua_pushvalue(L, -2); /* copy table */
  1700. lua_setmetatable(L, -2); /* set table as metatable for strings */
  1701. lua_pop(L, 1); /* pop dummy string */
  1702. lua_pushvalue(L, -2); /* get string library */
  1703. lua_setfield(L, -2, "__index"); /* metatable.__index = string */
  1704. lua_pop(L, 1); /* pop metatable */
  1705. }
  1706. /*
  1707. ** Open string library
  1708. */
  1709. LUAMOD_API int luaopen_string(lua_State *L)
  1710. {
  1711. luaL_newlib(L, strlib);
  1712. createmetatable(L);
  1713. return 1;
  1714. }