2023-06-19 20:44:32 +02:00
|
|
|
let get_bytecode ?(json = false) data =
|
2023-06-19 22:27:11 +02:00
|
|
|
let backslash = '\\' in
|
2023-06-05 19:56:59 +02:00
|
|
|
let result =
|
2023-06-19 22:27:11 +02:00
|
|
|
String.concat
|
|
|
|
""
|
|
|
|
(List.map
|
|
|
|
(fun byte ->
|
|
|
|
let code = Char.code byte in
|
|
|
|
if code >= 65 && code <= 122
|
|
|
|
then String.make 1 byte
|
|
|
|
else Printf.sprintf "%cx%02X" backslash code)
|
|
|
|
(List.of_seq (Bytes.to_seq data)))
|
2023-06-05 19:56:59 +02:00
|
|
|
in
|
2023-06-19 22:27:11 +02:00
|
|
|
if json
|
|
|
|
then
|
|
|
|
String.concat
|
|
|
|
""
|
|
|
|
(List.map
|
|
|
|
(fun c -> if c = backslash then String.make 2 c else String.make 1 c)
|
|
|
|
(List.of_seq (String.to_seq result)))
|
|
|
|
else result
|
2023-06-05 19:56:59 +02:00
|
|
|
;;
|
|
|
|
|
2023-06-19 23:15:49 +02:00
|
|
|
let get_ip ip =
|
|
|
|
Bytes.fold_left
|
|
|
|
(fun acc n ->
|
|
|
|
Printf.sprintf "%s%s%d" acc (if acc = "" then "" else ".") (int_of_char n))
|
|
|
|
""
|
|
|
|
ip
|
|
|
|
;;
|
|
|
|
|
2023-06-05 21:23:53 +02:00
|
|
|
let get_obj_size obj = Obj.size (Obj.repr obj)
|
|
|
|
|
|
|
|
let new_buffer size list =
|
2023-06-05 19:56:59 +02:00
|
|
|
let buffer = Bytes.create size in
|
|
|
|
let verification =
|
|
|
|
List.fold_left
|
|
|
|
(fun acc field ->
|
|
|
|
Bytes.set_uint16_be buffer acc field;
|
|
|
|
acc + 2)
|
|
|
|
0
|
|
|
|
list
|
|
|
|
in
|
|
|
|
if verification != size then failwith "Issue converting header to bytes";
|
|
|
|
buffer
|
|
|
|
;;
|
2023-06-13 02:36:32 +02:00
|
|
|
|
2023-06-13 02:37:33 +02:00
|
|
|
let unpack_short_be data offset =
|
|
|
|
let msb = int_of_char (Bytes.get data offset) in
|
|
|
|
let lsb = int_of_char (Bytes.get data (offset + 1)) in
|
2023-06-13 02:36:32 +02:00
|
|
|
(msb lsl 8) + lsb
|
|
|
|
;;
|
2023-06-14 16:34:49 +02:00
|
|
|
|
2023-06-19 20:03:07 +02:00
|
|
|
let unpack_int_be data offset =
|
|
|
|
let byte1 = int_of_char (Bytes.get data offset) in
|
|
|
|
let byte2 = int_of_char (Bytes.get data (offset + 1)) in
|
|
|
|
let byte3 = int_of_char (Bytes.get data (offset + 2)) in
|
|
|
|
let byte4 = int_of_char (Bytes.get data (offset + 3)) in
|
|
|
|
(byte1 lsl 24) lor (byte2 lsl 16) lor (byte3 lsl 8) lor byte4
|
|
|
|
;;
|
|
|
|
|
2023-06-14 16:34:49 +02:00
|
|
|
let bytes_forward data offset = Bytes.sub data offset (Bytes.length data - offset)
|