lstrlib.c 47 KB

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