kestrel/lib/Util.ml
2025-11-27 16:39:54 -03:00

17 lines
754 B
OCaml

let uint8_to_int8 i = (i lsl (Sys.int_size - 8)) asr (Sys.int_size - 8)
let uint16_to_int16 i = (i lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)
let get_uint16_wrap ?(wrap = 0xff) (bytes : bytes) (position : int) : int =
let i0 = position land wrap in
let hi = Bytes.get_uint8 bytes i0 in
let lo = Bytes.get_uint8 bytes ((i0 + 1) land wrap) in
(hi lsl 8) lor lo
let get_int16_wrap ?(wrap = 0xff) (bytes : bytes) (position : int) : int =
get_uint16_wrap ~wrap bytes position |> uint16_to_int16
let set_uint16_wrap ?(wrap = 0xff) (bytes : bytes) (position : int)
(value : int) : unit =
let i0 = position land wrap in
Bytes.set_uint8 bytes i0 ((value lsr 8) land 0xff);
Bytes.set_uint8 bytes ((i0 + 1) land wrap) (value land 0xff)