Coverage Report

Created: 2025-08-25 07:03

/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.03M
#define MAXUNICODE  0x10FFFFu
26
27
12.6k
#define MAXUTF    0x7FFFFFFFu
28
29
30
156
#define MSGInvalid  "invalid UTF-8 code"
31
32
33
16.7k
#define iscont(c) (((c) & 0xC0) == 0x80)
34
1.81k
#define iscontp(p)  iscont(*(p))
35
36
37
/* from strlib */
38
/* translate a relative string position: negative means back from end */
39
14.8k
static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
40
14.8k
  if (pos >= 0) return pos;
41
7.29k
  else if (0u - (size_t)pos > len) return 0;
42
6.94k
  else return (lua_Integer)len + pos + 1;
43
14.8k
}
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
523k
static const char *utf8_decode (const char *s, l_uint32 *val, int strict) {
54
523k
  static const l_uint32 limits[] =
55
523k
        {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
56
523k
  unsigned int c = (unsigned char)s[0];
57
523k
  l_uint32 res = 0;  /* final result */
58
523k
  if (c < 0x80)  /* ASCII? */
59
514k
    res = c;
60
8.95k
  else {
61
8.95k
    int count = 0;  /* to count number of continuation bytes */
62
19.0k
    for (; c & 0x40; c <<= 1) {  /* while it needs continuation bytes... */
63
12.6k
      unsigned int cc = (unsigned char)s[++count];  /* read next byte */
64
12.6k
      if (!iscont(cc))  /* not a continuation byte? */
65
2.53k
        return NULL;  /* invalid byte sequence */
66
10.1k
      res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */
67
10.1k
    }
68
6.41k
    res |= ((l_uint32)(c & 0x7F) << (count * 5));  /* add first byte */
69
6.41k
    if (count > 5 || res > MAXUTF || res < limits[count])
70
1.72k
      return NULL;  /* invalid byte sequence */
71
4.69k
    s += count;  /* skip continuation bytes read */
72
4.69k
  }
73
519k
  if (strict) {
74
    /* check for invalid code points; too large or surrogates */
75
519k
    if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
76
76
      return NULL;
77
519k
  }
78
518k
  if (val) *val = res;
79
518k
  return s + 1;  /* +1 to include first byte */
80
519k
}
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
7.29k
static int utflen (lua_State *L) {
89
7.29k
  lua_Integer n = 0;  /* counter for the number of characters */
90
7.29k
  size_t len;  /* string length in bytes */
91
7.29k
  const char *s = luaL_checklstring(L, 1, &len);
92
7.29k
  lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
93
7.29k
  lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
94
7.29k
  int lax = lua_toboolean(L, 4);
95
7.29k
  luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
96
7.29k
                   "initial position out of bounds");
97
7.29k
  luaL_argcheck(L, --posj < (lua_Integer)len, 3,
98
7.29k
                   "final position out of bounds");
99
524k
  while (posi <= posj) {
100
521k
    const char *s1 = utf8_decode(s + posi, NULL, !lax);
101
521k
    if (s1 == NULL) {  /* conversion error? */
102
4.22k
      luaL_pushfail(L);  /* return fail ... */
103
4.22k
      lua_pushinteger(L, posi + 1);  /* ... and current position */
104
4.22k
      return 2;
105
4.22k
    }
106
517k
    posi = ct_diff2S(s1 - s);
107
517k
    n++;
108
517k
  }
109
3.07k
  lua_pushinteger(L, n);
110
3.07k
  return 1;
111
7.29k
}
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
821
static void pushutfchar (lua_State *L, int arg) {
148
821
  lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
149
821
  luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
150
821
  lua_pushfstring(L, "%U", (long)code);
151
821
}
152
153
154
/*
155
** utfchar(n1, n2, ...)  -> char(n1)..char(n2)...
156
*/
157
813
static int utfchar (lua_State *L) {
158
813
  int n = lua_gettop(L);  /* number of arguments */
159
813
  if (n == 1)  /* optimize common case of single char */
160
419
    pushutfchar(L, 1);
161
394
  else {
162
394
    int i;
163
394
    luaL_Buffer b;
164
394
    luaL_buffinit(L, &b);
165
796
    for (i = 1; i <= n; i++) {
166
402
      pushutfchar(L, i);
167
402
      luaL_addvalue(&b);
168
402
    }
169
394
    luaL_pushresult(&b);
170
394
  }
171
813
  return 1;
172
813
}
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
19
static int byteoffset (lua_State *L) {
180
19
  size_t len;
181
19
  const char *s = luaL_checklstring(L, 1, &len);
182
19
  lua_Integer n  = luaL_checkinteger(L, 2);
183
19
  lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1;
184
19
  posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
185
19
  luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
186
19
                   "position out of bounds");
187
19
  if (n == 0) {
188
    /* find beginning of current byte sequence */
189
0
    while (posi > 0 && iscontp(s + posi)) posi--;
190
0
  }
191
19
  else {
192
19
    if (iscontp(s + posi))
193
0
      return luaL_error(L, "initial position is a continuation byte");
194
19
    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
19
    else {
203
19
      n--;  /* do not move for 1st character */
204
19
      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
19
    }
211
19
  }
212
19
  if (n != 0) {  /* did not find given character? */
213
0
    luaL_pushfail(L);
214
0
    return 1;
215
0
  }
216
19
  lua_pushinteger(L, posi + 1);  /* initial position */
217
19
  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
19
  lua_pushinteger(L, posi + 1);  /* 'posi' now is the final position */
225
19
  return 2;
226
19
}
227
228
229
4.18k
static int iter_aux (lua_State *L, int strict) {
230
4.18k
  size_t len;
231
4.18k
  const char *s = luaL_checklstring(L, 1, &len);
232
4.18k
  lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
233
4.18k
  if (n < len) {
234
1.95k
    while (iscontp(s + n)) n++;  /* go to next character */
235
1.95k
  }
236
4.18k
  if (n >= len)  /* (also handles original 'n' being negative) */
237
2.26k
    return 0;  /* no more codepoints */
238
1.92k
  else {
239
1.92k
    l_uint32 code;
240
1.92k
    const char *next = utf8_decode(s + n, &code, strict);
241
1.92k
    if (next == NULL || iscontp(next))
242
156
      return luaL_error(L, MSGInvalid);
243
1.76k
    lua_pushinteger(L, l_castU2S(n + 1));
244
1.76k
    lua_pushinteger(L, l_castU2S(code));
245
1.76k
    return 2;
246
1.92k
  }
247
4.18k
}
248
249
250
4.18k
static int iter_auxstrict (lua_State *L) {
251
4.18k
  return iter_aux(L, 1);
252
4.18k
}
253
254
0
static int iter_auxlax (lua_State *L) {
255
0
  return iter_aux(L, 0);
256
0
}
257
258
259
4.66k
static int iter_codes (lua_State *L) {
260
4.66k
  int lax = lua_toboolean(L, 2);
261
4.66k
  const char *s = luaL_checkstring(L, 1);
262
4.66k
  luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
263
4.66k
  lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
264
4.66k
  lua_pushvalue(L, 1);
265
4.66k
  lua_pushinteger(L, 0);
266
4.66k
  return 3;
267
4.66k
}
268
269
270
/* pattern to match a single UTF-8 character */
271
53.6k
#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
26.8k
LUAMOD_API int luaopen_utf8 (lua_State *L) {
287
26.8k
  luaL_newlib(L, funcs);
288
26.8k
  lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
289
26.8k
  lua_setfield(L, -2, "charpattern");
290
26.8k
  return 1;
291
26.8k
}
292