Coverage Report

Created: 2024-05-13 06:24

/src/quickjs/cutils.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * C utilities
3
 *
4
 * Copyright (c) 2017 Fabrice Bellard
5
 * Copyright (c) 2018 Charlie Gordon
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
#ifndef CUTILS_H
26
#define CUTILS_H
27
28
#include <stdlib.h>
29
#include <string.h>
30
#include <inttypes.h>
31
32
0
#define likely(x)       __builtin_expect(!!(x), 1)
33
66.1M
#define unlikely(x)     __builtin_expect(!!(x), 0)
34
#define force_inline inline __attribute__((always_inline))
35
#define no_inline __attribute__((noinline))
36
#define __maybe_unused __attribute__((unused))
37
38
#define xglue(x, y) x ## y
39
#define glue(x, y) xglue(x, y)
40
#define stringify(s)    tostring(s)
41
#define tostring(s)     #s
42
43
#ifndef offsetof
44
#define offsetof(type, field) ((size_t) &((type *)0)->field)
45
#endif
46
#ifndef countof
47
0
#define countof(x) (sizeof(x) / sizeof((x)[0]))
48
#endif
49
#ifndef container_of
50
/* return the pointer of type 'type *' containing 'ptr' as field 'member' */
51
#define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))
52
#endif
53
54
#if !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
55
#define minimum_length(n)  static n
56
#else
57
#define minimum_length(n)  n
58
#endif
59
60
typedef int BOOL;
61
62
#ifndef FALSE
63
enum {
64
    FALSE = 0,
65
    TRUE = 1,
66
};
67
#endif
68
69
void pstrcpy(char *buf, int buf_size, const char *str);
70
char *pstrcat(char *buf, int buf_size, const char *s);
71
int strstart(const char *str, const char *val, const char **ptr);
72
int has_suffix(const char *str, const char *suffix);
73
74
/* Prevent UB when n == 0 and (src == NULL or dest == NULL) */
75
3.28M
static inline void memcpy_no_ub(void *dest, const void *src, size_t n) {
76
3.28M
    if (n)
77
3.28M
        memcpy(dest, src, n);
78
3.28M
}
Unexecuted instantiation: libregexp.c:memcpy_no_ub
cutils.c:memcpy_no_ub
Line
Count
Source
75
3.28M
static inline void memcpy_no_ub(void *dest, const void *src, size_t n) {
76
3.28M
    if (n)
77
3.28M
        memcpy(dest, src, n);
78
3.28M
}
Unexecuted instantiation: libunicode.c:memcpy_no_ub
79
80
static inline int max_int(int a, int b)
81
493k
{
82
493k
    if (a > b)
83
124k
        return a;
84
368k
    else
85
368k
        return b;
86
493k
}
Unexecuted instantiation: libregexp.c:max_int
Unexecuted instantiation: cutils.c:max_int
libunicode.c:max_int
Line
Count
Source
81
493k
{
82
493k
    if (a > b)
83
124k
        return a;
84
368k
    else
85
368k
        return b;
86
493k
}
87
88
static inline int min_int(int a, int b)
89
0
{
90
0
    if (a < b)
91
0
        return a;
92
0
    else
93
0
        return b;
94
0
}
Unexecuted instantiation: libregexp.c:min_int
Unexecuted instantiation: cutils.c:min_int
Unexecuted instantiation: libunicode.c:min_int
95
96
static inline uint32_t max_uint32(uint32_t a, uint32_t b)
97
0
{
98
0
    if (a > b)
99
0
        return a;
100
0
    else
101
0
        return b;
102
0
}
Unexecuted instantiation: libregexp.c:max_uint32
Unexecuted instantiation: cutils.c:max_uint32
Unexecuted instantiation: libunicode.c:max_uint32
103
104
static inline uint32_t min_uint32(uint32_t a, uint32_t b)
105
0
{
106
0
    if (a < b)
107
0
        return a;
108
0
    else
109
0
        return b;
110
0
}
Unexecuted instantiation: libregexp.c:min_uint32
Unexecuted instantiation: cutils.c:min_uint32
Unexecuted instantiation: libunicode.c:min_uint32
111
112
static inline int64_t max_int64(int64_t a, int64_t b)
113
0
{
114
0
    if (a > b)
115
0
        return a;
116
0
    else
117
0
        return b;
118
0
}
Unexecuted instantiation: libregexp.c:max_int64
Unexecuted instantiation: cutils.c:max_int64
Unexecuted instantiation: libunicode.c:max_int64
119
120
static inline int64_t min_int64(int64_t a, int64_t b)
121
0
{
122
0
    if (a < b)
123
0
        return a;
124
0
    else
125
0
        return b;
126
0
}
Unexecuted instantiation: libregexp.c:min_int64
Unexecuted instantiation: cutils.c:min_int64
Unexecuted instantiation: libunicode.c:min_int64
127
128
/* WARNING: undefined if a = 0 */
129
static inline int clz32(unsigned int a)
130
0
{
131
0
    return __builtin_clz(a);
132
0
}
Unexecuted instantiation: libregexp.c:clz32
Unexecuted instantiation: cutils.c:clz32
Unexecuted instantiation: libunicode.c:clz32
133
134
/* WARNING: undefined if a = 0 */
135
static inline int clz64(uint64_t a)
136
0
{
137
0
    return __builtin_clzll(a);
138
0
}
Unexecuted instantiation: libregexp.c:clz64
Unexecuted instantiation: cutils.c:clz64
Unexecuted instantiation: libunicode.c:clz64
139
140
/* WARNING: undefined if a = 0 */
141
static inline int ctz32(unsigned int a)
142
0
{
143
0
    return __builtin_ctz(a);
144
0
}
Unexecuted instantiation: libregexp.c:ctz32
Unexecuted instantiation: cutils.c:ctz32
Unexecuted instantiation: libunicode.c:ctz32
145
146
/* WARNING: undefined if a = 0 */
147
static inline int ctz64(uint64_t a)
148
0
{
149
0
    return __builtin_ctzll(a);
150
0
}
Unexecuted instantiation: libregexp.c:ctz64
Unexecuted instantiation: cutils.c:ctz64
Unexecuted instantiation: libunicode.c:ctz64
151
152
struct __attribute__((packed)) packed_u64 {
153
    uint64_t v;
154
};
155
156
struct __attribute__((packed)) packed_u32 {
157
    uint32_t v;
158
};
159
160
struct __attribute__((packed)) packed_u16 {
161
    uint16_t v;
162
};
163
164
static inline uint64_t get_u64(const uint8_t *tab)
165
0
{
166
0
    return ((const struct packed_u64 *)tab)->v;
167
0
}
Unexecuted instantiation: libregexp.c:get_u64
Unexecuted instantiation: cutils.c:get_u64
Unexecuted instantiation: libunicode.c:get_u64
168
169
static inline int64_t get_i64(const uint8_t *tab)
170
0
{
171
0
    return (int64_t)((const struct packed_u64 *)tab)->v;
172
0
}
Unexecuted instantiation: libregexp.c:get_i64
Unexecuted instantiation: cutils.c:get_i64
Unexecuted instantiation: libunicode.c:get_i64
173
174
static inline void put_u64(uint8_t *tab, uint64_t val)
175
0
{
176
0
    ((struct packed_u64 *)tab)->v = val;
177
0
}
Unexecuted instantiation: libregexp.c:put_u64
Unexecuted instantiation: cutils.c:put_u64
Unexecuted instantiation: libunicode.c:put_u64
178
179
static inline uint32_t get_u32(const uint8_t *tab)
180
438M
{
181
438M
    return ((const struct packed_u32 *)tab)->v;
182
438M
}
libregexp.c:get_u32
Line
Count
Source
180
438M
{
181
438M
    return ((const struct packed_u32 *)tab)->v;
182
438M
}
Unexecuted instantiation: cutils.c:get_u32
Unexecuted instantiation: libunicode.c:get_u32
183
184
static inline int32_t get_i32(const uint8_t *tab)
185
0
{
186
0
    return (int32_t)((const struct packed_u32 *)tab)->v;
187
0
}
Unexecuted instantiation: libregexp.c:get_i32
Unexecuted instantiation: cutils.c:get_i32
Unexecuted instantiation: libunicode.c:get_i32
188
189
static inline void put_u32(uint8_t *tab, uint32_t val)
190
192k
{
191
192k
    ((struct packed_u32 *)tab)->v = val;
192
192k
}
libregexp.c:put_u32
Line
Count
Source
190
192k
{
191
192k
    ((struct packed_u32 *)tab)->v = val;
192
192k
}
Unexecuted instantiation: cutils.c:put_u32
Unexecuted instantiation: libunicode.c:put_u32
193
194
static inline uint32_t get_u16(const uint8_t *tab)
195
112M
{
196
112M
    return ((const struct packed_u16 *)tab)->v;
197
112M
}
libregexp.c:get_u16
Line
Count
Source
195
112M
{
196
112M
    return ((const struct packed_u16 *)tab)->v;
197
112M
}
Unexecuted instantiation: cutils.c:get_u16
Unexecuted instantiation: libunicode.c:get_u16
198
199
static inline int32_t get_i16(const uint8_t *tab)
200
0
{
201
0
    return (int16_t)((const struct packed_u16 *)tab)->v;
202
0
}
Unexecuted instantiation: libregexp.c:get_i16
Unexecuted instantiation: cutils.c:get_i16
Unexecuted instantiation: libunicode.c:get_i16
203
204
static inline void put_u16(uint8_t *tab, uint16_t val)
205
0
{
206
0
    ((struct packed_u16 *)tab)->v = val;
207
0
}
Unexecuted instantiation: libregexp.c:put_u16
Unexecuted instantiation: cutils.c:put_u16
Unexecuted instantiation: libunicode.c:put_u16
208
209
static inline uint32_t get_u8(const uint8_t *tab)
210
0
{
211
0
    return *tab;
212
0
}
Unexecuted instantiation: libregexp.c:get_u8
Unexecuted instantiation: cutils.c:get_u8
Unexecuted instantiation: libunicode.c:get_u8
213
214
static inline int32_t get_i8(const uint8_t *tab)
215
0
{
216
0
    return (int8_t)*tab;
217
0
}
Unexecuted instantiation: libregexp.c:get_i8
Unexecuted instantiation: cutils.c:get_i8
Unexecuted instantiation: libunicode.c:get_i8
218
219
static inline void put_u8(uint8_t *tab, uint8_t val)
220
0
{
221
0
    *tab = val;
222
0
}
Unexecuted instantiation: libregexp.c:put_u8
Unexecuted instantiation: cutils.c:put_u8
Unexecuted instantiation: libunicode.c:put_u8
223
224
#ifndef bswap16
225
static inline uint16_t bswap16(uint16_t x)
226
0
{
227
0
    return (x >> 8) | (x << 8);
228
0
}
Unexecuted instantiation: libregexp.c:bswap16
Unexecuted instantiation: cutils.c:bswap16
Unexecuted instantiation: libunicode.c:bswap16
229
#endif
230
231
#ifndef bswap32
232
static inline uint32_t bswap32(uint32_t v)
233
0
{
234
0
    return ((v & 0xff000000) >> 24) | ((v & 0x00ff0000) >>  8) |
235
0
        ((v & 0x0000ff00) <<  8) | ((v & 0x000000ff) << 24);
236
0
}
Unexecuted instantiation: libregexp.c:bswap32
Unexecuted instantiation: cutils.c:bswap32
Unexecuted instantiation: libunicode.c:bswap32
237
#endif
238
239
#ifndef bswap64
240
static inline uint64_t bswap64(uint64_t v)
241
0
{
242
0
    return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) |
243
0
        ((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) |
244
0
        ((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) |
245
0
        ((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) |
246
0
        ((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) |
247
0
        ((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) |
248
0
        ((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) |
249
0
        ((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8));
250
0
}
Unexecuted instantiation: libregexp.c:bswap64
Unexecuted instantiation: cutils.c:bswap64
Unexecuted instantiation: libunicode.c:bswap64
251
#endif
252
253
/* XXX: should take an extra argument to pass slack information to the caller */
254
typedef void *DynBufReallocFunc(void *opaque, void *ptr, size_t size);
255
256
typedef struct DynBuf {
257
    uint8_t *buf;
258
    size_t size;
259
    size_t allocated_size;
260
    BOOL error; /* true if a memory allocation error occurred */
261
    DynBufReallocFunc *realloc_func;
262
    void *opaque; /* for realloc_func */
263
} DynBuf;
264
265
void dbuf_init(DynBuf *s);
266
void dbuf_init2(DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func);
267
int dbuf_realloc(DynBuf *s, size_t new_size);
268
int dbuf_write(DynBuf *s, size_t offset, const uint8_t *data, size_t len);
269
int dbuf_put(DynBuf *s, const uint8_t *data, size_t len);
270
int dbuf_put_self(DynBuf *s, size_t offset, size_t len);
271
int dbuf_putc(DynBuf *s, uint8_t c);
272
int dbuf_putstr(DynBuf *s, const char *str);
273
static inline int dbuf_put_u16(DynBuf *s, uint16_t val)
274
1.69M
{
275
1.69M
    return dbuf_put(s, (uint8_t *)&val, 2);
276
1.69M
}
libregexp.c:dbuf_put_u16
Line
Count
Source
274
1.69M
{
275
1.69M
    return dbuf_put(s, (uint8_t *)&val, 2);
276
1.69M
}
Unexecuted instantiation: cutils.c:dbuf_put_u16
Unexecuted instantiation: libunicode.c:dbuf_put_u16
277
static inline int dbuf_put_u32(DynBuf *s, uint32_t val)
278
243k
{
279
243k
    return dbuf_put(s, (uint8_t *)&val, 4);
280
243k
}
libregexp.c:dbuf_put_u32
Line
Count
Source
278
243k
{
279
243k
    return dbuf_put(s, (uint8_t *)&val, 4);
280
243k
}
Unexecuted instantiation: cutils.c:dbuf_put_u32
Unexecuted instantiation: libunicode.c:dbuf_put_u32
281
static inline int dbuf_put_u64(DynBuf *s, uint64_t val)
282
0
{
283
0
    return dbuf_put(s, (uint8_t *)&val, 8);
284
0
}
Unexecuted instantiation: libregexp.c:dbuf_put_u64
Unexecuted instantiation: cutils.c:dbuf_put_u64
Unexecuted instantiation: libunicode.c:dbuf_put_u64
285
int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s,
286
                                                      const char *fmt, ...);
287
void dbuf_free(DynBuf *s);
288
204k
static inline BOOL dbuf_error(DynBuf *s) {
289
204k
    return s->error;
290
204k
}
libregexp.c:dbuf_error
Line
Count
Source
288
204k
static inline BOOL dbuf_error(DynBuf *s) {
289
204k
    return s->error;
290
204k
}
Unexecuted instantiation: cutils.c:dbuf_error
Unexecuted instantiation: libunicode.c:dbuf_error
291
static inline void dbuf_set_error(DynBuf *s)
292
0
{
293
0
    s->error = TRUE;
294
0
}
Unexecuted instantiation: libregexp.c:dbuf_set_error
Unexecuted instantiation: cutils.c:dbuf_set_error
Unexecuted instantiation: libunicode.c:dbuf_set_error
295
296
50.5k
#define UTF8_CHAR_LEN_MAX 6
297
298
int unicode_to_utf8(uint8_t *buf, unsigned int c);
299
int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp);
300
301
static inline BOOL is_surrogate(uint32_t c)
302
0
{
303
0
    return (c >> 11) == (0xD800 >> 11); // 0xD800-0xDFFF
304
0
}
Unexecuted instantiation: libregexp.c:is_surrogate
Unexecuted instantiation: cutils.c:is_surrogate
Unexecuted instantiation: libunicode.c:is_surrogate
305
306
static inline BOOL is_hi_surrogate(uint32_t c)
307
11.9k
{
308
11.9k
    return (c >> 10) == (0xD800 >> 10); // 0xD800-0xDBFF
309
11.9k
}
libregexp.c:is_hi_surrogate
Line
Count
Source
307
11.9k
{
308
11.9k
    return (c >> 10) == (0xD800 >> 10); // 0xD800-0xDBFF
309
11.9k
}
Unexecuted instantiation: cutils.c:is_hi_surrogate
Unexecuted instantiation: libunicode.c:is_hi_surrogate
310
311
static inline BOOL is_lo_surrogate(uint32_t c)
312
1.36k
{
313
1.36k
    return (c >> 10) == (0xDC00 >> 10); // 0xDC00-0xDFFF
314
1.36k
}
libregexp.c:is_lo_surrogate
Line
Count
Source
312
1.36k
{
313
1.36k
    return (c >> 10) == (0xDC00 >> 10); // 0xDC00-0xDFFF
314
1.36k
}
Unexecuted instantiation: cutils.c:is_lo_surrogate
Unexecuted instantiation: libunicode.c:is_lo_surrogate
315
316
static inline uint32_t get_hi_surrogate(uint32_t c)
317
0
{
318
0
    return (c >> 10) - (0x10000 >> 10) + 0xD800;
319
0
}
Unexecuted instantiation: libregexp.c:get_hi_surrogate
Unexecuted instantiation: cutils.c:get_hi_surrogate
Unexecuted instantiation: libunicode.c:get_hi_surrogate
320
321
static inline uint32_t get_lo_surrogate(uint32_t c)
322
0
{
323
0
    return (c & 0x3FF) | 0xDC00;
324
0
}
Unexecuted instantiation: libregexp.c:get_lo_surrogate
Unexecuted instantiation: cutils.c:get_lo_surrogate
Unexecuted instantiation: libunicode.c:get_lo_surrogate
325
326
static inline uint32_t from_surrogate(uint32_t hi, uint32_t lo)
327
674
{
328
674
    return 0x10000 + 0x400 * (hi - 0xD800) + (lo - 0xDC00);
329
674
}
libregexp.c:from_surrogate
Line
Count
Source
327
674
{
328
674
    return 0x10000 + 0x400 * (hi - 0xD800) + (lo - 0xDC00);
329
674
}
Unexecuted instantiation: cutils.c:from_surrogate
Unexecuted instantiation: libunicode.c:from_surrogate
330
331
static inline int from_hex(int c)
332
59.3k
{
333
59.3k
    if (c >= '0' && c <= '9')
334
18.3k
        return c - '0';
335
40.9k
    else if (c >= 'A' && c <= 'F')
336
16.4k
        return c - 'A' + 10;
337
24.5k
    else if (c >= 'a' && c <= 'f')
338
14.9k
        return c - 'a' + 10;
339
9.52k
    else
340
9.52k
        return -1;
341
59.3k
}
libregexp.c:from_hex
Line
Count
Source
332
59.3k
{
333
59.3k
    if (c >= '0' && c <= '9')
334
18.3k
        return c - '0';
335
40.9k
    else if (c >= 'A' && c <= 'F')
336
16.4k
        return c - 'A' + 10;
337
24.5k
    else if (c >= 'a' && c <= 'f')
338
14.9k
        return c - 'a' + 10;
339
9.52k
    else
340
9.52k
        return -1;
341
59.3k
}
Unexecuted instantiation: cutils.c:from_hex
Unexecuted instantiation: libunicode.c:from_hex
342
343
void rqsort(void *base, size_t nmemb, size_t size,
344
            int (*cmp)(const void *, const void *, void *),
345
            void *arg);
346
347
#endif  /* CUTILS_H */