#include #include static unsigned char input[1 << 20] __attribute__((aligned(16))); static unsigned char output[5]; uint32_t input_ptr(void) { return (uint32_t)(uintptr_t)input; } uint32_t input_bytes_cap(void) { return sizeof input; } uint32_t output_ptr(void) { return (uint32_t)(uintptr_t)output; } uint32_t output_utf8_cap(void) { return sizeof output; } static inline v128_t range(v128_t x, unsigned lo, unsigned hi) { return wasm_v128_and(wasm_u8x16_ge(x, wasm_i8x16_splat(lo)), wasm_u8x16_le(x, wasm_i8x16_splat(hi))); } static int scalar(uint32_t i, uint32_t n) { while (i < n) { unsigned a = input[i++], count, lo = 0x80, hi = 0xbf; if (a < 0x80) continue; if (a >= 0xc2 && a <= 0xdf) count = 1; else if (a >= 0xe0 && a <= 0xef) { count = 2; if (a == 0xe0) lo = 0xa0; if (a == 0xed) hi = 0x9f; } else if (a >= 0xf0 && a <= 0xf4) { count = 3; if (a == 0xf0) lo = 0x90; if (a == 0xf4) hi = 0x8f; } else return 0; if (n - i < count || input[i] < lo || input[i] > hi) return 0; ++i; while (--count) { if (input[i] < 0x80 || input[i] > 0xbf) return 0; ++i; } } return 1; } static int valid(uint32_t n) { uint32_t i = 0; v128_t prev = wasm_i8x16_splat(0); for (; n - i >= 16; i += 16) { v128_t x = wasm_v128_load(input + i); // ASCII is valid if the preceding block did not end in a lead byte // or a continuation belonging to a still-incomplete character. if (!wasm_i8x16_bitmask(x) && !wasm_i8x16_bitmask(prev)) { prev = x; continue; } v128_t p1 = wasm_i8x16_shuffle(prev, x, 15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30); v128_t p2 = wasm_i8x16_shuffle(prev, x, 14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29); v128_t p3 = wasm_i8x16_shuffle(prev, x, 13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28); v128_t need = wasm_v128_or(range(p1,0xc2,0xf4), wasm_v128_or(range(p2,0xe0,0xf4), range(p3,0xf0,0xf4))); v128_t bad = wasm_v128_xor(need, range(x,0x80,0xbf)); bad = wasm_v128_or(bad, range(x,0xc0,0xc1)); bad = wasm_v128_or(bad, range(x,0xf5,0xff)); // Reject overlong encodings, surrogates, and values above U+10FFFF. bad = wasm_v128_or(bad, wasm_v128_and(wasm_i8x16_eq(p1,wasm_i8x16_splat(0xe0)), wasm_u8x16_lt(x,wasm_i8x16_splat(0xa0)))); bad = wasm_v128_or(bad, wasm_v128_and(wasm_i8x16_eq(p1,wasm_i8x16_splat(0xed)), wasm_u8x16_gt(x,wasm_i8x16_splat(0x9f)))); bad = wasm_v128_or(bad, wasm_v128_and(wasm_i8x16_eq(p1,wasm_i8x16_splat(0xf0)), wasm_u8x16_lt(x,wasm_i8x16_splat(0x90)))); bad = wasm_v128_or(bad, wasm_v128_and(wasm_i8x16_eq(p1,wasm_i8x16_splat(0xf4)), wasm_u8x16_gt(x,wasm_i8x16_splat(0x8f)))); if (wasm_v128_any_true(bad)) return 0; prev = x; } // Recheck the character crossing the boundary, including a truncated // character when the input ends exactly on a vector boundary. uint32_t tail = i; while (tail && input[tail - 1] >= 0x80 && input[tail - 1] <= 0xbf) --tail; if (tail && input[tail - 1] >= 0xc2) --tail; return scalar(tail, n); } uint64_t render(uint32_t len) { int ok = len <= sizeof input && valid(len); if (ok) { output[0]='t'; output[1]='r'; output[2]='u'; output[3]='e'; } else { output[0]='f'; output[1]='a'; output[2]='l'; output[3]='s'; output[4]='e'; } return ((uint64_t)output_ptr() << 32) | (ok ? 4u : 5u); }