Skip to content

Commit 93221a5

Browse files
committed
adding a hexstring function
1 parent 5d79758 commit 93221a5

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

Makefile.dep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ bin,ocamlbuild
1919
lib,unix
2020
lib,str
2121
lib,bitstring
22+
lib,batteries

myocamlbuild.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ let _ = dispatch begin function
7373
tag_any
7474
["pkg_str";
7575
"pkg_unix";
76+
"pkg_batteries";
7677
"pkg_bitstring";
7778
"pkg_bitstring.syntax";
7879
];

src/ocamlExploit.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ let ( * ) str num =
5757
let int32_to_hexstring i =
5858
Printf.sprintf "%08lx" i
5959

60+
let hexstring_to_string hexstr =
61+
let hexchar prev curr =
62+
int_of_string (Printf.sprintf "0x%c%c" prev curr) |> char_of_int
63+
in
64+
let len = String.length hexstr in
65+
assert (len mod 2 = 0);
66+
let str = String.make (len / 2) '\x00' in
67+
let _ =
68+
BatString.fold_left (fun (pos, prev) ch ->
69+
match prev with
70+
| None -> pos, Some ch
71+
| Some prev -> str.[pos] <- (hexchar prev ch); pos+1, None
72+
) (0, None) hexstr
73+
in
74+
str
75+
6076
let print str = print_endline str; flush stdout
6177

6278
let explode s =

src/ocamlExploit.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ val ( * ) : string -> int -> string
3939
(** 32bit int to hex *)
4040
val int32_to_hexstring : int32 -> string
4141

42+
(** get a string from a hexstring *)
43+
val hexstring_to_string : string -> string
44+
4245
(** simple print, but always flush *)
4346
val print : string -> unit
4447

0 commit comments

Comments
 (0)