Coverage Report

Created: 2026-02-26 07:19

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
81.8M
#define LUA_MAXCAPTURES   32
37
#endif
38
39
40
8.41k
static int str_len (lua_State *L) {
41
8.41k
  size_t l;
42
8.41k
  luaL_checklstring(L, 1, &l);
43
8.41k
  lua_pushinteger(L, (lua_Integer)l);
44
8.41k
  return 1;
45
8.41k
}
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.05M
static size_t posrelatI (lua_Integer pos, size_t len) {
57
1.05M
  if (pos > 0)
58
1.02M
    return (size_t)pos;
59
32.4k
  else if (pos == 0)
60
5.14k
    return 1;
61
27.3k
  else if (pos < -(lua_Integer)len)  /* inverted comparison */
62
22.4k
    return 1;  /* clip to 1 */
63
4.85k
  else return len + (size_t)pos + 1;
64
1.05M
}
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
780k
                         size_t len) {
74
780k
  lua_Integer pos = luaL_optinteger(L, arg, def);
75
780k
  if (pos > (lua_Integer)len)
76
6.29k
    return len;
77
774k
  else if (pos >= 0)
78
759k
    return (size_t)pos;
79
15.2k
  else if (pos < -(lua_Integer)len)
80
3.66k
    return 0;
81
11.6k
  else return len + (size_t)pos + 1;
82
780k
}
83
84
85
733k
static int str_sub (lua_State *L) {
86
733k
  size_t l;
87
733k
  const char *s = luaL_checklstring(L, 1, &l);
88
733k
  size_t start = posrelatI(luaL_checkinteger(L, 2), l);
89
733k
  size_t end = getendpos(L, 3, -1, l);
90
733k
  if (start <= end)
91
721k
    lua_pushlstring(L, s + start - 1, (end - start) + 1);
92
12.7k
  else lua_pushliteral(L, "");
93
733k
  return 1;
94
733k
}
95
96
97
2.57k
static int str_reverse (lua_State *L) {
98
2.57k
  size_t l, i;
99
2.57k
  luaL_Buffer b;
100
2.57k
  const char *s = luaL_checklstring(L, 1, &l);
101
2.57k
  char *p = luaL_buffinitsize(L, &b, l);
102
2.96M
  for (i = 0; i < l; i++)
103
2.95M
    p[i] = s[l - i - 1];
104
2.57k
  luaL_pushresultsize(&b, l);
105
2.57k
  return 1;
106
2.57k
}
107
108
109
1.73k
static int str_lower (lua_State *L) {
110
1.73k
  size_t l;
111
1.73k
  size_t i;
112
1.73k
  luaL_Buffer b;
113
1.73k
  const char *s = luaL_checklstring(L, 1, &l);
114
1.73k
  char *p = luaL_buffinitsize(L, &b, l);
115
434k
  for (i=0; i<l; i++)
116
432k
    p[i] = cast_char(tolower(cast_uchar(s[i])));
117
1.73k
  luaL_pushresultsize(&b, l);
118
1.73k
  return 1;
119
1.73k
}
120
121
122
5.26k
static int str_upper (lua_State *L) {
123
5.26k
  size_t l;
124
5.26k
  size_t i;
125
5.26k
  luaL_Buffer b;
126
5.26k
  const char *s = luaL_checklstring(L, 1, &l);
127
5.26k
  char *p = luaL_buffinitsize(L, &b, l);
128
630k
  for (i=0; i<l; i++)
129
625k
    p[i] = cast_char(toupper(cast_uchar(s[i])));
130
5.26k
  luaL_pushresultsize(&b, l);
131
5.26k
  return 1;
132
5.26k
}
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
6.33k
static int str_rep (lua_State *L) {
140
6.33k
  size_t len, lsep;
141
6.33k
  const char *s = luaL_checklstring(L, 1, &len);
142
6.33k
  lua_Integer n = luaL_checkinteger(L, 2);
143
6.33k
  const char *sep = luaL_optlstring(L, 3, "", &lsep);
144
6.33k
  if (n <= 0 || (len | lsep) == 0)
145
1.74k
    lua_pushliteral(L, "");  /* no repetitions or both strings empty */
146
4.58k
  else if (l_unlikely(len > MAX_SIZE - lsep ||
147
4.58k
               cast_st2S(len + lsep) > cast_st2S(MAX_SIZE) / n))
148
469
    return luaL_error(L, "resulting string too large");
149
4.11k
  else {
150
4.11k
    size_t totallen = (cast_sizet(n) * (len + lsep)) - lsep;
151
4.11k
    luaL_Buffer b;
152
4.11k
    char *p = luaL_buffinitsize(L, &b, totallen);
153
32.1M
    while (n-- > 1) {  /* first n-1 copies (followed by separator) */
154
32.1M
      memcpy(p, s, len * sizeof(char)); p += len;
155
32.1M
      if (lsep > 0) {  /* empty 'memcpy' is not that cheap */
156
778
        memcpy(p, sep, lsep * sizeof(char)); p += lsep;
157
778
      }
158
32.1M
    }
159
4.11k
    memcpy(p, s, len * sizeof(char));  /* last copy without separator */
160
4.11k
    luaL_pushresultsize(&b, totallen);
161
4.11k
  }
162
5.86k
  return 1;
163
6.33k
}
164
165
166
53.4k
static int str_byte (lua_State *L) {
167
53.4k
  size_t l;
168
53.4k
  const char *s = luaL_checklstring(L, 1, &l);
169
53.4k
  lua_Integer pi = luaL_optinteger(L, 2, 1);
170
53.4k
  size_t posi = posrelatI(pi, l);
171
53.4k
  size_t pose = getendpos(L, 3, pi, l);
172
53.4k
  int n, i;
173
53.4k
  if (posi > pose) return 0;  /* empty interval; return no values */
174
50.9k
  if (l_unlikely(pose - posi >= (size_t)INT_MAX))  /* arithmetic overflow? */
175
0
    return luaL_error(L, "string slice too long");
176
50.9k
  n = (int)(pose -  posi) + 1;
177
50.9k
  luaL_checkstack(L, n, "string slice too long");
178
124k
  for (i=0; i<n; i++)
179
73.3k
    lua_pushinteger(L, cast_uchar(s[posi + cast_uint(i) - 1]));
180
50.9k
  return n;
181
50.9k
}
182
183
184
6.24k
static int str_char (lua_State *L) {
185
6.24k
  int n = lua_gettop(L);  /* number of arguments */
186
6.24k
  int i;
187
6.24k
  luaL_Buffer b;
188
6.24k
  char *p = luaL_buffinitsize(L, &b, cast_uint(n));
189
22.9k
  for (i=1; i<=n; i++) {
190
16.6k
    lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i);
191
16.6k
    luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range");
192
16.6k
    p[i - 1] = cast_char(cast_uchar(c));
193
16.6k
  }
194
6.24k
  luaL_pushresultsize(&b, cast_uint(n));
195
6.24k
  return 1;
196
6.24k
}
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.80M
static int writer (lua_State *L, const void *b, size_t size, void *ud) {
212
3.80M
  struct str_Writer *state = (struct str_Writer *)ud;
213
3.80M
  if (!state->init) {
214
54.4k
    state->init = 1;
215
54.4k
    luaL_buffinit(L, &state->B);
216
54.4k
  }
217
3.80M
  if (b == NULL) {  /* finishing dump? */
218
54.4k
    luaL_pushresult(&state->B);  /* push result */
219
54.4k
    lua_replace(L, 1);  /* move it to reserved slot */
220
54.4k
  }
221
3.75M
  else
222
3.75M
    luaL_addlstring(&state->B, (const char *)b, size);
223
3.80M
  return 0;
224
3.80M
}
225
226
227
55.5k
static int str_dump (lua_State *L) {
228
55.5k
  struct str_Writer state;
229
55.5k
  int strip = lua_toboolean(L, 2);
230
55.5k
  luaL_argcheck(L, lua_type(L, 1) == LUA_TFUNCTION && !lua_iscfunction(L, 1),
231
55.5k
                   1, "Lua function expected");
232
  /* ensure function is on the top of the stack and vacate slot 1 */
233
55.5k
  lua_pushvalue(L, 1);
234
55.5k
  state.init = 0;
235
55.5k
  lua_dump(L, writer, &state, strip);
236
55.5k
  lua_settop(L, 1);  /* leave final result on top */
237
55.5k
  return 1;
238
55.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
813k
static int tonum (lua_State *L, int arg) {
260
813k
  if (lua_type(L, arg) == LUA_TNUMBER) {  /* already a number? */
261
367k
    lua_pushvalue(L, arg);
262
367k
    return 1;
263
367k
  }
264
445k
  else {  /* check whether it is a numerical string */
265
445k
    size_t len;
266
445k
    const char *s = lua_tolstring(L, arg, &len);
267
445k
    return (s != NULL && lua_stringtonumber(L, s) == len + 1);
268
445k
  }
269
813k
}
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
31.1k
static void trymt (lua_State *L, const char *mtkey, const char *opname) {
280
31.1k
  lua_settop(L, 2);  /* back to the original arguments */
281
31.1k
  if (l_unlikely(lua_type(L, 2) == LUA_TSTRING ||
282
31.1k
                 !luaL_getmetafield(L, 2, mtkey)))
283
29.1k
    luaL_error(L, "attempt to %s a '%s' with a '%s'", opname,
284
29.1k
                  luaL_typename(L, -2), luaL_typename(L, -1));
285
31.1k
  lua_insert(L, -3);  /* put metamethod before arguments */
286
31.1k
  lua_call(L, 2, 1);  /* call metamethod */
287
31.1k
}
288
289
290
410k
static int arith (lua_State *L, int op, const char *mtname) {
291
410k
  if (tonum(L, 1) && tonum(L, 2))
292
379k
    lua_arith(L, op);  /* result will be on the top */
293
31.1k
  else
294
31.1k
    trymt(L, mtname, mtname + 2);
295
410k
  return 1;
296
410k
}
297
298
299
93.6k
static int arith_add (lua_State *L) {
300
93.6k
  return arith(L, LUA_OPADD, "__add");
301
93.6k
}
302
303
55.2k
static int arith_sub (lua_State *L) {
304
55.2k
  return arith(L, LUA_OPSUB, "__sub");
305
55.2k
}
306
307
27.2k
static int arith_mul (lua_State *L) {
308
27.2k
  return arith(L, LUA_OPMUL, "__mul");
309
27.2k
}
310
311
70.9k
static int arith_mod (lua_State *L) {
312
70.9k
  return arith(L, LUA_OPMOD, "__mod");
313
70.9k
}
314
315
19.1k
static int arith_pow (lua_State *L) {
316
19.1k
  return arith(L, LUA_OPPOW, "__pow");
317
19.1k
}
318
319
33.6k
static int arith_div (lua_State *L) {
320
33.6k
  return arith(L, LUA_OPDIV, "__div");
321
33.6k
}
322
323
99.6k
static int arith_idiv (lua_State *L) {
324
99.6k
  return arith(L, LUA_OPIDIV, "__idiv");
325
99.6k
}
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
272M
#define CAP_UNFINISHED  (-1)
357
7.58M
#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
678k
#define MAXCCALLS 200
381
#endif
382
383
384
183M
#define L_ESC   '%'
385
201k
#define SPECIALS  "^$*+?.([%-"
386
387
388
78.5M
static int check_capture (MatchState *ms, int l) {
389
78.5M
  l -= '1';
390
78.5M
  if (l_unlikely(l < 0 || l >= ms->level ||
391
78.5M
                 ms->capture[l].len == CAP_UNFINISHED))
392
2.41k
    return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
393
78.5M
  return l;
394
78.5M
}
395
396
397
73.9M
static int capture_to_close (MatchState *ms) {
398
73.9M
  int level = ms->level;
399
123M
  for (level--; level>=0; level--)
400
123M
    if (ms->capture[level].len == CAP_UNFINISHED) return level;
401
2.89k
  return luaL_error(ms->L, "invalid pattern capture");
402
73.9M
}
403
404
405
192M
static const char *classend (MatchState *ms, const char *p) {
406
192M
  switch (*p++) {
407
4.39M
    case L_ESC: {
408
4.39M
      if (l_unlikely(p == ms->p_end))
409
485
        luaL_error(ms->L, "malformed pattern (ends with '%%')");
410
4.39M
      return p+1;
411
0
    }
412
3.35M
    case '[': {
413
3.35M
      if (*p == '^') p++;
414
10.0M
      do {  /* look for a ']' */
415
10.0M
        if (l_unlikely(p == ms->p_end))
416
1.05k
          luaL_error(ms->L, "malformed pattern (missing ']')");
417
10.0M
        if (*(p++) == L_ESC && p < ms->p_end)
418
1.47M
          p++;  /* skip escapes (e.g. '%]') */
419
10.0M
      } while (*p != ']');
420
3.35M
      return p+1;
421
0
    }
422
184M
    default: {
423
184M
      return p;
424
0
    }
425
192M
  }
426
192M
}
427
428
429
6.28M
static int match_class (int c, int cl) {
430
6.28M
  int res;
431
6.28M
  switch (tolower(cl)) {
432
520k
    case 'a' : res = isalpha(c); break;
433
288
    case 'c' : res = iscntrl(c); break;
434
2.28M
    case 'd' : res = isdigit(c); break;
435
7.25k
    case 'g' : res = isgraph(c); break;
436
20
    case 'l' : res = islower(c); break;
437
3
    case 'p' : res = ispunct(c); break;
438
1.32M
    case 's' : res = isspace(c); break;
439
71.6k
    case 'u' : res = isupper(c); break;
440
8.93k
    case 'w' : res = isalnum(c); break;
441
1.44k
    case 'x' : res = isxdigit(c); break;
442
858k
    case 'z' : res = (c == 0); break;  /* deprecated option */
443
1.20M
    default: return (cl == c);
444
6.28M
  }
445
5.08M
  return (islower(cl) ? res : !res);
446
6.28M
}
447
448
449
5.28M
static int matchbracketclass (int c, const char *p, const char *ec) {
450
5.28M
  int sig = 1;
451
5.28M
  if (*(p+1) == '^') {
452
3.82M
    sig = 0;
453
3.82M
    p++;  /* skip the '^' */
454
3.82M
  }
455
15.0M
  while (++p < ec) {
456
9.95M
    if (*p == L_ESC) {
457
1.44M
      p++;
458
1.44M
      if (match_class(c, cast_uchar(*p)))
459
120k
        return sig;
460
1.44M
    }
461
8.50M
    else if ((*(p+1) == '-') && (p+2 < ec)) {
462
967k
      p+=2;
463
967k
      if (cast_uchar(*(p-2)) <= c && c <= cast_uchar(*p))
464
18.8k
        return sig;
465
967k
    }
466
7.53M
    else if (cast_uchar(*p) == c) return sig;
467
9.95M
  }
468
5.11M
  return !sig;
469
5.28M
}
470
471
472
static int singlematch (MatchState *ms, const char *s, const char *p,
473
347M
                        const char *ep) {
474
347M
  if (s >= ms->src_end)
475
554k
    return 0;
476
346M
  else {
477
346M
    int c = cast_uchar(*s);
478
346M
    switch (*p) {
479
128M
      case '.': return 1;  /* matches any char */
480
4.84M
      case L_ESC: return match_class(c, cast_uchar(*(p+1)));
481
5.27M
      case '[': return matchbracketclass(c, p, ep-1);
482
208M
      default:  return (cast_uchar(*p) == c);
483
346M
    }
484
346M
  }
485
347M
}
486
487
488
static const char *matchbalance (MatchState *ms, const char *s,
489
922k
                                   const char *p) {
490
922k
  if (l_unlikely(p >= ms->p_end - 1))
491
442
    luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')");
492
922k
  if (*s != *p) return NULL;
493
2.53k
  else {
494
2.53k
    int b = *p;
495
2.53k
    int e = *(p+1);
496
2.53k
    int cont = 1;
497
16.5M
    while (++s < ms->src_end) {
498
16.5M
      if (*s == e) {
499
459k
        if (--cont == 0) return s+1;
500
459k
      }
501
16.1M
      else if (*s == b) cont++;
502
16.5M
    }
503
2.53k
  }
504
1.78k
  return NULL;  /* string ends out of balance */
505
922k
}
506
507
508
static const char *max_expand (MatchState *ms, const char *s,
509
870k
                                 const char *p, const char *ep) {
510
870k
  ptrdiff_t i = 0;  /* counts maximum expand for item */
511
111M
  while (singlematch(ms, s + i, p, ep))
512
110M
    i++;
513
  /* keeps trying to match with the maximum repetitions */
514
86.1M
  while (i>=0) {
515
85.3M
    const char *res = match(ms, (s+i), ep+1);
516
85.3M
    if (res) return res;
517
85.3M
    i--;  /* else didn't match; reduce 1 repetition to try again */
518
85.3M
  }
519
838k
  return NULL;
520
870k
}
521
522
523
static const char *min_expand (MatchState *ms, const char *s,
524
34.0k
                                 const char *p, const char *ep) {
525
43.9M
  for (;;) {
526
43.9M
    const char *res = match(ms, s, ep+1);
527
43.9M
    if (res != NULL)
528
8.07k
      return res;
529
43.9M
    else if (singlematch(ms, s, p, ep))
530
43.8M
      s++;  /* try with one more repetition */
531
25.9k
    else return NULL;
532
43.9M
  }
533
34.0k
}
534
535
536
static const char *start_capture (MatchState *ms, const char *s,
537
81.8M
                                    const char *p, int what) {
538
81.8M
  const char *res;
539
81.8M
  int level = ms->level;
540
81.8M
  if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
541
81.8M
  ms->capture[level].init = s;
542
81.8M
  ms->capture[level].len = what;
543
81.8M
  ms->level = level+1;
544
81.8M
  if ((res=match(ms, s, p)) == NULL)  /* match failed? */
545
81.3M
    ms->level--;  /* undo capture */
546
81.8M
  return res;
547
81.8M
}
548
549
550
static const char *end_capture (MatchState *ms, const char *s,
551
73.9M
                                  const char *p) {
552
73.9M
  int l = capture_to_close(ms);
553
73.9M
  const char *res;
554
73.9M
  ms->capture[l].len = s - ms->capture[l].init;  /* close capture */
555
73.9M
  if ((res = match(ms, s, p)) == NULL)  /* match failed? */
556
73.8M
    ms->capture[l].len = CAP_UNFINISHED;  /* undo capture */
557
73.9M
  return res;
558
73.9M
}
559
560
561
78.5M
static const char *match_capture (MatchState *ms, const char *s, int l) {
562
78.5M
  size_t len;
563
78.5M
  l = check_capture(ms, l);
564
78.5M
  len = cast_sizet(ms->capture[l].len);
565
78.5M
  if ((size_t)(ms->src_end-s) >= len &&
566
47.2M
      memcmp(ms->capture[l].init, s, len) == 0)
567
3.06M
    return s+len;
568
75.4M
  else return NULL;
569
78.5M
}
570
571
572
344M
static const char *match (MatchState *ms, const char *s, const char *p) {
573
344M
  if (l_unlikely(ms->matchdepth-- == 0))
574
1
    luaL_error(ms->L, "pattern too complex");
575
476M
  init: /* using goto to optimize tail recursion */
576
476M
  if (p != ms->p_end) {  /* end of pattern? */
577
475M
    switch (*p) {
578
81.8M
      case '(': {  /* start capture */
579
81.8M
        if (*(p + 1) == ')')  /* position capture? */
580
6.73M
          s = start_capture(ms, s, p + 2, CAP_POSITION);
581
75.1M
        else
582
75.1M
          s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
583
81.8M
        break;
584
0
      }
585
73.9M
      case ')': {  /* end capture */
586
73.9M
        s = end_capture(ms, s, p + 1);
587
73.9M
        break;
588
0
      }
589
48.0M
      case '$': {
590
48.0M
        if ((p + 1) != ms->p_end)  /* is the '$' the last char in pattern? */
591
7.81k
          goto dflt;  /* no; go to default */
592
47.9M
        s = (s == ms->src_end) ? s : NULL;  /* check end of string */
593
47.9M
        break;
594
48.0M
      }
595
83.8M
      case L_ESC: {  /* escaped sequences not in the format class[*+?-]? */
596
83.8M
        switch (*(p + 1)) {
597
922k
          case 'b': {  /* balanced string? */
598
922k
            s = matchbalance(ms, s, p + 2);
599
922k
            if (s != NULL) {
600
748
              p += 4; goto init;  /* return match(ms, s, p + 4); */
601
748
            }  /* else fail (s == NULL) */
602
922k
            break;
603
922k
          }
604
922k
          case 'f': {  /* frontier? */
605
4.94k
            const char *ep; char previous;
606
4.94k
            p += 2;
607
4.94k
            if (l_unlikely(*p != '['))
608
4
              luaL_error(ms->L, "missing '[' after '%%f' in pattern");
609
4.94k
            ep = classend(ms, p);  /* points to what is next */
610
4.94k
            previous = (s == ms->src_init) ? '\0' : *(s - 1);
611
4.94k
            if (!matchbracketclass(cast_uchar(previous), p, ep - 1) &&
612
3.97k
               matchbracketclass(cast_uchar(*s), p, ep - 1)) {
613
32
              p = ep; goto init;  /* return match(ms, s, ep); */
614
32
            }
615
4.91k
            s = NULL;  /* match failed */
616
4.91k
            break;
617
4.94k
          }
618
48.2M
          case '0': case '1': case '2': case '3':
619
76.2M
          case '4': case '5': case '6': case '7':
620
78.5M
          case '8': case '9': {  /* capture results (%0-%9)? */
621
78.5M
            s = match_capture(ms, s, cast_uchar(*(p + 1)));
622
78.5M
            if (s != NULL) {
623
3.06M
              p += 2; goto init;  /* return match(ms, s, p + 2) */
624
3.06M
            }
625
75.4M
            break;
626
78.5M
          }
627
75.4M
          default: goto dflt;
628
83.8M
        }
629
76.3M
        break;
630
83.8M
      }
631
192M
      default: dflt: {  /* pattern class plus optional suffix */
632
192M
        const char *ep = classend(ms, p);  /* points to optional suffix */
633
        /* does not match at least once? */
634
192M
        if (!singlematch(ms, s, p, ep)) {
635
140M
          if (*ep == '*' || *ep == '?' || *ep == '-') {  /* accept empty? */
636
77.2M
            p = ep + 1; goto init;  /* return match(ms, s, ep + 1); */
637
77.2M
          }
638
62.8M
          else  /* '+' or no suffix */
639
62.8M
            s = NULL;  /* fail */
640
140M
        }
641
52.1M
        else {  /* matched once */
642
52.1M
          switch (*ep) {  /* handle optional suffix */
643
46.6M
            case '?': {  /* optional */
644
46.6M
              const char *res;
645
46.6M
              if ((res = match(ms, s + 1, ep + 1)) != NULL)
646
2.85k
                s = res;
647
46.6M
              else {
648
46.6M
                p = ep + 1; goto init;  /* else return match(ms, s, ep + 1); */
649
46.6M
              }
650
2.85k
              break;
651
46.6M
            }
652
99.0k
            case '+':  /* 1 or more repetitions */
653
99.0k
              s++;  /* 1 match already done */
654
              /* FALLTHROUGH */
655
870k
            case '*':  /* 0 or more repetitions */
656
870k
              s = max_expand(ms, s, p, ep);
657
870k
              break;
658
34.0k
            case '-':  /* 0 or more repetitions (minimum) */
659
34.0k
              s = min_expand(ms, s, p, ep);
660
34.0k
              break;
661
4.62M
            default:  /* no suffix */
662
4.62M
              s++; p = ep; goto init;  /* return match(ms, s + 1, ep); */
663
52.1M
          }
664
52.1M
        }
665
63.7M
        break;
666
192M
      }
667
475M
    }
668
475M
  }
669
344M
  ms->matchdepth++;
670
344M
  return s;
671
476M
}
672
673
674
675
static const char *lmemfind (const char *s1, size_t l1,
676
6.19k
                               const char *s2, size_t l2) {
677
6.19k
  if (l2 == 0) return s1;  /* empty strings are everywhere */
678
5.31k
  else if (l2 > l1) return NULL;  /* avoids a negative 'l1' */
679
4.83k
  else {
680
4.83k
    const char *init;  /* to search for a '*s2' inside 's1' */
681
4.83k
    l2--;  /* 1st char will be checked by 'memchr' */
682
4.83k
    l1 = l1-l2;  /* 's2' cannot be found after that */
683
23.7k
    while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
684
19.5k
      init++;   /* 1st char is already checked */
685
19.5k
      if (memcmp(init, s2+1, l2) == 0)
686
636
        return init-1;
687
18.9k
      else {  /* correct 'l1' and 's1' to try again */
688
18.9k
        l1 -= ct_diff2sz(init - s1);
689
18.9k
        s1 = init;
690
18.9k
      }
691
19.5k
    }
692
4.20k
    return NULL;  /* not found */
693
4.83k
  }
694
6.19k
}
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
520k
                              const char *e, const char **cap) {
706
520k
  if (i >= ms->level) {
707
179k
    if (l_unlikely(i != 0))
708
2.49k
      luaL_error(ms->L, "invalid capture index %%%d", i + 1);
709
179k
    *cap = s;
710
179k
    return (e - s);
711
179k
  }
712
340k
  else {
713
340k
    ptrdiff_t capl = ms->capture[i].len;
714
340k
    *cap = ms->capture[i].init;
715
340k
    if (l_unlikely(capl == CAP_UNFINISHED))
716
1.01k
      luaL_error(ms->L, "unfinished capture");
717
339k
    else if (capl == CAP_POSITION)
718
271k
      lua_pushinteger(ms->L,
719
271k
          ct_diff2S(ms->capture[i].init - ms->src_init) + 1);
720
340k
    return capl;
721
340k
  }
722
520k
}
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
315k
                                                    const char *e) {
730
315k
  const char *cap;
731
315k
  ptrdiff_t l = get_onecapture(ms, i, s, e, &cap);
732
315k
  if (l != CAP_POSITION)
733
187k
    lua_pushlstring(ms->L, cap, cast_sizet(l));
734
  /* else position was already pushed */
735
315k
}
736
737
738
300k
static int push_captures (MatchState *ms, const char *s, const char *e) {
739
300k
  int i;
740
300k
  int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
741
300k
  luaL_checkstack(ms->L, nlevels, "too many captures");
742
609k
  for (i = 0; i < nlevels; i++)
743
309k
    push_onecapture(ms, i, s, e);
744
300k
  return nlevels;  /* number of strings pushed */
745
300k
}
746
747
748
/* check whether pattern has no special characters */
749
185k
static int nospecials (const char *p, size_t l) {
750
185k
  size_t upto = 0;
751
201k
  do {
752
201k
    if (strpbrk(p + upto, SPECIALS))
753
179k
      return 0;  /* pattern has a special character */
754
21.8k
    upto += strlen(p + upto) + 1;  /* may have more after \0 */
755
21.8k
  } while (upto <= l);
756
6.15k
  return 1;  /* no special chars found */
757
185k
}
758
759
760
static void prepstate (MatchState *ms, lua_State *L,
761
678k
                       const char *s, size_t ls, const char *p, size_t lp) {
762
678k
  ms->L = L;
763
678k
  ms->matchdepth = MAXCCALLS;
764
678k
  ms->src_init = s;
765
678k
  ms->src_end = s + ls;
766
678k
  ms->p_end = p + lp;
767
678k
}
768
769
770
12.9M
static void reprepstate (MatchState *ms) {
771
12.9M
  ms->level = 0;
772
12.9M
  lua_assert(ms->matchdepth == MAXCCALLS);
773
12.9M
}
774
775
776
204k
static int str_find_aux (lua_State *L, int find) {
777
204k
  size_t ls, lp;
778
204k
  const char *s = luaL_checklstring(L, 1, &ls);
779
204k
  const char *p = luaL_checklstring(L, 2, &lp);
780
204k
  size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
781
204k
  if (init > ls) {  /* start after string's end? */
782
8.18k
    luaL_pushfail(L);  /* cannot find anything */
783
8.18k
    return 1;
784
8.18k
  }
785
  /* explicit request or no special characters? */
786
195k
  if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
787
    /* do a plain search */
788
6.19k
    const char *s2 = lmemfind(s + init, ls - init, p, lp);
789
6.19k
    if (s2) {
790
1.51k
      lua_pushinteger(L, ct_diff2S(s2 - s) + 1);
791
1.51k
      lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp));
792
1.51k
      return 2;
793
1.51k
    }
794
6.19k
  }
795
189k
  else {
796
189k
    MatchState ms;
797
189k
    const char *s1 = s + init;
798
189k
    int anchor = (*p == '^');
799
189k
    if (anchor) {
800
1.83k
      p++; lp--;  /* skip anchor character */
801
1.83k
    }
802
189k
    prepstate(&ms, L, s, ls, p, lp);
803
4.89M
    do {
804
4.89M
      const char *res;
805
4.89M
      reprepstate(&ms);
806
4.89M
      if ((res=match(&ms, s1, p)) != NULL) {
807
174k
        if (find) {
808
171k
          lua_pushinteger(L, ct_diff2S(s1 - s) + 1);  /* start */
809
171k
          lua_pushinteger(L, ct_diff2S(res - s));   /* end */
810
171k
          return push_captures(&ms, NULL, 0) + 2;
811
171k
        }
812
2.86k
        else
813
2.86k
          return push_captures(&ms, s1, res);
814
174k
      }
815
4.89M
    } while (s1++ < ms.src_end && !anchor);
816
189k
  }
817
20.2k
  luaL_pushfail(L);  /* not found */
818
20.2k
  return 1;
819
195k
}
820
821
822
186k
static int str_find (lua_State *L) {
823
186k
  return str_find_aux(L, 1);
824
186k
}
825
826
827
17.8k
static int str_match (lua_State *L) {
828
17.8k
  return str_find_aux(L, 0);
829
17.8k
}
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
9.94k
static int gmatch_aux (lua_State *L) {
842
9.94k
  GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3));
843
9.94k
  const char *src;
844
9.94k
  gm->ms.L = L;
845
398k
  for (src = gm->src; src <= gm->ms.src_end; src++) {
846
391k
    const char *e;
847
391k
    reprepstate(&gm->ms);
848
391k
    if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) {
849
2.36k
      gm->src = gm->lastmatch = e;
850
2.36k
      return push_captures(&gm->ms, src, e);
851
2.36k
    }
852
391k
  }
853
7.57k
  return 0;  /* not found */
854
9.94k
}
855
856
857
39.8k
static int gmatch (lua_State *L) {
858
39.8k
  size_t ls, lp;
859
39.8k
  const char *s = luaL_checklstring(L, 1, &ls);
860
39.8k
  const char *p = luaL_checklstring(L, 2, &lp);
861
39.8k
  size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
862
39.8k
  GMatchState *gm;
863
39.8k
  lua_settop(L, 2);  /* keep strings on closure to avoid being collected */
864
39.8k
  gm = (GMatchState *)lua_newuserdatauv(L, sizeof(GMatchState), 0);
865
39.8k
  if (init > ls)  /* start after string's end? */
866
18.9k
    init = ls + 1;  /* avoid overflows in 's + init' */
867
39.8k
  prepstate(&gm->ms, L, s, ls, p, lp);
868
39.8k
  gm->src = s + init; gm->p = p; gm->lastmatch = NULL;
869
39.8k
  lua_pushcclosure(L, gmatch_aux, 3);
870
39.8k
  return 1;
871
39.8k
}
872
873
874
static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
875
234k
                                                   const char *e) {
876
234k
  size_t l;
877
234k
  lua_State *L = ms->L;
878
234k
  const char *news = lua_tolstring(L, 3, &l);
879
234k
  const char *p;
880
4.17M
  while ((p = (char *)memchr(news, L_ESC, l)) != NULL) {
881
3.93M
    luaL_addlstring(b, news, ct_diff2sz(p - news));
882
3.93M
    p++;  /* skip ESC */
883
3.93M
    if (*p == L_ESC)  /* '%%' */
884
3.67M
      luaL_addchar(b, *p);
885
259k
    else if (*p == '0')  /* '%0' */
886
53.0k
        luaL_addlstring(b, s, ct_diff2sz(e - s));
887
206k
    else if (isdigit(cast_uchar(*p))) {  /* '%n' */
888
204k
      const char *cap;
889
204k
      ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap);
890
204k
      if (resl == CAP_POSITION)
891
144k
        luaL_addvalue(b);  /* add position to accumulated result */
892
60.2k
      else
893
60.2k
        luaL_addlstring(b, cap, cast_sizet(resl));
894
204k
    }
895
1.76k
    else
896
1.76k
      luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
897
3.93M
    l -= ct_diff2sz(p + 1 - news);
898
3.93M
    news = p + 1;
899
3.93M
  }
900
234k
  luaL_addlstring(b, news, l);
901
234k
}
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
364k
                                      const char *e, int tr) {
911
364k
  lua_State *L = ms->L;
912
364k
  switch (tr) {
913
123k
    case LUA_TFUNCTION: {  /* call the function */
914
123k
      int n;
915
123k
      lua_pushvalue(L, 3);  /* push the function */
916
123k
      n = push_captures(ms, s, e);  /* all captures as arguments */
917
123k
      lua_call(L, n, 1);  /* call it */
918
123k
      break;
919
0
    }
920
6.10k
    case LUA_TTABLE: {  /* index the table */
921
6.10k
      push_onecapture(ms, 0, s, e);  /* first capture is the index */
922
6.10k
      lua_gettable(L, 3);
923
6.10k
      break;
924
0
    }
925
234k
    default: {  /* LUA_TNUMBER or LUA_TSTRING */
926
234k
      add_s(ms, b, s, e);  /* add value to the buffer */
927
234k
      return 1;  /* something changed */
928
0
    }
929
364k
  }
930
129k
  if (!lua_toboolean(L, -1)) {  /* nil or false? */
931
6.97k
    lua_pop(L, 1);  /* remove value */
932
6.97k
    luaL_addlstring(b, s, ct_diff2sz(e - s));  /* keep original text */
933
6.97k
    return 0;  /* no changes */
934
6.97k
  }
935
122k
  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
122k
  else {
939
122k
    luaL_addvalue(b);  /* add result to accumulator */
940
122k
    return 1;  /* something changed */
941
122k
  }
942
129k
}
943
944
945
450k
static int str_gsub (lua_State *L) {
946
450k
  size_t srcl, lp;
947
450k
  const char *src = luaL_checklstring(L, 1, &srcl);  /* subject */
948
450k
  const char *p = luaL_checklstring(L, 2, &lp);  /* pattern */
949
450k
  const char *lastmatch = NULL;  /* end of last match */
950
450k
  int tr = lua_type(L, 3);  /* replacement type */
951
  /* max replacements */
952
450k
  lua_Integer max_s = luaL_optinteger(L, 4, cast_st2S(srcl) + 1);
953
450k
  int anchor = (*p == '^');
954
450k
  lua_Integer n = 0;  /* replacement count */
955
450k
  int changed = 0;  /* change flag */
956
450k
  MatchState ms;
957
450k
  luaL_Buffer b;
958
450k
  luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
959
450k
                   tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
960
450k
                      "string/function/table");
961
450k
  luaL_buffinit(L, &b);
962
450k
  if (anchor) {
963
7.88k
    p++; lp--;  /* skip anchor character */
964
7.88k
  }
965
450k
  prepstate(&ms, L, src, srcl, p, lp);
966
7.67M
  while (n < max_s) {
967
7.64M
    const char *e;
968
7.64M
    reprepstate(&ms);  /* (re)prepare state for new match */
969
7.64M
    if ((e = match(&ms, src, p)) != NULL && e != lastmatch) {  /* match? */
970
364k
      n++;
971
364k
      changed = add_value(&ms, &b, src, e, tr) || changed;
972
364k
      src = lastmatch = e;
973
364k
    }
974
7.28M
    else if (src < ms.src_end)  /* otherwise, skip one character */
975
6.86M
      luaL_addchar(&b, *src++);
976
420k
    else break;  /* end of subject */
977
7.22M
    if (anchor) break;
978
7.22M
  }
979
450k
  if (!changed)  /* no changes? */
980
418k
    lua_pushvalue(L, 1);  /* return original string */
981
31.8k
  else {  /* something changed */
982
31.8k
    luaL_addlstring(&b, src, ct_diff2sz(ms.src_end - src));
983
31.8k
    luaL_pushresult(&b);  /* create and return new string */
984
31.8k
  }
985
450k
  lua_pushinteger(L, n);  /* number of substitutions */
986
450k
  return 2;
987
450k
}
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
1.22k
#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
766k
#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
1.09M
#define L_FMTFLAGSF "-+#0 "
1101
1102
/* valid flags for o, x, and X conversions */
1103
22.4k
#define L_FMTFLAGSX "-#0"
1104
1105
/* valid flags for d and i conversions */
1106
11.6k
#define L_FMTFLAGSI "-+0 "
1107
1108
/* valid flags for u conversions */
1109
329
#define L_FMTFLAGSU "-0"
1110
1111
/* valid flags for c, p, and s conversions */
1112
36.9k
#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
709k
#define MAX_FORMAT  32
1124
1125
1126
122k
static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
1127
122k
  luaL_addchar(b, '"');
1128
457M
  while (len--) {
1129
457M
    if (*s == '"' || *s == '\\' || *s == '\n') {
1130
1.58M
      luaL_addchar(b, '\\');
1131
1.58M
      luaL_addchar(b, *s);
1132
1.58M
    }
1133
456M
    else if (iscntrl(cast_uchar(*s))) {
1134
5.52M
      char buff[10];
1135
5.52M
      if (!isdigit(cast_uchar(*(s+1))))
1136
5.20M
        l_sprintf(buff, sizeof(buff), "\\%d", (int)cast_uchar(*s));
1137
318k
      else
1138
318k
        l_sprintf(buff, sizeof(buff), "\\%03d", (int)cast_uchar(*s));
1139
5.52M
      luaL_addstring(b, buff);
1140
5.52M
    }
1141
450M
    else
1142
450M
      luaL_addchar(b, *s);
1143
457M
    s++;
1144
457M
  }
1145
122k
  luaL_addchar(b, '"');
1146
122k
}
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.8k
static int quotefloat (lua_State *L, char *buff, lua_Number n) {
1156
25.8k
  const char *s;  /* for the fixed representations */
1157
25.8k
  if (n == (lua_Number)HUGE_VAL)  /* inf? */
1158
7.07k
    s = "1e9999";
1159
18.7k
  else if (n == -(lua_Number)HUGE_VAL)  /* -inf? */
1160
4.17k
    s = "-1e9999";
1161
14.6k
  else if (n != n)  /* NaN? */
1162
2.77k
    s = "(0/0)";
1163
11.8k
  else {  /* format number as hexadecimal */
1164
11.8k
    int  nb = lua_number2strx(L, buff, MAX_ITEM,
1165
11.8k
                                 "%" LUA_NUMBER_FRMLEN "a", n);
1166
    /* ensures that 'buff' string uses a dot as the radix character */
1167
11.8k
    if (memchr(buff, '.', cast_uint(nb)) == NULL) {  /* no dot? */
1168
1.99k
      char point = lua_getlocaledecpoint();  /* try locale point */
1169
1.99k
      char *ppoint = (char *)memchr(buff, point, cast_uint(nb));
1170
1.99k
      if (ppoint) *ppoint = '.';  /* change it to a dot */
1171
1.99k
    }
1172
11.8k
    return nb;
1173
11.8k
  }
1174
  /* for the fixed representations */
1175
14.0k
  return l_sprintf(buff, MAX_ITEM, "%s", s);
1176
25.8k
}
1177
1178
1179
154k
static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
1180
154k
  switch (lua_type(L, arg)) {
1181
122k
    case LUA_TSTRING: {
1182
122k
      size_t len;
1183
122k
      const char *s = lua_tolstring(L, arg, &len);
1184
122k
      addquoted(b, s, len);
1185
122k
      break;
1186
0
    }
1187
27.1k
    case LUA_TNUMBER: {
1188
27.1k
      char *buff = luaL_prepbuffsize(b, MAX_ITEM);
1189
27.1k
      int nb;
1190
27.1k
      if (!lua_isinteger(L, arg))  /* float? */
1191
25.8k
        nb = quotefloat(L, buff, lua_tonumber(L, arg));
1192
1.24k
      else {  /* integers */
1193
1.24k
        lua_Integer n = lua_tointeger(L, arg);
1194
1.24k
        const char *format = (n == LUA_MININTEGER)  /* corner case? */
1195
1.24k
                           ? "0x%" LUA_INTEGER_FRMLEN "x"  /* use hex */
1196
1.24k
                           : LUA_INTEGER_FMT;  /* else use default format */
1197
1.24k
        nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n);
1198
1.24k
      }
1199
27.1k
      luaL_addsize(b, cast_uint(nb));
1200
27.1k
      break;
1201
0
    }
1202
5.48k
    case LUA_TNIL: case LUA_TBOOLEAN: {
1203
5.48k
      luaL_tolstring(L, arg, NULL);
1204
5.48k
      luaL_addvalue(b);
1205
5.48k
      break;
1206
2.66k
    }
1207
0
    default: {
1208
0
      luaL_argerror(L, arg, "value has no literal form");
1209
0
    }
1210
154k
  }
1211
154k
}
1212
1213
1214
794k
static const char *get2digits (const char *s) {
1215
794k
  if (isdigit(cast_uchar(*s))) {
1216
519k
    s++;
1217
519k
    if (isdigit(cast_uchar(*s))) s++;  /* (2 digits at most) */
1218
519k
  }
1219
794k
  return s;
1220
794k
}
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
428k
                                       int precision) {
1231
428k
  const char *spec = form + 1;  /* skip '%' */
1232
428k
  spec += strspn(spec, flags);  /* skip flags */
1233
428k
  if (*spec != '0') {  /* a width cannot start with '0' */
1234
426k
    spec = get2digits(spec);  /* skip width */
1235
426k
    if (*spec == '.' && precision) {
1236
367k
      spec++;
1237
367k
      spec = get2digits(spec);  /* skip precision */
1238
367k
    }
1239
426k
  }
1240
428k
  if (!isalpha(cast_uchar(*spec)))  /* did not go to the end? */
1241
6.90k
    luaL_error(L, "invalid conversion specification: '%s'", form);
1242
428k
}
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
709k
                                            char *form) {
1251
  /* spans flags, width, and precision ('0' is included as a flag) */
1252
709k
  size_t len = strspn(strfrmt, L_FMTFLAGSF "123456789.");
1253
709k
  len++;  /* adds following character (should be the specifier) */
1254
  /* still needs space for '%', '\0', plus a length modifier */
1255
709k
  if (len >= MAX_FORMAT - 10)
1256
4.45k
    luaL_error(L, "invalid format (too long)");
1257
709k
  *(form++) = '%';
1258
709k
  memcpy(form, strfrmt, len * sizeof(char));
1259
709k
  *(form + len) = '\0';
1260
709k
  return strfrmt + len - 1;
1261
709k
}
1262
1263
1264
/*
1265
** add length modifier into formats
1266
*/
1267
393k
static void addlenmod (char *form, const char *lenmod) {
1268
393k
  size_t l = strlen(form);
1269
393k
  size_t lm = strlen(lenmod);
1270
393k
  char spec = form[l - 1];
1271
393k
  strcpy(form + l - 1, lenmod);
1272
393k
  form[l + lm - 1] = spec;
1273
393k
  form[l + lm] = '\0';
1274
393k
}
1275
1276
1277
667k
static int str_format (lua_State *L) {
1278
667k
  int top = lua_gettop(L);
1279
667k
  int arg = 1;
1280
667k
  size_t sfl;
1281
667k
  const char *strfrmt = luaL_checklstring(L, arg, &sfl);
1282
667k
  const char *strfrmt_end = strfrmt+sfl;
1283
667k
  const char *flags;
1284
667k
  luaL_Buffer b;
1285
667k
  luaL_buffinit(L, &b);
1286
52.1M
  while (strfrmt < strfrmt_end) {
1287
51.5M
    if (*strfrmt != L_ESC)
1288
50.6M
      luaL_addchar(&b, *strfrmt++);
1289
921k
    else if (*++strfrmt == L_ESC)
1290
181k
      luaL_addchar(&b, *strfrmt++);  /* %% */
1291
739k
    else { /* format item */
1292
739k
      char form[MAX_FORMAT];  /* to store the format ('%...') */
1293
739k
      unsigned maxitem = MAX_ITEM;  /* maximum length for the result */
1294
739k
      char *buff = luaL_prepbuffsize(&b, maxitem);  /* to put result */
1295
739k
      int nb = 0;  /* number of bytes in result */
1296
739k
      if (++arg > top)
1297
30.8k
        return luaL_argerror(L, arg, "no value");
1298
709k
      strfrmt = getformat(L, strfrmt, form);
1299
709k
      switch (*strfrmt++) {
1300
2.73k
        case 'c': {
1301
2.73k
          checkformat(L, form, L_FMTFLAGSC, 0);
1302
2.73k
          nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg));
1303
2.73k
          break;
1304
0
        }
1305
11.6k
        case 'd': case 'i':
1306
11.6k
          flags = L_FMTFLAGSI;
1307
11.6k
          goto intcase;
1308
329
        case 'u':
1309
329
          flags = L_FMTFLAGSU;
1310
329
          goto intcase;
1311
22.4k
        case 'o': case 'x': case 'X':
1312
22.4k
          flags = L_FMTFLAGSX;
1313
34.4k
         intcase: {
1314
34.4k
          lua_Integer n = luaL_checkinteger(L, arg);
1315
34.4k
          checkformat(L, form, flags, 1);
1316
34.4k
          addlenmod(form, LUA_INTEGER_FRMLEN);
1317
34.4k
          nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n);
1318
34.4k
          break;
1319
22.4k
        }
1320
8.12k
        case 'a': case 'A':
1321
8.12k
          checkformat(L, form, L_FMTFLAGSF, 1);
1322
8.12k
          addlenmod(form, LUA_NUMBER_FRMLEN);
1323
8.12k
          nb = lua_number2strx(L, buff, maxitem, form,
1324
8.12k
                                  luaL_checknumber(L, arg));
1325
8.12k
          break;
1326
1.22k
        case 'f':
1327
1.22k
          maxitem = MAX_ITEMF;  /* extra space for '%f' */
1328
1.22k
          buff = luaL_prepbuffsize(&b, maxitem);
1329
          /* FALLTHROUGH */
1330
377k
        case 'e': case 'E': case 'g': case 'G': {
1331
377k
          lua_Number n = luaL_checknumber(L, arg);
1332
377k
          checkformat(L, form, L_FMTFLAGSF, 1);
1333
377k
          addlenmod(form, LUA_NUMBER_FRMLEN);
1334
377k
          nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n);
1335
377k
          break;
1336
367k
        }
1337
13.3k
        case 'p': {
1338
13.3k
          const void *p = lua_topointer(L, arg);
1339
13.3k
          checkformat(L, form, L_FMTFLAGSC, 0);
1340
13.3k
          if (p == NULL) {  /* avoid calling 'printf' with argument NULL */
1341
6.97k
            p = "(null)";  /* result */
1342
6.97k
            form[strlen(form) - 1] = 's';  /* format it as a string */
1343
6.97k
          }
1344
13.3k
          nb = l_sprintf(buff, maxitem, form, p);
1345
13.3k
          break;
1346
367k
        }
1347
154k
        case 'q': {
1348
154k
          if (form[2] != '\0')  /* modifiers? */
1349
30
            return luaL_error(L, "specifier '%%q' cannot have modifiers");
1350
154k
          addliteral(L, &b, arg);
1351
154k
          break;
1352
154k
        }
1353
95.6k
        case 's': {
1354
95.6k
          size_t l;
1355
95.6k
          const char *s = luaL_tolstring(L, arg, &l);
1356
95.6k
          if (form[2] == '\0')  /* no modifiers? */
1357
74.8k
            luaL_addvalue(&b);  /* keep entire string */
1358
20.8k
          else {
1359
20.8k
            luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
1360
20.8k
            checkformat(L, form, L_FMTFLAGSC, 1);
1361
20.8k
            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.4k
            else {  /* format the string into 'buff' */
1366
17.4k
              nb = l_sprintf(buff, maxitem, form, s);
1367
17.4k
              lua_pop(L, 1);  /* remove result from 'luaL_tolstring' */
1368
17.4k
            }
1369
20.8k
          }
1370
95.6k
          break;
1371
154k
        }
1372
18.1k
        default: {  /* also treat cases 'pnLlh' */
1373
18.1k
          return luaL_error(L, "invalid conversion '%s' to 'format'", form);
1374
154k
        }
1375
709k
      }
1376
643k
      lua_assert(cast_uint(nb) < maxitem);
1377
643k
      luaL_addsize(&b, cast_uint(nb));
1378
643k
    }
1379
51.5M
  }
1380
571k
  luaL_pushresult(&b);
1381
571k
  return 1;
1382
667k
}
1383
1384
/* }====================================================== */
1385
1386
1387
/*
1388
** {======================================================
1389
** PACK/UNPACK
1390
** =======================================================
1391
*/
1392
1393
1394
/* value used for padding */
1395
#if !defined(LUAL_PACKPADBYTE)
1396
478k
#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
3.03M
#define NB  CHAR_BIT
1404
1405
/* mask for one character (NB 1's) */
1406
866k
#define MC  ((1 << NB) - 1)
1407
1408
/* size of a lua_Integer */
1409
649k
#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
3.04M
static int digit (int c) { return '0' <= c && c <= '9'; }
1452
1453
753k
static size_t getnum (const char **fmt, size_t df) {
1454
753k
  if (!digit(**fmt))  /* no number? */
1455
161k
    return df;  /* return default value */
1456
592k
  else {
1457
592k
    size_t a = 0;
1458
2.29M
    do {
1459
2.29M
      a = a*10 + cast_uint(*((*fmt)++) - '0');
1460
2.29M
    } while (digit(**fmt) && a <= (MAX_SIZE - 9)/10);
1461
592k
    return a;
1462
592k
  }
1463
753k
}
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
269k
static unsigned getnumlimit (Header *h, const char **fmt, size_t df) {
1471
269k
  size_t sz = getnum(fmt, df);
1472
269k
  if (l_unlikely((sz - 1u) >= MAXINTSIZE))
1473
2.70k
    return cast_uint(luaL_error(h->L,
1474
266k
               "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE));
1475
266k
  return cast_uint(sz);
1476
269k
}
1477
1478
1479
/*
1480
** Initialize Header
1481
*/
1482
697k
static void initheader (lua_State *L, Header *h) {
1483
697k
  h->L = L;
1484
697k
  h->islittle = nativeendian.little;
1485
697k
  h->maxalign = 1;
1486
697k
}
1487
1488
1489
/*
1490
** Read and classify next option. 'size' is filled with option's size.
1491
*/
1492
3.91M
static KOption getoption (Header *h, const char **fmt, size_t *size) {
1493
  /* dummy structure to get native alignment requirements */
1494
3.91M
  struct cD { char c; union { LUAI_MAXALIGN; } u; };
1495
3.91M
  int opt = *((*fmt)++);
1496
3.91M
  *size = 0;  /* default */
1497
3.91M
  switch (opt) {
1498
5.10k
    case 'b': *size = sizeof(char); return Kint;
1499
4.65k
    case 'B': *size = sizeof(char); return Kuint;
1500
271
    case 'h': *size = sizeof(short); return Kint;
1501
7.31k
    case 'H': *size = sizeof(short); return Kuint;
1502
147k
    case 'l': *size = sizeof(long); return Kint;
1503
7.43k
    case 'L': *size = sizeof(long); return Kuint;
1504
35.6k
    case 'j': *size = sizeof(lua_Integer); return Kint;
1505
574
    case 'J': *size = sizeof(lua_Integer); return Kuint;
1506
2
    case 'T': *size = sizeof(size_t); return Kuint;
1507
77.3k
    case 'f': *size = sizeof(float); return Kfloat;
1508
10.0k
    case 'n': *size = sizeof(lua_Number); return Knumber;
1509
4.03k
    case 'd': *size = sizeof(double); return Kdouble;
1510
104k
    case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint;
1511
8.13k
    case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint;
1512
16.1k
    case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring;
1513
484k
    case 'c':
1514
484k
      *size = getnum(fmt, cast_sizet(-1));
1515
484k
      if (l_unlikely(*size == cast_sizet(-1)))
1516
861
        luaL_error(h->L, "missing size for format option 'c'");
1517
484k
      return Kchar;
1518
1.10k
    case 'z': return Kzstr;
1519
2.70M
    case 'x': *size = 1; return Kpadding;
1520
16.9k
    case 'X': return Kpaddalign;
1521
16.8k
    case ' ': break;
1522
3.31k
    case '<': h->islittle = 1; break;
1523
104k
    case '>': h->islittle = 0; break;
1524
7.93k
    case '=': h->islittle = nativeendian.little; break;
1525
140k
    case '!': {
1526
140k
      const size_t maxalign = offsetof(struct cD, u);
1527
140k
      h->maxalign = getnumlimit(h, fmt, maxalign);
1528
140k
      break;
1529
0
    }
1530
4.33k
    default: luaL_error(h->L, "invalid format option '%c'", opt);
1531
3.91M
  }
1532
272k
  return Knop;
1533
3.91M
}
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.89M
                           size_t *psize, unsigned *ntoalign) {
1547
3.89M
  KOption opt = getoption(h, fmt, psize);
1548
3.89M
  size_t align = *psize;  /* usually, alignment follows size */
1549
3.89M
  if (opt == Kpaddalign) {  /* 'X' gets alignment from following option */
1550
16.9k
    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.9k
  }
1553
3.89M
  if (align <= 1 || opt == Kchar)  /* need no alignment? */
1554
3.47M
    *ntoalign = 0;
1555
418k
  else {
1556
418k
    if (align > h->maxalign)  /* enforce maximum alignment */
1557
394k
      align = h->maxalign;
1558
418k
    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
416k
    else {
1563
      /* 'szmoda' = totalsize % align */
1564
416k
      unsigned szmoda = cast_uint(totalsize & (align - 1));
1565
416k
      *ntoalign = cast_uint((align - szmoda) & (align - 1));
1566
416k
    }
1567
418k
  }
1568
3.89M
  return opt;
1569
3.89M
}
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
83.0k
                     int islittle, unsigned size, int neg) {
1580
83.0k
  char *buff = luaL_prepbuffsize(b, size);
1581
83.0k
  unsigned i;
1582
83.0k
  buff[islittle ? 0 : size - 1] = (char)(n & MC);  /* first byte */
1583
857k
  for (i = 1; i < size; i++) {
1584
774k
    n >>= NB;
1585
774k
    buff[islittle ? i : size - 1 - i] = (char)(n & MC);
1586
774k
  }
1587
83.0k
  if (neg && size > SZINT) {  /* negative number need sign extension? */
1588
1.09k
    for (i = SZINT; i < size; i++)  /* correct extra bytes */
1589
929
      buff[islittle ? i : size - 1 - i] = (char)MC;
1590
163
  }
1591
83.0k
  luaL_addsize(b, size);  /* add result to buffer */
1592
83.0k
}
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.1k
                            unsigned size, int islittle) {
1601
14.1k
  if (islittle == nativeendian.little)
1602
6.89k
    memcpy(dest, src, size);
1603
7.29k
  else {
1604
7.29k
    dest += size - 1;
1605
56.7k
    while (size-- != 0)
1606
49.4k
      *(dest--) = *(src++);
1607
7.29k
  }
1608
14.1k
}
1609
1610
1611
664k
static int str_pack (lua_State *L) {
1612
664k
  luaL_Buffer b;
1613
664k
  Header h;
1614
664k
  const char *fmt = luaL_checkstring(L, 1);  /* format string */
1615
664k
  int arg = 1;  /* current argument to pack */
1616
664k
  size_t totalsize = 0;  /* accumulate total size of result */
1617
664k
  initheader(L, &h);
1618
664k
  lua_pushnil(L);  /* mark to separate arguments from string buffer */
1619
664k
  luaL_buffinit(L, &b);
1620
4.17M
  while (*fmt != '\0') {
1621
3.59M
    unsigned ntoalign;
1622
3.59M
    size_t size;
1623
3.59M
    KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
1624
3.59M
    luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg,
1625
3.59M
                     "result too long");
1626
3.59M
    totalsize += ntoalign + size;
1627
3.63M
    while (ntoalign-- > 0)
1628
39.9k
     luaL_addchar(&b, LUAL_PACKPADBYTE);  /* fill alignment */
1629
3.59M
    arg++;
1630
3.59M
    switch (opt) {
1631
82.7k
      case Kint: {  /* signed integers */
1632
82.7k
        lua_Integer n = luaL_checkinteger(L, arg);
1633
82.7k
        if (size < SZINT) {  /* need overflow check? */
1634
2.66k
          lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
1635
2.66k
          luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
1636
2.66k
        }
1637
82.7k
        packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), (n < 0));
1638
82.7k
        break;
1639
0
      }
1640
2.40k
      case Kuint: {  /* unsigned integers */
1641
2.40k
        lua_Integer n = luaL_checkinteger(L, arg);
1642
2.40k
        if (size < SZINT)  /* need overflow check? */
1643
1.67k
          luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
1644
2.40k
                           arg, "unsigned overflow");
1645
2.40k
        packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), 0);
1646
2.40k
        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
2.01k
      case Knumber: {  /* Lua float */
1657
2.01k
        lua_Number f = luaL_checknumber(L, arg);  /* get argument */
1658
2.01k
        char *buff = luaL_prepbuffsize(&b, sizeof(f));
1659
        /* move 'f' to final result, correcting endianness if needed */
1660
2.01k
        copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
1661
2.01k
        luaL_addsize(&b, size);
1662
2.01k
        break;
1663
0
      }
1664
568
      case Kdouble: {  /* C double */
1665
568
        double f = (double)luaL_checknumber(L, arg);  /* get argument */
1666
568
        char *buff = luaL_prepbuffsize(&b, sizeof(f));
1667
        /* move 'f' to final result, correcting endianness if needed */
1668
568
        copywithendian(buff, (char *)&f, sizeof(f), h.islittle);
1669
568
        luaL_addsize(&b, size);
1670
568
        break;
1671
0
      }
1672
478k
      case Kchar: {  /* fixed-size string */
1673
478k
        size_t len;
1674
478k
        const char *s = luaL_checklstring(L, arg, &len);
1675
478k
        luaL_argcheck(L, len <= size, arg, "string longer than given size");
1676
478k
        luaL_addlstring(&b, s, len);  /* add string */
1677
478k
        if (len < size) {  /* does it need padding? */
1678
478k
          size_t psize = size - len;  /* pad size */
1679
478k
          char *buff = luaL_prepbuffsize(&b, psize);
1680
478k
          memset(buff, LUAL_PACKPADBYTE, psize);
1681
478k
          luaL_addsize(&b, psize);
1682
478k
        }
1683
478k
        break;
1684
0
      }
1685
4.56k
      case Kstring: {  /* strings with length count */
1686
4.56k
        size_t len;
1687
4.56k
        const char *s = luaL_checklstring(L, arg, &len);
1688
4.56k
        luaL_argcheck(L, size >= sizeof(lua_Unsigned) ||
1689
4.56k
                         len < ((lua_Unsigned)1 << (size * NB)),
1690
4.56k
                         arg, "string length does not fit in given size");
1691
        /* pack length */
1692
4.56k
        packint(&b, (lua_Unsigned)len, h.islittle, cast_uint(size), 0);
1693
4.56k
        luaL_addlstring(&b, s, len);
1694
4.56k
        totalsize += len;
1695
4.56k
        break;
1696
0
      }
1697
280
      case Kzstr: {  /* zero-terminated string */
1698
280
        size_t len;
1699
280
        const char *s = luaL_checklstring(L, arg, &len);
1700
280
        luaL_argcheck(L, strlen(s) == len, arg, "string contains zeros");
1701
280
        luaL_addlstring(&b, s, len);
1702
280
        luaL_addchar(&b, '\0');  /* add zero at the end */
1703
280
        totalsize += len + 1;
1704
280
        break;
1705
0
      }
1706
2.69M
      case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE);  /* FALLTHROUGH */
1707
2.94M
      case Kpaddalign: case Knop:
1708
2.94M
        arg--;  /* undo increment */
1709
2.94M
        break;
1710
3.59M
    }
1711
3.59M
  }
1712
577k
  luaL_pushresult(&b);
1713
577k
  return 1;
1714
664k
}
1715
1716
1717
1.72k
static int str_packsize (lua_State *L) {
1718
1.72k
  Header h;
1719
1.72k
  const char *fmt = luaL_checkstring(L, 1);  /* format string */
1720
1.72k
  size_t totalsize = 0;  /* accumulate total size of result */
1721
1.72k
  initheader(L, &h);
1722
53.5k
  while (*fmt != '\0') {
1723
51.8k
    unsigned ntoalign;
1724
51.8k
    size_t size;
1725
51.8k
    KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
1726
51.8k
    luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1,
1727
51.8k
                     "variable-length format");
1728
51.8k
    size += ntoalign;  /* total space used by option */
1729
51.8k
    luaL_argcheck(L, totalsize <= MAX_SIZE - size,
1730
51.8k
                     1, "format result too large");
1731
51.8k
    totalsize += size;
1732
51.8k
  }
1733
1.72k
  lua_pushinteger(L, cast_st2S(totalsize));
1734
1.72k
  return 1;
1735
1.72k
}
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
188k
                              int islittle, int size, int issigned) {
1748
188k
  lua_Unsigned res = 0;
1749
188k
  int i;
1750
188k
  int limit = (size  <= SZINT) ? size : SZINT;
1751
1.57M
  for (i = limit - 1; i >= 0; i--) {
1752
1.38M
    res <<= NB;
1753
1.38M
    res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i];
1754
1.38M
  }
1755
188k
  if (size < SZINT) {  /* real size smaller than lua_Integer? */
1756
21.1k
    if (issigned) {  /* needs sign extension? */
1757
10.5k
      lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
1758
10.5k
      res = ((res ^ mask) - mask);  /* do sign extension */
1759
10.5k
    }
1760
21.1k
  }
1761
167k
  else if (size > SZINT) {  /* must check unread bytes */
1762
17.3k
    int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
1763
90.5k
    for (i = limit; i < size; i++) {
1764
73.1k
      if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask))
1765
4.73k
        luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
1766
73.1k
    }
1767
17.3k
  }
1768
188k
  return (lua_Integer)res;
1769
188k
}
1770
1771
1772
31.1k
static int str_unpack (lua_State *L) {
1773
31.1k
  Header h;
1774
31.1k
  const char *fmt = luaL_checkstring(L, 1);
1775
31.1k
  size_t ld;
1776
31.1k
  const char *data = luaL_checklstring(L, 2, &ld);
1777
31.1k
  size_t pos = posrelatI(luaL_optinteger(L, 3, 1), ld) - 1;
1778
31.1k
  int n = 0;  /* number of results */
1779
31.1k
  luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
1780
31.1k
  initheader(L, &h);
1781
267k
  while (*fmt != '\0') {
1782
250k
    unsigned ntoalign;
1783
250k
    size_t size;
1784
250k
    KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
1785
250k
    luaL_argcheck(L, ntoalign + size <= ld - pos, 2,
1786
250k
                    "data string too short");
1787
250k
    pos += ntoalign;  /* skip alignment */
1788
    /* stack space for item + next position */
1789
250k
    luaL_checkstack(L, 2, "too many results");
1790
250k
    n++;
1791
250k
    switch (opt) {
1792
164k
      case Kint:
1793
179k
      case Kuint: {
1794
179k
        lua_Integer res = unpackint(L, data + pos, h.islittle,
1795
179k
                                       cast_int(size), (opt == Kint));
1796
179k
        lua_pushinteger(L, res);
1797
179k
        break;
1798
164k
      }
1799
5.79k
      case Kfloat: {
1800
5.79k
        float f;
1801
5.79k
        copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
1802
5.79k
        lua_pushnumber(L, (lua_Number)f);
1803
5.79k
        break;
1804
164k
      }
1805
2.65k
      case Knumber: {
1806
2.65k
        lua_Number f;
1807
2.65k
        copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
1808
2.65k
        lua_pushnumber(L, f);
1809
2.65k
        break;
1810
164k
      }
1811
2.09k
      case Kdouble: {
1812
2.09k
        double f;
1813
2.09k
        copywithendian((char *)&f, data + pos, sizeof(f), h.islittle);
1814
2.09k
        lua_pushnumber(L, (lua_Number)f);
1815
2.09k
        break;
1816
164k
      }
1817
4.10k
      case Kchar: {
1818
4.10k
        lua_pushlstring(L, data + pos, size);
1819
4.10k
        break;
1820
164k
      }
1821
9.31k
      case Kstring: {
1822
9.31k
        lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos,
1823
9.31k
                                          h.islittle, cast_int(size), 0);
1824
9.31k
        luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
1825
9.31k
        lua_pushlstring(L, data + pos + size, cast_sizet(len));
1826
9.31k
        pos += cast_sizet(len);  /* skip string */
1827
9.31k
        break;
1828
164k
      }
1829
821
      case Kzstr: {
1830
821
        size_t len = strlen(data + pos);
1831
821
        luaL_argcheck(L, pos + len < ld, 2,
1832
821
                         "unfinished string for format 'z'");
1833
821
        lua_pushlstring(L, data + pos, len);
1834
821
        pos += len + 1;  /* skip string plus final '\0' */
1835
821
        break;
1836
164k
      }
1837
42.3k
      case Kpaddalign: case Kpadding: case Knop:
1838
42.3k
        n--;  /* undo increment */
1839
42.3k
        break;
1840
250k
    }
1841
236k
    pos += size;
1842
236k
  }
1843
16.6k
  lua_pushinteger(L, cast_st2S(pos) + 1);  /* next position */
1844
16.6k
  return n + 1;
1845
31.1k
}
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
13.0k
static void createmetatable (lua_State *L) {
1873
  /* table to be metatable for strings */
1874
13.0k
  luaL_newlibtable(L, stringmetamethods);
1875
13.0k
  luaL_setfuncs(L, stringmetamethods, 0);
1876
13.0k
  lua_pushliteral(L, "");  /* dummy string */
1877
13.0k
  lua_pushvalue(L, -2);  /* copy table */
1878
13.0k
  lua_setmetatable(L, -2);  /* set table as metatable for strings */
1879
13.0k
  lua_pop(L, 1);  /* pop dummy string */
1880
13.0k
  lua_pushvalue(L, -2);  /* get string library */
1881
13.0k
  lua_setfield(L, -2, "__index");  /* metatable.__index = string */
1882
13.0k
  lua_pop(L, 1);  /* pop metatable */
1883
13.0k
}
1884
1885
1886
/*
1887
** Open string library
1888
*/
1889
13.0k
LUAMOD_API int luaopen_string (lua_State *L) {
1890
13.0k
  luaL_newlib(L, strlib);
1891
13.0k
  createmetatable(L);
1892
13.0k
  return 1;
1893
13.0k
}
1894