Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
type t = Utf8 | Utf16le | Utf16be
let equal (a : t) b = a = b
let to_string = function | Utf8 -> "UTF-8" | Utf16le -> "UTF-16le" | Utf16be -> "UTF-16be"
let pp ppf e = Fmt.string ppf (to_string e)let native = if Sys.big_endian then Utf16be else Utf16le
let of_string name = match String.lowercase_ascii name with | "utf8" | "utf-8" -> Some Utf8 | "utf-16le" | "utf16le" -> Some Utf16le | "utf-16be" | "utf16be" -> Some Utf16be | "utf-16" | "utf16" -> Some native | _ -> None
let code = function Utf8 -> 1 | Utf16le -> 2 | Utf16be -> 3let of_code n = match n land 3 with 2 -> Utf16le | 3 -> Utf16be | _ -> Utf8
(* sqlite3's arithmetic is on 32-bit unsigned characters. *)let u32 = 0xFFFF_FFFF
(* The bits a UTF-8 lead byte from 0xC0 up contributes (utf.c, sqlite3Utf8Trans1): five, four, three, two and one, then none for 0xFE and 0xFF. *)let lead_bits c = if c < 0xE0 then c land 0x1F else if c < 0xF0 then c land 0x0F else if c < 0xF8 then c land 0x07 else if c < 0xFC then c land 0x03 else if c < 0xFE then c land 0x01 else 0
(* One UTF-16 code unit written in [e]'s byte order at [b.(i)]. *)let[@inline] set_unit be b i u = if be then Bytes.set_uint16_be b i u else Bytes.set_uint16_le b i u
(* UTF-8 to UTF-16 as utf.c's READ_UTF8 and WRITE_UTF16LE/BE: a lead byte takes every continuation byte after it, the character an overlong form of ASCII or a surrogate makes is U+FFFD, and a character past U+FFFF is written as the two units its formula makes, past U+10FFFF too. *)let utf16_of_utf8 ~be s = let n = String.length s in let b = Bytes.create (4 * n) in let o = ref 0 and i = ref 0 in while !i < n do let c0 = Char.code (String.unsafe_get s !i) in incr i; let c = if c0 < 0xC0 then c0 else begin let c = ref (lead_bits c0) in while !i < n && Char.code (String.unsafe_get s !i) land 0xC0 = 0x80 do c := ((!c lsl 6) + (Char.code (String.unsafe_get s !i) land 0x3F)) land u32; incr i done; if !c < 0x80 || !c land 0xFFFF_F800 = 0xD800 then 0xFFFD else !c end in if c <= 0xFFFF then begin set_unit be b !o c; o := !o + 2 end else begin let d = (c - 0x10000) land u32 in let hi = ((0xD8 + ((d lsr 18) land 0x03)) lsl 8) lor (((c lsr 10) land 0x3F) + ((d lsr 10) land 0xC0)) in let lo = ((0xDC + ((c lsr 8) land 0x03)) lsl 8) lor (c land 0xFF) in set_unit be b !o hi; set_unit be b (!o + 2) lo; o := !o + 4 end done; Bytes.sub_string b 0 !o
let[@inline] unit_at be s i = if be then String.get_uint16_be s i else String.get_uint16_le s i
(* The character [c] written as UTF-8 at [b.(o)] (utf.c, WRITE_UTF8), and the offset past it. *)let put_utf8 b o c = let set i v = Bytes.unsafe_set b (o + i) (Char.unsafe_chr v) in if c < 0x80 then begin set 0 c; o + 1 end else if c < 0x800 then begin set 0 (0xC0 lor ((c lsr 6) land 0x1F)); set 1 (0x80 lor (c land 0x3F)); o + 2 end else if c < 0x10000 then begin set 0 (0xE0 lor ((c lsr 12) land 0x0F)); set 1 (0x80 lor ((c lsr 6) land 0x3F)); set 2 (0x80 lor (c land 0x3F)); o + 3 end else begin set 0 (0xF0 lor ((c lsr 18) land 0x07)); set 1 (0x80 lor ((c lsr 12) land 0x3F)); set 2 (0x80 lor ((c lsr 6) land 0x3F)); set 3 (0x80 lor (c land 0x3F)); o + 4 end
(* UTF-16 to UTF-8 as sqlite3VdbeMemTranslate: an odd last byte is dropped, a surrogate takes the next unit as its pair whatever it is, and a lone one at the end is written as the character it names. The second answer is whether that was exact: whether {!utf16_of_utf8} gives the bytes back, which it does unless a byte was dropped or a surrogate was not the high half of a pair. *)(* Whether the units [c] and [c2] are a high and a low surrogate. *)let[@inline] is_pair c c2 = c < 0xDC00 && c2 >= 0xDC00 && c2 < 0xE000
(* The character READ_UTF16 makes of a surrogate [c] and the unit [c2] after it, whatever [c2] is. *)let[@inline] paired c c2 = (c2 land 0x03FF) + ((c land 0x003F) lsl 10) + (((c land 0x03C0) + 0x0040) lsl 10)
let utf8_of_utf16 ~be s off len = if off < 0 || len < 0 || off > String.length s - len then invalid_arg "Sql.Encoding.to_utf8: a slice outside its string"; let ends = off + (len land lnot 1) in let b = Bytes.create (3 * (len / 2)) in let o = ref 0 and i = ref off and exact = ref (len land 1 = 0) in while !i < ends do let c = unit_at be s !i in i := !i + 2; let c = if c >= 0xD800 && c < 0xE000 then begin if !i < ends then begin let c2 = unit_at be s !i in i := !i + 2; if not (is_pair c c2) then exact := false; paired c c2 end else begin exact := false; c end end else c in o := put_utf8 b !o c done; (Bytes.sub_string b 0 !o, !exact)
(* The texts read inexactly ({!utf8_of_utf16}), each with the encoding and the bytes it was read from: a table open-addressed by the text's hash, whose key is the string the read made, held weakly and compared by identity, so that the same text spelt by anything else is that text, converted, and an entry goes with the last copy of its text. A slot whose key is gone and whose data is [Some] is a tombstone a search goes past; one whose data is [None] was never used and ends it. The table is built by the first inexact read, so a process that never reads one carries no table and takes no lock; it is shared by every domain under its lock. *)type registry = { lock : Mutex.t; mutable keys : string Weak.t; mutable data : (t * string) option array; mutable used : int; (** slots whose data is [Some], live or not *)}
let stored : registry option Atomic.t = Atomic.make None
let rec registry () = match Atomic.get stored with | Some r -> r | None -> let r = { lock = Mutex.create (); keys = Weak.create 64; data = Array.make 64 None; used = 0; } in if Atomic.compare_and_set stored None (Some r) then r else registry ()
(* The slot of [text] in [r], or [-1]: the probe from its hash goes past tombstones and stops at a slot never used. *)let slot_of r text = let n = Array.length r.data in let rec probe i k = if k = n then -1 else let j = (i + k) land (n - 1) in match (Weak.get r.keys j, r.data.(j)) with | Some s, Some _ when s == text -> j | _, None -> -1 | _, Some _ -> probe i (k + 1) in probe (Hashtbl.hash text) 0
(* [text] put in the first slot of its probe that holds no live key. *)let rec insert r text v = let n = Array.length r.data in let rec probe i = let j = i land (n - 1) in if Weak.check r.keys j then probe (i + 1) else begin if r.data.(j) = None then r.used <- r.used + 1; Weak.set r.keys j (Some text); r.data.(j) <- Some v end in if 2 * (r.used + 1) > n then begin rebuild r; insert r text v end else probe (Hashtbl.hash text)
(* [r] laid out again with its live entries alone, in a table twice as wide as they need. *)and rebuild r = let live = ref [] in Array.iteri (fun j d -> match (Weak.get r.keys j, d) with | Some k, Some v -> live := (k, v) :: !live | _ -> ()) r.data; let n = ref 64 in while !n < 4 * (List.length !live + 1) do n := 2 * !n done; r.keys <- Weak.create !n; r.data <- Array.make !n None; r.used <- 0; List.iter (fun (k, v) -> insert r k v) !live
let remember e text raw = let r = registry () in Mutex.protect r.lock (fun () -> match slot_of r text with | -1 -> insert r text (e, raw) | j -> r.data.(j) <- Some (e, raw))
let stored_bytes e text = match Atomic.get stored with | None -> None | Some r -> ( match Mutex.protect r.lock (fun () -> match slot_of r text with -1 -> None | j -> r.data.(j)) with | Some (e', raw) when e' = e -> Some raw | Some _ | None -> None)
let utf16 ~be e s off len = let text, exact = utf8_of_utf16 ~be s off len in if not exact then remember e text (String.sub s off len); text
let to_utf8 e s off len = match e with | Utf8 -> if off < 0 || len < 0 || off > String.length s - len then invalid_arg "Sql.Encoding.to_utf8: a slice outside its string"; String.sub s off len | Utf16le -> utf16 ~be:false Utf16le s off len | Utf16be -> utf16 ~be:true Utf16be s off len
let of_utf8 e s = match e with | Utf8 -> s | Utf16le | Utf16be -> ( match stored_bytes e s with | Some raw -> raw | None -> utf16_of_utf8 ~be:(e = Utf16be) s)
let compare_text e x y = match e with | Utf8 -> String.compare x y | Utf16le | Utf16be -> String.compare (of_utf8 e x) (of_utf8 e y)