repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
BugChecker
github_2023
vitoplantamura
c
scan_digit_nz
static inline limb_t scan_digit_nz(const bfdec_t *r, slimb_t bit_pos) { slimb_t pos; limb_t v, q; int shift; if (bit_pos < 0) return 0; pos = (limb_t)bit_pos / LIMB_DIGITS; shift = (limb_t)bit_pos % LIMB_DIGITS; fast_shr_rem_dec(q, v, r->tab[pos], shift + 1); (void)q; if (v != 0) return 1; pos--; while (pos >= 0) { if (r->tab[pos] != 0) return 1; pos--; } return 0; }
/* return != 0 if one digit between 0 and bit_pos inclusive is not zero. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6295-L6316
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
bfdec_get_rnd_add
static int bfdec_get_rnd_add(int *pret, const bfdec_t *r, limb_t l, slimb_t prec, int rnd_mode) { int add_one, inexact; limb_t digit1, digit0; // bfdec_print_str("get_rnd_add", r); if (rnd_mode == BF_RNDF) { digit0 = 1; /* faithful rounding does not honor the INEXACT flag */ } else { /* starting limb for bit 'prec + 1' */ digit0 = scan_digit_nz(r, l * LIMB_DIGITS - 1 - bf_max(0, prec + 1)); } /* get the digit at 'prec' */ digit1 = get_digit(r->tab, l, l * LIMB_DIGITS - 1 - prec); inexact = (digit1 | digit0) != 0; add_one = 0; switch(rnd_mode) { case BF_RNDZ: break; case BF_RNDN: if (digit1 == 5) { if (digit0) { add_one = 1; } else { /* round to even */ add_one = get_digit(r->tab, l, l * LIMB_DIGITS - 1 - (prec - 1)) & 1; } } else if (digit1 > 5) { add_one = 1; } break; case BF_RNDD: case BF_RNDU: if (r->sign == (rnd_mode == BF_RNDD)) add_one = inexact; break; case BF_RNDNA: case BF_RNDF: add_one = (digit1 >= 5); break; case BF_RNDA: add_one = inexact; break; default: abort(); } if (inexact) *pret |= BF_ST_INEXACT; return add_one; }
/* return the addend for rounding. Note that prec can be <= 0 for bf_rint() */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6358-L6412
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
__bfdec_round
static int __bfdec_round(bfdec_t *r, limb_t prec1, bf_flags_t flags, limb_t l) { int shift, add_one, rnd_mode, ret; slimb_t i, bit_pos, pos, e_min, e_max, e_range, prec; /* XXX: align to IEEE 754 2008 for decimal numbers ? */ e_range = (limb_t)1 << (bf_get_exp_bits(flags) - 1); e_min = -e_range + 3; e_max = e_range; if (flags & BF_FLAG_RADPNT_PREC) { /* 'prec' is the precision after the decimal point */ if (prec1 != BF_PREC_INF) prec = r->expn + prec1; else prec = prec1; } else if (unlikely(r->expn < e_min) && (flags & BF_FLAG_SUBNORMAL)) { /* restrict the precision in case of potentially subnormal result */ assert(prec1 != BF_PREC_INF); prec = prec1 - (e_min - r->expn); } else { prec = prec1; } /* round to prec bits */ rnd_mode = flags & BF_RND_MASK; ret = 0; add_one = bfdec_get_rnd_add(&ret, r, l, prec, rnd_mode); if (prec <= 0) { if (add_one) { bfdec_resize(r, 1); /* cannot fail because r is non zero */ r->tab[0] = BF_DEC_BASE / 10; r->expn += 1 - prec; ret |= BF_ST_UNDERFLOW | BF_ST_INEXACT; return ret; } else { goto underflow; } } else if (add_one) { limb_t carry; /* add one starting at digit 'prec - 1' */ bit_pos = l * LIMB_DIGITS - 1 - (prec - 1); pos = bit_pos / LIMB_DIGITS; carry = mp_pow_dec[bit_pos % LIMB_DIGITS]; carry = mp_add_ui_dec(r->tab + pos, carry, l - pos); if (carry) { /* shift right by one digit */ mp_shr_dec(r->tab + pos, r->tab + pos, l - pos, 1, 1); r->expn++; } } /* check underflow */ if (unlikely(r->expn < e_min)) { if (flags & BF_FLAG_SUBNORMAL) { /* if inexact, also set the underflow flag */ if (ret & BF_ST_INEXACT) ret |= BF_ST_UNDERFLOW; } else { underflow: bfdec_set_zero(r, r->sign); ret |= BF_ST_UNDERFLOW | BF_ST_INEXACT; return ret; } } /* check overflow */ if (unlikely(r->expn > e_max)) { bfdec_set_inf(r, r->sign); ret |= BF_ST_OVERFLOW | BF_ST_INEXACT; return ret; } /* keep the bits starting at 'prec - 1' */ bit_pos = l * LIMB_DIGITS - 1 - (prec - 1); i = floor_div(bit_pos, LIMB_DIGITS); if (i >= 0) { shift = smod(bit_pos, LIMB_DIGITS); if (shift != 0) { r->tab[i] = fast_shr_dec(r->tab[i], shift) * mp_pow_dec[shift]; } } else { i = 0; } /* remove trailing zeros */ while (r->tab[i] == 0) i++; if (i > 0) { l -= i; memmove(r->tab, r->tab + i, l * sizeof(limb_t)); } bfdec_resize(r, l); /* cannot fail */ return ret; }
/* round to prec1 bits assuming 'r' is non zero and finite. 'r' is assumed to have length 'l' (1 <= l <= r->len). prec1 can be BF_PREC_INF. BF_FLAG_SUBNORMAL is not supported. Cannot fail with BF_ST_MEM_ERROR. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6419-L6516
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
bfdec_round
int bfdec_round(bfdec_t *r, limb_t prec, bf_flags_t flags) { if (r->len == 0) return 0; return __bfdec_round(r, prec, flags, r->len); }
/* Cannot fail with BF_ST_MEM_ERROR. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6519-L6524
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
bfdec_normalize_and_round
int bfdec_normalize_and_round(bfdec_t *r, limb_t prec1, bf_flags_t flags) { limb_t l, v; int shift, ret; // bfdec_print_str("bf_renorm", r); l = r->len; while (l > 0 && r->tab[l - 1] == 0) l--; if (l == 0) { /* zero */ r->expn = BF_EXP_ZERO; bfdec_resize(r, 0); /* cannot fail */ ret = 0; } else { r->expn -= (r->len - l) * LIMB_DIGITS; /* shift to have the MSB set to '1' */ v = r->tab[l - 1]; shift = clz_dec(v); if (shift != 0) { mp_shl_dec(r->tab, r->tab, l, shift, 0); r->expn -= shift; } ret = __bfdec_round(r, prec1, flags, l); } // bf_print_str("r_final", r); return ret; }
/* 'r' must be a finite number. Cannot fail with BF_ST_MEM_ERROR. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6527-L6554
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
bfdec_tdivremu
static void bfdec_tdivremu(bf_context_t *s, bfdec_t *q, bfdec_t *r, const bfdec_t *a, const bfdec_t *b) { if (bfdec_cmpu(a, b) < 0) { bfdec_set_ui(q, 0); bfdec_set(r, a); } else { bfdec_div(q, a, b, 0, BF_RNDZ | BF_FLAG_RADPNT_PREC); bfdec_mul(r, q, b, BF_PREC_INF, BF_RNDZ); bfdec_sub(r, a, r, BF_PREC_INF, BF_RNDZ); } }
/* a and b must be finite numbers with a >= 0 and b > 0. 'q' is the integer defined as floor(a/b) and r = a - q * b. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6928-L6939
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
bfdec_divrem
int bfdec_divrem(bfdec_t *q, bfdec_t *r, const bfdec_t *a, const bfdec_t *b, limb_t prec, bf_flags_t flags, int rnd_mode) { bf_context_t *s = q->ctx; bfdec_t a1_s, *a1 = &a1_s; bfdec_t b1_s, *b1 = &b1_s; bfdec_t r1_s, *r1 = &r1_s; int q_sign, res; BOOL is_ceil, is_rndn; assert(q != a && q != b); assert(r != a && r != b); assert(q != r); if (a->len == 0 || b->len == 0) { bfdec_set_zero(q, 0); if (a->expn == BF_EXP_NAN || b->expn == BF_EXP_NAN) { bfdec_set_nan(r); return 0; } else if (a->expn == BF_EXP_INF || b->expn == BF_EXP_ZERO) { bfdec_set_nan(r); return BF_ST_INVALID_OP; } else { bfdec_set(r, a); return bfdec_round(r, prec, flags); } } q_sign = a->sign ^ b->sign; is_rndn = (rnd_mode == BF_RNDN || rnd_mode == BF_RNDNA); switch(rnd_mode) { default: case BF_RNDZ: case BF_RNDN: case BF_RNDNA: is_ceil = FALSE; break; case BF_RNDD: is_ceil = q_sign; break; case BF_RNDU: is_ceil = q_sign ^ 1; break; case BF_RNDA: is_ceil = TRUE; break; case BF_DIVREM_EUCLIDIAN: is_ceil = a->sign; break; } a1->expn = a->expn; a1->tab = a->tab; a1->len = a->len; a1->sign = 0; b1->expn = b->expn; b1->tab = b->tab; b1->len = b->len; b1->sign = 0; // bfdec_print_str("a1", a1); // bfdec_print_str("b1", b1); /* XXX: could improve to avoid having a large 'q' */ bfdec_tdivremu(s, q, r, a1, b1); if (bfdec_is_nan(q) || bfdec_is_nan(r)) goto fail; // bfdec_print_str("q", q); // bfdec_print_str("r", r); if (r->len != 0) { if (is_rndn) { bfdec_init(s, r1); if (bfdec_set(r1, r)) goto fail; if (bfdec_mul_si(r1, r1, 2, BF_PREC_INF, BF_RNDZ)) { bfdec_delete(r1); goto fail; } res = bfdec_cmpu(r1, b); bfdec_delete(r1); if (res > 0 || (res == 0 && (rnd_mode == BF_RNDNA || (get_digit(q->tab, q->len, q->len * LIMB_DIGITS - q->expn) & 1) != 0))) { goto do_sub_r; } } else if (is_ceil) { do_sub_r: res = bfdec_add_si(q, q, 1, BF_PREC_INF, BF_RNDZ); res |= bfdec_sub(r, r, b1, BF_PREC_INF, BF_RNDZ); if (res & BF_ST_MEM_ERROR) goto fail; } } r->sign ^= a->sign; q->sign = q_sign; return bfdec_round(r, prec, flags); fail: bfdec_set_nan(q); bfdec_set_nan(r); return BF_ST_MEM_ERROR; }
/* division and remainder. rnd_mode is the rounding mode for the quotient. The additional rounding mode BF_RND_EUCLIDIAN is supported. 'q' is an integer. 'r' is rounded with prec and flags (prec can be BF_PREC_INF). */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6949-L7052
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
bfdec_rint
int bfdec_rint(bfdec_t *r, int rnd_mode) { return bfdec_round(r, 0, rnd_mode | BF_FLAG_RADPNT_PREC); }
/* convert to integer (infinite precision) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7067-L7070
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
bfdec_get_int32
int bfdec_get_int32(int *pres, const bfdec_t *a) { uint32_t v; int ret; if (a->expn >= BF_EXP_INF) { ret = 0; if (a->expn == BF_EXP_INF) { v = (uint32_t)INT32_MAX + a->sign; /* XXX: return overflow ? */ } else { v = INT32_MAX; } } else if (a->expn <= 0) { v = 0; ret = 0; } else if (a->expn <= 9) { v = fast_shr_dec(a->tab[a->len - 1], LIMB_DIGITS - a->expn); if (a->sign) v = -v; ret = 0; } else if (a->expn == 10) { uint64_t v1; uint32_t v_max; #if LIMB_BITS == 64 v1 = fast_shr_dec(a->tab[a->len - 1], LIMB_DIGITS - a->expn); #else v1 = (uint64_t)a->tab[a->len - 1] * 10 + get_digit(a->tab, a->len, (a->len - 1) * LIMB_DIGITS - 1); #endif v_max = (uint32_t)INT32_MAX + a->sign; if (v1 > v_max) { v = v_max; ret = BF_ST_OVERFLOW; } else { v = v1; if (a->sign) v = -v; ret = 0; } } else { v = (uint32_t)INT32_MAX + a->sign; ret = BF_ST_OVERFLOW; } *pres = v; return ret; }
/* The rounding mode is always BF_RNDZ. Return BF_ST_OVERFLOW if there is an overflow and 0 otherwise. No memory error is possible. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7156-L7201
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
bfdec_pow_ui
int bfdec_pow_ui(bfdec_t *r, const bfdec_t *a, limb_t b) { int ret, n_bits, i; assert(r != a); if (b == 0) return bfdec_set_ui(r, 1); ret = bfdec_set(r, a); n_bits = LIMB_BITS - clz(b); for(i = n_bits - 2; i >= 0; i--) { ret |= bfdec_mul(r, r, r, BF_PREC_INF, BF_RNDZ); if ((b >> i) & 1) ret |= bfdec_mul(r, r, a, BF_PREC_INF, BF_RNDZ); } return ret; }
/* power to an integer with infinite precision */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7204-L7219
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
put_bits
static inline void put_bits(limb_t *tab, limb_t len, slimb_t pos, limb_t val) { limb_t i; int p; i = pos >> LIMB_LOG2_BITS; p = pos & (LIMB_BITS - 1); if (i < len) tab[i] |= val << p; if (p != 0) { i++; if (i < len) { tab[i] |= val >> (LIMB_BITS - p); } } }
/***************************************************************/ /* Integer multiplication with FFT */ /* or LIMB_BITS at bit position 'pos' in tab */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7241-L7256
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
add_mod
static inline limb_t add_mod(limb_t a, limb_t b, limb_t m) { limb_t r; r = a + b; if (r >= m) r -= m; return r; }
/* add modulo with up to (LIMB_BITS-1) bit modulo */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7368-L7375
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
sub_mod
static inline limb_t sub_mod(limb_t a, limb_t b, limb_t m) { limb_t r; r = a - b; if (r > a) r += m; return r; }
/* sub modulo with up to LIMB_BITS bit modulo */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7378-L7385
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
mod_fast
static inline limb_t mod_fast(dlimb_t r, limb_t m, limb_t m_inv) { limb_t a1, q, t0, r1, r0; a1 = r >> NTT_MOD_LOG2_MIN; q = ((dlimb_t)a1 * m_inv) >> LIMB_BITS; r = r - (dlimb_t)q * m - m * 2; r1 = r >> LIMB_BITS; t0 = (slimb_t)r1 >> 1; r += m & t0; r0 = r; r1 = r >> LIMB_BITS; r0 += m & r1; return r0; }
/* return (r0+r1*B) mod m precondition: 0 <= r0+r1*B < 2^(64+NTT_MOD_LOG2_MIN) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7390-L7406
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
mul_mod_fast
static inline limb_t mul_mod_fast(limb_t a, limb_t b, limb_t m, limb_t m_inv) { dlimb_t r; r = (dlimb_t)a * (dlimb_t)b; return mod_fast(r, m, m_inv); }
/* faster version using precomputed modulo inverse. precondition: 0 <= a * b < 2^(64+NTT_MOD_LOG2_MIN) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7410-L7416
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
mul_mod_fast2
static inline limb_t mul_mod_fast2(limb_t a, limb_t b, limb_t m, limb_t b_inv) { limb_t r, q; q = ((dlimb_t)a * (dlimb_t)b_inv) >> LIMB_BITS; r = a * b - q * m; if (r >= m) r -= m; return r; }
/* Faster version used when the multiplier is constant. 0 <= a < 2^64, 0 <= b < m. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7429-L7439
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
mul_mod_fast3
static inline limb_t mul_mod_fast3(limb_t a, limb_t b, limb_t m, limb_t b_inv) { limb_t r, q; q = ((dlimb_t)a * (dlimb_t)b_inv) >> LIMB_BITS; r = a * b - q * m; return r; }
/* Faster version used when the multiplier is constant. 0 <= a < 2^64, 0 <= b < m. Let r = a * b mod m. The return value is 'r' or 'r + m'. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7444-L7452
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
ntt_mod1
static inline __m256d ntt_mod1(__m256d r, __m256d m) { return _mm256_blendv_pd(r, r + m, r); }
/* return r + m if r < 0 otherwise r. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7485-L7488
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
ntt_mod
static inline __m256d ntt_mod(__m256d r, __m256d mf, __m256d m2f) { return _mm256_blendv_pd(r, r + m2f, r) - mf; }
/* input: abs(r) < 2 * m. Output: abs(r) < m */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7491-L7494
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
ntt_mul_mod
static inline __m256d ntt_mul_mod(__m256d a, __m256d b, __m256d mf, __m256d m_inv) { __m256d r, q, ab1, ab0, qm0, qm1; ab1 = a * b; q = _mm256_round_pd(ab1 * m_inv, 0); /* round to nearest */ qm1 = q * mf; qm0 = _mm256_fmsub_pd(q, mf, qm1); /* low part */ ab0 = _mm256_fmsub_pd(a, b, ab1); /* low part */ r = (ab1 - qm1) + (ab0 - qm0); return r; }
/* input: abs(a*b) < 2 * m^2, output: abs(r) < m */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7497-L7508
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
ntt_fft_partial
static int ntt_fft_partial(BFNTTState *s, NTTLimb *buf1, int k1, int k2, limb_t n1, limb_t n2, int inverse, limb_t m_idx) { limb_t i, j, c_mul, c0, m, m_inv, strip_len, l; NTTLimb *buf2, *buf3; buf2 = NULL; buf3 = ntt_malloc(s, sizeof(NTTLimb) * n1); if (!buf3) goto fail; if (k2 == 0) { if (ntt_fft(s, buf1, buf1, buf3, k1, inverse, m_idx)) goto fail; } else { strip_len = STRIP_LEN; buf2 = ntt_malloc(s, sizeof(NTTLimb) * n1 * strip_len); if (!buf2) goto fail; m = ntt_mods[m_idx]; m_inv = s->ntt_mods_div[m_idx]; c0 = s->ntt_proot_pow[m_idx][inverse][k1 + k2]; c_mul = 1; assert((n2 % strip_len) == 0); for(j = 0; j < n2; j += strip_len) { for(i = 0; i < n1; i++) { for(l = 0; l < strip_len; l++) { buf2[i + l * n1] = buf1[i * n2 + (j + l)]; } } for(l = 0; l < strip_len; l++) { if (inverse) mul_trig(buf2 + l * n1, n1, c_mul, m, m_inv); if (ntt_fft(s, buf2 + l * n1, buf2 + l * n1, buf3, k1, inverse, m_idx)) goto fail; if (!inverse) mul_trig(buf2 + l * n1, n1, c_mul, m, m_inv); c_mul = mul_mod_fast(c_mul, c0, m, m_inv); } for(i = 0; i < n1; i++) { for(l = 0; l < strip_len; l++) { buf1[i * n2 + (j + l)] = buf2[i + l *n1]; } } } ntt_free(s, buf2); } ntt_free(s, buf3); return 0; fail: ntt_free(s, buf2); ntt_free(s, buf3); return -1; }
/* dst = buf1, src = buf2 */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7882-L7936
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
ntt_conv
static int ntt_conv(BFNTTState *s, NTTLimb *buf1, NTTLimb *buf2, int k, int k_tot, limb_t m_idx) { limb_t n1, n2, i; int k1, k2; if (k <= NTT_TRIG_K_MAX) { k1 = k; } else { /* recursive split of the FFT */ k1 = bf_min(k / 2, NTT_TRIG_K_MAX); } k2 = k - k1; n1 = (limb_t)1 << k1; n2 = (limb_t)1 << k2; if (ntt_fft_partial(s, buf1, k1, k2, n1, n2, 0, m_idx)) return -1; if (ntt_fft_partial(s, buf2, k1, k2, n1, n2, 0, m_idx)) return -1; if (k2 == 0) { ntt_vec_mul(s, buf1, buf2, k, k_tot, m_idx); } else { for(i = 0; i < n1; i++) { ntt_conv(s, buf1 + i * n2, buf2 + i * n2, k2, k_tot, m_idx); } } if (ntt_fft_partial(s, buf1, k1, k2, n1, n2, 1, m_idx)) return -1; return 0; }
/* dst = buf1, src = buf2, tmp = buf3 */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L7940-L7970
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
fft_mul
static no_inline int fft_mul(bf_context_t *s1, bf_t *res, limb_t *a_tab, limb_t a_len, limb_t *b_tab, limb_t b_len, int mul_flags) { BFNTTState *s; int dpl, fft_len_log2, j, nb_mods, reduced_mem; slimb_t len, fft_len; NTTLimb *buf1, *buf2, *ptr; #if defined(USE_MUL_CHECK) limb_t ha, hb, hr, h_ref; #endif if (ntt_static_init(s1)) return -1; s = s1->ntt_state; /* find the optimal number of digits per limb (dpl) */ len = a_len + b_len; fft_len_log2 = bf_get_fft_size(&dpl, &nb_mods, len); fft_len = (uint64_t)1 << fft_len_log2; // printf("len=%" PRId64 " fft_len_log2=%d dpl=%d\n", len, fft_len_log2, dpl); #if defined(USE_MUL_CHECK) ha = mp_mod1(a_tab, a_len, BF_CHKSUM_MOD, 0); hb = mp_mod1(b_tab, b_len, BF_CHKSUM_MOD, 0); #endif if ((mul_flags & (FFT_MUL_R_OVERLAP_A | FFT_MUL_R_OVERLAP_B)) == 0) { if (!(mul_flags & FFT_MUL_R_NORESIZE)) bf_resize(res, 0); } else if (mul_flags & FFT_MUL_R_OVERLAP_B) { limb_t *tmp_tab, tmp_len; /* it is better to free 'b' first */ tmp_tab = a_tab; a_tab = b_tab; b_tab = tmp_tab; tmp_len = a_len; a_len = b_len; b_len = tmp_len; } buf1 = ntt_malloc(s, sizeof(NTTLimb) * fft_len * nb_mods); if (!buf1) return -1; limb_to_ntt(s, buf1, fft_len, a_tab, a_len, dpl, NB_MODS - nb_mods, nb_mods); if ((mul_flags & (FFT_MUL_R_OVERLAP_A | FFT_MUL_R_OVERLAP_B)) == FFT_MUL_R_OVERLAP_A) { if (!(mul_flags & FFT_MUL_R_NORESIZE)) bf_resize(res, 0); } reduced_mem = (fft_len_log2 >= 14); if (!reduced_mem) { buf2 = ntt_malloc(s, sizeof(NTTLimb) * fft_len * nb_mods); if (!buf2) goto fail; limb_to_ntt(s, buf2, fft_len, b_tab, b_len, dpl, NB_MODS - nb_mods, nb_mods); if (!(mul_flags & FFT_MUL_R_NORESIZE)) bf_resize(res, 0); /* in case res == b */ } else { buf2 = ntt_malloc(s, sizeof(NTTLimb) * fft_len); if (!buf2) goto fail; } for(j = 0; j < nb_mods; j++) { if (reduced_mem) { limb_to_ntt(s, buf2, fft_len, b_tab, b_len, dpl, NB_MODS - nb_mods + j, 1); ptr = buf2; } else { ptr = buf2 + fft_len * j; } if (ntt_conv(s, buf1 + fft_len * j, ptr, fft_len_log2, fft_len_log2, j + NB_MODS - nb_mods)) goto fail; } if (!(mul_flags & FFT_MUL_R_NORESIZE)) bf_resize(res, 0); /* in case res == b and reduced mem */ ntt_free(s, buf2); buf2 = NULL; if (!(mul_flags & FFT_MUL_R_NORESIZE)) { if (bf_resize(res, len)) goto fail; } ntt_to_limb(s, res->tab, len, buf1, fft_len_log2, dpl, nb_mods); ntt_free(s, buf1); #if defined(USE_MUL_CHECK) hr = mp_mod1(res->tab, len, BF_CHKSUM_MOD, 0); h_ref = mul_mod(ha, hb, BF_CHKSUM_MOD); if (hr != h_ref) { printf("ntt_mul_error: len=%" PRId_LIMB " fft_len_log2=%d dpl=%d nb_mods=%d\n", len, fft_len_log2, dpl, nb_mods); // printf("ha=0x" FMT_LIMB" hb=0x" FMT_LIMB " hr=0x" FMT_LIMB " expected=0x" FMT_LIMB "\n", ha, hb, hr, h_ref); exit(1); } #endif return 0; fail: ntt_free(s, buf1); ntt_free(s, buf2); return -1; }
/* return 0 if OK, -1 if memory error */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L8358-L8457
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
bf_get_fft_size
int bf_get_fft_size(int *pdpl, int *pnb_mods, limb_t len) { return 0; }
/* USE_FFT_MUL */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L8461-L8464
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
dbuf_insert
static int dbuf_insert(DynBuf *s, int pos, int len) { if (dbuf_realloc(s, s->size + len)) return -1; memmove(s->buf + pos + len, s->buf + pos, s->size - pos); s->size += len; return 0; }
/* insert 'len' bytes at position 'pos'. Return < 0 if error. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L114-L121
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
lre_canonicalize
static uint32_t lre_canonicalize(uint32_t c, BOOL is_utf16) { uint32_t res[LRE_CC_RES_LEN_MAX]; int len; if (is_utf16) { if (likely(c < 128)) { if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a'; } else { lre_case_conv(res, c, 2); c = res[0]; } } else { if (likely(c < 128)) { if (c >= 'a' && c <= 'z') c = c - 'a' + 'A'; } else { /* legacy regexp: to upper case if single char >= 128 */ len = lre_case_conv(res, c, FALSE); if (len == 1 && res[0] >= 128) c = res[0]; } } return c; }
/* canonicalize with the specific JS regexp rules */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L124-L148
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
re_emit_op_u32
static int re_emit_op_u32(REParseState *s, int op, uint32_t val) { int pos; dbuf_putc(&s->byte_code, op); pos = s->byte_code.size; dbuf_put_u32(&s->byte_code, val); return pos; }
/* return the offset of the u32 value */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L400-L407
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
parse_digits
static int parse_digits(const uint8_t **pp, BOOL allow_overflow) { const uint8_t *p; uint64_t v; int c; p = *pp; v = 0; for(;;) { c = *p; if (c < '0' || c > '9') break; v = v * 10 + c - '0'; if (v >= INT32_MAX) { if (allow_overflow) v = INT32_MAX; else return -1; } p++; } *pp = p; return v; }
/* If allow_overflow is false, return -1 in case of overflow. Otherwise return INT32_MAX. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L446-L469
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
lre_parse_escape
int lre_parse_escape(const uint8_t **pp, int allow_utf16) { const uint8_t *p; uint32_t c; p = *pp; c = *p++; switch(c) { case 'b': c = '\b'; break; case 'f': c = '\f'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; case 'v': c = '\v'; break; case 'x': case 'u': { int h, n, i; uint32_t c1; if (*p == '{' && allow_utf16) { p++; c = 0; for(;;) { h = from_hex(*p++); if (h < 0) return -1; c = (c << 4) | h; if (c > 0x10FFFF) return -1; if (*p == '}') break; } p++; } else { if (c == 'x') { n = 2; } else { n = 4; } c = 0; for(i = 0; i < n; i++) { h = from_hex(*p++); if (h < 0) { return -1; } c = (c << 4) | h; } if (c >= 0xd800 && c < 0xdc00 && allow_utf16 == 2 && p[0] == '\\' && p[1] == 'u') { /* convert an escaped surrogate pair into a unicode char */ c1 = 0; for(i = 0; i < 4; i++) { h = from_hex(p[2 + i]); if (h < 0) break; c1 = (c1 << 4) | h; } if (i == 4 && c1 >= 0xdc00 && c1 < 0xe000) { p += 6; c = (((c & 0x3ff) << 10) | (c1 & 0x3ff)) + 0x10000; } } } } break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': c -= '0'; if (allow_utf16 == 2) { /* only accept \0 not followed by digit */ if (c != 0 || is_digit(*p)) return -1; } else { /* parse a legacy octal sequence */ uint32_t v; v = *p - '0'; if (v > 7) break; c = (c << 3) | v; p++; if (c >= 32) break; v = *p - '0'; if (v > 7) break; c = (c << 3) | v; p++; } break; default: return -2; } *pp = p; return c; }
/* Parse an escape sequence, *pp points after the '\': allow_utf16 value: 0 : no UTF-16 escapes allowed 1 : UTF-16 escapes allowed 2 : UTF-16 escapes allowed and escapes of surrogate pairs are converted to a unicode character (unicode regexp case). Return the unicode char and update *pp if recognized, return -1 if malformed escape, return -2 otherwise. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L492-L601
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
is_unicode_char
static BOOL is_unicode_char(int c) { return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')); }
/* XXX: we use the same chars for name and value */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L605-L611
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
get_class_atom
static int get_class_atom(REParseState *s, CharRange *cr, const uint8_t **pp, BOOL inclass) { const uint8_t *p; uint32_t c; int ret; p = *pp; c = *p; switch(c) { case '\\': p++; if (p >= s->buf_end) goto unexpected_end; c = *p++; switch(c) { case 'd': c = CHAR_RANGE_d; goto class_range; case 'D': c = CHAR_RANGE_D; goto class_range; case 's': c = CHAR_RANGE_s; goto class_range; case 'S': c = CHAR_RANGE_S; goto class_range; case 'w': c = CHAR_RANGE_w; goto class_range; case 'W': c = CHAR_RANGE_W; class_range: if (cr_init_char_range(s, cr, c)) return -1; c = CLASS_RANGE_BASE; break; case 'c': c = *p; if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (((c >= '0' && c <= '9') || c == '_') && inclass && !s->is_utf16)) { /* Annex B.1.4 */ c &= 0x1f; p++; } else if (s->is_utf16) { goto invalid_escape; } else { /* otherwise return '\' and 'c' */ p--; c = '\\'; } break; #ifdef CONFIG_ALL_UNICODE case 'p': case 'P': if (s->is_utf16) { if (parse_unicode_property(s, cr, &p, (c == 'P'))) return -1; c = CLASS_RANGE_BASE; break; } /* fall thru */ #endif default: p--; ret = lre_parse_escape(&p, s->is_utf16 * 2); if (ret >= 0) { c = ret; } else { if (ret == -2 && *p != '\0' && strchr("^$\\.*+?()[]{}|/", *p)) { /* always valid to escape these characters */ goto normal_char; } else if (s->is_utf16) { invalid_escape: return re_parse_error(s, "invalid escape sequence in regular expression"); } else { /* just ignore the '\' */ goto normal_char; } } break; } break; case '\0': if (p >= s->buf_end) { unexpected_end: return re_parse_error(s, "unexpected end"); } /* fall thru */ default: normal_char: /* normal char */ if (c >= 128) { c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p); if ((unsigned)c > 0xffff && !s->is_utf16) { /* XXX: should handle non BMP-1 code points */ return re_parse_error(s, "malformed unicode char"); } } else { p++; } break; } *pp = p; return c; }
/* CONFIG_ALL_UNICODE */ /* return -1 if error otherwise the character or a class range (CLASS_RANGE_BASE). In case of class range, 'cr' is initialized. Otherwise, it is ignored. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L711-L819
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
re_check_advance
static int re_check_advance(const uint8_t *bc_buf, int bc_buf_len) { int pos, opcode, ret, len, i; uint32_t val, last; BOOL has_back_reference; uint8_t capture_bitmap[CAPTURE_COUNT_MAX]; ret = -2; /* not known yet */ pos = 0; has_back_reference = FALSE; memset(capture_bitmap, 0, sizeof(capture_bitmap)); while (pos < bc_buf_len) { opcode = bc_buf[pos]; len = reopcode_info[opcode].size; switch(opcode) { case REOP_range: val = get_u16(bc_buf + pos + 1); len += val * 4; goto simple_char; case REOP_range32: val = get_u16(bc_buf + pos + 1); len += val * 8; goto simple_char; case REOP_char: case REOP_char32: case REOP_dot: case REOP_any: simple_char: if (ret == -2) ret = 1; break; case REOP_line_start: case REOP_line_end: case REOP_push_i32: case REOP_push_char_pos: case REOP_drop: case REOP_word_boundary: case REOP_not_word_boundary: case REOP_prev: /* no effect */ break; case REOP_save_start: case REOP_save_end: val = bc_buf[pos + 1]; capture_bitmap[val] |= 1; break; case REOP_save_reset: { val = bc_buf[pos + 1]; last = bc_buf[pos + 2]; while (val < last) capture_bitmap[val++] |= 1; } break; case REOP_back_reference: case REOP_backward_back_reference: val = bc_buf[pos + 1]; capture_bitmap[val] |= 2; has_back_reference = TRUE; break; default: /* safe behvior: we cannot predict the outcome */ if (ret == -2) ret = 0; break; } pos += len; } if (has_back_reference) { /* check if there is back reference which references a capture made in the some code */ for(i = 0; i < CAPTURE_COUNT_MAX; i++) { if (capture_bitmap[i] == 3) return -1; } } if (ret == -2) ret = 0; return ret; }
/* Return: 1 if the opcodes in bc_buf[] always advance the character pointer. 0 if the character pointer may not be advanced. -1 if the code may depend on side effects of its previous execution (backreference) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L950-L1030
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
re_is_simple_quantifier
static int re_is_simple_quantifier(const uint8_t *bc_buf, int bc_buf_len) { int pos, opcode, len, count; uint32_t val; count = 0; pos = 0; while (pos < bc_buf_len) { opcode = bc_buf[pos]; len = reopcode_info[opcode].size; switch(opcode) { case REOP_range: val = get_u16(bc_buf + pos + 1); len += val * 4; goto simple_char; case REOP_range32: val = get_u16(bc_buf + pos + 1); len += val * 8; goto simple_char; case REOP_char: case REOP_char32: case REOP_dot: case REOP_any: simple_char: count++; break; case REOP_line_start: case REOP_line_end: case REOP_word_boundary: case REOP_not_word_boundary: break; default: return -1; } pos += len; } return count; }
/* return -1 if a simple quantifier cannot be used. Otherwise return the number of characters in the atom. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L1034-L1071
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
re_parse_group_name
static int re_parse_group_name(char *buf, int buf_size, const uint8_t **pp, BOOL is_utf16) { const uint8_t *p; uint32_t c; char *q; p = *pp; q = buf; for(;;) { c = *p; if (c == '\\') { p++; if (*p != 'u') return -1; c = lre_parse_escape(&p, is_utf16 * 2); } else if (c == '>') { break; } else if (c >= 128) { c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p); } else { p++; } if (c > 0x10FFFF) return -1; if (q == buf) { if (!lre_js_is_ident_first(c)) return -1; } else { if (!lre_js_is_ident_next(c)) return -1; } if ((q - buf + UTF8_CHAR_LEN_MAX + 1) > buf_size) return -1; if (c < 128) { *q++ = c; } else { q += unicode_to_utf8((uint8_t*)q, c); } } if (q == buf) return -1; *q = '\0'; p++; *pp = p; return 0; }
/* '*pp' is the first char after '<' */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L1074-L1120
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
re_parse_captures
static int re_parse_captures(REParseState *s, int *phas_named_captures, const char *capture_name) { const uint8_t *p; int capture_index; char name[TMP_BUF_SIZE]; capture_index = 1; *phas_named_captures = 0; for (p = s->buf_start; p < s->buf_end; p++) { switch (*p) { case '(': if (p[1] == '?') { if (p[2] == '<' && p[3] != '=' && p[3] != '!') { *phas_named_captures = 1; /* potential named capture */ if (capture_name) { p += 3; if (re_parse_group_name(name, sizeof(name), &p, s->is_utf16) == 0) { if (!strcmp(name, capture_name)) return capture_index; } } capture_index++; if (capture_index >= CAPTURE_COUNT_MAX) goto done; } } else { capture_index++; if (capture_index >= CAPTURE_COUNT_MAX) goto done; } break; case '\\': p++; break; case '[': for (p += 1 + (*p == ']'); p < s->buf_end && *p != ']'; p++) { if (*p == '\\') p++; } break; } } done: if (capture_name) return -1; else return capture_index; }
/* if capture_name = NULL: return the number of captures + 1. Otherwise, return the capture index corresponding to capture_name or -1 if none */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L1125-L1175
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
compute_stack_size
static int compute_stack_size(const uint8_t *bc_buf, int bc_buf_len) { int stack_size, stack_size_max, pos, opcode, len; uint32_t val; stack_size = 0; stack_size_max = 0; bc_buf += RE_HEADER_LEN; bc_buf_len -= RE_HEADER_LEN; pos = 0; while (pos < bc_buf_len) { opcode = bc_buf[pos]; len = reopcode_info[opcode].size; assert(opcode < REOP_COUNT); assert((pos + len) <= bc_buf_len); switch(opcode) { case REOP_push_i32: case REOP_push_char_pos: stack_size++; if (stack_size > stack_size_max) { if (stack_size > STACK_SIZE_MAX) return -1; stack_size_max = stack_size; } break; case REOP_drop: case REOP_bne_char_pos: assert(stack_size > 0); stack_size--; break; case REOP_range: val = get_u16(bc_buf + pos + 1); len += val * 4; break; case REOP_range32: val = get_u16(bc_buf + pos + 1); len += val * 8; break; } pos += len; } return stack_size_max; }
/* the control flow is recursive so the analysis can be linear */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L1773-L1815
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
lre_exec_backtrack
static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture, StackInt *stack, int stack_len, const uint8_t *pc, const uint8_t *cptr, BOOL no_recurse) { int opcode, ret; int cbuf_type; uint32_t val, c; const uint8_t *cbuf_end; cbuf_type = s->cbuf_type; cbuf_end = s->cbuf_end; for(;;) { // printf("top=%p: pc=%d\n", th_list.top, (int)(pc - (bc_buf + RE_HEADER_LEN))); opcode = *pc++; switch(opcode) { case REOP_match: { REExecState *rs; if (no_recurse) return (intptr_t)cptr; ret = 1; goto recurse; no_match: if (no_recurse) return 0; ret = 0; recurse: for(;;) { if (s->state_stack_len == 0) return ret; rs = (REExecState *)(s->state_stack + (s->state_stack_len - 1) * s->state_size); if (rs->type == RE_EXEC_STATE_SPLIT) { if (!ret) { pop_state: memcpy(capture, rs->buf, sizeof(capture[0]) * 2 * s->capture_count); pop_state1: pc = rs->pc; cptr = rs->cptr; stack_len = rs->stack_len; memcpy(stack, rs->buf + 2 * s->capture_count, stack_len * sizeof(stack[0])); s->state_stack_len--; break; } } else if (rs->type == RE_EXEC_STATE_GREEDY_QUANT) { if (!ret) { uint32_t char_count, i; memcpy(capture, rs->buf, sizeof(capture[0]) * 2 * s->capture_count); stack_len = rs->stack_len; memcpy(stack, rs->buf + 2 * s->capture_count, stack_len * sizeof(stack[0])); pc = rs->pc; cptr = rs->cptr; /* go backward */ char_count = get_u32(pc + 12); for(i = 0; i < char_count; i++) { PREV_CHAR(cptr, s->cbuf); } pc = (pc + 16) + (int)get_u32(pc); rs->cptr = cptr; rs->count--; if (rs->count == 0) { s->state_stack_len--; } break; } } else { ret = ((rs->type == RE_EXEC_STATE_LOOKAHEAD && ret) || (rs->type == RE_EXEC_STATE_NEGATIVE_LOOKAHEAD && !ret)); if (ret) { /* keep the capture in case of positive lookahead */ if (rs->type == RE_EXEC_STATE_LOOKAHEAD) goto pop_state1; else goto pop_state; } } s->state_stack_len--; } } break; case REOP_char32: val = get_u32(pc); pc += 4; goto test_char; case REOP_char: val = get_u16(pc); pc += 2; test_char: if (cptr >= cbuf_end) goto no_match; GET_CHAR(c, cptr, cbuf_end); if (s->ignore_case) { c = lre_canonicalize(c, s->is_utf16); } if (val != c) goto no_match; break; case REOP_split_goto_first: case REOP_split_next_first: { const uint8_t *pc1; val = get_u32(pc); pc += 4; if (opcode == REOP_split_next_first) { pc1 = pc + (int)val; } else { pc1 = pc; pc = pc + (int)val; } ret = push_state(s, capture, stack, stack_len, pc1, cptr, RE_EXEC_STATE_SPLIT, 0); if (ret < 0) return -1; break; } case REOP_lookahead: case REOP_negative_lookahead: val = get_u32(pc); pc += 4; ret = push_state(s, capture, stack, stack_len, pc + (int)val, cptr, RE_EXEC_STATE_LOOKAHEAD + opcode - REOP_lookahead, 0); if (ret < 0) return -1; break; case REOP_goto: val = get_u32(pc); pc += 4 + (int)val; break; case REOP_line_start: if (cptr == s->cbuf) break; if (!s->multi_line) goto no_match; PEEK_PREV_CHAR(c, cptr, s->cbuf); if (!is_line_terminator(c)) goto no_match; break; case REOP_line_end: if (cptr == cbuf_end) break; if (!s->multi_line) goto no_match; PEEK_CHAR(c, cptr, cbuf_end); if (!is_line_terminator(c)) goto no_match; break; case REOP_dot: if (cptr == cbuf_end) goto no_match; GET_CHAR(c, cptr, cbuf_end); if (is_line_terminator(c)) goto no_match; break; case REOP_any: if (cptr == cbuf_end) goto no_match; GET_CHAR(c, cptr, cbuf_end); break; case REOP_save_start: case REOP_save_end: val = *pc++; assert(val < s->capture_count); capture[2 * val + opcode - REOP_save_start] = (uint8_t *)cptr; break; case REOP_save_reset: { uint32_t val2; val = pc[0]; val2 = pc[1]; pc += 2; assert(val2 < s->capture_count); while (val <= val2) { capture[2 * val] = NULL; capture[2 * val + 1] = NULL; val++; } } break; case REOP_push_i32: val = get_u32(pc); pc += 4; stack[stack_len++] = val; break; case REOP_drop: stack_len--; break; case REOP_loop: val = get_u32(pc); pc += 4; if (--stack[stack_len - 1] != 0) { pc += (int)val; } break; case REOP_push_char_pos: stack[stack_len++] = (uintptr_t)cptr; break; case REOP_bne_char_pos: val = get_u32(pc); pc += 4; if (stack[--stack_len] != (uintptr_t)cptr) pc += (int)val; break; case REOP_word_boundary: case REOP_not_word_boundary: { BOOL v1, v2; /* char before */ if (cptr == s->cbuf) { v1 = FALSE; } else { PEEK_PREV_CHAR(c, cptr, s->cbuf); v1 = is_word_char(c); } /* current char */ if (cptr >= cbuf_end) { v2 = FALSE; } else { PEEK_CHAR(c, cptr, cbuf_end); v2 = is_word_char(c); } if (v1 ^ v2 ^ (REOP_not_word_boundary - opcode)) goto no_match; } break; case REOP_back_reference: case REOP_backward_back_reference: { const uint8_t *cptr1, *cptr1_end, *cptr1_start; uint32_t c1, c2; val = *pc++; if (val >= s->capture_count) goto no_match; cptr1_start = capture[2 * val]; cptr1_end = capture[2 * val + 1]; if (!cptr1_start || !cptr1_end) break; if (opcode == REOP_back_reference) { cptr1 = cptr1_start; while (cptr1 < cptr1_end) { if (cptr >= cbuf_end) goto no_match; GET_CHAR(c1, cptr1, cptr1_end); GET_CHAR(c2, cptr, cbuf_end); if (s->ignore_case) { c1 = lre_canonicalize(c1, s->is_utf16); c2 = lre_canonicalize(c2, s->is_utf16); } if (c1 != c2) goto no_match; } } else { cptr1 = cptr1_end; while (cptr1 > cptr1_start) { if (cptr == s->cbuf) goto no_match; GET_PREV_CHAR(c1, cptr1, cptr1_start); GET_PREV_CHAR(c2, cptr, s->cbuf); if (s->ignore_case) { c1 = lre_canonicalize(c1, s->is_utf16); c2 = lre_canonicalize(c2, s->is_utf16); } if (c1 != c2) goto no_match; } } } break; case REOP_range: { int n; uint32_t low, high, idx_min, idx_max, idx; n = get_u16(pc); /* n must be >= 1 */ pc += 2; if (cptr >= cbuf_end) goto no_match; GET_CHAR(c, cptr, cbuf_end); if (s->ignore_case) { c = lre_canonicalize(c, s->is_utf16); } idx_min = 0; low = get_u16(pc + 0 * 4); if (c < low) goto no_match; idx_max = n - 1; high = get_u16(pc + idx_max * 4 + 2); /* 0xffff in for last value means +infinity */ if (unlikely(c >= 0xffff) && high == 0xffff) goto range_match; if (c > high) goto no_match; while (idx_min <= idx_max) { idx = (idx_min + idx_max) / 2; low = get_u16(pc + idx * 4); high = get_u16(pc + idx * 4 + 2); if (c < low) idx_max = idx - 1; else if (c > high) idx_min = idx + 1; else goto range_match; } goto no_match; range_match: pc += 4 * n; } break; case REOP_range32: { int n; uint32_t low, high, idx_min, idx_max, idx; n = get_u16(pc); /* n must be >= 1 */ pc += 2; if (cptr >= cbuf_end) goto no_match; GET_CHAR(c, cptr, cbuf_end); if (s->ignore_case) { c = lre_canonicalize(c, s->is_utf16); } idx_min = 0; low = get_u32(pc + 0 * 8); if (c < low) goto no_match; idx_max = n - 1; high = get_u32(pc + idx_max * 8 + 4); if (c > high) goto no_match; while (idx_min <= idx_max) { idx = (idx_min + idx_max) / 2; low = get_u32(pc + idx * 8); high = get_u32(pc + idx * 8 + 4); if (c < low) idx_max = idx - 1; else if (c > high) idx_min = idx + 1; else goto range32_match; } goto no_match; range32_match: pc += 8 * n; } break; case REOP_prev: /* go to the previous char */ if (cptr == s->cbuf) goto no_match; PREV_CHAR(cptr, s->cbuf); break; case REOP_simple_greedy_quant: { uint32_t next_pos, quant_min, quant_max; size_t q; intptr_t res; const uint8_t *pc1; next_pos = get_u32(pc); quant_min = get_u32(pc + 4); quant_max = get_u32(pc + 8); pc += 16; pc1 = pc; pc += (int)next_pos; q = 0; for(;;) { res = lre_exec_backtrack(s, capture, stack, stack_len, pc1, cptr, TRUE); if (res == -1) return res; if (!res) break; cptr = (uint8_t *)res; q++; if (q >= quant_max && quant_max != INT32_MAX) break; } if (q < quant_min) goto no_match; if (q > quant_min) { /* will examine all matches down to quant_min */ ret = push_state(s, capture, stack, stack_len, pc1 - 16, cptr, RE_EXEC_STATE_GREEDY_QUANT, q - quant_min); if (ret < 0) return -1; } } break; default: abort(); } } }
/* return 1 if match, 0 if not match or -1 if error. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L2089-L2494
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
lre_exec
int lre_exec(uint8_t **capture, const uint8_t *bc_buf, const uint8_t *cbuf, int cindex, int clen, int cbuf_type, void *opaque) { REExecContext s_s, *s = &s_s; int re_flags, i, alloca_size, ret; StackInt *stack_buf; re_flags = bc_buf[RE_HEADER_FLAGS]; s->multi_line = (re_flags & LRE_FLAG_MULTILINE) != 0; s->ignore_case = (re_flags & LRE_FLAG_IGNORECASE) != 0; s->is_utf16 = (re_flags & LRE_FLAG_UTF16) != 0; s->capture_count = bc_buf[RE_HEADER_CAPTURE_COUNT]; s->stack_size_max = bc_buf[RE_HEADER_STACK_SIZE]; s->cbuf = cbuf; s->cbuf_end = cbuf + (clen << cbuf_type); s->cbuf_type = cbuf_type; if (s->cbuf_type == 1 && s->is_utf16) s->cbuf_type = 2; s->opaque = opaque; s->state_size = sizeof(REExecState) + s->capture_count * sizeof(capture[0]) * 2 + s->stack_size_max * sizeof(stack_buf[0]); s->state_stack = NULL; s->state_stack_len = 0; s->state_stack_size = 0; for(i = 0; i < s->capture_count * 2; i++) capture[i] = NULL; alloca_size = s->stack_size_max * sizeof(stack_buf[0]); stack_buf = alloca(alloca_size); ret = lre_exec_backtrack(s, capture, stack_buf, 0, bc_buf + RE_HEADER_LEN, cbuf + (cindex << cbuf_type), FALSE); lre_realloc(s->opaque, s->state_stack, 0); return ret; }
/* Return 1 if match, 0 if not match or -1 if error. cindex is the starting position of the match and must be such as 0 <= cindex <= clen. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libregexp.c#L2499-L2535
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
lre_case_conv
int lre_case_conv(uint32_t *res, uint32_t c, int conv_type) { if (c < 128) { if (conv_type) { if (c >= 'A' && c <= 'Z') { c = c - 'A' + 'a'; } } else { if (c >= 'a' && c <= 'z') { c = c - 'a' + 'A'; } } } else { uint32_t v, code, data, type, len, a, is_lower; int idx, idx_min, idx_max; is_lower = (conv_type != 0); idx_min = 0; idx_max = countof(case_conv_table1) - 1; while (idx_min <= idx_max) { idx = (unsigned)(idx_max + idx_min) / 2; v = case_conv_table1[idx]; code = v >> (32 - 17); len = (v >> (32 - 17 - 7)) & 0x7f; if (c < code) { idx_max = idx - 1; } else if (c >= code + len) { idx_min = idx + 1; } else { type = (v >> (32 - 17 - 7 - 4)) & 0xf; data = ((v & 0xf) << 8) | case_conv_table2[idx]; switch(type) { case RUN_TYPE_U: case RUN_TYPE_L: case RUN_TYPE_UF: case RUN_TYPE_LF: if (conv_type == (type & 1) || (type >= RUN_TYPE_UF && conv_type == 2)) { c = c - code + (case_conv_table1[data] >> (32 - 17)); } break; case RUN_TYPE_UL: a = c - code; if ((a & 1) != (1 - is_lower)) break; c = (a ^ 1) + code; break; case RUN_TYPE_LSU: a = c - code; if (a == 1) { c += 2 * is_lower - 1; } else if (a == (1 - is_lower) * 2) { c += (2 * is_lower - 1) * 2; } break; case RUN_TYPE_U2L_399_EXT2: if (!is_lower) { res[0] = c - code + case_conv_ext[data >> 6]; res[1] = 0x399; return 2; } else { c = c - code + case_conv_ext[data & 0x3f]; } break; case RUN_TYPE_UF_D20: if (conv_type == 1) break; c = data + (conv_type == 2) * 0x20; break; case RUN_TYPE_UF_D1_EXT: if (conv_type == 1) break; c = case_conv_ext[data] + (conv_type == 2); break; case RUN_TYPE_U_EXT: case RUN_TYPE_LF_EXT: if (is_lower != (type - RUN_TYPE_U_EXT)) break; c = case_conv_ext[data]; break; case RUN_TYPE_U_EXT2: case RUN_TYPE_L_EXT2: if (conv_type != (type - RUN_TYPE_U_EXT2)) break; res[0] = c - code + case_conv_ext[data >> 6]; res[1] = case_conv_ext[data & 0x3f]; return 2; default: case RUN_TYPE_U_EXT3: if (conv_type != 0) break; res[0] = case_conv_ext[data >> 8]; res[1] = case_conv_ext[(data >> 4) & 0xf]; res[2] = case_conv_ext[data & 0xf]; return 3; } break; } } } res[0] = c; return 1; }
/* conv_type: 0 = to upper 1 = to lower 2 = case folding (= to lower with modifications) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L56-L158
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
get_index_pos
static int get_index_pos(uint32_t *pcode, uint32_t c, const uint8_t *index_table, int index_table_len) { uint32_t code, v; int idx_min, idx_max, idx; idx_min = 0; v = get_le24(index_table); code = v & ((1 << 21) - 1); if (c < code) { *pcode = 0; return 0; } idx_max = index_table_len - 1; code = get_le24(index_table + idx_max * 3); if (c >= code) return -1; /* invariant: tab[idx_min] <= c < tab2[idx_max] */ while ((idx_max - idx_min) > 1) { idx = (idx_max + idx_min) / 2; v = get_le24(index_table + idx * 3); code = v & ((1 << 21) - 1); if (c < code) { idx_max = idx; } else { idx_min = idx; } } v = get_le24(index_table + idx_min * 3); *pcode = v & ((1 << 21) - 1); return (idx_min + 1) * UNICODE_INDEX_BLOCK_LEN + (v >> 21); }
/* return -1 if not in table, otherwise the offset in the block */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L172-L203
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
cr_dump
static __maybe_unused void cr_dump(CharRange *cr) { int i; for(i = 0; i < cr->len; i++) printf("%d: 0x%04x\n", i, cr->points[i]); }
/* character range */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L274-L279
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
cr_compress
static void cr_compress(CharRange *cr) { int i, j, k, len; uint32_t *pt; pt = cr->points; len = cr->len; i = 0; j = 0; k = 0; while ((i + 1) < len) { if (pt[i] == pt[i + 1]) { /* empty interval */ i += 2; } else { j = i; while ((j + 3) < len && pt[j + 1] == pt[j + 2]) j += 2; /* just copy */ pt[k] = pt[i]; pt[k + 1] = pt[j + 1]; k += 2; i = j + 2; } } cr->len = k; }
/* merge consecutive intervals and remove empty intervals */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L326-L352
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
cr_op
int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len, const uint32_t *b_pt, int b_len, int op) { int a_idx, b_idx, is_in; uint32_t v; a_idx = 0; b_idx = 0; for(;;) { /* get one more point from a or b in increasing order */ if (a_idx < a_len && b_idx < b_len) { if (a_pt[a_idx] < b_pt[b_idx]) { goto a_add; } else if (a_pt[a_idx] == b_pt[b_idx]) { v = a_pt[a_idx]; a_idx++; b_idx++; } else { goto b_add; } } else if (a_idx < a_len) { a_add: v = a_pt[a_idx++]; } else if (b_idx < b_len) { b_add: v = b_pt[b_idx++]; } else { break; } /* add the point if the in/out status changes */ switch(op) { case CR_OP_UNION: is_in = (a_idx & 1) | (b_idx & 1); break; case CR_OP_INTER: is_in = (a_idx & 1) & (b_idx & 1); break; case CR_OP_XOR: is_in = (a_idx & 1) ^ (b_idx & 1); break; default: abort(); } if (is_in != (cr->len & 1)) { if (cr_add_point(cr, v)) return -1; } } cr_compress(cr); return 0; }
/* union or intersection */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L355-L405
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
unicode_decomp_char
static int unicode_decomp_char(uint32_t *res, uint32_t c, BOOL is_compat1) { uint32_t v, type, is_compat, code, len; int idx_min, idx_max, idx; idx_min = 0; idx_max = countof(unicode_decomp_table1) - 1; while (idx_min <= idx_max) { idx = (idx_max + idx_min) / 2; v = unicode_decomp_table1[idx]; code = v >> (32 - 18); len = (v >> (32 - 18 - 7)) & 0x7f; // printf("idx=%d code=%05x len=%d\n", idx, code, len); if (c < code) { idx_max = idx - 1; } else if (c >= code + len) { idx_min = idx + 1; } else { is_compat = v & 1; if (is_compat1 < is_compat) break; type = (v >> (32 - 18 - 7 - 6)) & 0x3f; return unicode_decomp_entry(res, c, idx, code, len, type); } } return 0; }
/* return the length of the decomposition (length <= UNICODE_DECOMP_LEN_MAX) or 0 if no decomposition */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L657-L683
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
unicode_compose_pair
static int unicode_compose_pair(uint32_t c0, uint32_t c1) { uint32_t code, len, type, v, idx1, d_idx, d_offset, ch; int idx_min, idx_max, idx, d; uint32_t pair[2]; idx_min = 0; idx_max = countof(unicode_comp_table) - 1; while (idx_min <= idx_max) { idx = (idx_max + idx_min) / 2; idx1 = unicode_comp_table[idx]; /* idx1 represent an entry of the decomposition table */ d_idx = idx1 >> 6; d_offset = idx1 & 0x3f; v = unicode_decomp_table1[d_idx]; code = v >> (32 - 18); len = (v >> (32 - 18 - 7)) & 0x7f; type = (v >> (32 - 18 - 7 - 6)) & 0x3f; ch = code + d_offset; unicode_decomp_entry(pair, ch, d_idx, code, len, type); d = c0 - pair[0]; if (d == 0) d = c1 - pair[1]; if (d < 0) { idx_max = idx - 1; } else if (d > 0) { idx_min = idx + 1; } else { return ch; } } return 0; }
/* return 0 if no pair found */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L686-L719
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
unicode_get_cc
static int unicode_get_cc(uint32_t c) { uint32_t code, n, type, cc, c1, b; int pos; const uint8_t *p; pos = get_index_pos(&code, c, unicode_cc_index, sizeof(unicode_cc_index) / 3); if (pos < 0) return 0; p = unicode_cc_table + pos; for(;;) { b = *p++; type = b >> 6; n = b & 0x3f; if (n < 48) { } else if (n < 56) { n = (n - 48) << 8; n |= *p++; n += 48; } else { n = (n - 56) << 8; n |= *p++ << 8; n |= *p++; n += 48 + (1 << 11); } if (type <= 1) p++; c1 = code + n + 1; if (c < c1) { switch(type) { case 0: cc = p[-1]; break; case 1: cc = p[-1] + c - code; break; case 2: cc = 0; break; default: case 3: cc = 230; break; } return cc; } code = c1; } }
/* return the combining class of character c (between 0 and 255) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L722-L771
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
compose_pair
static int compose_pair(uint32_t c0, uint32_t c1) { /* Hangul composition */ if (c0 >= 0x1100 && c0 < 0x1100 + 19 && c1 >= 0x1161 && c1 < 0x1161 + 21) { return 0xac00 + (c0 - 0x1100) * 588 + (c1 - 0x1161) * 28; } else if (c0 >= 0xac00 && c0 < 0xac00 + 11172 && (c0 - 0xac00) % 28 == 0 && c1 >= 0x11a7 && c1 < 0x11a7 + 28) { return c0 + c1 - 0x11a7; } else { return unicode_compose_pair(c0, c1); } }
/* return 0 if not found */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L838-L851
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
unicode_find_name
static int unicode_find_name(const char *name_table, const char *name) { const char *p, *r; int pos; size_t name_len, len; p = name_table; pos = 0; name_len = strlen(name); while (*p) { for(;;) { r = strchr(p, ','); if (!r) len = strlen(p); else len = r - p; if (len == name_len && !memcmp(p, name, name_len)) return pos; p += len + 1; if (!r) break; } pos++; } return -1; }
/* char ranges for various unicode properties */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L928-L953
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
unicode_script
int unicode_script(CharRange *cr, const char *script_name, BOOL is_ext) { int script_idx; const uint8_t *p, *p_end; uint32_t c, c1, b, n, v, v_len, i, type; CharRange cr1_s, *cr1; CharRange cr2_s, *cr2 = &cr2_s; BOOL is_common; script_idx = unicode_find_name(unicode_script_name_table, script_name); if (script_idx < 0) return -2; /* Note: we remove the "Unknown" Script */ script_idx += UNICODE_SCRIPT_Unknown + 1; is_common = (script_idx == UNICODE_SCRIPT_Common || script_idx == UNICODE_SCRIPT_Inherited); if (is_ext) { cr1 = &cr1_s; cr_init(cr1, cr->mem_opaque, cr->realloc_func); cr_init(cr2, cr->mem_opaque, cr->realloc_func); } else { cr1 = cr; } p = unicode_script_table; p_end = unicode_script_table + countof(unicode_script_table); c = 0; while (p < p_end) { b = *p++; type = b >> 7; n = b & 0x7f; if (n < 96) { } else if (n < 112) { n = (n - 96) << 8; n |= *p++; n += 96; } else { n = (n - 112) << 16; n |= *p++ << 8; n |= *p++; n += 96 + (1 << 12); } if (type == 0) v = 0; else v = *p++; c1 = c + n + 1; if (v == script_idx) { if (cr_add_interval(cr1, c, c1)) goto fail; } c = c1; } if (is_ext) { /* add the script extensions */ p = unicode_script_ext_table; p_end = unicode_script_ext_table + countof(unicode_script_ext_table); c = 0; while (p < p_end) { b = *p++; if (b < 128) { n = b; } else if (b < 128 + 64) { n = (b - 128) << 8; n |= *p++; n += 128; } else { n = (b - 128 - 64) << 16; n |= *p++ << 8; n |= *p++; n += 128 + (1 << 14); } c1 = c + n + 1; v_len = *p++; if (is_common) { if (v_len != 0) { if (cr_add_interval(cr2, c, c1)) goto fail; } } else { for(i = 0; i < v_len; i++) { if (p[i] == script_idx) { if (cr_add_interval(cr2, c, c1)) goto fail; break; } } } p += v_len; c = c1; } if (is_common) { /* remove all the characters with script extensions */ if (cr_invert(cr2)) goto fail; if (cr_op(cr, cr1->points, cr1->len, cr2->points, cr2->len, CR_OP_INTER)) goto fail; } else { if (cr_op(cr, cr1->points, cr1->len, cr2->points, cr2->len, CR_OP_UNION)) goto fail; } cr_free(cr1); cr_free(cr2); } return 0; fail: if (is_ext) { cr_free(cr1); cr_free(cr2); } goto fail; }
/* 'cr' must be initialized and empty. Return 0 if OK, -1 if error, -2 if not found */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L957-L1073
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
unicode_case1
static int unicode_case1(CharRange *cr, int case_mask) { #define MR(x) (1 << RUN_TYPE_ ## x) const uint32_t tab_run_mask[3] = { MR(U) | MR(UF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(UF_D20) | MR(UF_D1_EXT) | MR(U_EXT) | MR(U_EXT2) | MR(U_EXT3), MR(L) | MR(LF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(LF_EXT) | MR(L_EXT2), MR(UF) | MR(LF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(LF_EXT) | MR(UF_D20) | MR(UF_D1_EXT) | MR(LF_EXT), }; #undef MR uint32_t mask, v, code, type, len, i, idx; if (case_mask == 0) return 0; mask = 0; for(i = 0; i < 3; i++) { if ((case_mask >> i) & 1) mask |= tab_run_mask[i]; } for(idx = 0; idx < countof(case_conv_table1); idx++) { v = case_conv_table1[idx]; type = (v >> (32 - 17 - 7 - 4)) & 0xf; code = v >> (32 - 17); len = (v >> (32 - 17 - 7)) & 0x7f; if ((mask >> type) & 1) { // printf("%d: type=%d %04x %04x\n", idx, type, code, code + len - 1); switch(type) { case RUN_TYPE_UL: if ((case_mask & CASE_U) && (case_mask & (CASE_L | CASE_F))) goto def_case; code += ((case_mask & CASE_U) != 0); for(i = 0; i < len; i += 2) { if (cr_add_interval(cr, code + i, code + i + 1)) return -1; } break; case RUN_TYPE_LSU: if ((case_mask & CASE_U) && (case_mask & (CASE_L | CASE_F))) goto def_case; if (!(case_mask & CASE_U)) { if (cr_add_interval(cr, code, code + 1)) return -1; } if (cr_add_interval(cr, code + 1, code + 2)) return -1; if (case_mask & CASE_U) { if (cr_add_interval(cr, code + 2, code + 3)) return -1; } break; default: def_case: if (cr_add_interval(cr, code, code + len)) return -1; break; } } } return 0; }
/* use the case conversion table to generate range of characters. CASE_U: set char if modified by uppercasing, CASE_L: set char if modified by lowercasing, CASE_F: set char if modified by case folding, */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L1177-L1238
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
unicode_general_category
int unicode_general_category(CharRange *cr, const char *gc_name) { int gc_idx; uint32_t gc_mask; gc_idx = unicode_find_name(unicode_gc_name_table, gc_name); if (gc_idx < 0) return -2; if (gc_idx <= UNICODE_GC_Co) { gc_mask = (uint64_t)1 << gc_idx; } else { gc_mask = unicode_gc_mask_table[gc_idx - UNICODE_GC_LC]; } return unicode_general_category1(cr, gc_mask); }
/* 'cr' must be initialized and empty. Return 0 if OK, -1 if error, -2 if not found */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L1341-L1355
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
unicode_prop
int unicode_prop(CharRange *cr, const char *prop_name) { int prop_idx, ret; prop_idx = unicode_find_name(unicode_prop_name_table, prop_name); if (prop_idx < 0) return -2; prop_idx += UNICODE_PROP_ASCII_Hex_Digit; ret = 0; switch(prop_idx) { case UNICODE_PROP_ASCII: if (cr_add_interval(cr, 0x00, 0x7f + 1)) return -1; break; case UNICODE_PROP_Any: if (cr_add_interval(cr, 0x00000, 0x10ffff + 1)) return -1; break; case UNICODE_PROP_Assigned: ret = unicode_prop_ops(cr, POP_GC, M(Cn), POP_INVERT, POP_END); break; case UNICODE_PROP_Math: ret = unicode_prop_ops(cr, POP_GC, M(Sm), POP_PROP, UNICODE_PROP_Other_Math, POP_UNION, POP_END); break; case UNICODE_PROP_Lowercase: ret = unicode_prop_ops(cr, POP_GC, M(Ll), POP_PROP, UNICODE_PROP_Other_Lowercase, POP_UNION, POP_END); break; case UNICODE_PROP_Uppercase: ret = unicode_prop_ops(cr, POP_GC, M(Lu), POP_PROP, UNICODE_PROP_Other_Uppercase, POP_UNION, POP_END); break; case UNICODE_PROP_Cased: ret = unicode_prop_ops(cr, POP_GC, M(Lu) | M(Ll) | M(Lt), POP_PROP, UNICODE_PROP_Other_Uppercase, POP_UNION, POP_PROP, UNICODE_PROP_Other_Lowercase, POP_UNION, POP_END); break; case UNICODE_PROP_Alphabetic: ret = unicode_prop_ops(cr, POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl), POP_PROP, UNICODE_PROP_Other_Uppercase, POP_UNION, POP_PROP, UNICODE_PROP_Other_Lowercase, POP_UNION, POP_PROP, UNICODE_PROP_Other_Alphabetic, POP_UNION, POP_END); break; case UNICODE_PROP_Grapheme_Base: ret = unicode_prop_ops(cr, POP_GC, M(Cc) | M(Cf) | M(Cs) | M(Co) | M(Cn) | M(Zl) | M(Zp) | M(Me) | M(Mn), POP_PROP, UNICODE_PROP_Other_Grapheme_Extend, POP_UNION, POP_INVERT, POP_END); break; case UNICODE_PROP_Grapheme_Extend: ret = unicode_prop_ops(cr, POP_GC, M(Me) | M(Mn), POP_PROP, UNICODE_PROP_Other_Grapheme_Extend, POP_UNION, POP_END); break; case UNICODE_PROP_XID_Start: ret = unicode_prop_ops(cr, POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl), POP_PROP, UNICODE_PROP_Other_ID_Start, POP_UNION, POP_PROP, UNICODE_PROP_Pattern_Syntax, POP_PROP, UNICODE_PROP_Pattern_White_Space, POP_UNION, POP_PROP, UNICODE_PROP_XID_Start1, POP_UNION, POP_INVERT, POP_INTER, POP_END); break; case UNICODE_PROP_XID_Continue: ret = unicode_prop_ops(cr, POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl) | M(Mn) | M(Mc) | M(Nd) | M(Pc), POP_PROP, UNICODE_PROP_Other_ID_Start, POP_UNION, POP_PROP, UNICODE_PROP_Other_ID_Continue, POP_UNION, POP_PROP, UNICODE_PROP_Pattern_Syntax, POP_PROP, UNICODE_PROP_Pattern_White_Space, POP_UNION, POP_PROP, UNICODE_PROP_XID_Continue1, POP_UNION, POP_INVERT, POP_INTER, POP_END); break; case UNICODE_PROP_Changes_When_Uppercased: ret = unicode_case1(cr, CASE_U); break; case UNICODE_PROP_Changes_When_Lowercased: ret = unicode_case1(cr, CASE_L); break; case UNICODE_PROP_Changes_When_Casemapped: ret = unicode_case1(cr, CASE_U | CASE_L | CASE_F); break; case UNICODE_PROP_Changes_When_Titlecased: ret = unicode_prop_ops(cr, POP_CASE, CASE_U, POP_PROP, UNICODE_PROP_Changes_When_Titlecased1, POP_XOR, POP_END); break; case UNICODE_PROP_Changes_When_Casefolded: ret = unicode_prop_ops(cr, POP_CASE, CASE_F, POP_PROP, UNICODE_PROP_Changes_When_Casefolded1, POP_XOR, POP_END); break; case UNICODE_PROP_Changes_When_NFKC_Casefolded: ret = unicode_prop_ops(cr, POP_CASE, CASE_F, POP_PROP, UNICODE_PROP_Changes_When_NFKC_Casefolded1, POP_XOR, POP_END); break; #if 0 case UNICODE_PROP_ID_Start: ret = unicode_prop_ops(cr, POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl), POP_PROP, UNICODE_PROP_Other_ID_Start, POP_UNION, POP_PROP, UNICODE_PROP_Pattern_Syntax, POP_PROP, UNICODE_PROP_Pattern_White_Space, POP_UNION, POP_INVERT, POP_INTER, POP_END); break; case UNICODE_PROP_ID_Continue: ret = unicode_prop_ops(cr, POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl) | M(Mn) | M(Mc) | M(Nd) | M(Pc), POP_PROP, UNICODE_PROP_Other_ID_Start, POP_UNION, POP_PROP, UNICODE_PROP_Other_ID_Continue, POP_UNION, POP_PROP, UNICODE_PROP_Pattern_Syntax, POP_PROP, UNICODE_PROP_Pattern_White_Space, POP_UNION, POP_INVERT, POP_INTER, POP_END); break; case UNICODE_PROP_Case_Ignorable: ret = unicode_prop_ops(cr, POP_GC, M(Mn) | M(Cf) | M(Lm) | M(Sk), POP_PROP, UNICODE_PROP_Case_Ignorable1, POP_XOR, POP_END); break; #else /* we use the existing tables */ case UNICODE_PROP_ID_Continue: ret = unicode_prop_ops(cr, POP_PROP, UNICODE_PROP_ID_Start, POP_PROP, UNICODE_PROP_ID_Continue1, POP_XOR, POP_END); break; #endif default: if (prop_idx >= countof(unicode_prop_table)) return -2; ret = unicode_prop1(cr, prop_idx); break; } return ret; }
/* 'cr' must be initialized and empty. Return 0 if OK, -1 if error, -2 if not found */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libunicode.c#L1360-L1554
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_loadScript
static JSValue js_loadScript(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { uint8_t *buf; const char *filename; JSValue ret; size_t buf_len; filename = JS_ToCString(ctx, argv[0]); if (!filename) return JS_EXCEPTION; buf = js_load_file(ctx, &buf_len, filename); if (!buf) { JS_ThrowReferenceError(ctx, "could not load '%s'", filename); JS_FreeCString(ctx, filename); return JS_EXCEPTION; } ret = JS_Eval(ctx, (char *)buf, buf_len, filename, JS_EVAL_TYPE_GLOBAL); js_free(ctx, buf); JS_FreeCString(ctx, filename); return ret; }
/* load and evaluate a file */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L417-L439
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_std_loadFile
static JSValue js_std_loadFile(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { uint8_t *buf; const char *filename; JSValue ret; size_t buf_len; filename = JS_ToCString(ctx, argv[0]); if (!filename) return JS_EXCEPTION; buf = js_load_file(ctx, &buf_len, filename); JS_FreeCString(ctx, filename); if (!buf) return JS_NULL; ret = JS_NewStringLen(ctx, (char *)buf, buf_len); js_free(ctx, buf); return ret; }
/* load a file as a UTF-8 encoded string */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L442-L460
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_module_set_import_meta
int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val, JS_BOOL use_realpath, JS_BOOL is_main) { JSModuleDef *m; char buf[PATH_MAX + 16]; JSValue meta_obj; JSAtom module_name_atom; const char *module_name; assert(JS_VALUE_GET_TAG(func_val) == JS_TAG_MODULE); m = JS_VALUE_GET_PTR(func_val); module_name_atom = JS_GetModuleName(ctx, m); module_name = JS_AtomToCString(ctx, module_name_atom); JS_FreeAtom(ctx, module_name_atom); if (!module_name) return -1; if (!strchr(module_name, ':')) { strcpy(buf, "file://"); #if !defined(_WIN32) /* realpath() cannot be used with modules compiled with qjsc because the corresponding module source code is not necessarily present */ if (use_realpath) { char *res = realpath(module_name, buf + strlen(buf)); if (!res) { JS_ThrowTypeError(ctx, "realpath failure"); JS_FreeCString(ctx, module_name); return -1; } } else #endif { pstrcat(buf, sizeof(buf), module_name); } } else { pstrcpy(buf, sizeof(buf), module_name); } JS_FreeCString(ctx, module_name); meta_obj = JS_GetImportMeta(ctx, m); if (JS_IsException(meta_obj)) return -1; JS_DefinePropertyValueStr(ctx, meta_obj, "url", JS_NewString(ctx, buf), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, meta_obj, "main", JS_NewBool(ctx, is_main), JS_PROP_C_W_E); JS_FreeValue(ctx, meta_obj); return 0; }
/* !_WIN32 */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L524-L575
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_std_setenv
static JSValue js_std_setenv(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { const char *name, *value; name = JS_ToCString(ctx, argv[0]); if (!name) return JS_EXCEPTION; value = JS_ToCString(ctx, argv[1]); if (!value) { JS_FreeCString(ctx, name); return JS_EXCEPTION; } setenv(name, value, TRUE); JS_FreeCString(ctx, name); JS_FreeCString(ctx, value); return JS_UNDEFINED; }
/* _WIN32 */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L658-L674
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_std_getenviron
static JSValue js_std_getenviron(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { char **envp; const char *name, *p, *value; JSValue obj; uint32_t idx; size_t name_len; JSAtom atom; int ret; obj = JS_NewObject(ctx); if (JS_IsException(obj)) return JS_EXCEPTION; envp = environ; for(idx = 0; envp[idx] != NULL; idx++) { name = envp[idx]; p = strchr(name, '='); name_len = p - name; if (!p) continue; value = p + 1; atom = JS_NewAtomLen(ctx, name, name_len); if (atom == JS_ATOM_NULL) goto fail; ret = JS_DefinePropertyValue(ctx, obj, atom, JS_NewString(ctx, value), JS_PROP_C_W_E); JS_FreeAtom(ctx, atom); if (ret < 0) goto fail; } return obj; fail: JS_FreeValue(ctx, obj); return JS_EXCEPTION; }
/* return an object containing the list of the available environment variables. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L690-L725
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_std_file_getline
static JSValue js_std_file_getline(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { FILE *f = js_std_file_get(ctx, this_val); int c; DynBuf dbuf; JSValue obj; if (!f) return JS_EXCEPTION; js_std_dbuf_init(ctx, &dbuf); for(;;) { c = fgetc(f); if (c == EOF) { if (dbuf.size == 0) { /* EOF */ dbuf_free(&dbuf); return JS_NULL; } else { break; } } if (c == '\n') break; if (dbuf_putc(&dbuf, c)) { dbuf_free(&dbuf); return JS_ThrowOutOfMemory(ctx); } } obj = JS_NewStringLen(ctx, (const char *)dbuf.buf, dbuf.size); dbuf_free(&dbuf); return obj; }
/* XXX: could use less memory and go faster */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L1185-L1218
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_std_file_readAsString
static JSValue js_std_file_readAsString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { FILE *f = js_std_file_get(ctx, this_val); int c; DynBuf dbuf; JSValue obj; uint64_t max_size64; size_t max_size; JSValueConst max_size_val; if (!f) return JS_EXCEPTION; if (argc >= 1) max_size_val = argv[0]; else max_size_val = JS_UNDEFINED; max_size = (size_t)-1; if (!JS_IsUndefined(max_size_val)) { if (JS_ToIndex(ctx, &max_size64, max_size_val)) return JS_EXCEPTION; if (max_size64 < max_size) max_size = max_size64; } js_std_dbuf_init(ctx, &dbuf); while (max_size != 0) { c = fgetc(f); if (c == EOF) break; if (dbuf_putc(&dbuf, c)) { dbuf_free(&dbuf); return JS_EXCEPTION; } max_size--; } obj = JS_NewStringLen(ctx, (const char *)dbuf.buf, dbuf.size); dbuf_free(&dbuf); return obj; }
/* XXX: could use less memory and go faster */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L1221-L1261
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_open
static JSValue js_os_open(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { const char *filename; int flags, mode, ret; filename = JS_ToCString(ctx, argv[0]); if (!filename) return JS_EXCEPTION; if (JS_ToInt32(ctx, &flags, argv[1])) goto fail; if (argc >= 3 && !JS_IsUndefined(argv[2])) { if (JS_ToInt32(ctx, &mode, argv[2])) { fail: JS_FreeCString(ctx, filename); return JS_EXCEPTION; } } else { mode = 0666; } #if defined(_WIN32) /* force binary mode by default */ if (!(flags & O_TEXT)) flags |= O_BINARY; #endif ret = js_get_errno(open(filename, flags, mode)); JS_FreeCString(ctx, filename); return JS_NewInt32(ctx, ret); }
/**********************************************************/ /* 'os' object */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L1580-L1608
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_ttySetRaw
static JSValue js_os_ttySetRaw(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { struct termios tty; int fd; if (JS_ToInt32(ctx, &fd, argv[0])) return JS_EXCEPTION; memset(&tty, 0, sizeof(tty)); tcgetattr(fd, &tty); oldtty = tty; tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |INLCR|IGNCR|ICRNL|IXON); tty.c_oflag |= OPOST; tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); tty.c_cflag &= ~(CSIZE|PARENB); tty.c_cflag |= CS8; tty.c_cc[VMIN] = 1; tty.c_cc[VTIME] = 0; tcsetattr(fd, TCSANOW, &tty); atexit(term_exit); return JS_UNDEFINED; }
/* XXX: should add a way to go back to normal mode */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L1754-L1780
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_remove
static JSValue js_os_remove(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { const char *filename; int ret; filename = JS_ToCString(ctx, argv[0]); if (!filename) return JS_EXCEPTION; #if defined(_WIN32) { struct stat st; if (stat(filename, &st) == 0 && S_ISDIR(st.st_mode)) { ret = rmdir(filename); } else { ret = unlink(filename); } } #else ret = remove(filename); #endif ret = js_get_errno(ret); JS_FreeCString(ctx, filename); return JS_NewInt32(ctx, ret); }
/* !_WIN32 */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L1784-L1808
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
get_time_ms
static int64_t get_time_ms(void) { struct timeval tv; gettimeofday(&tv, NULL); return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000); }
/* more portable, but does not work if the date is updated */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L1985-L1990
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
handle_posted_message
static int handle_posted_message(JSRuntime *rt, JSContext *ctx, JSWorkerMessageHandler *port) { JSWorkerMessagePipe *ps = port->recv_pipe; int ret; struct list_head *el; JSWorkerMessage *msg; JSValue obj, data_obj, func, retval; pthread_mutex_lock(&ps->mutex); if (!list_empty(&ps->msg_queue)) { el = ps->msg_queue.next; msg = list_entry(el, JSWorkerMessage, link); /* remove the message from the queue */ list_del(&msg->link); if (list_empty(&ps->msg_queue)) { uint8_t buf[16]; int ret; for(;;) { ret = read(ps->read_fd, buf, sizeof(buf)); if (ret >= 0) break; if (errno != EAGAIN && errno != EINTR) break; } } pthread_mutex_unlock(&ps->mutex); data_obj = JS_ReadObject(ctx, msg->data, msg->data_len, JS_READ_OBJ_SAB | JS_READ_OBJ_REFERENCE); js_free_message(msg); if (JS_IsException(data_obj)) goto fail; obj = JS_NewObject(ctx); if (JS_IsException(obj)) { JS_FreeValue(ctx, data_obj); goto fail; } JS_DefinePropertyValueStr(ctx, obj, "data", data_obj, JS_PROP_C_W_E); /* 'func' might be destroyed when calling itself (if it frees the handler), so must take extra care */ func = JS_DupValue(ctx, port->on_message_func); retval = JS_Call(ctx, func, JS_UNDEFINED, 1, (JSValueConst *)&obj); JS_FreeValue(ctx, obj); JS_FreeValue(ctx, func); if (JS_IsException(retval)) { fail: js_std_dump_error(ctx); } else { JS_FreeValue(ctx, retval); } ret = 1; } else { pthread_mutex_unlock(&ps->mutex); ret = 0; } return ret; }
/* return 1 if a message was handled, 0 if no message */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2170-L2233
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
make_obj_error
static JSValue make_obj_error(JSContext *ctx, JSValue obj, int err) { JSValue arr; if (JS_IsException(obj)) return obj; arr = JS_NewArray(ctx); if (JS_IsException(arr)) return JS_EXCEPTION; JS_DefinePropertyValueUint32(ctx, arr, 0, obj, JS_PROP_C_W_E); JS_DefinePropertyValueUint32(ctx, arr, 1, JS_NewInt32(ctx, err), JS_PROP_C_W_E); return arr; }
/* !_WIN32 */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2357-L2372
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_getcwd
static JSValue js_os_getcwd(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { char buf[PATH_MAX]; int err; if (!getcwd(buf, sizeof(buf))) { buf[0] = '\0'; err = errno; } else { err = 0; } return make_string_error(ctx, buf, err); }
/* return [cwd, errorcode] */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2382-L2395
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_readdir
static JSValue js_os_readdir(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { const char *path; DIR *f; struct dirent *d; JSValue obj; int err; uint32_t len; path = JS_ToCString(ctx, argv[0]); if (!path) return JS_EXCEPTION; obj = JS_NewArray(ctx); if (JS_IsException(obj)) { JS_FreeCString(ctx, path); return JS_EXCEPTION; } f = opendir(path); if (!f) err = errno; else err = 0; JS_FreeCString(ctx, path); if (!f) goto done; len = 0; for(;;) { errno = 0; d = readdir(f); if (!d) { err = errno; break; } JS_DefinePropertyValueUint32(ctx, obj, len++, JS_NewString(ctx, d->d_name), JS_PROP_C_W_E); } closedir(f); done: return make_obj_error(ctx, obj, err); }
/* return [array, errorcode] */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2437-L2478
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_stat
static JSValue js_os_stat(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int is_lstat) { const char *path; int err, res; struct stat st; JSValue obj; path = JS_ToCString(ctx, argv[0]); if (!path) return JS_EXCEPTION; #if defined(_WIN32) res = stat(path, &st); #else if (is_lstat) res = lstat(path, &st); else res = stat(path, &st); #endif JS_FreeCString(ctx, path); if (res < 0) { err = errno; obj = JS_NULL; } else { err = 0; obj = JS_NewObject(ctx); if (JS_IsException(obj)) return JS_EXCEPTION; JS_DefinePropertyValueStr(ctx, obj, "dev", JS_NewInt64(ctx, st.st_dev), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "ino", JS_NewInt64(ctx, st.st_ino), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "mode", JS_NewInt32(ctx, st.st_mode), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "nlink", JS_NewInt64(ctx, st.st_nlink), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "uid", JS_NewInt64(ctx, st.st_uid), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "gid", JS_NewInt64(ctx, st.st_gid), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "rdev", JS_NewInt64(ctx, st.st_rdev), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "size", JS_NewInt64(ctx, st.st_size), JS_PROP_C_W_E); #if !defined(_WIN32) JS_DefinePropertyValueStr(ctx, obj, "blocks", JS_NewInt64(ctx, st.st_blocks), JS_PROP_C_W_E); #endif #if defined(_WIN32) JS_DefinePropertyValueStr(ctx, obj, "atime", JS_NewInt64(ctx, (int64_t)st.st_atime * 1000), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "mtime", JS_NewInt64(ctx, (int64_t)st.st_mtime * 1000), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "ctime", JS_NewInt64(ctx, (int64_t)st.st_ctime * 1000), JS_PROP_C_W_E); #elif defined(__APPLE__) JS_DefinePropertyValueStr(ctx, obj, "atime", JS_NewInt64(ctx, timespec_to_ms(&st.st_atimespec)), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "mtime", JS_NewInt64(ctx, timespec_to_ms(&st.st_mtimespec)), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "ctime", JS_NewInt64(ctx, timespec_to_ms(&st.st_ctimespec)), JS_PROP_C_W_E); #elif defined(ANDROID) JS_DefinePropertyValueStr(ctx, obj, "atime", JS_NewInt64(ctx, timespec_to_ms(&st.st_atime)), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "mtime", JS_NewInt64(ctx, timespec_to_ms(&st.st_mtime)), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "ctime", JS_NewInt64(ctx, timespec_to_ms(&st.st_ctime)), JS_PROP_C_W_E); #else JS_DefinePropertyValueStr(ctx, obj, "atime", JS_NewInt64(ctx, timespec_to_ms(&st.st_atim)), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "mtime", JS_NewInt64(ctx, timespec_to_ms(&st.st_mtim)), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "ctime", JS_NewInt64(ctx, timespec_to_ms(&st.st_ctim)), JS_PROP_C_W_E); #endif } return make_obj_error(ctx, obj, err); }
/* return [obj, errcode] */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2488-L2588
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_sleep
static JSValue js_os_sleep(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { int64_t delay; int ret; if (JS_ToInt64(ctx, &delay, argv[0])) return JS_EXCEPTION; if (delay < 0) delay = 0; #if defined(_WIN32) { if (delay > INT32_MAX) delay = INT32_MAX; Sleep(delay); ret = 0; } #else { struct timespec ts; ts.tv_sec = delay / 1000; ts.tv_nsec = (delay % 1000) * 1000000; ret = js_get_errno(nanosleep(&ts, NULL)); } #endif return JS_NewInt32(ctx, ret); }
/* sleep(delay_ms) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2632-L2659
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_realpath
static JSValue js_os_realpath(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { const char *path; char buf[PATH_MAX], *res; int err; path = JS_ToCString(ctx, argv[0]); if (!path) return JS_EXCEPTION; res = realpath(path, buf); JS_FreeCString(ctx, path); if (!res) { buf[0] = '\0'; err = errno; } else { err = 0; } return make_string_error(ctx, buf, err); }
/* return [path, errorcode] */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2674-L2693
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_readlink
static JSValue js_os_readlink(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { const char *path; char buf[PATH_MAX]; int err; ssize_t res; path = JS_ToCString(ctx, argv[0]); if (!path) return JS_EXCEPTION; res = readlink(path, buf, sizeof(buf) - 1); if (res < 0) { buf[0] = '\0'; err = errno; } else { buf[res] = '\0'; err = 0; } JS_FreeCString(ctx, path); return make_string_error(ctx, buf, err); }
/* return [path, errorcode] */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2717-L2738
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
my_execvpe
static int my_execvpe(const char *filename, char **argv, char **envp) { char *path, *p, *p_next, *p1; char buf[PATH_MAX]; size_t filename_len, path_len; BOOL eacces_error; filename_len = strlen(filename); if (filename_len == 0) { errno = ENOENT; return -1; } if (strchr(filename, '/')) return execve(filename, argv, envp); path = getenv("PATH"); if (!path) path = (char *)"/bin:/usr/bin"; eacces_error = FALSE; p = path; for(p = path; p != NULL; p = p_next) { p1 = strchr(p, ':'); if (!p1) { p_next = NULL; path_len = strlen(p); } else { p_next = p1 + 1; path_len = p1 - p; } /* path too long */ if ((path_len + 1 + filename_len + 1) > PATH_MAX) continue; memcpy(buf, p, path_len); buf[path_len] = '/'; memcpy(buf + path_len + 1, filename, filename_len); buf[path_len + 1 + filename_len] = '\0'; execve(buf, argv, envp); switch(errno) { case EACCES: eacces_error = TRUE; break; case ENOENT: case ENOTDIR: break; default: return -1; } } if (eacces_error) errno = EACCES; return -1; }
/* execvpe is not available on non GNU systems */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2800-L2853
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_exec
static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { JSValueConst options, args = argv[0]; JSValue val, ret_val; const char **exec_argv, *file = NULL, *str, *cwd = NULL; char **envp = environ; uint32_t exec_argc, i; int ret, pid, status; BOOL block_flag = TRUE, use_path = TRUE; static const char *std_name[3] = { "stdin", "stdout", "stderr" }; int std_fds[3]; uint32_t uid = -1, gid = -1; val = JS_GetPropertyStr(ctx, args, "length"); if (JS_IsException(val)) return JS_EXCEPTION; ret = JS_ToUint32(ctx, &exec_argc, val); JS_FreeValue(ctx, val); if (ret) return JS_EXCEPTION; /* arbitrary limit to avoid overflow */ if (exec_argc < 1 || exec_argc > 65535) { return JS_ThrowTypeError(ctx, "invalid number of arguments"); } exec_argv = js_mallocz(ctx, sizeof(exec_argv[0]) * (exec_argc + 1)); if (!exec_argv) return JS_EXCEPTION; for(i = 0; i < exec_argc; i++) { val = JS_GetPropertyUint32(ctx, args, i); if (JS_IsException(val)) goto exception; str = JS_ToCString(ctx, val); JS_FreeValue(ctx, val); if (!str) goto exception; exec_argv[i] = str; } exec_argv[exec_argc] = NULL; for(i = 0; i < 3; i++) std_fds[i] = i; /* get the options, if any */ if (argc >= 2) { options = argv[1]; if (get_bool_option(ctx, &block_flag, options, "block")) goto exception; if (get_bool_option(ctx, &use_path, options, "usePath")) goto exception; val = JS_GetPropertyStr(ctx, options, "file"); if (JS_IsException(val)) goto exception; if (!JS_IsUndefined(val)) { file = JS_ToCString(ctx, val); JS_FreeValue(ctx, val); if (!file) goto exception; } val = JS_GetPropertyStr(ctx, options, "cwd"); if (JS_IsException(val)) goto exception; if (!JS_IsUndefined(val)) { cwd = JS_ToCString(ctx, val); JS_FreeValue(ctx, val); if (!cwd) goto exception; } /* stdin/stdout/stderr handles */ for(i = 0; i < 3; i++) { val = JS_GetPropertyStr(ctx, options, std_name[i]); if (JS_IsException(val)) goto exception; if (!JS_IsUndefined(val)) { int fd; ret = JS_ToInt32(ctx, &fd, val); JS_FreeValue(ctx, val); if (ret) goto exception; std_fds[i] = fd; } } val = JS_GetPropertyStr(ctx, options, "env"); if (JS_IsException(val)) goto exception; if (!JS_IsUndefined(val)) { envp = build_envp(ctx, val); JS_FreeValue(ctx, val); if (!envp) goto exception; } val = JS_GetPropertyStr(ctx, options, "uid"); if (JS_IsException(val)) goto exception; if (!JS_IsUndefined(val)) { ret = JS_ToUint32(ctx, &uid, val); JS_FreeValue(ctx, val); if (ret) goto exception; } val = JS_GetPropertyStr(ctx, options, "gid"); if (JS_IsException(val)) goto exception; if (!JS_IsUndefined(val)) { ret = JS_ToUint32(ctx, &gid, val); JS_FreeValue(ctx, val); if (ret) goto exception; } } pid = fork(); if (pid < 0) { JS_ThrowTypeError(ctx, "fork error"); goto exception; } if (pid == 0) { /* child */ int fd_max = sysconf(_SC_OPEN_MAX); /* remap the stdin/stdout/stderr handles if necessary */ for(i = 0; i < 3; i++) { if (std_fds[i] != i) { if (dup2(std_fds[i], i) < 0) _exit(127); } } for(i = 3; i < fd_max; i++) close(i); if (cwd) { if (chdir(cwd) < 0) _exit(127); } if (uid != -1) { if (setuid(uid) < 0) _exit(127); } if (gid != -1) { if (setgid(gid) < 0) _exit(127); } if (!file) file = exec_argv[0]; if (use_path) ret = my_execvpe(file, (char **)exec_argv, envp); else ret = execve(file, (char **)exec_argv, envp); _exit(127); } /* parent */ if (block_flag) { for(;;) { ret = waitpid(pid, &status, 0); if (ret == pid) { if (WIFEXITED(status)) { ret = WEXITSTATUS(status); break; } else if (WIFSIGNALED(status)) { ret = -WTERMSIG(status); break; } } } } else { ret = pid; } ret_val = JS_NewInt32(ctx, ret); done: JS_FreeCString(ctx, file); JS_FreeCString(ctx, cwd); for(i = 0; i < exec_argc; i++) JS_FreeCString(ctx, exec_argv[i]); js_free(ctx, exec_argv); if (envp != environ) { char **p; p = envp; while (*p != NULL) { js_free(ctx, *p); p++; } js_free(ctx, envp); } return ret_val; exception: ret_val = JS_EXCEPTION; goto done; }
/* exec(args[, options]) -> exitcode */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L2856-L3051
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_waitpid
static JSValue js_os_waitpid(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { int pid, status, options, ret; JSValue obj; if (JS_ToInt32(ctx, &pid, argv[0])) return JS_EXCEPTION; if (JS_ToInt32(ctx, &options, argv[1])) return JS_EXCEPTION; ret = waitpid(pid, &status, options); if (ret < 0) { ret = -errno; status = 0; } obj = JS_NewArray(ctx); if (JS_IsException(obj)) return obj; JS_DefinePropertyValueUint32(ctx, obj, 0, JS_NewInt32(ctx, ret), JS_PROP_C_W_E); JS_DefinePropertyValueUint32(ctx, obj, 1, JS_NewInt32(ctx, status), JS_PROP_C_W_E); return obj; }
/* waitpid(pid, block) -> [pid, status] */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L3054-L3079
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_pipe
static JSValue js_os_pipe(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { int pipe_fds[2], ret; JSValue obj; ret = pipe(pipe_fds); if (ret < 0) return JS_NULL; obj = JS_NewArray(ctx); if (JS_IsException(obj)) return obj; JS_DefinePropertyValueUint32(ctx, obj, 0, JS_NewInt32(ctx, pipe_fds[0]), JS_PROP_C_W_E); JS_DefinePropertyValueUint32(ctx, obj, 1, JS_NewInt32(ctx, pipe_fds[1]), JS_PROP_C_W_E); return obj; }
/* pipe() -> [read_fd, write_fd] or null if error */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L3082-L3099
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_kill
static JSValue js_os_kill(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { int pid, sig, ret; if (JS_ToInt32(ctx, &pid, argv[0])) return JS_EXCEPTION; if (JS_ToInt32(ctx, &sig, argv[1])) return JS_EXCEPTION; ret = js_get_errno(kill(pid, sig)); return JS_NewInt32(ctx, ret); }
/* kill(pid, sig) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L3102-L3113
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_dup
static JSValue js_os_dup(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { int fd, ret; if (JS_ToInt32(ctx, &fd, argv[0])) return JS_EXCEPTION; ret = js_get_errno(dup(fd)); return JS_NewInt32(ctx, ret); }
/* sleep(delay_ms) */ /*static JSValue js_os_sleep(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { int64_t delay; struct timespec ts; int ret; if (JS_ToInt64(ctx, &delay, argv[0])) return JS_EXCEPTION; ts.tv_sec = delay / 1000; ts.tv_nsec = (delay % 1000) * 1000000; ret = js_get_errno(nanosleep(&ts, NULL)); return JS_NewInt32(ctx, ret); }*/ /* dup(fd) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L3132-L3141
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_os_dup2
static JSValue js_os_dup2(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { int fd, fd2, ret; if (JS_ToInt32(ctx, &fd, argv[0])) return JS_EXCEPTION; if (JS_ToInt32(ctx, &fd2, argv[1])) return JS_EXCEPTION; ret = js_get_errno(dup2(fd, fd2)); return JS_NewInt32(ctx, ret); }
/* dup2(fd) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L3144-L3155
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_std_set_worker_new_context_func
void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt)) { #ifdef USE_WORKER js_worker_new_context_func = func; #endif }
/* USE_WORKER */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L3597-L3602
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_print
static JSValue js_print(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { int i; const char *str; size_t len; for(i = 0; i < argc; i++) { if (i != 0) putchar(' '); str = JS_ToCStringLen(ctx, &len, argv[i]); if (!str) return JS_EXCEPTION; fwrite(str, 1, len, stdout); JS_FreeCString(ctx, str); } putchar('\n'); return JS_UNDEFINED; }
/**********************************************************/
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L3753-L3771
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_std_loop
void js_std_loop(JSContext *ctx) { JSContext *ctx1; int err; for(;;) { /* execute the pending jobs */ for(;;) { err = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1); if (err <= 0) { if (err < 0) { js_std_dump_error(ctx1); } break; } } if (!os_poll_func || os_poll_func(ctx)) break; } }
/* main loop which calls the user JS callbacks */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs-libc.c#L3914-L3934
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_get_stack_pointer
static inline uintptr_t js_get_stack_pointer(void) { return 0; }
/* no stack limitation */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L1629-L1632
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_get_stack_pointer
static inline uintptr_t js_get_stack_pointer(void) { return (uintptr_t)__builtin_frame_address(0); }
/* Note: OS and CPU dependent */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L1640-L1643
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
JS_StringToBigInt
static JSValue JS_StringToBigInt(JSContext *ctx, JSValue val) { const char *str, *p; size_t len; int flags; str = JS_ToCStringLen(ctx, &len, val); JS_FreeValue(ctx, val); if (!str) return JS_EXCEPTION; p = str; p += skip_spaces(p); if ((p - str) == len) { val = JS_NewBigInt64(ctx, 0); } else { flags = ATOD_INT_ONLY | ATOD_ACCEPT_BIN_OCT | ATOD_TYPE_BIG_INT; if (is_math_mode(ctx)) flags |= ATOD_MODE_BIGINT; val = js_atof(ctx, p, &p, 0, flags); p += skip_spaces(p); if (!JS_IsException(val)) { if ((p - str) != len) { JS_FreeValue(ctx, val); val = JS_NAN; } } } JS_FreeCString(ctx, str); return val; }
/* return NaN if bad bigint literal */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L12296-L12325
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
JS_FreeBigInt
static void JS_FreeBigInt(JSContext *ctx, bf_t *a, bf_t *buf) { if (a == buf) { bf_delete(a); } else { JSBigFloat *p = (JSBigFloat *)((uint8_t *)a - offsetof(JSBigFloat, num)); JS_FreeValue(ctx, JS_MKPTR(JS_TAG_BIG_FLOAT, p)); } }
/* free the bf_t allocated by JS_ToBigInt */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L12439-L12448
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
JS_ToBigInt64Free
static int JS_ToBigInt64Free(JSContext *ctx, int64_t *pres, JSValue val) { bf_t a_s, *a; a = JS_ToBigIntFree(ctx, &a_s, val); if (!a) { *pres = 0; return -1; } bf_get_int64(pres, a, BF_GET_INT_MOD); JS_FreeBigInt(ctx, a, &a_s); return 0; }
/* XXX: merge with JS_ToInt64Free with a specific flag */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L12451-L12463
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
JS_CompactBigInt
static JSValue JS_CompactBigInt(JSContext *ctx, JSValue val) { return JS_CompactBigInt1(ctx, val, is_math_mode(ctx)); }
/* Convert the big int to a safe integer if in math mode. normalize the zero representation. Could also be used to convert the bigint to a short bigint value. The reference count of the value must be 1. Cannot fail */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L12539-L12542
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_call_binary_op_fallback
static __exception int js_call_binary_op_fallback(JSContext *ctx, JSValue *pret, JSValueConst op1, JSValueConst op2, OPCodeEnum op, BOOL is_numeric, int hint) { JSValue opset1_obj, opset2_obj, method, ret, new_op1, new_op2; JSOperatorSetData *opset1, *opset2; JSOverloadableOperatorEnum ovop; JSObject *p; JSValueConst args[2]; if (!ctx->allow_operator_overloading) return 0; opset2_obj = JS_UNDEFINED; opset1_obj = JS_GetProperty(ctx, op1, JS_ATOM_Symbol_operatorSet); if (JS_IsException(opset1_obj)) goto exception; if (JS_IsUndefined(opset1_obj)) return 0; opset1 = JS_GetOpaque2(ctx, opset1_obj, JS_CLASS_OPERATOR_SET); if (!opset1) goto exception; opset2_obj = JS_GetProperty(ctx, op2, JS_ATOM_Symbol_operatorSet); if (JS_IsException(opset2_obj)) goto exception; if (JS_IsUndefined(opset2_obj)) { JS_FreeValue(ctx, opset1_obj); return 0; } opset2 = JS_GetOpaque2(ctx, opset2_obj, JS_CLASS_OPERATOR_SET); if (!opset2) goto exception; if (opset1->is_primitive && opset2->is_primitive) { JS_FreeValue(ctx, opset1_obj); JS_FreeValue(ctx, opset2_obj); return 0; } ovop = get_ovop_from_opcode(op); if (opset1->operator_counter == opset2->operator_counter) { p = opset1->self_ops[ovop]; } else if (opset1->operator_counter > opset2->operator_counter) { p = find_binary_op(&opset1->left, opset2->operator_counter, ovop); } else { p = find_binary_op(&opset2->right, opset1->operator_counter, ovop); } if (!p) { JS_ThrowTypeError(ctx, "operator %s: no function defined", js_overloadable_operator_names[ovop]); goto exception; } if (opset1->is_primitive) { if (is_numeric) { new_op1 = JS_ToNumeric(ctx, op1); } else { new_op1 = JS_ToPrimitive(ctx, op1, hint); } if (JS_IsException(new_op1)) goto exception; } else { new_op1 = JS_DupValue(ctx, op1); } if (opset2->is_primitive) { if (is_numeric) { new_op2 = JS_ToNumeric(ctx, op2); } else { new_op2 = JS_ToPrimitive(ctx, op2, hint); } if (JS_IsException(new_op2)) { JS_FreeValue(ctx, new_op1); goto exception; } } else { new_op2 = JS_DupValue(ctx, op2); } /* XXX: could apply JS_ToPrimitive() if primitive type so that the operator function does not get a value object */ method = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p)); if (ovop == JS_OVOP_LESS && (op == OP_lte || op == OP_gt)) { args[0] = new_op2; args[1] = new_op1; } else { args[0] = new_op1; args[1] = new_op2; } ret = JS_CallFree(ctx, method, JS_UNDEFINED, 2, args); JS_FreeValue(ctx, new_op1); JS_FreeValue(ctx, new_op2); if (JS_IsException(ret)) goto exception; if (ovop == JS_OVOP_EQ) { BOOL res = JS_ToBoolFree(ctx, ret); if (op == OP_neq) res ^= 1; ret = JS_NewBool(ctx, res); } else if (ovop == JS_OVOP_LESS) { if (JS_IsUndefined(ret)) { ret = JS_FALSE; } else { BOOL res = JS_ToBoolFree(ctx, ret); if (op == OP_lte || op == OP_gte) res ^= 1; ret = JS_NewBool(ctx, res); } } JS_FreeValue(ctx, opset1_obj); JS_FreeValue(ctx, opset2_obj); *pret = ret; return 1; exception: JS_FreeValue(ctx, opset1_obj); JS_FreeValue(ctx, opset2_obj); *pret = JS_UNDEFINED; return -1; }
/* return -1 if exception, 0 if no operator overloading, 1 if overloaded operator called */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L12634-L12759
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_call_binary_op_simple
static __exception int js_call_binary_op_simple(JSContext *ctx, JSValue *pret, JSValueConst obj, JSValueConst op1, JSValueConst op2, OPCodeEnum op) { JSValue opset1_obj, method, ret, new_op1, new_op2; JSOperatorSetData *opset1; JSOverloadableOperatorEnum ovop; JSObject *p; JSValueConst args[2]; opset1_obj = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_operatorSet); if (JS_IsException(opset1_obj)) goto exception; if (JS_IsUndefined(opset1_obj)) return 0; opset1 = JS_GetOpaque2(ctx, opset1_obj, JS_CLASS_OPERATOR_SET); if (!opset1) goto exception; ovop = get_ovop_from_opcode(op); p = opset1->self_ops[ovop]; if (!p) { JS_FreeValue(ctx, opset1_obj); return 0; } new_op1 = JS_ToNumeric(ctx, op1); if (JS_IsException(new_op1)) goto exception; new_op2 = JS_ToNumeric(ctx, op2); if (JS_IsException(new_op2)) { JS_FreeValue(ctx, new_op1); goto exception; } method = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p)); args[0] = new_op1; args[1] = new_op2; ret = JS_CallFree(ctx, method, JS_UNDEFINED, 2, args); JS_FreeValue(ctx, new_op1); JS_FreeValue(ctx, new_op2); if (JS_IsException(ret)) goto exception; JS_FreeValue(ctx, opset1_obj); *pret = ret; return 1; exception: JS_FreeValue(ctx, opset1_obj); *pret = JS_UNDEFINED; return -1; }
/* try to call the operation on the operatorSet field of 'obj'. Only used for "/" and "**" on the BigInt prototype in math mode */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L12763-L12816
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_call_unary_op_fallback
static __exception int js_call_unary_op_fallback(JSContext *ctx, JSValue *pret, JSValueConst op1, OPCodeEnum op) { JSValue opset1_obj, method, ret; JSOperatorSetData *opset1; JSOverloadableOperatorEnum ovop; JSObject *p; if (!ctx->allow_operator_overloading) return 0; opset1_obj = JS_GetProperty(ctx, op1, JS_ATOM_Symbol_operatorSet); if (JS_IsException(opset1_obj)) goto exception; if (JS_IsUndefined(opset1_obj)) return 0; opset1 = JS_GetOpaque2(ctx, opset1_obj, JS_CLASS_OPERATOR_SET); if (!opset1) goto exception; if (opset1->is_primitive) { JS_FreeValue(ctx, opset1_obj); return 0; } ovop = get_ovop_from_opcode(op); p = opset1->self_ops[ovop]; if (!p) { JS_ThrowTypeError(ctx, "no overloaded operator %s", js_overloadable_operator_names[ovop]); goto exception; } method = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p)); ret = JS_CallFree(ctx, method, JS_UNDEFINED, 1, &op1); if (JS_IsException(ret)) goto exception; JS_FreeValue(ctx, opset1_obj); *pret = ret; return 1; exception: JS_FreeValue(ctx, opset1_obj); *pret = JS_UNDEFINED; return -1; }
/* return -1 if exception, 0 if no operator overloading, 1 if overloaded operator called */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L12820-L12865
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_bfdec_pow
static int js_bfdec_pow(bfdec_t *r, const bfdec_t *a, const bfdec_t *b) { bfdec_t b1; int32_t b2; int ret; bfdec_init(b->ctx, &b1); ret = bfdec_set(&b1, b); if (ret) { bfdec_delete(&b1); return ret; } ret = bfdec_rint(&b1, BF_RNDZ); if (ret) { bfdec_delete(&b1); return BF_ST_INVALID_OP; /* must be an integer */ } ret = bfdec_get_int32(&b2, &b1); bfdec_delete(&b1); if (ret) return ret; /* overflow */ if (b2 < 0) return BF_ST_INVALID_OP; /* must be positive */ return bfdec_pow_ui(r, a, b2); }
/* b must be a positive integer */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L13397-L13421
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_compare_bigfloat
static int js_compare_bigfloat(JSContext *ctx, OPCodeEnum op, JSValue op1, JSValue op2) { bf_t a_s, b_s, *a, *b; int res; a = JS_ToBigFloat(ctx, &a_s, op1); if (!a) { JS_FreeValue(ctx, op2); return -1; } b = JS_ToBigFloat(ctx, &b_s, op2); if (!b) { if (a == &a_s) bf_delete(a); JS_FreeValue(ctx, op1); return -1; } switch(op) { case OP_lt: res = bf_cmp_lt(a, b); /* if NaN return false */ break; case OP_lte: res = bf_cmp_le(a, b); /* if NaN return false */ break; case OP_gt: res = bf_cmp_lt(b, a); /* if NaN return false */ break; case OP_gte: res = bf_cmp_le(b, a); /* if NaN return false */ break; case OP_eq: res = bf_cmp_eq(a, b); /* if NaN return false */ break; default: abort(); } if (a == &a_s) bf_delete(a); if (b == &b_s) bf_delete(b); JS_FreeValue(ctx, op1); JS_FreeValue(ctx, op2); return res; }
/* Note: also used for bigint */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L13856-L13900
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
JS_ThrowUnsupportedBigint
static JSValue JS_ThrowUnsupportedBigint(JSContext *ctx) { return JS_ThrowTypeError(ctx, "bigint is not supported"); }
/* !CONFIG_BIGNUM */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L14365-L14368
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_post_inc_slow
static __exception int js_post_inc_slow(JSContext *ctx, JSValue *sp, OPCodeEnum op) { JSValue op1; double d, r; op1 = sp[-1]; if (unlikely(JS_ToFloat64Free(ctx, &d, op1))) { sp[-1] = JS_UNDEFINED; return -1; } r = d + 2 * (op - OP_post_dec) - 1; sp[0] = JS_NewFloat64(ctx, r); sp[-1] = JS_NewFloat64(ctx, d); return 0; }
/* specific case necessary for correct return value semantics */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L14419-L14434
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
JS_CallConstructorInternal
static JSValue JS_CallConstructorInternal(JSContext *ctx, JSValueConst func_obj, JSValueConst new_target, int argc, JSValue *argv, int flags) { JSObject *p; JSFunctionBytecode *b; if (js_poll_interrupts(ctx)) return JS_EXCEPTION; flags |= JS_CALL_FLAG_CONSTRUCTOR; if (unlikely(JS_VALUE_GET_TAG(func_obj) != JS_TAG_OBJECT)) goto not_a_function; p = JS_VALUE_GET_OBJ(func_obj); if (unlikely(!p->is_constructor)) return JS_ThrowTypeError(ctx, "not a constructor"); if (unlikely(p->class_id != JS_CLASS_BYTECODE_FUNCTION)) { JSClassCall *call_func; call_func = ctx->rt->class_array[p->class_id].call; if (!call_func) { not_a_function: return JS_ThrowTypeError(ctx, "not a function"); } return call_func(ctx, func_obj, new_target, argc, (JSValueConst *)argv, flags); } b = p->u.func.function_bytecode; if (b->is_derived_class_constructor) { return JS_CallInternal(ctx, func_obj, JS_UNDEFINED, new_target, argc, argv, flags); } else { JSValue obj, ret; /* legacy constructor behavior */ obj = js_create_from_ctor(ctx, new_target, JS_CLASS_OBJECT); if (JS_IsException(obj)) return JS_EXCEPTION; ret = JS_CallInternal(ctx, func_obj, obj, new_target, argc, argv, flags); if (JS_VALUE_GET_TAG(ret) == JS_TAG_OBJECT || JS_IsException(ret)) { JS_FreeValue(ctx, obj); return ret; } else { JS_FreeValue(ctx, ret); return obj; } } }
/* argv[] is modified if (flags & JS_CALL_FLAG_COPY_ARGV) = 0. */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L19065-L19111
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
async_func_init
static __exception int async_func_init(JSContext *ctx, JSAsyncFunctionState *s, JSValueConst func_obj, JSValueConst this_obj, int argc, JSValueConst *argv) { JSObject *p; JSFunctionBytecode *b; JSStackFrame *sf; int local_count, i, arg_buf_len, n; sf = &s->frame; init_list_head(&sf->var_ref_list); p = JS_VALUE_GET_OBJ(func_obj); b = p->u.func.function_bytecode; sf->js_mode = b->js_mode; sf->cur_pc = b->byte_code_buf; arg_buf_len = max_int(b->arg_count, argc); local_count = arg_buf_len + b->var_count + b->stack_size; sf->arg_buf = js_malloc(ctx, sizeof(JSValue) * max_int(local_count, 1)); if (!sf->arg_buf) return -1; sf->cur_func = JS_DupValue(ctx, func_obj); s->this_val = JS_DupValue(ctx, this_obj); s->argc = argc; sf->arg_count = arg_buf_len; sf->var_buf = sf->arg_buf + arg_buf_len; sf->cur_sp = sf->var_buf + b->var_count; for(i = 0; i < argc; i++) sf->arg_buf[i] = JS_DupValue(ctx, argv[i]); n = arg_buf_len + b->var_count; for(i = argc; i < n; i++) sf->arg_buf[i] = JS_UNDEFINED; return 0; }
/* JSAsyncFunctionState (used by generator and async functions) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L19149-L19181
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_async_function_terminate
static void js_async_function_terminate(JSRuntime *rt, JSAsyncFunctionData *s) { if (s->is_active) { async_func_free(rt, &s->func_state); s->is_active = FALSE; } }
/* AsyncFunction */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L19415-L19421
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_async_generator_next
static JSValue js_async_generator_next(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic) { JSAsyncGeneratorData *s = JS_GetOpaque(this_val, JS_CLASS_ASYNC_GENERATOR); JSValue promise, resolving_funcs[2]; JSAsyncGeneratorRequest *req; promise = JS_NewPromiseCapability(ctx, resolving_funcs); if (JS_IsException(promise)) return JS_EXCEPTION; if (!s) { JSValue err, res2; JS_ThrowTypeError(ctx, "not an AsyncGenerator object"); err = JS_GetException(ctx); res2 = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED, 1, (JSValueConst *)&err); JS_FreeValue(ctx, err); JS_FreeValue(ctx, res2); JS_FreeValue(ctx, resolving_funcs[0]); JS_FreeValue(ctx, resolving_funcs[1]); return promise; } req = js_mallocz(ctx, sizeof(*req)); if (!req) goto fail; req->completion_type = magic; req->result = JS_DupValue(ctx, argv[0]); req->promise = JS_DupValue(ctx, promise); req->resolving_funcs[0] = resolving_funcs[0]; req->resolving_funcs[1] = resolving_funcs[1]; list_add_tail(&req->link, &s->queue); if (s->state != JS_ASYNC_GENERATOR_STATE_EXECUTING) { js_async_generator_resume_next(ctx, s); } return promise; fail: JS_FreeValue(ctx, resolving_funcs[0]); JS_FreeValue(ctx, resolving_funcs[1]); JS_FreeValue(ctx, promise); return JS_EXCEPTION; }
/* magic = GEN_MAGIC_x */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L19951-L19992
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
js_object___setOwnProperty
static JSValue js_object___setOwnProperty(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { int ret; ret = JS_DefinePropertyValueValue(ctx, argv[0], JS_DupValue(ctx, argv[1]), JS_DupValue(ctx, argv[2]), JS_PROP_C_W_E | JS_PROP_THROW); if (ret < 0) return JS_EXCEPTION; else return JS_NewBool(ctx, ret); }
/* Note: corresponds to ECMA spec: CreateDataPropertyOrThrow() */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L37585-L37596
8b81e76efe457b59be3a6e752efd43916ba0cabb
BugChecker
github_2023
vitoplantamura
c
JS_ToUTF32String
static int JS_ToUTF32String(JSContext *ctx, uint32_t **pbuf, JSValueConst val1) { JSValue val; JSString *p; uint32_t *buf; int i, j, len; val = JS_ToString(ctx, val1); if (JS_IsException(val)) return -1; p = JS_VALUE_GET_STRING(val); len = p->len; /* UTF32 buffer length is len minus the number of correct surrogates pairs */ buf = js_malloc(ctx, sizeof(buf[0]) * max_int(len, 1)); if (!buf) { JS_FreeValue(ctx, val); goto fail; } for(i = j = 0; i < len;) buf[j++] = string_getc(p, &i); JS_FreeValue(ctx, val); *pbuf = buf; return j; fail: *pbuf = NULL; return -1; }
/* return (-1, NULL) if exception, otherwise (len, buf) */
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/quickjs.c#L41825-L41851
8b81e76efe457b59be3a6e752efd43916ba0cabb