lstrlib.c 45 KB

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