X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=bitmatch.mli;h=95cc63c38c17483c26479f2daebba76cf9af8bf0;hb=a02d4dc211b61d5dd8827ce5727adf07ca4ccffb;hp=aee63e24f76673d8ba3da2bb58a423b0514d7187;hpb=7e5cbc974185646e6b0aeb21ce678d04a530356b;p=ocaml-bitstring.git diff --git a/bitmatch.mli b/bitmatch.mli index aee63e2..95cc63c 100644 --- a/bitmatch.mli +++ b/bitmatch.mli @@ -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.mli,v 1.17 2008-04-26 20:35:02 rjones Exp $ + * $Id$ *) (** @@ -29,7 +29,7 @@ this module to both parse and generate binary formats, for example, communications protocols, disk formats and binary files. - {{:http://et.redhat.com/~rjones/bitmatch/}OCaml bitmatch website} + {{:http://code.google.com/p/bitmatch/}OCaml bitmatch website} {2 Examples} @@ -186,7 +186,7 @@ let make_message typ subtype param = match then the standard library [Match_failure] exception is thrown. - Patterns look a bit different from normal match patterns. The + Patterns look a bit different from normal match patterns. They consist of a list of bitfields separated by [;] where each bitfield contains a bind variable, the width (in bits) of the field, and other information. Some example patterns: @@ -290,17 +290,21 @@ bitmatch bits with A bitstring field of length 0 matches an empty bitstring (occasionally useful when matching optional subfields). - Qualifiers are a list of identifiers which control the type, + Qualifiers are a list of identifiers/expressions which control the type, signedness and endianness of the field. Permissible qualifiers are: - - [int] (field has an integer type) - - [string] (field is a string type) - - [bitstring] (field is a bitstring type) - - [signed] (field is signed) - - [unsigned] (field is unsigned) - - [bigendian] (field is big endian - a.k.a network byte order) - - [littleendian] (field is little endian - a.k.a Intel byte order) - - [nativeendian] (field is same endianness as the machine) + - [int]: field has an integer type + - [string]: field is a string type + - [bitstring]: field is a bitstring type + - [signed]: field is signed + - [unsigned]: field is unsigned + - [bigendian]: field is big endian - a.k.a network byte order + - [littleendian]: field is little endian - a.k.a Intel byte order + - [nativeendian]: field is same endianness as the machine + - [endian (expr)]: [expr] should be an expression which evaluates to + a {!endian} type, ie. [LittleEndian], [BigEndian] or [NativeEndian]. + The expression is an arbitrary OCaml expression and can use the + value of earlier fields in the bitmatch. The default settings are [int], [unsigned], [bigendian]. @@ -381,9 +385,8 @@ Bitmatch.hexdump_bitstring stdout bits ;; computed expression. Detection of compile-time constants is quite simplistic so only an - immediate, simple integer is recognised as a constant and anything - else is considered a computed expression, even expressions such as - [5-2] which are obviously (to our eyes) constant. + simple integer literals and simple expressions (eg. [5*8]) are + recognized as constants. In any case the bit size of an integer is limited to the range \[1..64\]. This is detected as a compile-time error if that is @@ -414,6 +417,10 @@ Bitmatch.hexdump_bitstring stdout bits ;; still need to be a runtime check to enforce the size). + {2 Named patterns and persistent patterns} + + Please see {!Bitmatch_persistent} for documentation on this subject. + {2 Compiling} Using the compiler directly you can do: @@ -527,6 +534,11 @@ Bitmatch.hexdump_bitstring stdout bits ;; {3 Types} *) +type endian = BigEndian | LittleEndian | NativeEndian + +val string_of_endian : endian -> string +(** Endianness. *) + type bitstring = string * int * int (** [bitstring] is the basic type used to store bitstrings. @@ -630,7 +642,35 @@ val string_of_bitstring : bitstring -> string This function is inefficient. In the best case when the bitstring is nicely byte-aligned we do a [String.sub] operation. If the bitstring isn't aligned then this involves a lot of bit twiddling - and is particularly inefficient. *) + and is particularly inefficient. + + If the bitstring is not a multiple of 8 bits wide then the + final byte of the string contains the high bits set to the + remaining bits and the low bits set to 0. *) + +val bitstring_to_file : bitstring -> string -> unit +(** [bitstring_to_file bits filename] writes the bitstring [bits] + to the file [filename]. It overwrites the output file. + + Some restrictions apply, see {!bitstring_to_chan}. *) + +val bitstring_to_chan : bitstring -> out_channel -> unit +(** [bitstring_to_file bits filename] writes the bitstring [bits] + to the channel [chan]. + + Channels are made up of bytes, bitstrings can be any bit length + including fractions of bytes. So this function only works + if the length of the bitstring is an exact multiple of 8 bits + (otherwise it raises [Invalid_argument "bitstring_to_chan"]). + + Furthermore the function is efficient only in the case where + the bitstring is stored fully aligned, otherwise it has to + do inefficient bit twiddling like {!string_of_bitstring}. + + In the common case where the bitstring was generated by the + [BITSTRING] operator and is an exact multiple of 8 bits wide, + then this function will always work efficiently. +*) (** {3 Printing bitstrings} *) @@ -655,6 +695,12 @@ end (** {3 Miscellaneous} *) +val package : string +(** The package name, always ["ocaml-bitmatch"] *) + +val version : string +(** The package version as a string. *) + val debug : bool ref (** Set this variable to true to enable extended debugging. This only works if debugging was also enabled in the @@ -679,16 +725,46 @@ val extract_int_be_unsigned : string -> int -> int -> int -> int * int * int val extract_int_le_unsigned : string -> int -> int -> int -> int * int * int +val extract_int_ne_unsigned : string -> int -> int -> int -> int * int * int + +val extract_int_ee_unsigned : endian -> string -> int -> int -> int -> int * int * int + val extract_int32_be_unsigned : string -> int -> int -> int -> int32 * int * int val extract_int32_le_unsigned : string -> int -> int -> int -> int32 * int * int +val extract_int32_ne_unsigned : string -> int -> int -> int -> int32 * int * int + +val extract_int32_ee_unsigned : endian -> string -> int -> int -> int -> int32 * int * int + val extract_int64_be_unsigned : string -> int -> int -> int -> int64 * int * int -val construct_bit : Buffer.t -> bool -> int -> unit +val extract_int64_le_unsigned : string -> int -> int -> int -> int64 * int * int + +val extract_int64_ne_unsigned : string -> int -> int -> int -> int64 * int * int + +val extract_int64_ee_unsigned : endian -> string -> int -> int -> int -> int64 * int * int + +val construct_bit : Buffer.t -> bool -> int -> exn -> unit val construct_char_unsigned : Buffer.t -> int -> int -> exn -> unit val construct_int_be_unsigned : Buffer.t -> int -> int -> exn -> unit +val construct_int_ne_unsigned : Buffer.t -> int -> int -> exn -> unit + +val construct_int_ee_unsigned : endian -> Buffer.t -> int -> int -> exn -> unit + +val construct_int32_be_unsigned : Buffer.t -> int32 -> int -> exn -> unit + +val construct_int32_ne_unsigned : Buffer.t -> int32 -> int -> exn -> unit + +val construct_int32_ee_unsigned : endian -> Buffer.t -> int32 -> int -> exn -> unit + val construct_int64_be_unsigned : Buffer.t -> int64 -> int -> exn -> unit + +val construct_int64_ne_unsigned : Buffer.t -> int64 -> int -> exn -> unit + +val construct_int64_ee_unsigned : endian -> Buffer.t -> int64 -> int -> exn -> unit + +val construct_string : Buffer.t -> string -> unit