Coverage Report

Created: 2025-08-09 06:54

/src/testdir/build/lua-master/source/lutf8lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** $Id: lutf8lib.c $
3
** Standard library for UTF-8 manipulation
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lutf8lib_c
8
#define LUA_LIB
9
10
#include "lprefix.h"
11
12
13
#include <assert.h>
14
#include <limits.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "lua.h"
19
20
#include "lauxlib.h"
21
#include "lualib.h"
22
#include "llimits.h"
23
24
25
1.04M
#define MAXUNICODE  0x10FFFFu
26
27
3.27k
#define MAXUTF    0x7FFFFFFFu
28
29
30
113
#define MSGInvalid  "invalid UTF-8 code"
31
32
33
11.1k
#define iscont(c) (((c) & 0xC0) == 0x80)
34
1.73k
#define iscontp(p)  iscont(*(p))
35
36
37
/* from strlib */
38
/* translate a relative string position: negative means back from end */
39
8.11k
static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
40
8.11k
  if (pos >= 0) return pos;
41
3.95k
  else if (0u - (size_t)pos > len) return 0;
42
3.53k
  else return (lua_Integer)len + pos + 1;
43
8.11k
}
44
45
46
/*
47
** Decode one UTF-8 sequence, returning NULL if byte sequence is
48
** invalid.  The array 'limits' stores the minimum value for each
49
** sequence length, to check for overlong representations. Its first
50
** entry forces an error for non-ASCII bytes with no continuation
51
** bytes (count == 0).
52
*/
53
525k
static const char *utf8_decode (const char *s, l_uint32 *val, int strict) {
54
525k
  static const l_uint32 limits[] =
55
525k
        {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
56
525k
  unsigned int c = (unsigned char)s[0];
57
525k
  l_uint32 res = 0;  /* final result */
58
525k
  if (c < 0x80)  /* ASCII? */
59
522k
    res = c;
60
3.04k
  else {
61
3.04k
    int count = 0;  /* to count number of continuation bytes */
62
9.03k
    for (; c & 0x40; c <<= 1) {  /* while it needs continuation bytes... */
63
7.28k
      unsigned int cc = (unsigned char)s[++count];  /* read next byte */
64
7.28k
      if (!iscont(cc))  /* not a continuation byte? */
65
1.28k
        return NULL;  /* invalid byte sequence */
66
5.99k
      res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */
67
5.99k
    }
68
1.75k
    res |= ((l_uint32)(c & 0x7F) << (count * 5));  /* add first byte */
69
1.75k
    if (count > 5 || res > MAXUTF || res < limits[count])
70
363
      return NULL;  /* invalid byte sequence */
71
1.39k
    s += count;  /* skip continuation bytes read */
72
1.39k
  }
73
524k
  if (strict) {
74
    /* check for invalid code points; too large or surrogates */
75
524k
    if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
76
76
      return NULL;
77
524k
  }
78
524k
  if (val) *val = res;
79
524k
  return s + 1;  /* +1 to include first byte */
80
524k
}
81
82
83
/*
84
** utf8len(s [, i [, j [, lax]]]) --> number of characters that
85
** start in the range [i,j], or nil + current position if 's' is not
86
** well formed in that interval
87
*/
88
3.95k
static int utflen (lua_State *L) {
89
3.95k
  lua_Integer n = 0;  /* counter for the number of characters */
90
3.95k
  size_t len;  /* string length in bytes */
91
3.95k
  const char *s = luaL_checklstring(L, 1, &len);
92
3.95k
  lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
93
3.95k
  lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
94
3.95k
  int lax = lua_toboolean(L, 4);
95
3.95k
  luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
96
3.95k
                   "initial position out of bounds");
97
3.95k
  luaL_argcheck(L, --posj < (lua_Integer)len, 3,
98
3.95k
                   "final position out of bounds");
99
526k
  while (posi <= posj) {
100
523k
    const char *s1 = utf8_decode(s + posi, NULL, !lax);
101
523k
    if (s1 == NULL) {  /* conversion error? */
102
1.66k
      luaL_pushfail(L);  /* return fail ... */
103
1.66k
      lua_pushinteger(L, posi + 1);  /* ... and current position */
104
1.66k
      return 2;
105
1.66k
    }
106
522k
    posi = ct_diff2S(s1 - s);
107
522k
    n++;
108
522k
  }
109
2.28k
  lua_pushinteger(L, n);
110
2.28k
  return 1;
111
3.95k
}
112
113
114
/*
115
** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
116
** characters that start in the range [i,j]
117
*/
118
105
static int codepoint (lua_State *L) {
119
105
  size_t len;
120
105
  const char *s = luaL_checklstring(L, 1, &len);
121
105
  lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
122
105
  lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
123
105
  int lax = lua_toboolean(L, 4);
124
105
  int n;
125
105
  const char *se;
126
105
  luaL_argcheck(L, posi >= 1, 2, "out of bounds");
127
105
  luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
128
105
  if (posi > pose) return 0;  /* empty interval; return no values */
129
105
  if (pose - posi >= INT_MAX)  /* (lua_Integer -> int) overflow? */
130
0
    return luaL_error(L, "string slice too long");
131
105
  n = (int)(pose -  posi) + 1;  /* upper bound for number of returns */
132
105
  luaL_checkstack(L, n, "string slice too long");
133
105
  n = 0;  /* count the number of returns */
134
105
  se = s + pose;  /* string end */
135
208
  for (s += posi - 1; s < se;) {
136
103
    l_uint32 code;
137
103
    s = utf8_decode(s, &code, !lax);
138
103
    if (s == NULL)
139
0
      return luaL_error(L, MSGInvalid);
140
103
    lua_pushinteger(L, l_castU2S(code));
141
103
    n++;
142
103
  }
143
105
  return n;
144
105
}
145
146
147
408
static void pushutfchar (lua_State *L, int arg) {
148
408
  lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
149
408
  luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
150
408
  lua_pushfstring(L, "%U", (long)code);
151
408
}
152
153
154
/*
155
** utfchar(n1, n2, ...)  -> char(n1)..char(n2)...
156
*/
157
434
static int utfchar (lua_State *L) {
158
434
  int n = lua_gettop(L);  /* number of arguments */
159
434
  if (n == 1)  /* optimize common case of single char */
160
408
    pushutfchar(L, 1);
161
26
  else {
162
26
    int i;
163
26
    luaL_Buffer b;
164
26
    luaL_buffinit(L, &b);
165
26
    for (i = 1; i <= n; i++) {
166
0
      pushutfchar(L, i);
167
0
      luaL_addvalue(&b);
168
0
    }
169
26
    luaL_pushresult(&b);
170
26
  }
171
434
  return 1;
172
434
}
173
174
175
/*
176
** offset(s, n, [i])  -> indices where n-th character counting from
177
**   position 'i' starts and ends; 0 means character at 'i'.
178
*/
179
8
static int byteoffset (lua_State *L) {
180
8
  size_t len;
181
8
  const char *s = luaL_checklstring(L, 1, &len);
182
8
  lua_Integer n  = luaL_checkinteger(L, 2);
183
8
  lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1;
184
8
  posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
185
8
  luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
186
8
                   "position out of bounds");
187
8
  if (n == 0) {
188
    /* find beginning of current byte sequence */
189
0
    while (posi > 0 && iscontp(s + posi)) posi--;
190
0
  }
191
8
  else {
192
8
    if (iscontp(s + posi))
193
0
      return luaL_error(L, "initial position is a continuation byte");
194
8
    if (n < 0) {
195
0
      while (n < 0 && posi > 0) {  /* move back */
196
0
        do {  /* find beginning of previous character */
197
0
          posi--;
198
0
        } while (posi > 0 && iscontp(s + posi));
199
0
        n++;
200
0
      }
201
0
    }
202
8
    else {
203
8
      n--;  /* do not move for 1st character */
204
8
      while (n > 0 && posi < (lua_Integer)len) {
205
0
        do {  /* find beginning of next character */
206
0
          posi++;
207
0
        } while (iscontp(s + posi));  /* (cannot pass final '\0') */
208
0
        n--;
209
0
      }
210
8
    }
211
8
  }
212
8
  if (n != 0) {  /* did not find given character? */
213
0
    luaL_pushfail(L);
214
0
    return 1;
215
0
  }
216
8
  lua_pushinteger(L, posi + 1);  /* initial position */
217
8
  if ((s[posi] & 0x80) != 0) {  /* multi-byte character? */
218
0
    if (iscont(s[posi]))
219
0
      return luaL_error(L, "initial position is a continuation byte");
220
0
    while (iscontp(s + posi + 1))
221
0
      posi++;  /* skip to last continuation byte */
222
0
  }
223
  /* else one-byte character: final position is the initial one */
224
8
  lua_pushinteger(L, posi + 1);  /* 'posi' now is the final position */
225
8
  return 2;
226
8
}
227
228
229
3.75k
static int iter_aux (lua_State *L, int strict) {
230
3.75k
  size_t len;
231
3.75k
  const char *s = luaL_checklstring(L, 1, &len);
232
3.75k
  lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
233
3.75k
  if (n < len) {
234
1.84k
    while (iscontp(s + n)) n++;  /* go to next character */
235
1.84k
  }
236
3.75k
  if (n >= len)  /* (also handles original 'n' being negative) */
237
1.91k
    return 0;  /* no more codepoints */
238
1.84k
  else {
239
1.84k
    l_uint32 code;
240
1.84k
    const char *next = utf8_decode(s + n, &code, strict);
241
1.84k
    if (next == NULL || iscontp(next))
242
113
      return luaL_error(L, MSGInvalid);
243
1.72k
    lua_pushinteger(L, l_castU2S(n + 1));
244
1.72k
    lua_pushinteger(L, l_castU2S(code));
245
1.72k
    return 2;
246
1.84k
  }
247
3.75k
}
248
249
250
3.75k
static int iter_auxstrict (lua_State *L) {
251
3.75k
  return iter_aux(L, 1);
252
3.75k
}
253
254
0
static int iter_auxlax (lua_State *L) {
255
0
  return iter_aux(L, 0);
256
0
}
257
258
259
4.21k
static int iter_codes (lua_State *L) {
260
4.21k
  int lax = lua_toboolean(L, 2);
261
4.21k
  const char *s = luaL_checkstring(L, 1);
262
4.21k
  luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
263
4.21k
  lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
264
4.21k
  lua_pushvalue(L, 1);
265
4.21k
  lua_pushinteger(L, 0);
266
4.21k
  return 3;
267
4.21k
}
268
269
270
/* pattern to match a single UTF-8 character */
271
50.7k
#define UTF8PATT  "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
272
273
274
static const luaL_Reg funcs[] = {
275
  {"offset", byteoffset},
276
  {"codepoint", codepoint},
277
  {"char", utfchar},
278
  {"len", utflen},
279
  {"codes", iter_codes},
280
  /* placeholders */
281
  {"charpattern", NULL},
282
  {NULL, NULL}
283
};
284
285
286
25.3k
LUAMOD_API int luaopen_utf8 (lua_State *L) {
287
25.3k
  luaL_newlib(L, funcs);
288
25.3k
  lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
289
25.3k
  lua_setfield(L, -2, "charpattern");
290
25.3k
  return 1;
291
25.3k
}
292