drop md4 i?86 specific asm implementations
[mldonkey.git] / src / utils / net / anyEndian.ml
blob2c65c15d6badd9d1a90e540e219eccc8da6e5398
1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
2 (*
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
20 open Printf2
21 (* opposite of network order
23 Big Endian: strongest byte first (network order)
24 Little Endian: weakest byte first
27 open Md4
28 open Autoconf
31 external get_byte: string -> int -> int = "%string_safe_get"
32 external set_byte: string -> int -> int -> unit = "%string_safe_set"
34 let buf_int8 buf i =
35 Buffer.add_char buf (char_of_int (i land 255))
37 let get_uint8 s pos =
38 check_string s pos;
39 int_of_char s.[pos]
42 let buf_int32_8 buf i =
43 Buffer.add_char buf (char_of_int (Int32.to_int (
44 and32 i 0xffl)))
46 let get_int32_8 s pos =
47 check_string 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 =
56 check_string 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
71 buf_int8 b len;
72 List.iter (buf_item b) list
74 let buf_sha1 buf s = Buffer.add_string buf (Sha1.direct_to_string s)
76 let get_sha1 s pos =
77 try Sha1.direct_of_string (String.sub s pos 20)
78 with e ->
79 (* lprintf "exception in get_sha1 %d s=%s\n" pos (String.escaped s); *)
80 raise e
82 let buf_md4 buf s = Buffer.add_string buf (Md4.direct_to_string s)
84 let get_md4 s pos =
85 try Md4.direct_of_string (String.sub s pos 16)
86 with e ->
87 (* lprintf "exception in get_md4 %d s=%s\n" pos (String.escaped s); *)
88 raise e
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
98 let bdump_hex buf s =
99 let len = String.length s in
100 let asc = Buffer.create 16 in
101 let hex = Buffer.create 50 in
102 let rec iter i =
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);
109 else begin
110 if i mod 16 = 0 then begin
111 if i > 0
112 then Printf.bprintf buf "%s|%-16s|\n" (Buffer.contents hex) (Buffer.contents asc);
113 Printf.bprintf buf "%08x: " i;
114 Buffer.clear asc;
115 Buffer.clear hex;
116 end;
117 let c = s.[i] in
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 ' ';
123 iter (i + 1);
124 end;
126 iter 0
128 let dump_hex_s s =
129 let buf = Buffer.create (String.length s * 4) in
130 bdump_hex buf s;
131 Buffer.contents buf
133 let dump_hex s =
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
140 let c = s.[i] in
141 let n = int_of_char c in
142 if n > 31 && n < 127 then
143 Printf.bprintf buf " %c" c
144 else
145 Printf.bprintf buf "(%d)" n
146 done;
147 Printf.bprintf buf "]\n"
149 let dump_ascii s =
150 let buf = Buffer.create 1000 in
151 bdump_ascii buf s;
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
158 let c = s.[i] in
159 let n = int_of_char c in
160 Printf.bprintf buf "(%d)" n
161 done;
162 Printf.bprintf buf "]\n"
164 let dump_dec s =
165 let buf = Buffer.create 1000 in
166 bdump_dec buf s;
167 lprintf "%s" (Buffer.contents buf)
169 let dump s =
170 dump_ascii s;
171 dump_dec s;
172 dump_hex s
174 let bdump_sub buf s pos len =
175 Printf.bprintf buf "dec: [";
176 for i = 0 to len - 1 do
177 let c = s.[pos+i] in
178 let n = int_of_char c in
179 Printf.bprintf buf "(%d)" n
180 done;
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)
188 let bdump buf s =
189 bdump_ascii buf s;
190 bdump_dec buf s;
191 bdump_hex buf s
193 let sdump s =
194 let buf = Buffer.create 1000 in
195 bdump buf s;
196 Buffer.contents buf
198 let dump s =
199 lprintf "%s" (sdump s)