Coverage Report

Created: 2026-01-25 07:04

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