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