Bitstring 1.9.7

bitstrings and bitstring matching for OCaml

  Add it to your Download Basket!

 Add it to your Watch List!


Rate it!
send us
an update
GPL (GNU General Public License) 
2.1/5 7
Richard Jones
ROOT \ Programming \ Code Generators
Bitstring package contains bitstrings and bitmatching for OCaml.

The ocaml-bitstring project adds Erlang-style bitstrings and matching over bitstrings as a syntax extension and library for OCaml.

(This project was formerly known as "bitmatch").

You can use this module to both parse and generate binary formats, files and protocols.

Bitstring handling is added as primitives to the language, making it exceptionally simple to use and very powerful.

Here is how to parse the header from a GIF image:

let bits = Bitstring.bitstring_of_file "image.gif" in
bitmatch bits with
| { ("GIF87a"|"GIF89a") : 6*8 : string; (* GIF magic. *)
width : 16 : littleendian;
height : 16 : littleendian } ->
printf "%s: GIF image is %d x %d pixels" filename width height
| { _ } ->
eprintf "%s: Not a GIF imagen" filename

This example shows how to parse the first few fields in a Linux ELF binary (see < elf.h >):

let bits = Bitstring.bitstring_of_file "/bin/ls" in
bitmatch bits with
| { 0x7f : 8; "ELF" : 24 : string; (* ELF magic number *)
e_ident : 12*8 : bitstring; (* ELF identifier *)
e_type : 16 : littleendian; (* object file type *)
e_machine : 16 : littleendian (* architecture *)
} ->
printf "This is an ELF binary, type %d, arch %dn"
e_type e_machine;

Bitstring handles integers, strings, sub-bitstrings, big-, little- and native-endianness, signed and unsigned types, variable-width fields, fields with arbitrary bit alignment.

Last updated on July 26th, 2010


#OCaml bitstrings #OCaml bitmatching #OCaml library #OCaml #bitstrings #bitmatching #library

Add your review!