2 * $Id: bitmatch.ml,v 1.4 2008-04-01 10:58:53 rjones Exp $
7 (* A bitstring is simply the data itself (as a string), and the
8 * bitoffset and the bitlength within the string. Note offset/length
9 * are counted in bits, not bytes.
11 type bitstring = string * int * int
13 (* Functions to create and load bitstrings. *)
14 let empty_bitstring = "", 0, 0
16 let make_bitstring len c = String.make ((len+7) lsr 3) c, 0, len
18 let create_bitstring len = make_bitstring len '\000'
20 let bitstring_of_chan chan =
21 let tmpsize = 16384 in
22 let buf = Buffer.create tmpsize in
23 let tmp = String.create tmpsize in
25 while n := input chan tmp 0 tmpsize; !n > 0 do
26 Buffer.add_substring buf tmp 0 !n;
28 Buffer.contents buf, 0, Buffer.length buf lsl 3
30 let bitstring_of_file fname =
31 let chan = open_in_bin fname in
32 let bs = bitstring_of_chan chan in
36 (*----------------------------------------------------------------------*)
37 (* Extraction functions.
39 * NB: internal functions, called from the generated macros, and
40 * the parameters should have been checked for sanity already).
44 let extract_bitstring data off len flen =
45 (data, off, flen), off+flen, len-flen
47 let extract_remainder data off len =
48 (data, off, len), off+len, 0
50 (* Extract and convert to numeric. A single bit is returned as
51 * a boolean. There are no endianness or signedness considerations.
53 let extract_bit data off len _ = (* final param is always 1 *)
54 let byteoff = off lsr 3 in
55 let bitmask = 1 lsl (7 - (off land 7)) in
56 let b = Char.code data.[byteoff] land bitmask <> 0 in
59 (* Returns 8 bit unsigned aligned bytes from the string.
60 * If the string ends then this returns 0's.
62 let _get_byte data byteoff strlen =
63 if strlen > byteoff then Char.code data.[byteoff] else 0
64 let _get_byte32 data byteoff strlen =
65 if strlen > byteoff then Int32.of_int (Char.code data.[byteoff]) else 0l
66 let _get_byte64 data byteoff strlen =
67 if strlen > byteoff then Int64.of_int (Char.code data.[byteoff]) else 0L
69 (* Extract [2..8] bits. Because the result fits into a single
70 * byte we don't have to worry about endianness, only signedness.
72 let extract_char_unsigned data off len flen =
73 let byteoff = off lsr 3 in
75 (* Optimize the common (byte-aligned) case. *)
76 if off land 7 = 0 then (
77 let byte = Char.code data.[byteoff] in
78 byte lsr (8 - flen), off+flen, len-flen
80 (* Extract the 16 bits at byteoff and byteoff+1 (note that the
81 * second byte might not exist in the original string).
83 let strlen = String.length data in
86 (_get_byte data byteoff strlen lsl 8) +
87 _get_byte data (byteoff+1) strlen in
89 (* Mask off the top bits. *)
90 let bitmask = (1 lsl (16 - (off land 7))) - 1 in
91 let word = word land bitmask in
92 (* Shift right to get rid of the bottom bits. *)
93 let shift = 16 - ((off land 7) + flen) in
94 let word = word lsr shift in
96 word, off+flen, len-flen
99 (* Extract [9..31] bits. We have to consider endianness and signedness. *)
100 let extract_int_be_unsigned data off len flen =
101 let byteoff = off lsr 3 in
103 let strlen = String.length data in
106 (* Optimize the common (byte-aligned) case. *)
107 if off land 7 = 0 then (
109 (_get_byte data byteoff strlen lsl 23) +
110 (_get_byte data (byteoff+1) strlen lsl 15) +
111 (_get_byte data (byteoff+2) strlen lsl 7) +
112 (_get_byte data (byteoff+3) strlen lsr 1) in
114 ) else if flen <= 24 then (
115 (* Extract the 31 bits at byteoff .. byteoff+3. *)
117 (_get_byte data byteoff strlen lsl 23) +
118 (_get_byte data (byteoff+1) strlen lsl 15) +
119 (_get_byte data (byteoff+2) strlen lsl 7) +
120 (_get_byte data (byteoff+3) strlen lsr 1) in
121 (* Mask off the top bits. *)
122 let bitmask = (1 lsl (31 - (off land 7))) - 1 in
123 let word = word land bitmask in
124 (* Shift right to get rid of the bottom bits. *)
125 let shift = 31 - ((off land 7) + flen) in
128 (* Extract the next 31 bits, slow method. *)
130 let c0, off, len = extract_char_unsigned data off len 8 in
131 let c1, off, len = extract_char_unsigned data off len 8 in
132 let c2, off, len = extract_char_unsigned data off len 8 in
133 let c3, off, len = extract_char_unsigned data off len 7 in
134 (c0 lsl 23) + (c1 lsl 15) + (c2 lsl 7) + c3 in
137 word, off+flen, len-flen
139 (* Extract exactly 32 bits. We have to consider endianness and signedness. *)
140 let extract_int32_be_unsigned data off len flen =
141 let byteoff = off lsr 3 in
143 let strlen = String.length data in
146 (* Optimize the common (byte-aligned) case. *)
147 if off land 7 = 0 then (
152 (Int32.shift_left (_get_byte32 data byteoff strlen) 24)
153 (Int32.shift_left (_get_byte32 data (byteoff+1) strlen) 16))
154 (Int32.shift_left (_get_byte32 data (byteoff+2) strlen) 8))
155 (_get_byte32 data (byteoff+3) strlen) in
156 Int32.shift_right word (32 - flen)
158 (* Extract the next 32 bits, slow method. *)
160 let c0, off, len = extract_char_unsigned data off len 8 in
161 let c1, off, len = extract_char_unsigned data off len 8 in
162 let c2, off, len = extract_char_unsigned data off len 8 in
163 let c3, off, len = extract_char_unsigned data off len 8 in
164 let c0 = Int32.shift_left (Int32.of_int c0) 24 in
165 let c1 = Int32.shift_left (Int32.of_int c1) 16 in
166 let c2 = Int32.shift_left (Int32.of_int c2) 8 in
167 let c3 = Int32.of_int c3 in
168 Int32.add c0 (Int32.add c1 (Int32.add c2 c3)) in
169 Int32.shift_right word (32 - flen)
171 word, off+flen, len-flen
174 (*----------------------------------------------------------------------*)
175 (* Display functions. *)
178 let c = Char.code c in
181 let hexdump_bitstring chan (data, off, len) =
185 let linelen = ref 0 in
186 let linechars = String.make 16 ' ' in
188 fprintf chan "00000000 ";
191 let bits = min !len 8 in
192 let byte, off', len' = extract_char_unsigned data !off !len bits in
193 off := off'; len := len';
195 let byte = byte lsl (8-bits) in
196 fprintf chan "%02x " byte;
199 linechars.[!linelen] <-
200 (let c = Char.chr byte in
201 if isprint c then c else '.');
203 if !linelen = 8 then fprintf chan " ";
204 if !linelen = 16 then (
205 fprintf chan " |%s|\n%08x " linechars !count;
207 for i = 0 to 15 do linechars.[i] <- ' ' done
211 if !linelen > 0 then (
212 let skip = (16 - !linelen) * 3 + if !linelen < 8 then 1 else 0 in
213 for i = 0 to skip-1 do fprintf chan " " done;
214 fprintf chan " |%s|\n" linechars