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