X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=bitmatch.ml;h=7065d661edaa4f528ad822fa5758428cf74bd0b0;hb=a8bf42a6bd16d6a2067f0e1475e440dbdb5bc153;hp=39d7743a3458ec5813e2b398dc32aca4a6498353;hpb=b445ee443ecc3a469f350a040b626188f5243983;p=ocaml-bitstring.git diff --git a/bitmatch.ml b/bitmatch.ml index 39d7743..7065d66 100644 --- a/bitmatch.ml +++ b/bitmatch.ml @@ -15,11 +15,14 @@ * 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.14 2008-05-12 20:32:55 rjones Exp $ + * $Id$ *) open Printf +include Bitmatch_types +include Bitmatch_config + (* Enable runtime debug messages. Must also have been enabled * in pa_bitmatch.ml. *) @@ -103,9 +106,13 @@ let bitstring_of_file_descr_max fd max = let bitstring_of_file fname = let chan = open_in_bin fname in - let bs = bitstring_of_chan chan in - close_in chan; - bs + try + let bs = bitstring_of_chan chan in + close_in chan; + bs + with exn -> + close_in chan; + raise exn let bitstring_length (_, _, len) = len @@ -408,6 +415,11 @@ let extract_int_le_unsigned data off len flen = let v = I.byteswap v flen in v, off, len +let extract_int_ne_unsigned = + if nativeendian = BigEndian + then extract_int_be_unsigned + else extract_int_le_unsigned + let _make_int32_be c0 c1 c2 c3 = Int32.logor (Int32.logor @@ -463,6 +475,11 @@ let extract_int32_le_unsigned data off len flen = let v = I32.byteswap v flen in v, off, len +let extract_int32_ne_unsigned = + if nativeendian = BigEndian + then extract_int32_be_unsigned + else extract_int32_le_unsigned + let _make_int64_be c0 c1 c2 c3 c4 c5 c6 c7 = Int64.logor (Int64.logor @@ -570,6 +587,11 @@ let extract_int64_le_unsigned data off len flen = ) in word, off+flen, len-flen +let extract_int64_ne_unsigned = + if nativeendian = BigEndian + then extract_int64_be_unsigned + else extract_int64_le_unsigned + (*----------------------------------------------------------------------*) (* Constructor functions. *) @@ -689,6 +711,12 @@ 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 +let construct_int_ne_unsigned = + if nativeendian = BigEndian + then construct_int_be_unsigned + else (*construct_int_le_unsigned*) + fun _ _ _ _ -> failwith "construct_int_le_unsigned" + (* Construct a field of exactly 32 bits. *) let construct_int32_be_unsigned buf v flen _ = Buffer.add_byte buf @@ -700,6 +728,12 @@ let construct_int32_be_unsigned buf v flen _ = Buffer.add_byte buf (Int32.to_int (Int32.logand v 0xff_l)) +let construct_int32_ne_unsigned = + if nativeendian = BigEndian + then construct_int32_be_unsigned + else (*construct_int32_le_unsigned*) + fun _ _ _ _ -> failwith "construct_int32_le_unsigned" + (* Construct a field of up to 64 bits. *) let construct_int64_be_unsigned buf v flen exn = (* Check value is within range. *) @@ -707,6 +741,12 @@ 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 +let construct_int64_ne_unsigned = + if nativeendian = BigEndian + then construct_int64_be_unsigned + else (*construct_int64_le_unsigned*) + fun _ _ _ _ -> failwith "construct_int64_le_unsigned" + (* Construct from a string of bytes, exact multiple of 8 bits * in length of course. *) @@ -731,6 +771,9 @@ let string_of_bitstring (data, off, len) = str.[i] <- Char.chr c; loop data off len (i+1) ) else if len > 0 then ( + (* XXX Is this correct? It should write into the high bits + * of the last byte. + *) let c, off, len = extract_char_unsigned data off len len in str.[i] <- Char.chr c ) @@ -739,6 +782,30 @@ let string_of_bitstring (data, off, len) = str ) +(* To channel. *) + +let bitstring_to_chan ((data, off, len) as bits) chan = + (* Fail if the bitstring length isn't a multiple of 8. *) + if len land 7 <> 0 then invalid_arg "bitstring_to_chan"; + + if off land 7 = 0 then + (* Easy case: string is byte-aligned. *) + output chan data (off lsr 3) (len lsr 3) + else ( + (* Bit-twiddling case: reuse string_of_bitstring *) + let str = string_of_bitstring bits in + output_string chan str + ) + +let bitstring_to_file bits filename = + let chan = open_out_bin filename in + try + bitstring_to_chan bits chan; + close_out chan + with exn -> + close_out chan; + raise exn + (*----------------------------------------------------------------------*) (* Display functions. *)