9
3

lstrlib.c 46 KB

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