-bitmatch.cmo: bitmatch_types.cmo bitmatch_config.cmo bitmatch.cmi
-bitmatch.cmx: bitmatch_types.cmx bitmatch_config.cmx bitmatch.cmi
-bitmatch_objinfo.cmo: bitmatch_persistent.cmi bitmatch.cmi
-bitmatch_objinfo.cmx: bitmatch_persistent.cmx bitmatch.cmx
-bitmatch_persistent.cmo: bitmatch.cmi bitmatch_persistent.cmi
-bitmatch_persistent.cmx: bitmatch.cmx bitmatch_persistent.cmi
-bitmatch_persistent.cmi: bitmatch.cmi
-create_test_pattern.cmo: bitmatch_persistent.cmi
-create_test_pattern.cmx: bitmatch_persistent.cmx
+bitstring.cmo: bitstring_types.cmo bitstring_config.cmo bitstring.cmi
+bitstring.cmx: bitstring_types.cmx bitstring_config.cmx bitstring.cmi
+bitstring_objinfo.cmo: bitstring_persistent.cmi bitstring.cmi
+bitstring_objinfo.cmx: bitstring_persistent.cmx bitstring.cmx
+bitstring_persistent.cmo: bitstring.cmi bitstring_persistent.cmi
+bitstring_persistent.cmx: bitstring.cmx bitstring_persistent.cmi
+bitstring_persistent.cmi: bitstring.cmi
+create_test_pattern.cmo: bitstring_persistent.cmi
+create_test_pattern.cmx: bitstring_persistent.cmx
aclocal.m4
bitmatch.ml
-bitmatch.mli
-bitmatch_config.ml.in
-bitmatch_objinfo.ml
-bitmatch_persistent.ml
-bitmatch_persistent.mli
-bitmatch_types.ml
+bitstring.ml
+bitstring.mli
+bitstring_config.ml.in
+bitstring_objinfo.ml
+bitstring_persistent.ml
+bitstring_persistent.mli
+bitstring_types.ml
.depend
-cil-tools/bitmatch_import_c.ml
-cil-tools/bitmatch-import-prefix.h
+cil-tools/bitstring_import_c.ml
+cil-tools/bitstring-import-prefix.h
cil-tools/.depend
cil-tools/ext3.c
cil-tools/ext3.ml
MANIFEST
META.in
README
-pa_bitmatch.ml
+pa_bitstring.ml
tests/01_load.ml
tests/02_run.ml
tests/10_match_bits.ml
-name = "bitmatch"
+name = "bitstring"
version = "@PACKAGE_VERSION@"
description = "Bitstrings and bitstring matching"
requires = "unix"
-archive(byte) = "bitmatch.cma"
-archive(native) = "bitmatch.cmxa"
+archive(byte) = "bitstring.cma"
+archive(native) = "bitstring.cmxa"
package "persistent" (
- requires = "bitmatch"
+ requires = "bitstring"
version = "@PACKAGE_VERSION@"
- description = "Persistent patterns for bitmatch (requires camlp4)"
- archive(byte) = "bitmatch_persistent.cma"
- archive(native) = "bitmatch_persistent.cmxa"
+ description = "Persistent patterns for bitstring (requires camlp4)"
+ archive(byte) = "bitstring_persistent.cma"
+ archive(native) = "bitstring_persistent.cmxa"
)
package "syntax" (
version = "@PACKAGE_VERSION@"
- description = "Syntax extension: bitmatch operators"
- archive(syntax,preprocessor) = "unix.cma bitmatch.cma bitmatch_persistent.cma pa_bitmatch.cmo"
- archive(syntax,toploop) = "unix.cma bitmatch.cma bitmatch_persistent.cma pa_bitmatch.cmo"
+ description = "Syntax extension: bitstring operators"
+ archive(syntax,preprocessor) = "unix.cma bitstring.cma bitstring_persistent.cma pa_bitstring.cmo"
+ archive(syntax,toploop) = "unix.cma bitstring.cma bitstring_persistent.cma pa_bitstring.cmo"
preprocessor = "camlp4of"
)
-# Bitmatch syntax extension.
+# Bitstring syntax extension.
# Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
#
# This library is free software; you can redistribute it and/or
TESTS = $(patsubst %.ml,%,$(wildcard tests/*.ml))
-all: bitmatch.cma bitmatch_persistent.cma \
- bitmatch.cmxa bitmatch_persistent.cmxa \
- pa_bitmatch.cmo \
- bitmatch-objinfo
+all: bitstring.cma bitstring_persistent.cma \
+ bitstring.cmxa bitstring_persistent.cmxa \
+ pa_bitstring.cmo \
+ bitstring-objinfo
@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done
-bitmatch.cma: bitmatch_types.cmo bitmatch_config.cmo bitmatch.cmo
+bitstring.cma: bitstring_types.cmo bitstring_config.cmo bitstring.cmo \
+ bitmatch.cmo
$(OCAMLFIND) ocamlc -a -o $@ $^
-bitmatch_persistent.cma: bitmatch_persistent.cmo
+bitstring_persistent.cma: bitstring_persistent.cmo
$(OCAMLFIND) ocamlc -a -o $@ $^
-bitmatch_persistent.cmo: bitmatch_persistent.ml
+bitstring_persistent.cmo: bitstring_persistent.ml
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) \
-I +camlp4 -pp camlp4of -c $<
-bitmatch.cmxa: bitmatch_types.cmx bitmatch_config.cmx bitmatch.cmx
+bitstring.cmxa: bitstring_types.cmx bitstring_config.cmx bitstring.cmx \
+ bitmatch.cmx
$(OCAMLFIND) ocamlopt -a -o $@ $^
-bitmatch_persistent.cmxa: bitmatch_persistent.cmx
+bitstring_persistent.cmxa: bitstring_persistent.cmx
$(OCAMLFIND) ocamlopt -a -o $@ $^
-bitmatch_persistent.cmx: bitmatch_persistent.ml
+bitstring_persistent.cmx: bitstring_persistent.ml
$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(OCAMLOPTPACKAGES) \
-I +camlp4 -pp camlp4of -c $<
-bitmatch_persistent.cmi: bitmatch_persistent.mli
+bitstring_persistent.cmi: bitstring_persistent.mli
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) \
-I +camlp4 -pp camlp4of -c $<
-pa_bitmatch.cmo: pa_bitmatch.ml bitmatch.cma bitmatch_persistent.cma
- $(OCAMLFIND) ocamlc bitmatch.cma -I +camlp4 camlp4lib.cma \
+pa_bitstring.cmo: pa_bitstring.ml bitstring.cma bitstring_persistent.cma
+ $(OCAMLFIND) ocamlc bitstring.cma -I +camlp4 camlp4lib.cma \
-pp camlp4of -c $< -o $@
-bitmatch-objinfo: bitmatch_objinfo.cmo bitmatch.cma bitmatch_persistent.cma
+bitstring-objinfo: bitstring_objinfo.cmo bitstring.cma bitstring_persistent.cma
$(OCAMLFIND) ocamlc -I +camlp4 unix.cma camlp4lib.cma \
- bitmatch.cma bitmatch_persistent.cma \
+ bitstring.cma bitstring_persistent.cma \
$< -o $@
# Tests and examples.
-PP = -pp "camlp4o bitmatch.cma bitmatch_persistent.cma pa_bitmatch.cmo"
+PP = -pp "camlp4o bitstring.cma bitstring_persistent.cma pa_bitstring.cmo"
-test: pa_bitmatch.cmo bitmatch.cma bitmatch_persistent.cma tests/test.bmpp
+test: pa_bitstring.cmo bitstring.cma bitstring_persistent.cma tests/test.bmpp
@for f in $(TESTS); do \
echo Building $$f; \
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(PP) \
- -package unix -linkpkg -I . bitmatch.cma $$f.ml -o $$f; \
+ -package unix -linkpkg -I . bitstring.cma $$f.ml -o $$f; \
if [ $$? -ne 0 ]; then exit 1; fi; \
echo Running $$f; \
$$f; \
create_test_pattern: create_test_pattern.cmo
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -pp camlp4of \
unix.cma -I +camlp4 camlp4lib.cma \
- -I . bitmatch.cma bitmatch_persistent.cma $< -o $@
+ -I . bitstring.cma bitstring_persistent.cma $< -o $@
create_test_pattern.cmo: create_test_pattern.ml
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -pp camlp4of \
unix.cma -I +camlp4 \
-I . -c $< -o $@
-examples: pa_bitmatch.cmo bitmatch.cma bitmatch_persistent.cma
+examples: pa_bitstring.cmo bitstring.cma bitstring_persistent.cma
@for f in $(EXAMPLES); do \
echo Building $$f; \
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(PP) \
- -package unix -linkpkg -I . bitmatch.cma $$f.ml -o $$f; \
+ -package unix -linkpkg -I . bitstring.cma $$f.ml -o $$f; \
if [ $$? -ne 0 ]; then exit 1; fi; \
done
@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done
-print-tests: pa_bitmatch.cmo bitmatch_persistent.cma
+print-tests: pa_bitstring.cmo bitstring_persistent.cma
@for f in $(TESTS); do \
echo Test: $$f.ml; \
- cmd="camlp4o bitmatch.cma bitmatch_persistent.cma pa_bitmatch.cmo \
+ cmd="camlp4o bitstring.cma bitstring_persistent.cma pa_bitstring.cmo \
-printer pr_o.cmo $$f.ml"; \
echo $$cmd; \
$$cmd; \
done
-print-examples: pa_bitmatch.cmo
+print-examples: pa_bitstring.cmo
@for f in $(EXAMPLES); do \
echo Example: $$f.ml; \
- camlp4o bitmatch.cma bitmatch_persistent.cma pa_bitmatch.cmo \
+ camlp4o bitstring.cma bitstring_persistent.cma pa_bitstring.cmo \
-printer pr_o.cmo $$f.ml; \
if [ $$? -ne 0 ]; then exit 1; fi; \
done
distclean: clean
rm -rf autom4te.cache
rm -f config.log config.status
- rm -f config.h Makefile META bitmatch_config.ml
+ rm -f config.h Makefile META bitstring_config.ml
+ rm -f bitstring-objinfo
@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done
# Documentation.
rm -rf html
mkdir html
$(OCAMLDOC) $(OCAMLDOCFLAGS) -d html -I +camlp4 \
- bitmatch.mli bitmatch_persistent.mli
+ bitstring.mli bitstring_persistent.mli
endif
# Install.
install:
- ocamlfind install bitmatch META *.mli *.cmx *.cma *.cmxa *.a \
- bitmatch.cmi \
- bitmatch_persistent.cmi \
- pa_bitmatch.cmo
+ ocamlfind install bitstring META *.mli *.cmx *.cma *.cmxa *.a \
+ bitstring.cmi \
+ bitstring_persistent.cmi \
+ pa_bitstring.cmo
@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done
# Standard rules.
depend: .depend
@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done
-.depend: bitmatch.ml bitmatch.mli \
- bitmatch_persistent.ml bitmatch_persistent.mli \
- bitmatch_objinfo.ml \
+.depend: bitstring.ml bitstring.mli \
+ bitstring_persistent.ml bitstring_persistent.mli \
+ bitstring_objinfo.ml \
create_test_pattern.ml
rm -f .depend
$(OCAMLFIND) ocamldep $(OCAMLCPACKAGES) \
- bitmatch.ml bitmatch.mli \
- bitmatch_objinfo.ml >> $@
+ bitstring.ml bitstring.mli \
+ bitstring_objinfo.ml >> $@
$(OCAMLFIND) ocamldep $(OCAMLCPACKAGES) -pp camlp4of \
- bitmatch_persistent.ml bitmatch_persistent.mli \
+ bitstring_persistent.ml bitstring_persistent.mli \
create_test_pattern.ml >> $@
ifeq ($(wildcard .depend),.depend)
-ocaml-bitmatch Copyright (C) 2008 Red Hat Inc, Richard W.M. Jones.
+ocaml-bitstring Copyright (C) 2008 Red Hat Inc, Richard W.M. Jones.
+
+This library was formerly known as 'bitmatch'.
Please see the html subdirectory for developer documentation.
--- /dev/null
+(** Bitmatch backwards compatibility library. *)
+(* Bitstring library.
+ * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
+ *
+ * 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,
+ * 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
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * 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$
+ *)
+(** This is just for backwards compatibility with code
+ from when the library was called bitmatch. You should
+ replace all references to this module with {!Bitstring}.
+*)
+
+include Bitstring
-(* Bitmatch library.
+(* Bitstring library.
* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
*
* This library is free software; you can redistribute it and/or
open Printf
-include Bitmatch_types
-include Bitmatch_config
+include Bitstring_types
+include Bitstring_config
(* Enable runtime debug messages. Must also have been enabled
- * in pa_bitmatch.ml.
+ * in pa_bitstring.ml.
*)
let debug = ref false
else if bits = 31 then
minus_one
else
- invalid_arg "Bitmatch.I.mask"
+ invalid_arg "Bitstring.I.mask"
(* Byte swap an int of a given size. *)
let byteswap v bits =
else if bits = 32 then
minus_one
else
- invalid_arg "Bitmatch.I32.mask"
+ invalid_arg "Bitstring.I32.mask"
(* Byte swap an int of a given size. *)
let byteswap v bits =
else if bits = 64 then
minus_one
else
- invalid_arg "Bitmatch.I64.mask"
+ invalid_arg "Bitstring.I64.mask"
(* Byte swap an int of a given size. *)
(* let byteswap v bits = *)
(* Add exactly 8 bits. *)
let add_byte ({ buf = buf; len = len; last = last } as t) byte =
- if byte < 0 || byte > 255 then invalid_arg "Bitmatch.Buffer.add_byte";
+ if byte < 0 || byte > 255 then invalid_arg "Bitstring.Buffer.add_byte";
let shift = len land 7 in
if shift = 0 then
(* Target buffer is byte-aligned. *)
* to call add_bit so it's slow.
*)
let _add_bits t c slen =
- if slen < 1 || slen >= 8 then invalid_arg "Bitmatch.Buffer._add_bits";
+ if slen < 1 || slen >= 8 then invalid_arg "Bitstring.Buffer._add_bits";
for i = slen-1 downto 0 do
let bit = c land (1 lsl i) <> 0 in
add_bit t bit
-(** Bitmatch library. *)
+(** Bitstring library. *)
(* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
*
* This library is free software; you can redistribute it and/or
{2 Introduction}
- Bitmatch adds Erlang-style bitstrings and matching over bitstrings
+ Bitstring adds Erlang-style bitstrings and matching over bitstrings
as a syntax extension and library for OCaml. You can use
this module to both parse and generate binary formats, for
example, communications protocols, disk formats and binary files.
- {{:http://code.google.com/p/bitmatch/}OCaml bitmatch website}
+ {{:http://code.google.com/p/bitstring/}OCaml bitstring website}
+
+ This library used to be called "bitmatch".
{2 Examples}
printf " checksum: %d\n" checksum;
printf " source: %lx dest: %lx\n" source dest;
printf " header options + padding:\n";
- Bitmatch.hexdump_bitstring stdout options;
+ Bitstring.hexdump_bitstring stdout options;
printf " packet payload:\n";
- Bitmatch.hexdump_bitstring stdout payload
+ Bitstring.hexdump_bitstring stdout payload
| { version : 4 } ->
eprintf "unknown IP version %d\n" version;
| { _ } as pkt ->
eprintf "data is smaller than one nibble:\n";
- Bitmatch.hexdump_bitstring stderr pkt;
+ Bitstring.hexdump_bitstring stderr pkt;
exit 1
]}
{{:http://lxr.linux.no/linux/include/linux/ext3_fs.h}Linux EXT3 filesystem superblocks}:
{[
-let bits = Bitmatch.bitstring_of_file "tests/ext3_sb"
+let bits = Bitstring.bitstring_of_file "tests/ext3_sb"
let () =
bitmatch bits with
{[
| { n : 4;
rest : -1 : bitstring }
- when Bitmatch.bitstring_length rest = 0 -> ...
+ when Bitstring.bitstring_length rest = 0 -> ...
(* Only matches exactly 4 bits. *)
]}
the integer 1, and the following 12 bits containing the integer 10,
arranged in network byte order. *)
-Bitmatch.hexdump_bitstring stdout bits ;;
+Bitstring.hexdump_bitstring stdout bits ;;
(* Prints:
{2 Named patterns and persistent patterns}
- Please see {!Bitmatch_persistent} for documentation on this subject.
+ Please see {!Bitstring_persistent} for documentation on this subject.
{2 Compiling}
Using the compiler directly you can do:
{v
- ocamlc -I +bitmatch \
- -pp "camlp4of bitmatch.cma bitmatch_persistent.cma \
- `ocamlc -where`/bitmatch/pa_bitmatch.cmo" \
- unix.cma bitmatch.cma test.ml -o test
+ ocamlc -I +bitstring \
+ -pp "camlp4of bitstring.cma bitstring_persistent.cma \
+ `ocamlc -where`/bitstring/pa_bitstring.cmo" \
+ unix.cma bitstring.cma test.ml -o test
v}
Simpler method using findlib:
{v
ocamlfind ocamlc \
- -package bitmatch,bitmatch.syntax -syntax bitmatch.syntax \
+ -package bitstring,bitstring.syntax -syntax bitstring.syntax \
-linkpkg test.ml -o test
v}
However the above does not necessarily apply to strings used in
matching, since they may cause the library to use the
- {!Bitmatch.string_of_bitstring} function, which allocates a string.
+ {!Bitstring.string_of_bitstring} function, which allocates a string.
So you should take care if you use the [string] type particularly
with a computed length that is derived from external input.
Note that the when-clause is evaluated {i last}, so if you are
relying on the when-clause to filter cases then your code may do a
lot of extra and unncessary pattern-matching work on fields which
- may never be needed just to evaluate the when-clause. You can
- usually rearrange the code to do only the first part of the match,
- followed by the when-clause, followed by a second inner bitmatch.
+ may never be needed just to evaluate the when-clause. Either
+ rearrange the code to do only the first part of the match,
+ followed by the when-clause, followed by a second inner bitmatch,
+ or use a [check()] qualifier within fields.
{3 Safety}
(** {3 Miscellaneous} *)
val package : string
-(** The package name, always ["ocaml-bitmatch"] *)
+(** The package name, always ["ocaml-bitstring"] *)
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
- [pa_bitmatch.ml] file at compile time, otherwise it
+ [pa_bitstring.ml] file at compile time, otherwise it
does nothing. *)
(**/**)
-(* Bitmatch library.
+(* Bitstring library.
* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
*
* @configure_input@
* configure script.
*)
-let nativeendian = Bitmatch_types.@NATIVEENDIAN@
+let nativeendian = Bitstring_types.@NATIVEENDIAN@
let package = "@PACKAGE_NAME@"
let version = "@PACKAGE_VERSION@"
-(* Bitmatch syntax extension.
+(* Bitstring syntax extension.
* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
*
* This program is free software; you can redistribute it and/or modify
open Printf
-open Bitmatch
-module P = Bitmatch_persistent
+open Bitstring
+module P = Bitstring_persistent
let () =
if Array.length Sys.argv <= 1 then
- failwith "bitmatch_objinfo filename.bmpp";
+ failwith "bitstring-objinfo filename.bmpp";
let filename = Sys.argv.(1) in
let chan = open_in filename in
let names = ref [] in
-(* Bitmatch persistent patterns.
+(* Bitstring persistent patterns.
* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
*
* This library is free software; you can redistribute it and/or
}
and field_type = Int | String | Bitstring (* field type *)
and endian_expr =
- | ConstantEndian of Bitmatch.endian (* a constant little/big/nativeendian *)
+ | ConstantEndian of Bitstring.endian (* a constant little/big/nativeendian *)
| EndianExpr of expr (* an endian expression *)
type pattern = patt field list
| None -> "[non-const-len]" in
let endian =
match endian with
- | ConstantEndian endian -> Bitmatch.string_of_endian endian
+ | ConstantEndian endian -> Bitstring.string_of_endian endian
| EndianExpr _ -> "endian([expr])" in
let signed = if signed then "signed" else "unsigned" in
let t = string_of_field_type t in
{
field = <:patt< _ >>;
flen = <:expr< 32 >>;
- endian = ConstantEndian Bitmatch.BigEndian;
+ endian = ConstantEndian Bitstring.BigEndian;
signed = false;
t = Int;
_loc = _loc;
{
field = <:expr< 0 >>;
flen = <:expr< 32 >>;
- endian = ConstantEndian Bitmatch.BigEndian;
+ endian = ConstantEndian Bitstring.BigEndian;
signed = false;
t = Int;
_loc = _loc;
-(** Bitmatch persistent patterns. *)
+(** Bitstring persistent patterns. *)
(* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
*
* This library is free software; you can redistribute it and/or
(**
{b Warning:} This documentation is for ADVANCED USERS ONLY.
If you are not an advanced user, you are probably looking
- for {{:Bitmatch.html}the Bitmatch documentation}.
+ for {{:Bitstring.html}the Bitstring documentation}.
{{:#reference}Jump straight to the reference section for
documentation on types and functions}.
{2 Introduction}
- Bitmatch allows you to name sets of fields and reuse them
+ Bitstring allows you to name sets of fields and reuse them
elsewhere. For example if you frequently need to parse
Pascal-style strings in the form length byte + string, then you
could name the [{ strlen : 8 : int; str : strlen*8 : string }]
'Persistent' refers to the fact that they can be saved into binary
files. However these binary files use OCaml [Marshal] module and
depend (sometimes) on the version of OCaml used to generate them
- and (sometimes) the version of bitmatch used. So your build system
+ and (sometimes) the version of bitstring used. So your build system
should rebuild these files from source when your code is rebuilt.
Persistent patterns are syntactic. They work in the same way
Programs which generate and manipulate persistent patterns have to
link to camlp4. Since camlp4 in OCaml >= 3.10 is rather large, we
have placed this code into this separate submodule, so that
- programs which just use bitmatch don't need to pull in the whole of
+ programs which just use bitstring don't need to pull in the whole of
camlp4. This restriction does not apply to code which only uses
persistent patterns but does not generate them. If the distinction
isn't clear, use [ocamlobjinfo] to look at the dependencies of your
[*.cmo] files.
Persistent patterns can be generated in several ways, but they
- can only be {i used} by the [pa_bitmatch] syntax extension.
+ can only be {i used} by the [pa_bitstring] syntax extension.
This means they are purely compile-time constructs. You
cannot use them to make arbitrary patterns and run those
patterns (not unless your program runs [ocamlc] to make a [*.cmo]
- Relative to the current directory
- Relative to the OCaml library directory
- {3 bitmatch-objinfo}
+ {3 bitstring-objinfo}
- The [bitmatch-objinfo] command can be run on a file in order
+ The [bitstring-objinfo] command can be run on a file in order
to print out the patterns in the file.
{3 Constructors}
Note: This is not type safe. The pattern/constructor must
have been written out under the same version of OCaml and
- the same version of bitmatch. *)
+ the same version of bitstring. *)
val named_from_string : string -> int -> named
(** Load a pattern/constructor from a string at offset within the string.
Note: This is not type safe. The pattern/constructor must
have been written out under the same version of OCaml and
- the same version of bitmatch. *)
+ the same version of bitstring. *)
(** {3 Create pattern fields}
The effect is that the field [{ len : 2*i : string }] could
be created by calling [set_length field <:expr< 2*i >>]. *)
-val set_endian : 'a field -> Bitmatch.endian -> 'a field
+val set_endian : 'a field -> Bitstring.endian -> 'a field
(** Sets the endianness of a field to the constant endianness.
The effect is that the field [{ _ : 16 : bigendian }] could
- be created by calling [set_endian field Bitmatch.BigEndian]. *)
+ be created by calling [set_endian field Bitstring.BigEndian]. *)
val set_endian_expr : 'a field -> expr -> 'a field
(** Sets the endianness of a field to an endianness expression.
val set_location : 'a field -> loc_t -> 'a field
(** Sets the source code location of a field. This is used when
- pa_bitmatch displays error messages. *)
+ pa_bitstring displays error messages. *)
val set_offset_int : 'a field -> int -> 'a field
(** Set the offset expression for a field to the given number.
is an OCaml expression, since lengths can be non-constant. *)
type endian_expr =
- | ConstantEndian of Bitmatch.endian
+ | ConstantEndian of Bitstring.endian
| EndianExpr of expr
val get_endian : 'a field -> endian_expr
-(* Bitmatch library.
+(* Bitstring library.
* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
*
* This library is free software; you can redistribute it and/or
-bitmatch_import_c.cmo: ../bitmatch_persistent.cmi ../bitmatch_config.cmo \
- ../bitmatch.cmi
-bitmatch_import_c.cmx: ../bitmatch_persistent.cmx ../bitmatch_config.cmx \
- ../bitmatch.cmx
+bitstring_import_c.cmo: ../bitstring_persistent.cmi ../bitstring_config.cmo \
+ ../bitstring.cmi
+bitstring_import_c.cmx: ../bitstring_persistent.cmx ../bitstring_config.cmx \
+ ../bitstring.cmx
-# Bitmatch syntax extension.
+# Bitstring CIL tools for processing C code.
# Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
#
# This library is free software; you can redistribute it and/or
OCAMLCFLAGS = -g
OCAMLCPACKAGES = -package unix,str,extlib,cil -I +camlp4 -I ..
-OCAMLCLIBS = $(OCAMLCPACKAGES) camlp4lib.cma -linkpkg ../bitmatch.cma ../bitmatch_persistent.cma
+OCAMLCLIBS = $(OCAMLCPACKAGES) camlp4lib.cma -linkpkg ../bitstring.cma ../bitstring_persistent.cma
OCAMLOPTFLAGS =
OCAMLOPTPACKAGES = $(OCAMLCPACKAGES)
-OCAMLOPTLIBS = $(OCAMLOPTPACKAGES) camlp4lib.cmxa -linkpkg ../bitmatch.cmxa ../bitmatch_persistent.cmxa
+OCAMLOPTLIBS = $(OCAMLOPTPACKAGES) camlp4lib.cmxa -linkpkg ../bitstring.cmxa ../bitstring_persistent.cmxa
-PP = -pp "camlp4o -I .. bitmatch.cma bitmatch_persistent.cma pa_bitmatch.cmo"
+PP = -pp "camlp4o -I .. bitstring.cma bitstring_persistent.cma pa_bitstring.cmo"
OCAMLDOCFLAGS = -html -sort
-all: bitmatch-import-c bitmatch-import-c.opt
+all: bitstring-import-c bitstring-import-c.opt
-bitmatch-import-c: bitmatch_import_c.cmo
+bitstring-import-c: bitstring_import_c.cmo
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCLIBS) $< -o $@
-bitmatch-import-c.opt: bitmatch_import_c.cmx
+bitstring-import-c.opt: bitstring_import_c.cmx
$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(OCAMLOPTLIBS) $< -o $@
test:
ext3.cmo: ext3.ml ext3.bmpp
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) $(PP) -c $< -o $@
-ext3.bmpp: ext3.c bitmatch-import-c
+ext3.bmpp: ext3.c bitstring-import-c
rm -f $@.new
- ./bitmatch-import-c $(DEBUG) $< > $@.new
+ ./bitstring-import-c $(DEBUG) $< > $@.new
mv $@.new $@
task_struct: task_struct.cmo
task_struct.cmo: task_struct.ml task_struct.bmpp
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) $(PP) -c $< -o $@
-task_struct.bmpp: task_struct.c bitmatch-import-c
+task_struct.bmpp: task_struct.c bitstring-import-c
cd $(LINUX_HEADERS) && ln -sf asm-x86 asm
rm -f $@.new
- ./bitmatch-import-c $(DEBUG) $(LINUX_INCLUDES) $< > $@.new
+ ./bitstring-import-c $(DEBUG) $(LINUX_INCLUDES) $< > $@.new
mv $@.new $@
# Clean.
clean:
rm -f core *~ *.cmi *.cmo *.cmx *.cma *.cmxa *.a *.o *.bmpp
- rm -f bitmatch-import-c bitmatch-import-c.opt
+ rm -f bitstring-import-c bitstring-import-c.opt
distclean: clean
depend: .depend
-.depend: bitmatch_import_c.ml
+.depend: bitstring_import_c.ml
rm -f .depend
$(OCAMLFIND) ocamldep $(OCAMLCPACKAGES) $^ > $@
#define _Bool unsigned
#endif
-#define BITMATCH_IMPORT(name) __bitmatch_import_##name
-#define BITMATCH_CONSTANT_STRING(name,val) \
- char *__bitmatch_constant_##name = val
-#define BITMATCH_CONSTANT_INT32(name,val) \
- int __bitmatch_constant_##name = val
+#define BITSTRING_IMPORT(name) __bitstring_import_##name
+#define BITSTRING_CONSTANT_STRING(name,val) \
+ char *__bitstring_constant_##name = val
+#define BITSTRING_CONSTANT_INT32(name,val) \
+ int __bitstring_constant_##name = val
open Cil
-module P = Bitmatch_persistent
+module P = Bitstring_persistent
let (//) = Filename.concat
let debug = ref false in
let save_temps = ref false in
let version () =
- printf "bitmatch-import-c %s" Bitmatch.version;
+ printf "bitstring-import-c %s" Bitstring.version;
exit 1
in
let cpp_args = ref [] in
match !input_file with
| None -> input_file := Some str
| Some _ ->
- eprintf "bitmatch-import-c: only give a single input file\n";
+ eprintf "bitstring-import-c: only give a single input file\n";
exit 1
in
let usage_msg = "\
-bitmatch-import-c: Import C structures and constants and
+bitstring-import-c: Import C structures and constants and
generate bitmatching functions from them. Please see the
- manual page bitmatch-import-c(1) for more information.
+ manual page bitstring-import-c(1) for more information.
OPTIONS" in
match !input_file with
| Some f -> f
| None ->
- eprintf "bitmatch-import-c: no input file specified\n";
+ eprintf "bitstring-import-c: no input file specified\n";
exit 1 in
let cpp_args = List.rev !cpp_args in
) in
let cmd =
- sprintf "cpp %s -I %s -include bitmatch-import-prefix.h %s > %s"
+ sprintf "cpp %s -I %s -include bitstring-import-prefix.h %s > %s"
(String.concat " " (List.map Filename.quote cpp_args))
- (Filename.quote (Bitmatch_config.ocamllibdir // "bitmatch"))
+ (Filename.quote (Bitstring_config.ocamllibdir // "bitstring"))
(Filename.quote input_file) (Filename.quote tmp) in
if debug then prerr_endline cmd;
if Sys.command cmd <> 0 then (
delete_tmp ();
(* Find out which structures, #defines, etc. are to be imported.
- * (cf. the macros in bitmatch-import-prefix.h)
+ * (cf. the macros in bitstring-import-prefix.h)
*)
let constants =
List.filter_map (
| GVar ({vname = vname; vtype = vtype},
{ init = Some (SingleInit vinit) },
loc)
- when String.starts_with vname "__bitmatch_constant_" ->
+ when String.starts_with vname "__bitstring_constant_" ->
let vname = String.sub vname 20 (String.length vname - 20) in
(* Do constant folding on the initializer and then calculate
List.filter_map (
function
| GType ({tname = tname; ttype = ttype}, loc)
- when String.starts_with tname "__bitmatch_import_" ->
+ when String.starts_with tname "__bitstring_import_" ->
let tname = String.sub tname 18 (String.length tname - 18) in
Some (tname, ttype, loc)
| _ -> None
* __attribute__((bitwise)).
* ttype CIL type of struct.
* Returns:
- * pattern A bitmatch persistent pattern.
+ * pattern A bitstring persistent pattern.
*)
let rec pattern_of_struct ?(names=[]) ?(offset=NoOffset) ?(endian=None)
ttype =
when hasAttribute "bitwise" attrs ->
let endian =
if String.starts_with tname "__le" then
- Some Bitmatch.LittleEndian
+ Some Bitstring.LittleEndian
else if String.starts_with tname "__be" then
- Some Bitmatch.BigEndian
+ Some Bitstring.BigEndian
else (
Errormsg.warn "%a: unknown bitwise attribute typename: %s\n"
d_loc loc tname;
let field =
match endian with
| Some endian -> P.set_endian field endian
- | None -> P.set_endian field Bitmatch.NativeEndian in
+ | None -> P.set_endian field Bitstring.NativeEndian in
field
/* This is an example import file, showing how to import the ext3
* superblock automatically from Linux header files.
*
- * Use: bitmatch-import-c ext3.c > ext3.bmpp
+ * Use: bitstring-import-c ext3.c > ext3.bmpp
*
* Tip: Add the --debug flag to that command line to see what's going on.
- * Also use bitmatch-objinfo to examine the bmpp file.
+ * Also use bitstring-objinfo to examine the bmpp file.
*/
/* These are required by Linux in order to get the little/big-endian
#include <linux/ext2_fs.h>
/* This tells the importer program what structures and constants to import. */
-//typedef struct ext3_super_block BITMATCH_IMPORT(ext3_super_block);
-typedef struct ext2_super_block BITMATCH_IMPORT(ext3_super_block);
-BITMATCH_CONSTANT_INT32 (ext3_super_magic, EXT3_SUPER_MAGIC);
+//typedef struct ext3_super_block BITSTRING_IMPORT(ext3_super_block);
+typedef struct ext2_super_block BITSTRING_IMPORT(ext3_super_block);
+BITSTRING_CONSTANT_INT32 (ext3_super_magic, EXT3_SUPER_MAGIC);
(* This is an example program using an imported C structure.
*
* The C structure is imported from <linux/ext3_fs.h> via "ext3.c"
- * by the bitmatch-import-c program, and saved as "ext3.bmpp".
+ * by the bitstring-import-c program, and saved as "ext3.bmpp".
*
* Then we can load "ext3.bmpp" here.
*)
let () =
(* Load a real ext3 superblock from the examples directory. *)
- let bits = Bitmatch.bitstring_of_file "examples/ext3_sb" in
+ let bits = Bitstring.bitstring_of_file "examples/ext3_sb" in
bitmatch bits with
| { :ext2_super_block } ->
/* This is an example import file, showing how to import the
* Linux task_struct structure from header files.
*
- * Use: bitmatch-import-c task_struct.c > task_struct.ml
+ * Use: bitstring-import-c task_struct.c > task_struct.ml
*
* Tip: Add the --debug flag to that command line to see what's going on.
- * Also use bitmatch-objinfo to examine the bmpp file.
+ * Also use bitstring-objinfo to examine the bmpp file.
*/
/* Any defines, etc. necessary to get the include to work. */
#include "linux/sched.h"
/* This tells the importer program what structures and constants to import. */
-typedef struct task_struct BITMATCH_IMPORT(task_struct);
+typedef struct task_struct BITSTRING_IMPORT(task_struct);
(* This is an example program using an imported C structure.
*
* The Linux process table entry structure is imported from
- * <linux/sched.h> via "task_struct.c" by the bitmatch-import-c
+ * <linux/sched.h> via "task_struct.c" by the bitstring-import-c
* program, and saved as "task_struct.bmpp".
*
* Then we can load "task_struct.bmpp" here.
(*
let () =
- let bits = Bitmatch.bitstring_of_file "examples/ext3_sb" in
+ let bits = Bitstring.bitstring_of_file "examples/ext3_sb" in
*)
-# Bitmatch syntax extension.
+# Bitstring syntax extension.
# Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
#
# This library is free software; you can redistribute it and/or
dnl Process this file with autoconf to produce a configure script.
-AC_INIT(ocaml-bitmatch,1.9.6)
+AC_INIT(ocaml-bitstring,1.9.7)
dnl Check for basic C environment.
AC_PROG_CC
dnl Produce output files.
AC_CONFIG_HEADERS([config.h])
-AC_CONFIG_FILES([Makefile META bitmatch_config.ml cil-tools/Makefile])
+AC_CONFIG_FILES([Makefile META bitstring_config.ml cil-tools/Makefile])
AC_OUTPUT
* $Id$
*)
-open Bitmatch_persistent
+open Bitstring_persistent
open Camlp4.PreCast
open Syntax
let () =
let filename = "/bin/ls" in
- let bits = Bitmatch.bitstring_of_file filename in
+ let bits = Bitstring.bitstring_of_file filename in
bitmatch bits with
| { 0x7f : 8; "ELF" : 24 : string; (* ELF magic number *)
open Printf
-(*let () = Bitmatch.debug := true*)
+(*let () = Bitstring.debug := true*)
-let bits = Bitmatch.bitstring_of_file "ext3_sb"
+let bits = Bitstring.bitstring_of_file "ext3_sb"
(* The structure is straight from /usr/include/linux/ext3_fs.h *)
if Array.length Sys.argv <= 1 then
failwith "usage: gif input.gif";
let filename = Sys.argv.(1) in
- let bits = Bitmatch.bitstring_of_file filename in
+ let bits = Bitstring.bitstring_of_file filename in
bitmatch bits with
| { ("GIF87a"|"GIF89a") : 6*8 : string; (* GIF magic. *)
open Printf
-let header = Bitmatch.bitstring_of_file "ipv4_header.dat"
+let header = Bitstring.bitstring_of_file "ipv4_header.dat"
let () =
bitmatch header with
printf " checksum: %d\n" checksum;
printf " source: %lx dest: %lx\n" source dest;
printf " header options + padding:\n";
- Bitmatch.hexdump_bitstring stdout options;
+ Bitstring.hexdump_bitstring stdout options;
printf " packet payload:\n";
- Bitmatch.hexdump_bitstring stdout payload
+ Bitstring.hexdump_bitstring stdout payload
| { version : 4 } ->
eprintf "cannot parse IP version %d\n" version
| { _ } as header ->
eprintf "data is smaller than one nibble:\n";
- Bitmatch.hexdump_bitstring stderr header
+ Bitstring.hexdump_bitstring stderr header
let rec main () =
if Array.length Sys.argv <= 1 then failwith "libpcap dumpfile";
- let bits = Bitmatch.bitstring_of_file Sys.argv.(1) in
+ let bits = Bitstring.bitstring_of_file Sys.argv.(1) in
let endian, file_header, bits = libpcap_header bits in
(* Read the packets and print them out. *)
(* Determine the endianness (at runtime) from the magic number. *)
and endian_of = function
- | 0xa1b2c3d4_l -> Bitmatch.BigEndian
- | 0xd4c3b2a1_l -> Bitmatch.LittleEndian
+ | 0xa1b2c3d4_l -> Bitstring.BigEndian
+ | 0xd4c3b2a1_l -> Bitstring.LittleEndian
| _ -> assert false
and libpcap_header bits =
printf "IPv6 ";
| { _ } ->
- printf "\n"; Bitmatch.hexdump_bitstring stdout packet
+ printf "\n"; Bitstring.hexdump_bitstring stdout packet
)
| { _ } ->
- printf "\n"; Bitmatch.hexdump_bitstring stdout pkt_data
+ printf "\n"; Bitstring.hexdump_bitstring stdout pkt_data
);
printf "\n"
let checksum = 0
let source = 0xc0a80202_l (* 192.168.2.2 *)
let dest = 0xc0a80201_l (* 192.168.2.1 *)
-let options = Bitmatch.empty_bitstring
+let options = Bitstring.empty_bitstring
let payload_length = (length - hdrlen*4) * 8
-let payload = Bitmatch.create_bitstring payload_length
+let payload = Bitstring.create_bitstring payload_length
let header =
BITSTRING {
*)
}
-let () = Bitmatch.bitstring_to_file header "ipv4_header_out.dat"
+let () = Bitstring.bitstring_to_file header "ipv4_header_out.dat"
printf " checksum: %d\n" checksum;
printf " source: %lx dest: %lx\n" source dest;
printf " header options + padding:\n";
- Bitmatch.hexdump_bitstring stdout options;
+ Bitstring.hexdump_bitstring stdout options;
printf " packet payload:\n";
- Bitmatch.hexdump_bitstring stdout payload
+ Bitstring.hexdump_bitstring stdout payload
(* IPv6 packet header *)
| { 6 : 4; tclass : 8; flow : 20;
printf " next header: %d\n" nexthdr;
printf " ttl: %d\n" ttl;
printf " source address:\n";
- Bitmatch.hexdump_bitstring stdout source;
+ Bitstring.hexdump_bitstring stdout source;
printf " destination address:\n";
- Bitmatch.hexdump_bitstring stdout dest;
+ Bitstring.hexdump_bitstring stdout dest;
printf "packet payload:\n";
- Bitmatch.hexdump_bitstring stdout payload
+ Bitstring.hexdump_bitstring stdout payload
| { version : 4 } ->
eprintf "unknown IP version %d\n" version;
| { _ } as pkt ->
eprintf "data is smaller than one nibble:\n";
- Bitmatch.hexdump_bitstring stderr pkt;
+ Bitstring.hexdump_bitstring stderr pkt;
exit 1
let () =
- let pkt = Bitmatch.bitstring_of_file "ping.ipv4" in
+ let pkt = Bitstring.bitstring_of_file "ping.ipv4" in
display pkt;
- let pkt = Bitmatch.bitstring_of_file "ping.ipv6" in
+ let pkt = Bitstring.bitstring_of_file "ping.ipv6" in
display pkt
-(* Bitmatch syntax extension.
+(* Bitstring syntax extension.
* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
*
* This library is free software; you can redistribute it and/or
open Syntax
open Ast
-open Bitmatch
-module P = Bitmatch_persistent
+open Bitstring
+module P = Bitstring_persistent
(* If this is true then we emit some debugging code which can
* be useful to tell what is happening during matches. You
- * also need to do 'Bitmatch.debug := true' in your main program.
+ * also need to do 'Bitstring.debug := true' in your main program.
*
* If this is false then no extra debugging code is emitted.
*)
let i = ref 1000 in
fun name ->
incr i; let i = !i in
- sprintf "__pabitmatch_%s_%d" name i
+ sprintf "__pabitstring_%s_%d" name i
(* Used to keep track of which qualifiers we've seen in parse_field. *)
type whatset_t = {
match expr with
| <:expr< $lid:id$ >> -> id
| _ ->
- failwith "pa_bitmatch: internal error: save_offset_to only supports simple identifiers at the moment. In future we should support full patterns." in
+ failwith "pa_bitstring: internal error: save_offset_to only supports simple identifiers at the moment. In future we should support full patterns." in
let field = P.set_save_offset_to_lident field id in
{ whatset with save_offset_to_set = true }, field
| "save_offset_to", None ->
field
(* Choose the right constructor function. *)
-let build_bitmatch_call _loc funcname length endian signed =
+let build_bitstring_call _loc funcname length endian signed =
match length, endian, signed with
(* XXX The meaning of signed/unsigned breaks down at
* 31, 32, 63 and 64 bits.
*)
- | (Some 1, _, _) -> <:expr< Bitmatch.$lid:funcname ^ "_bit"$ >>
+ | (Some 1, _, _) -> <:expr< Bitstring.$lid:funcname ^ "_bit"$ >>
| (Some (2|3|4|5|6|7|8), _, sign) ->
let call = Printf.sprintf "%s_char_%s"
funcname (if sign then "signed" else "unsigned") in
- <:expr< Bitmatch.$lid:call$ >>
+ <:expr< Bitstring.$lid:call$ >>
| (len, endian, signed) ->
let t = match len with
| Some i when i <= 31 -> "int"
| NativeEndian -> "ne" in
let call = Printf.sprintf "%s_%s_%s_%s"
funcname t endianness sign in
- <:expr< Bitmatch.$lid:call$ >>
+ <:expr< Bitstring.$lid:call$ >>
| P.EndianExpr expr ->
let call = Printf.sprintf "%s_%s_%s_%s"
funcname t "ee" sign in
- <:expr< Bitmatch.$lid:call$ $expr$ >>
+ <:expr< Bitstring.$lid:call$ $expr$ >>
(* Generate the code for a constructor, ie. 'BITSTRING ...'. *)
let output_constructor _loc fields =
- (* This function makes code to raise a Bitmatch.Construct_failure exception
+ (* This function makes code to raise a Bitstring.Construct_failure exception
* containing a message and the current _loc context.
* (Thanks to Bluestorm for suggesting this).
*)
let construct_failure _loc msg =
<:expr<
- Bitmatch.Construct_failure
+ Bitstring.Construct_failure
($`str:msg$,
$`str:Loc.file_name _loc$,
$`int:Loc.start_line _loc$,
let flen_is_const = expr_is_constant flen in
let int_construct_const (i, endian, signed) =
- build_bitmatch_call _loc "construct" (Some i) endian signed in
+ build_bitstring_call _loc "construct" (Some i) endian signed in
let int_construct (endian, signed) =
- build_bitmatch_call _loc "construct" None endian signed in
+ build_bitstring_call _loc "construct" None endian signed in
let expr =
match t, flen_is_const with
<:expr<
let $lid:bs$ = $fexpr$ in
if String.length $lid:bs$ = $`int:j$ then
- Bitmatch.construct_string $lid:buffer$ $lid:bs$
+ Bitstring.construct_string $lid:buffer$ $lid:bs$
else
$raise_construct_failure _loc "length of string does not match declaration"$
>>
* with no checks.
*)
| P.String, Some (-1) ->
- <:expr< Bitmatch.construct_string $lid:buffer$ $fexpr$ >>
+ <:expr< Bitstring.construct_string $lid:buffer$ $fexpr$ >>
(* String, constant length = 0 is probably an error, and so is
* any other value.
if $lid:bslen$ land 7 = 0 then (
let $lid:bs$ = $fexpr$ in
if String.length $lid:bs$ = ($lid:bslen$ lsr 3) then
- Bitmatch.construct_string $lid:buffer$ $lid:bs$
+ Bitstring.construct_string $lid:buffer$ $lid:bs$
else
$raise_construct_failure _loc "length of string does not match declaration"$
) else
let bs = gensym "bs" in
<:expr<
let $lid:bs$ = $fexpr$ in
- if Bitmatch.bitstring_length $lid:bs$ = $`int:i$ then
- Bitmatch.construct_bitstring $lid:buffer$ $lid:bs$
+ if Bitstring.bitstring_length $lid:bs$ = $`int:i$ then
+ Bitstring.construct_bitstring $lid:buffer$ $lid:bs$
else
$raise_construct_failure _loc "length of bitstring does not match declaration"$
>>
* with no checks.
*)
| P.Bitstring, Some (-1) ->
- <:expr< Bitmatch.construct_bitstring $lid:buffer$ $fexpr$ >>
+ <:expr< Bitstring.construct_bitstring $lid:buffer$ $fexpr$ >>
(* Bitstring, constant length < -1 is an error. *)
| P.Bitstring, Some _ ->
let $lid:bslen$ = $flen$ in
if $lid:bslen$ >= 0 then (
let $lid:bs$ = $fexpr$ in
- if Bitmatch.bitstring_length $lid:bs$ = $lid:bslen$ then
- Bitmatch.construct_bitstring $lid:buffer$ $lid:bs$
+ if Bitstring.bitstring_length $lid:bs$ = $lid:bslen$ then
+ Bitstring.construct_bitstring $lid:buffer$ $lid:bs$
else
$raise_construct_failure _loc "length of bitstring does not match declaration"$
) else
let expr =
<:expr<
- let $lid:buffer$ = Bitmatch.Buffer.create () in
+ let $lid:buffer$ = Bitstring.Buffer.create () in
$fields$;
- Bitmatch.Buffer.contents $lid:buffer$
+ Bitstring.Buffer.contents $lid:buffer$
>> in
if !exn_used then
(* Now build the code which matches a field. *)
let int_extract_const (i, endian, signed) =
- build_bitmatch_call _loc "extract" (Some i) endian signed in
+ build_bitstring_call _loc "extract" (Some i) endian signed in
let int_extract (endian, signed) =
- build_bitmatch_call _loc "extract" None endian signed in
+ build_bitstring_call _loc "extract" None endian signed in
let expr =
match t, flen_is_const with
<:expr<
if $lid:len$ >= $`int:i$ then (
let $lid:bs$, $lid:off$, $lid:len$ =
- Bitmatch.extract_bitstring $lid:data$ $lid:off$ $lid:len$
+ Bitstring.extract_bitstring $lid:data$ $lid:off$ $lid:len$
$`int:i$ in
- match Bitmatch.string_of_bitstring $lid:bs$ with
+ match Bitstring.string_of_bitstring $lid:bs$ with
| $fpatt$ when true -> $expr$
| _ -> ()
)
let bs = gensym "bs" in
<:expr<
let $lid:bs$, $lid:off$, $lid:len$ =
- Bitmatch.extract_remainder $lid:data$ $lid:off$ $lid:len$ in
- match Bitmatch.string_of_bitstring $lid:bs$ with
+ Bitstring.extract_remainder $lid:data$ $lid:off$ $lid:len$ in
+ match Bitstring.string_of_bitstring $lid:bs$ with
| $fpatt$ when true -> $expr$
| _ -> ()
>>
if $flen$ >= 0 && $flen$ <= $lid:len$
&& $flen$ land 7 = 0 then (
let $lid:bs$, $lid:off$, $lid:len$ =
- Bitmatch.extract_bitstring
+ Bitstring.extract_bitstring
$lid:data$ $lid:off$ $lid:len$ $flen$ in
- match Bitmatch.string_of_bitstring $lid:bs$ with
+ match Bitstring.string_of_bitstring $lid:bs$ with
| $fpatt$ when true -> $expr$
| _ -> ()
)
<:expr<
if $lid:len$ >= $`int:i$ then (
let $lid:ident$, $lid:off$, $lid:len$ =
- Bitmatch.extract_bitstring $lid:data$ $lid:off$ $lid:len$
+ Bitstring.extract_bitstring $lid:data$ $lid:off$ $lid:len$
$`int:i$ in
$expr$
)
fail "cannot compare a bitstring to a constant" in
<:expr<
let $lid:ident$, $lid:off$, $lid:len$ =
- Bitmatch.extract_remainder $lid:data$ $lid:off$ $lid:len$ in
+ Bitstring.extract_remainder $lid:data$ $lid:off$ $lid:len$ in
$expr$
>>
<:expr<
if $flen$ >= 0 && $flen$ <= $lid:len$ then (
let $lid:ident$, $lid:off$, $lid:len$ =
- Bitmatch.extract_bitstring $lid:data$ $lid:off$ $lid:len$
+ Bitstring.extract_bitstring $lid:data$ $lid:off$ $lid:len$
$flen$ in
$expr$
)
let field = P.string_of_pattern_field field in
<:expr<
- if !Bitmatch.debug then (
- Printf.eprintf "PA_BITMATCH: TEST:\n";
+ if !Bitstring.debug then (
+ Printf.eprintf "PA_BITSTRING: TEST:\n";
Printf.eprintf " %s\n" $str:field$;
Printf.eprintf " off %d len %d\n%!" $lid:off$ $lid:len$;
- (*Bitmatch.hexdump_bitstring stderr
+ (*Bitstring.hexdump_bitstring stderr
($lid:data$,$lid:off$,$lid:len$);*)
);
$expr$
locfail _loc (sprintf "named pattern not found: %s" name)
(* Add named patterns from a file. See the documentation on the
- * directory search path in bitmatch_persistent.mli
+ * directory search path in bitstring_persistent.mli
*)
let load_patterns_from_file _loc filename =
let chan =
try open_in filename
with _ ->
(* Try OCaml library directory. *)
- try open_in (Filename.concat Bitmatch_config.ocamllibdir filename)
+ try open_in (Filename.concat Bitstring_config.ocamllibdir filename)
with exn -> Loc.raise _loc exn
) else (
try open_in filename
* $Id$
*)
-let _ = Bitmatch.extract_bit
+let _ = Bitstring.extract_bit
*)
let () =
- let bits = Bitmatch.create_bitstring 16 in
- ignore (Bitmatch.string_of_bitstring bits)
+ let bits = Bitstring.create_bitstring 16 in
+ ignore (Bitstring.string_of_bitstring bits)
(* Create a random string of bits. *)
let expected = List.map (fun _ -> Random.bool ()) (range 0 (len-1)) in
- let bits = Bitmatch.Buffer.create () in
- List.iter (Bitmatch.Buffer.add_bit bits) expected;
- let bits = Bitmatch.Buffer.contents bits in
+ let bits = Bitstring.Buffer.create () in
+ List.iter (Bitstring.Buffer.add_bit bits) expected;
+ let bits = Bitstring.Buffer.contents bits in
(* Now read the bitstring in groups of 1, 2, 3 .. etc. bits.
* In each case check the result against what we generated ('expected').
Random.int64 (Int64.sub (Int64.shift_left 1L bitlen) 1L))
(range 0 (len-1)) in
- let bits = Bitmatch.Buffer.create () in
+ let bits = Bitstring.Buffer.create () in
List.iter (fun i ->
- Bitmatch.construct_int64_be_unsigned bits i bitlen
+ Bitstring.construct_int64_be_unsigned bits i bitlen
(Failure "constructing string"))
expected;
- let bits = Bitmatch.Buffer.contents bits in
+ let bits = Bitstring.Buffer.contents bits in
(* Now read the bitstring as integers.
* In each case check the result against what we generated ('expected').
let rec loop bits =
bitmatch bits with
| { i : bitlen; rest : -1 : bitstring }
- when Bitmatch.bitstring_length rest = 0 -> [i]
+ when Bitstring.bitstring_length rest = 0 -> [i]
| { i : bitlen; rest : -1 : bitstring } -> i :: loop rest
| { _ } ->
failwith (sprintf "loop failed with len = %d, bitlen = %d"
let nr_passes = 10000
let max_size = 8 (* max field size in bits *)
-(* let () = Bitmatch.debug := true *)
+(* let () = Bitstring.debug := true *)
(* Return a full 64 bits of randomness. *)
let rand64 () =
eprintf " 1: %3d - %016Lx - %016Lx\n" n1sz n1 r1;
eprintf " 2: %3d - %016Lx - %016Lx\n" n2sz n2 r2;
eprintf " 3: %3d - %016Lx - %016Lx\n" n3sz n3 r3;
- eprintf "bits (length = %d):\n" (Bitmatch.bitstring_length bits);
- Bitmatch.hexdump_bitstring stderr bits;
+ eprintf "bits (length = %d):\n" (Bitstring.bitstring_length bits);
+ Bitstring.hexdump_bitstring stderr bits;
eprintf "%!"
let () =
n3 : n3sz
})
with
- Bitmatch.Construct_failure (msg, _, _, _) ->
+ Bitstring.Construct_failure (msg, _, _, _) ->
eprintf "FAILED: Construct_failure %s\n%!" msg;
dump n0 n0sz n1 n1sz n2 n2sz n3 n3sz
- (Bitmatch.empty_bitstring) 0L 0L 0L 0L;
+ (Bitstring.empty_bitstring) 0L 0L 0L 0L;
exit 2 in
let r0, r1, r2, r3 =
bitmatch bits with
| { r0 : n0sz; r1 : n1sz; r2 : n2sz; r3 : n3sz; rest : -1 : bitstring } ->
- let rest_len = Bitmatch.bitstring_length rest in
+ let rest_len = Bitstring.bitstring_length rest in
if rest_len <> 0 then (
eprintf "FAILED: rest is not zero length (length = %d)\n%!"
rest_len;
-(* Test the Bitmatch.Buffer module and string_of_bitstring in
+(* Test the Bitstring.Buffer module and string_of_bitstring in
* nasty non-aligned corner cases.
* $Id$
*)
* <---------------- len bits --------------->
*)
let bits =
- let bits = Bitmatch.Buffer.create () in
- Bitmatch.Buffer.add_bits bits str1 offset;
- Bitmatch.Buffer.add_bits bits expected len;
- Bitmatch.Buffer.contents bits in
+ let bits = Bitstring.Buffer.create () in
+ Bitstring.Buffer.add_bits bits str1 offset;
+ Bitstring.Buffer.add_bits bits expected len;
+ Bitstring.Buffer.contents bits in
(* Create a sub bitstring corresponding to what we want to check. *)
let subbits =
let bits, bitoffset, bitlen = bits in
(bits, bitoffset+offset, bitlen-offset) in
- assert (Bitmatch.bitstring_length subbits = len);
+ assert (Bitstring.bitstring_length subbits = len);
(* Now try to read out the substring using string_of_bitstring. *)
- let actual = Bitmatch.string_of_bitstring subbits in
+ let actual = Bitstring.string_of_bitstring subbits in
if actual <> expected then (
eprintf "MISMATCH between actual and expected, offset=%d, len=%d\n"
offset len;
eprintf " %02x" (Char.code actual.[i])
done;
eprintf "\nBITS:\n";
- Bitmatch.hexdump_bitstring stderr bits;
+ Bitstring.hexdump_bitstring stderr bits;
eprintf "SUBBITS:\n";
- Bitmatch.hexdump_bitstring stderr subbits;
+ Bitstring.hexdump_bitstring stderr subbits;
exit 1
);
done
*)
open Printf
-open Bitmatch
+open Bitstring
let () =
let rec loop = function
*)
open Printf
-open Bitmatch
+open Bitstring
(* A byte+length Pascal string. *)
let bitmatch pascal_string =
*)
open Printf
-open Bitmatch
+open Bitstring
open bitmatch "tests/test.bmpp"
*)
open Printf
-open Bitmatch
+open Bitstring
let make_bits i n j m k = (
let pad1 = ones_bitstring (n-8) in
*)
open Printf
-open Bitmatch
+open Bitstring
let make_bits si n sj m sk = (
let pad1 = ones_bitstring (n-64) in
*)
open Printf
-open Bitmatch
+open Bitstring
let make_bits p i n j m k = (
let pad0 = ones_bitstring p in
*)
open Printf
-open Bitmatch
+open Bitstring
let make_bits p i n j m k = (
let pad0 = ones_bitstring p in
*)
open Printf
-open Bitmatch
+open Bitstring
let bits = (BITSTRING { 101 : 16; 202 : 16 })