-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.ml
62 lines (55 loc) · 1.7 KB
/
misc.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let do_option f = function
Some x -> f x
| None -> ()
let push l x = (l := x :: !l)
let hexdump_fmt ~offset_format ~width outf buffer =
let format_offset ofs =
match offset_format with
| `Oct -> Printf.sprintf "%07o " ofs
| `Dec -> Printf.sprintf "%07d " ofs
| `Hex -> Printf.sprintf "%06x " ofs in
let buflen = Bytes.length buffer in
let rec drec ofs =
if ofs = buflen then () else begin
let fmt_offset = format_offset ofs in
let offset_len = (String.length fmt_offset) in
outf fmt_offset ;
let remaining = (width - offset_len) / 3 in
let remaining = min remaining (buflen - ofs) in
for i = 0 to remaining - 1 do
let c = Bytes.get buffer (ofs + i) in
outf (Printf.sprintf "%02x " (Char.code c))
done ;
outf "\n" ;
outf (String.make offset_len ' ') ;
let escape c =
let s = Char.escaped c in
if String.get s 0 = '\\'
&& (String.get s 1 = '0' ||
String.get s 1 = '1' ||
String.get s 1 = '2') then
" "
else s in
for i = 0 to remaining - 1 do
let c = Bytes.get buffer (ofs + i) in
let escs = escape c in
let nfill = max 0 (3 - (String.length escs)) in
outf escs ; outf (String.make nfill ' ') ;
done ;
outf "\n" ;
drec (ofs + remaining)
end
in
drec 0
let hexdump ?(offset_format=`Oct) ?(width=0) oc buffer =
let fd = Unix.descr_of_out_channel oc in
let isatty = Unix.isatty fd in
let width =
if width = 0 then
80
else width in
hexdump_fmt ~offset_format ~width (output_string oc) buffer
let hexdump_to_string ?(offset_format=`Oct) ?(width=80) buffer =
let b = Buffer.create width in
hexdump_fmt ~offset_format ~width (Buffer.add_string b) buffer ;
Buffer.contents b