Clarify licensing for Debian.
[ocaml-bitstring.git] / bitmatch.ml
index e382504..249bbff 100644 (file)
@@ -4,7 +4,8 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2 of the License, or (at your option) any later version,
+ * with the OCaml linking exception described in COPYING.LIB.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -40,10 +41,19 @@ type bitstring = string * int * int
 (* Functions to create and load bitstrings. *)
 let empty_bitstring = "", 0, 0
 
-let make_bitstring len c = String.make ((len+7) lsr 3) c, 0, len
+let make_bitstring len c =
+  if len >= 0 then String.make ((len+7) lsr 3) c, 0, len
+  else
+    invalid_arg (
+      sprintf "make_bitstring/create_bitstring: len %d < 0" len
+    )
 
 let create_bitstring len = make_bitstring len '\000'
 
+let zeroes_bitstring = create_bitstring
+
+let ones_bitstring len = make_bitstring len '\xff'
+
 let bitstring_of_string str = str, 0, String.length str lsl 3
 
 let bitstring_of_chan chan =
@@ -804,6 +814,33 @@ let construct_string buf str =
   let len = String.length str in
   Buffer.add_bits buf str (len lsl 3)
 
+(* Construct from a bitstring. *)
+let construct_bitstring buf (data, off, len) =
+  (* Add individual bits until we get to the next byte boundary of
+   * the underlying string.
+   *)
+  let blen = 7 - ((off + 7) land 7) in
+  let blen = min blen len in
+  let rec loop off len blen =
+    if blen = 0 then (off, len)
+    else (
+      let b, off, len = extract_bit data off len 1 in
+      Buffer.add_bit buf b;
+      loop off len (blen-1)
+    )
+  in
+  let off, len = loop off len blen in
+  assert (len = 0 || (off land 7) = 0);
+
+  (* Add the remaining 'len' bits. *)
+  let data =
+    let off = off lsr 3 in
+    (* XXX dangerous allocation *)
+    if off = 0 then data
+    else String.sub data off (String.length data - off) in
+
+  Buffer.add_bits buf data len
+
 (*----------------------------------------------------------------------*)
 (* Extract a string from a bitstring. *)