···11+module Config = Configurator.V1
22+33+let pre407 = {ocaml|external unsafe_set_uint16 : bytes -> int -> int -> unit = "%caml_string_set16u" [@@noalloc]|ocaml}
44+let standard = {ocaml|external unsafe_set_uint16 : bytes -> int -> int -> unit = "%caml_bytes_set16u" [@@noalloc]|ocaml}
55+66+type t =
77+ { major : int
88+ ; minor : int
99+ ; patch : int option
1010+ ; extra : string option }
1111+1212+let v ?patch ?extra major minor = { major; minor; patch; extra; }
1313+1414+let parse s =
1515+ try Scanf.sscanf s "%d.%d.%d+%s" (fun major minor patch extra -> v ~patch ~extra major minor)
1616+ with End_of_file | Scanf.Scan_failure _ ->
1717+ ( try Scanf.sscanf s "%d.%d+%s" (fun major minor extra -> v ~extra major minor)
1818+ with End_of_file | Scanf.Scan_failure _ ->
1919+ ( try Scanf.sscanf s "%d.%d.%d" (fun major minor patch -> v ~patch major minor)
2020+ with End_of_file | Scanf.Scan_failure _ ->
2121+ Scanf.sscanf s "%d.%d" (fun major minor -> v major minor) ) )
2222+2323+let ( >|= ) x f = match x with
2424+ | Some x -> Some (f x )
2525+ | None -> None
2626+2727+let ocaml_cp ~src ~dst =
2828+ let ic = open_in src in
2929+ let oc = open_out dst in
3030+ let bf = Bytes.create 0x1000 in
3131+ let rec go () = match input ic bf 0 (Bytes.length bf) with
3232+ | 0 -> ()
3333+ | len -> output oc bf 0 len ; go ()
3434+ | exception End_of_file -> () in
3535+ go () ; close_in ic ; close_out oc
3636+;;
3737+3838+let () =
3939+ Config.main ~name:"config-base64" @@ fun t ->
4040+ match Config.ocaml_config_var t "version" >|= parse with
4141+ | Some version ->
4242+ let dst = "unsafe.ml" in
4343+4444+ if (version.major, version.minor) >= (4, 7)
4545+ then ocaml_cp ~src:"unsafe_stable.ml" ~dst
4646+ else ocaml_cp ~src:"unsafe_pre407.ml" ~dst
4747+ | None -> Config.die "OCaml version is not available"
4848+ | exception exn -> Config.die "Got an exception: %s" (Printexc.to_string exn)
···32323333let unsafe_get_uint8 t off = Char.code (String.unsafe_get t off)
3434let unsafe_set_uint8 t off v = Bytes.unsafe_set t off (Char.chr v)
3535+let unsafe_set_uint16 = Unsafe.unsafe_set_uint16
35363636-external unsafe_set_uint16 : bytes -> int -> int -> unit = "%caml_string_set16u" [@@noalloc]
3737external unsafe_get_uint16 : string -> int -> int = "%caml_string_get16u" [@@noalloc]
3838external swap16 : int -> int = "%bswap16" [@@noalloc]
3939