Coverage Report

Created: 2025-07-11 06:33

/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
165k
#define MAXUNICODE  0x10FFFFu
26
27
982
#define MAXUTF    0x7FFFFFFFu
28
29
30
136
#define MSGInvalid  "invalid UTF-8 code"
31
32
33
5.42k
#define iscont(c) (((c) & 0xC0) == 0x80)
34
1.76k
#define iscontp(p)  iscont(*(p))
35
36
37
/* from strlib */
38
/* translate a relative string position: negative means back from end */
39
10.6k
static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
40
10.6k
  if (pos >= 0) return pos;
41
5.05k
  else if (0u - (size_t)pos > len) return 0;
42
4.83k
  else return (lua_Integer)len + pos + 1;
43
10.6k
}
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
82.9k
static const char *utf8_decode (const char *s, l_uint32 *val, int strict) {
54
82.9k
  static const l_uint32 limits[] =
55
82.9k
        {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
56
82.9k
  unsigned int c = (unsigned char)s[0];
57
82.9k
  l_uint32 res = 0;  /* final result */
58
82.9k
  if (c < 0x80)  /* ASCII? */
59
82.2k
    res = c;
60
685
  else {
61
685
    int count = 0;  /* to count number of continuation bytes */
62
1.36k
    for (; c & 0x40; c <<= 1) {  /* while it needs continuation bytes... */
63
870
      unsigned int cc = (unsigned char)s[++count];  /* read next byte */
64
870
      if (!iscont(cc))  /* not a continuation byte? */
65
194
        return NULL;  /* invalid byte sequence */
66
676
      res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */
67
676
    }
68
491
    res |= ((l_uint32)(c & 0x7F) << (count * 5));  /* add first byte */
69
491
    if (count > 5 || res > MAXUTF || res < limits[count])
70
8
      return NULL;  /* invalid byte sequence */
71
483
    s += count;  /* skip continuation bytes read */
72
483
  }
73
82.7k
  if (strict) {
74
    /* check for invalid code points; too large or surrogates */
75
82.7k
    if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
76
0
      return NULL;
77
82.7k
  }
78
82.7k
  if (val) *val = res;
79
82.7k
  return s + 1;  /* +1 to include first byte */
80
82.7k
}
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
5.05k
static int utflen (lua_State *L) {
89
5.05k
  lua_Integer n = 0;  /* counter for the number of characters */
90
5.05k
  size_t len;  /* string length in bytes */
91
5.05k
  const char *s = luaL_checklstring(L, 1, &len);
92
5.05k
  lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
93
5.05k
  lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
94
5.05k
  int lax = lua_toboolean(L, 4);
95
5.05k
  luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
96
5.05k
                   "initial position out of bounds");
97
5.05k
  luaL_argcheck(L, --posj < (lua_Integer)len, 3,
98
5.05k
                   "final position out of bounds");
99
85.7k
  while (posi <= posj) {
100
80.8k
    const char *s1 = utf8_decode(s + posi, NULL, !lax);
101
80.8k
    if (s1 == NULL) {  /* conversion error? */
102
118
      luaL_pushfail(L);  /* return fail ... */
103
118
      lua_pushinteger(L, posi + 1);  /* ... and current position */
104
118
      return 2;
105
118
    }
106
80.7k
    posi = ct_diff2S(s1 - s);
107
80.7k
    n++;
108
80.7k
  }
109
4.93k
  lua_pushinteger(L, n);
110
4.93k
  return 1;
111
5.05k
}
112
113
114
/*
115
** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
116
** characters that start in the range [i,j]
117
*/
118
292
static int codepoint (lua_State *L) {
119
292
  size_t len;
120
292
  const char *s = luaL_checklstring(L, 1, &len);
121
292
  lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
122
292
  lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
123
292
  int lax = lua_toboolean(L, 4);
124
292
  int n;
125
292
  const char *se;
126
292
  luaL_argcheck(L, posi >= 1, 2, "out of bounds");
127
292
  luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
128
292
  if (posi > pose) return 0;  /* empty interval; return no values */
129
292
  if (pose - posi >= INT_MAX)  /* (lua_Integer -> int) overflow? */
130
0
    return luaL_error(L, "string slice too long");
131
292
  n = (int)(pose -  posi) + 1;  /* upper bound for number of returns */
132
292
  luaL_checkstack(L, n, "string slice too long");
133
292
  n = 0;  /* count the number of returns */
134
292
  se = s + pose;  /* string end */
135
581
  for (s += posi - 1; s < se;) {
136
289
    l_uint32 code;
137
289
    s = utf8_decode(s, &code, !lax);
138
289
    if (s == NULL)
139
0
      return luaL_error(L, MSGInvalid);
140
289
    lua_pushinteger(L, l_castU2S(code));
141
289
    n++;
142
289
  }
143
292
  return n;
144
292
}
145
146
147
616
static void pushutfchar (lua_State *L, int arg) {
148
616
  lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
149
616
  luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
150
616
  lua_pushfstring(L, "%U", (long)code);
151
616
}
152
153
154
/*
155
** utfchar(n1, n2, ...)  -> char(n1)..char(n2)...
156
*/
157
769
static int utfchar (lua_State *L) {
158
769
  int n = lua_gettop(L);  /* number of arguments */
159
769
  if (n == 1)  /* optimize common case of single char */
160
616
    pushutfchar(L, 1);
161
153
  else {
162
153
    int i;
163
153
    luaL_Buffer b;
164
153
    luaL_buffinit(L, &b);
165
153
    for (i = 1; i <= n; i++) {
166
0
      pushutfchar(L, i);
167
0
      luaL_addvalue(&b);
168
0
    }
169
153
    luaL_pushresult(&b);
170
153
  }
171
769
  return 1;
172
769
}
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
22
static int byteoffset (lua_State *L) {
180
22
  size_t len;
181
22
  const char *s = luaL_checklstring(L, 1, &len);
182
22
  lua_Integer n  = luaL_checkinteger(L, 2);
183
22
  lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1;
184
22
  posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
185
22
  luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
186
22
                   "position out of bounds");
187
22
  if (n == 0) {
188
    /* find beginning of current byte sequence */
189
0
    while (posi > 0 && iscontp(s + posi)) posi--;
190
0
  }
191
22
  else {
192
22
    if (iscontp(s + posi))
193
0
      return luaL_error(L, "initial position is a continuation byte");
194
22
    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
22
    else {
203
22
      n--;  /* do not move for 1st character */
204
22
      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
22
    }
211
22
  }
212
22
  if (n != 0) {  /* did not find given character? */
213
0
    luaL_pushfail(L);
214
0
    return 1;
215
0
  }
216
22
  lua_pushinteger(L, posi + 1);  /* initial position */
217
22
  if ((s[posi] & 0x80) != 0) {  /* multi-byte character? */
218
0
    do {
219
0
      posi++;
220
0
    } while (iscontp(s + posi + 1));  /* skip to final byte */
221
0
  }
222
  /* else one-byte character: final position is the initial one */
223
22
  lua_pushinteger(L, posi + 1);  /* 'posi' now is the final position */
224
22
  return 2;
225
22
}
226
227
228
2.88k
static int iter_aux (lua_State *L, int strict) {
229
2.88k
  size_t len;
230
2.88k
  const char *s = luaL_checklstring(L, 1, &len);
231
2.88k
  lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
232
2.88k
  if (n < len) {
233
1.93k
    while (iscontp(s + n)) n++;  /* go to next character */
234
1.93k
  }
235
2.88k
  if (n >= len)  /* (also handles original 'n' being negative) */
236
1.05k
    return 0;  /* no more codepoints */
237
1.83k
  else {
238
1.83k
    l_uint32 code;
239
1.83k
    const char *next = utf8_decode(s + n, &code, strict);
240
1.83k
    if (next == NULL || iscontp(next))
241
136
      return luaL_error(L, MSGInvalid);
242
1.69k
    lua_pushinteger(L, l_castU2S(n + 1));
243
1.69k
    lua_pushinteger(L, l_castU2S(code));
244
1.69k
    return 2;
245
1.83k
  }
246
2.88k
}
247
248
249
2.88k
static int iter_auxstrict (lua_State *L) {
250
2.88k
  return iter_aux(L, 1);
251
2.88k
}
252
253
0
static int iter_auxlax (lua_State *L) {
254
0
  return iter_aux(L, 0);
255
0
}
256
257
258
3.53k
static int iter_codes (lua_State *L) {
259
3.53k
  int lax = lua_toboolean(L, 2);
260
3.53k
  const char *s = luaL_checkstring(L, 1);
261
3.53k
  luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
262
3.53k
  lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
263
3.53k
  lua_pushvalue(L, 1);
264
3.53k
  lua_pushinteger(L, 0);
265
3.53k
  return 3;
266
3.53k
}
267
268
269
/* pattern to match a single UTF-8 character */
270
59.9k
#define UTF8PATT  "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
271
272
273
static const luaL_Reg funcs[] = {
274
  {"offset", byteoffset},
275
  {"codepoint", codepoint},
276
  {"char", utfchar},
277
  {"len", utflen},
278
  {"codes", iter_codes},
279
  /* placeholders */
280
  {"charpattern", NULL},
281
  {NULL, NULL}
282
};
283
284
285
29.9k
LUAMOD_API int luaopen_utf8 (lua_State *L) {
286
29.9k
  luaL_newlib(L, funcs);
287
29.9k
  lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
288
29.9k
  lua_setfield(L, -2, "charpattern");
289
29.9k
  return 1;
290
29.9k
}
291