More complete handling of constant field length expressions.
[ocaml-bitstring.git] / bitmatch.ml
index 861cd5a..39d7743 100644 (file)
@@ -15,7 +15,7 @@
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  *
- * $Id: bitmatch.ml,v 1.9 2008-04-15 13:40:51 rjones Exp $
+ * $Id: bitmatch.ml,v 1.14 2008-05-12 20:32:55 rjones Exp $
  *)
 
 open Printf
@@ -41,6 +41,8 @@ let make_bitstring len c = String.make ((len+7) lsr 3) c, 0, len
 
 let create_bitstring len = make_bitstring len '\000'
 
+let bitstring_of_string str = str, 0, String.length str lsl 3
+
 let bitstring_of_chan chan =
   let tmpsize = 16384 in
   let buf = Buffer.create tmpsize in
@@ -51,6 +53,54 @@ let bitstring_of_chan chan =
   done;
   Buffer.contents buf, 0, Buffer.length buf lsl 3
 
+let bitstring_of_chan_max chan max =
+  let tmpsize = 16384 in
+  let buf = Buffer.create tmpsize in
+  let tmp = String.create tmpsize in
+  let len = ref 0 in
+  let rec loop () =
+    if !len < max then (
+      let r = min tmpsize (max - !len) in
+      let n = input chan tmp 0 r in
+      if n > 0 then (
+       Buffer.add_substring buf tmp 0 n;
+       len := !len + n;
+       loop ()
+      )
+    )
+  in
+  loop ();
+  Buffer.contents buf, 0, !len lsl 3
+
+let bitstring_of_file_descr fd =
+  let tmpsize = 16384 in
+  let buf = Buffer.create tmpsize in
+  let tmp = String.create tmpsize in
+  let n = ref 0 in
+  while n := Unix.read fd tmp 0 tmpsize; !n > 0 do
+    Buffer.add_substring buf tmp 0 !n;
+  done;
+  Buffer.contents buf, 0, Buffer.length buf lsl 3
+
+let bitstring_of_file_descr_max fd max =
+  let tmpsize = 16384 in
+  let buf = Buffer.create tmpsize in
+  let tmp = String.create tmpsize in
+  let len = ref 0 in
+  let rec loop () =
+    if !len < max then (
+      let r = min tmpsize (max - !len) in
+      let n = Unix.read fd tmp 0 r in
+      if n > 0 then (
+       Buffer.add_substring buf tmp 0 n;
+       len := !len + n;
+       loop ()
+      )
+    )
+  in
+  loop ();
+  Buffer.contents buf, 0, !len lsl 3
+
 let bitstring_of_file fname =
   let chan = open_in_bin fname in
   let bs = bitstring_of_chan chan in
@@ -430,6 +480,9 @@ let _make_int64_be c0 c1 c2 c3 c4 c5 c6 c7 =
        (Int64.shift_left c6 8))
     c7
 
+let _make_int64_le c0 c1 c2 c3 c4 c5 c6 c7 =
+  _make_int64_be c7 c6 c5 c4 c3 c2 c1 c0
+
 (* Extract [1..64] bits.  We have to consider endianness and signedness. *)
 let extract_int64_be_unsigned data off len flen =
   let byteoff = off lsr 3 in
@@ -474,6 +527,49 @@ let extract_int64_be_unsigned data off len flen =
     ) in
   word, off+flen, len-flen
 
+let extract_int64_le_unsigned data off len flen =
+  let byteoff = off lsr 3 in
+
+  let strlen = String.length data in
+
+  let word =
+    (* Optimize the common (byte-aligned) case. *)
+    if off land 7 = 0 then (
+      let word =
+       let c0 = _get_byte64 data byteoff strlen in
+       let c1 = _get_byte64 data (byteoff+1) strlen in
+       let c2 = _get_byte64 data (byteoff+2) strlen in
+       let c3 = _get_byte64 data (byteoff+3) strlen in
+       let c4 = _get_byte64 data (byteoff+4) strlen in
+       let c5 = _get_byte64 data (byteoff+5) strlen in
+       let c6 = _get_byte64 data (byteoff+6) strlen in
+       let c7 = _get_byte64 data (byteoff+7) strlen in
+       _make_int64_le c0 c1 c2 c3 c4 c5 c6 c7 in
+      Int64.logand word (I64.mask flen)
+    ) else (
+      (* Extract the next 64 bits, slow method. *)
+      let word =
+       let c0, off, len = extract_char_unsigned data off len 8 in
+       let c1, off, len = extract_char_unsigned data off len 8 in
+       let c2, off, len = extract_char_unsigned data off len 8 in
+       let c3, off, len = extract_char_unsigned data off len 8 in
+       let c4, off, len = extract_char_unsigned data off len 8 in
+       let c5, off, len = extract_char_unsigned data off len 8 in
+       let c6, off, len = extract_char_unsigned data off len 8 in
+       let c7, _, _ = extract_char_unsigned data off len 8 in
+       let c0 = Int64.of_int c0 in
+       let c1 = Int64.of_int c1 in
+       let c2 = Int64.of_int c2 in
+       let c3 = Int64.of_int c3 in
+       let c4 = Int64.of_int c4 in
+       let c5 = Int64.of_int c5 in
+       let c6 = Int64.of_int c6 in
+       let c7 = Int64.of_int c7 in
+       _make_int64_le c0 c1 c2 c3 c4 c5 c6 c7 in
+      Int64.logand word (I64.mask flen)
+    ) in
+  word, off+flen, len-flen
+
 (*----------------------------------------------------------------------*)
 (* Constructor functions. *)
 
@@ -574,7 +670,7 @@ module Buffer = struct
 end
 
 (* Construct a single bit. *)
-let construct_bit buf b _ =
+let construct_bit buf b _ =
   Buffer.add_bit buf b
 
 (* Construct a field, flen = [2..8]. *)
@@ -593,6 +689,17 @@ let construct_int_be_unsigned buf v flen exn =
   (* Add the bytes. *)
   I.map_bytes_be (Buffer._add_bits buf) (Buffer.add_byte buf) v flen
 
+(* Construct a field of exactly 32 bits. *)
+let construct_int32_be_unsigned buf v flen _ =
+  Buffer.add_byte buf
+    (Int32.to_int (Int32.shift_right_logical v 24));
+  Buffer.add_byte buf
+    (Int32.to_int ((Int32.logand (Int32.shift_right_logical v 16) 0xff_l)));
+  Buffer.add_byte buf
+    (Int32.to_int ((Int32.logand (Int32.shift_right_logical v 8) 0xff_l)));
+  Buffer.add_byte buf
+    (Int32.to_int (Int32.logand v 0xff_l))
+
 (* Construct a field of up to 64 bits. *)
 let construct_int64_be_unsigned buf v flen exn =
   (* Check value is within range. *)
@@ -600,6 +707,13 @@ let construct_int64_be_unsigned buf v flen exn =
   (* Add the bytes. *)
   I64.map_bytes_be (Buffer._add_bits buf) (Buffer.add_byte buf) v flen
 
+(* Construct from a string of bytes, exact multiple of 8 bits
+ * in length of course.
+ *)
+let construct_string buf str =
+  let len = String.length str in
+  Buffer.add_bits buf str (len lsl 3)
+
 (*----------------------------------------------------------------------*)
 (* Extract a string from a bitstring. *)