1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 (* opposite of network order
23 Big Endian: strongest byte first (network order)
24 Little Endian: weakest byte first
31 external get_byte
: string -> int -> int = "%string_safe_get"
32 external set_byte
: string -> int -> int -> unit = "%string_safe_set"
35 Buffer.add_char buf
(char_of_int
(i
land 255))
42 let buf_int32_8 buf i =
43 Buffer.add_char buf (char_of_int (Int32.to_int (
46 let get_int32_8 s pos =
48 Int32.of_int (int_of_char s.[pos])
51 let buf_int64_8 buf i
=
52 Buffer.add_char buf
(char_of_int
(Int64.to_int
(
53 Int64.logand i
0xffL
)))
55 let get_int64_8 s pos
=
57 Int64.of_int
(int_of_char s
.[pos
])
60 let rec get_list_rec get_item s pos len left
=
61 if len
= 0 then List.rev left
, pos
else
62 let (item
,pos
) = get_item s pos
in
63 get_list_rec get_item s pos
(len
-1) (item
:: left
)
65 let get_list8 get_item s pos
=
66 let len = get_uint8 s pos
in
67 get_list_rec get_item s
(pos
+1) len []
69 let buf_list8 buf_item b list
=
70 let len = List.length list
in
72 List.iter
(buf_item b
) list
74 let buf_sha1 buf s
= Buffer.add_string buf
(Sha1.direct_to_string s
)
77 try Sha1.direct_of_string
(String.sub s pos
20)
79 (* lprintf "exception in get_sha1 %d s=%s\n" pos (String.escaped s); *)
82 let buf_md4 buf s
= Buffer.add_string buf
(Md4.direct_to_string s
)
85 try Md4.direct_of_string
(String.sub s pos
16)
87 (* lprintf "exception in get_md4 %d s=%s\n" pos (String.escaped s); *)
90 let buf_string8 buf s
=
91 buf_int8 buf
(String.length s
);
92 Buffer.add_string buf s
94 let get_string8 s pos
=
95 let len = get_uint8 s pos
in
96 String.sub s
(pos
+1) len, pos
+1+len
99 let len = String.length s
in
100 let asc = Buffer.create
16 in
101 let hex = Buffer.create
50 in
103 if i
= len then begin
104 if Buffer.length
asc > 0 then begin
105 let fill = String.make
(50 - (Buffer.length
hex )) ' '
in
106 Printf.bprintf buf
"%s%s|%-16s|\n" (Buffer.contents
hex) fill (Buffer.contents
asc);
110 if i
mod 16 = 0 then begin
112 then Printf.bprintf buf
"%s|%-16s|\n" (Buffer.contents
hex) (Buffer.contents
asc);
113 Printf.bprintf buf
"%08x: " i
;
118 let ioc = int_of_char
c in
119 let bc = if ioc > 32 && ioc < 127 then c else '
.'
in
120 Buffer.add_char
asc bc;
121 Buffer.add_string
hex (Printf.sprintf
"%02x " ioc);
122 if (i
+ 1) mod 8 = 0 then Buffer.add_char
hex ' '
;
129 let buf = Buffer.create
(String.length s
* 4) in
134 lprintf
"%s" (dump_hex_s s
)
136 let bdump_ascii buf s
=
137 let len = String.length s
in
138 Printf.bprintf
buf "ascii: [";
139 for i
= 0 to len - 1 do
141 let n = int_of_char
c in
142 if n > 31 && n < 127 then
143 Printf.bprintf
buf " %c" c
145 Printf.bprintf
buf "(%d)" n
147 Printf.bprintf
buf "]\n"
150 let buf = Buffer.create
1000 in
152 lprintf
"%s" (Buffer.contents
buf)
154 let bdump_dec buf s
=
155 let len = String.length s
in
156 Printf.bprintf
buf "dec: [";
157 for i
= 0 to len - 1 do
159 let n = int_of_char
c in
160 Printf.bprintf
buf "(%d)" n
162 Printf.bprintf
buf "]\n"
165 let buf = Buffer.create
1000 in
167 lprintf
"%s" (Buffer.contents
buf)
174 let bdump_sub buf s pos
len =
175 Printf.bprintf
buf "dec: [";
176 for i
= 0 to len - 1 do
178 let n = int_of_char
c in
179 Printf.bprintf
buf "(%d)" n
181 Printf.bprintf
buf "]\n\n"
183 let dump_sub s pos
len =
184 let buf = Buffer.create
1000 in
185 bdump_sub buf s pos
len;
186 lprintf
"%s" (Buffer.contents
buf)
194 let buf = Buffer.create
1000 in
199 lprintf
"%s" (sdump s
)