Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > 3b99134b84e46237bdc23364f5fe8905 > files > 30

ocaml-bitstring-devel-2.0.4-11.mga7.armv7hl.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="Start" href="index.html">
<link rel="previous" href="Bitmatch.html">
<link rel="next" href="Bitstring_persistent.html">
<link rel="Up" href="index.html">
<link title="Index of types" rel=Appendix href="index_types.html">
<link title="Index of exceptions" rel=Appendix href="index_exceptions.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Bitmatch" rel="Chapter" href="Bitmatch.html">
<link title="Bitstring" rel="Chapter" href="Bitstring.html">
<link title="Bitstring_persistent" rel="Chapter" href="Bitstring_persistent.html"><link title="Introduction" rel="Section" href="#2_Introduction">
<link title="Examples" rel="Section" href="#2_Examples">
<link title="Loading, creating bitstrings" rel="Section" href="#2_Loadingcreatingbitstrings">
<link title="Matching bitstrings with patterns" rel="Section" href="#2_Matchingbitstringswithpatterns">
<link title="Constructing bitstrings" rel="Section" href="#2_Constructingbitstrings">
<link title="Integer types" rel="Section" href="#integertypes">
<link title="Advanced pattern-matching features" rel="Section" href="#2_Advancedpatternmatchingfeatures">
<link title="Named patterns and persistent patterns" rel="Section" href="#2_Namedpatternsandpersistentpatterns">
<link title="Compiling" rel="Section" href="#2_Compiling">
<link title="Security and type safety" rel="Section" href="#2_Securityandtypesafety">
<link title="Limits" rel="Section" href="#2_Limits">
<link title="Reference" rel="Section" href="#reference">
<link title="Pattern field reference" rel="Subsection" href="#patternfieldreference">
<link title="Other cases in bitmatch" rel="Subsection" href="#3_Othercasesinbitmatch">
<link title="Construction exception" rel="Subsection" href="#3_Constructionexception">
<link title="Computed offsets" rel="Subsection" href="#computedoffsets">
<link title="Check expressions" rel="Subsection" href="#3_Checkexpressions">
<link title="Bind expressions" rel="Subsection" href="#3_Bindexpressions">
<link title="Order of evaluation of check() and bind()" rel="Subsection" href="#3_Orderofevaluationofcheckandbind">
<link title="save_offset_to" rel="Subsection" href="#3_saveoffsetto">
<link title="Security on input" rel="Subsection" href="#3_Securityoninput">
<link title="Security on output" rel="Subsection" href="#3_Securityonoutput">
<link title="Order of evaluation" rel="Subsection" href="#3_Orderofevaluation">
<link title="Safety" rel="Subsection" href="#3_Safety">
<link title="Types" rel="Subsection" href="#3_Types">
<link title="Exceptions" rel="Subsection" href="#3_Exceptions">
<link title="Bitstring comparison" rel="Subsection" href="#3_Bitstringcomparison">
<link title="Bitstring manipulation" rel="Subsection" href="#3_Bitstringmanipulation">
<link title="Constructing bitstrings" rel="Subsection" href="#3_Constructingbitstrings">
<link title="Converting bitstrings" rel="Subsection" href="#3_Convertingbitstrings">
<link title="Printing bitstrings" rel="Subsection" href="#3_Printingbitstrings">
<link title="Bitstring buffer" rel="Subsection" href="#3_Bitstringbuffer">
<link title="Get/set bits" rel="Subsection" href="#3_Getsetbits">
<link title="Miscellaneous" rel="Subsection" href="#3_Miscellaneous">
<title>Bitstring</title>
</head>
<body>
<div class="navbar"><a class="pre" href="Bitmatch.html" title="Bitmatch">Previous</a>
&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;<a class="post" href="Bitstring_persistent.html" title="Bitstring_persistent">Next</a>
</div>
<h1>Module <a href="type_Bitstring.html">Bitstring</a></h1>

<pre><span id="MODULEBitstring"><span class="keyword">module</span> Bitstring</span>: <code class="code">sig</code> <a href="Bitstring.html">..</a> <code class="code">end</code></pre><div class="info module top">
<div class="info-desc">
<p>Bitstring library.</p>
</div>
</div>
<hr width="100%">
<p><a href="#reference">Jump straight to the reference section for
   documentation on types and functions</a>.</p>

<h3 id="2_Introduction">Introduction</h3>
<p>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.</p>

<p><a href="http://code.google.com/p/bitstring/">OCaml bitstring website</a></p>

<p>This library used to be called "bitmatch".</p>

<h3 id="2_Examples">Examples</h3>
<p>A function which can parse IPv4 packets:</p>

<pre class="codepre"><code class="code">let display pkt =
  bitmatch pkt with
  (* IPv4 packet header
    0                   1                   2                   3   
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   4   |  IHL  |Type of Service|          Total Length         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |         Identification        |Flags|      Fragment Offset    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Time to Live |    Protocol   |         Header Checksum       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                       Source Address                          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Destination Address                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  *)
  | { 4 : 4; hdrlen : 4; tos : 8;   length : 16;
      identification : 16;          flags : 3; fragoffset : 13;
      ttl : 8; protocol : 8;        checksum : 16;
      source : 32;
      dest : 32;
      options : (hdrlen-5)*32 : bitstring;
      payload : -1 : bitstring } -&gt;

    printf "IPv4:\n";
    printf "  header length: %d * 32 bit words\n" hdrlen;
    printf "  type of service: %d\n" tos;
    printf "  packet length: %d bytes\n" length;
    printf "  identification: %d\n" identification;
    printf "  flags: %d\n" flags;
    printf "  fragment offset: %d\n" fragoffset;
    printf "  ttl: %d\n" ttl;
    printf "  protocol: %d\n" protocol;
    printf "  checksum: %d\n" checksum;
    printf "  source: %lx  dest: %lx\n" source dest;
    printf "  header options + padding:\n";
    Bitstring.hexdump_bitstring stdout options;
    printf "  packet payload:\n";
    Bitstring.hexdump_bitstring stdout payload

  | { version : 4 } -&gt;
    eprintf "unknown IP version %d\n" version;
    exit 1

  | { _ } as pkt -&gt;
    eprintf "data is smaller than one nibble:\n";
    Bitstring.hexdump_bitstring stderr pkt;
    exit 1
</code></pre>
<p>A program which can parse
   <a href="http://lxr.linux.no/linux/include/linux/ext3_fs.h">Linux EXT3 filesystem superblocks</a>:</p>

<pre class="codepre"><code class="code">let bits = Bitstring.bitstring_of_file "tests/ext3_sb"

let () =
  bitmatch bits with
  | { s_inodes_count : 32 : littleendian;       (* Inodes count *)
      s_blocks_count : 32 : littleendian;       (* Blocks count *)
      s_r_blocks_count : 32 : littleendian;     (* Reserved blocks count *)
      s_free_blocks_count : 32 : littleendian;  (* Free blocks count *)
      s_free_inodes_count : 32 : littleendian;  (* Free inodes count *)
      s_first_data_block : 32 : littleendian;   (* First Data Block *)
      s_log_block_size : 32 : littleendian;     (* Block size *)
      s_log_frag_size : 32 : littleendian;      (* Fragment size *)
      s_blocks_per_group : 32 : littleendian;   (* # Blocks per group *)
      s_frags_per_group : 32 : littleendian;    (* # Fragments per group *)
      s_inodes_per_group : 32 : littleendian;   (* # Inodes per group *)
      s_mtime : 32 : littleendian;              (* Mount time *)
      s_wtime : 32 : littleendian;              (* Write time *)
      s_mnt_count : 16 : littleendian;          (* Mount count *)
      s_max_mnt_count : 16 : littleendian;      (* Maximal mount count *)
      0xef53 : 16 : littleendian } -&gt;           (* Magic signature *)

    printf "ext3 superblock:\n";
    printf "  s_inodes_count = %ld\n" s_inodes_count;
    printf "  s_blocks_count = %ld\n" s_blocks_count;
    printf "  s_free_inodes_count = %ld\n" s_free_inodes_count;
    printf "  s_free_blocks_count = %ld\n" s_free_blocks_count

  | { _ } -&gt;
    eprintf "not an ext3 superblock!\n%!";
    exit 2
</code></pre>
<p>Constructing packets for a simple binary message
   protocol:</p>

<pre class="codepre"><code class="code">(*
  +---------------+---------------+--------------------------+
  | type          | subtype       | parameter                |
  +---------------+---------------+--------------------------+
   &lt;-- 16 bits --&gt; &lt;-- 16 bits --&gt; &lt;------- 32 bits --------&gt;

  All fields are in network byte order.
*)

let make_message typ subtype param =
  (BITSTRING {
     typ : 16;
     subtype : 16;
     param : 32
   }) ;;
</code></pre>
<h3 id="2_Loadingcreatingbitstrings">Loading, creating bitstrings</h3>
<p>The basic data type is the <a href="Bitstring.html#TYPEbitstring"><code class="code">Bitstring.bitstring</code></a>, a string of bits of
   arbitrary length.  Bitstrings can be any length in bits and
   operations do not need to be byte-aligned (although they will
   generally be more efficient if they are byte-aligned).</p>

<p>Internally a bitstring is stored as a normal OCaml <code class="code">string</code>
   together with an offset and length, where the offset and length are
   measured in bits.  Thus one can efficiently form substrings of
   bitstrings, overlay a bitstring on existing data, and load and save
   bitstrings from files or other external sources.</p>

<p>To load a bitstring from a file use <a href="Bitstring.html#VALbitstring_of_file"><code class="code">Bitstring.bitstring_of_file</code></a> or
   <a href="Bitstring.html#VALbitstring_of_chan"><code class="code">Bitstring.bitstring_of_chan</code></a>.</p>

<p>There are also functions to create bitstrings from arbitrary data.
   See the <a href="#reference">reference</a> below.</p>

<h3 id="2_Matchingbitstringswithpatterns">Matching bitstrings with patterns</h3>
<p>Use the <code class="code">bitmatch</code> operator (part of the syntax extension) to break
   apart a bitstring into its fields.  <code class="code">bitmatch</code> works a lot like the
   OCaml <code class="code">match</code> operator.</p>

<p>The general form of <code class="code">bitmatch</code> is:</p>

<p><code class="code">bitmatch</code> <i>bitstring-expression</i> <code class="code">with</code></p>

<p><code class="code">| {</code> <i>pattern</i> <code class="code">} -&gt;</code> <i>code</i></p>

<p><code class="code">| {</code> <i>pattern</i> <code class="code">} -&gt;</code> <i>code</i></p>

<p><code class="code">|</code> ...</p>

<p>As with normal match, the statement attempts to match the
   bitstring against each pattern in turn.  If none of the patterns
   match then the standard library <code class="code">Match_failure</code> exception is
   thrown.</p>

<p>Patterns look a bit different from normal match patterns.  They
   consist of a list of bitfields separated by <code class="code">;</code> where each bitfield
   contains a bind variable, the width (in bits) of the field, and
   other information.  Some example patterns:</p>

<pre class="codepre"><code class="code">bitmatch bits with

| { version : 8; name : 8; param : 8 } -&gt; ...

   (* Bitstring of at least 3 bytes.  First byte is the version
      number, second byte is a field called name, third byte is
      a field called parameter. *)

| { flag : 1 } -&gt;
   printf "flag is %b\n" flag

   (* A single flag bit (mapped into an OCaml boolean). *)

| { len : 4; data : 1+len } -&gt;
   printf "len = %d, data = 0x%Lx\n" len data

   (* A 4-bit length, followed by 1-16 bits of data, where the
      length of the data is computed from len. *)

| { ipv6_source : 128 : bitstring;
    ipv6_dest : 128 : bitstring } -&gt; ...

   (* IPv6 source and destination addresses.  Each is 128 bits
      and is mapped into a bitstring type which will be a substring
      of the main bitstring expression. *)
</code></pre>
<p>You can also add conditional when-clauses:</p>

<pre class="codepre"><code class="code">| { version : 4 }
    when version = 4 || version = 6 -&gt; ...

   (* Only match and run the code when version is 4 or 6.  If
      it isn't we will drop through to the next case. *)
</code></pre>
<p>Note that the pattern is only compared against the first part of
   the bitstring (there may be more data in the bitstring following
   the pattern, which is not matched).  In terms of regular
   expressions you might say that the pattern matches <code class="code">^pattern</code>, not
   <code class="code">^pattern$</code>.  To ensure that the bitstring contains only the
   pattern, add a length -1 bitstring to the end and test that its
   length is zero in the when-clause:</p>

<pre class="codepre"><code class="code">| { n : 4;
    rest : -1 : bitstring }
    when Bitstring.bitstring_length rest = 0 -&gt; ...

   (* Only matches exactly 4 bits. *)
</code></pre>
<p>Normally the first part of each field is a binding variable,
   but you can also match a constant, as in:</p>

<pre class="codepre"><code class="code">| { (4|6) : 4 } -&gt; ...

   (* Only matches if the first 4 bits contain either
      the integer 4 or the integer 6. *)
</code></pre>
<p>One may also match on strings:</p>

<pre class="codepre"><code class="code">| { "MAGIC" : 5*8 : string } -&gt; ...

   (* Only matches if the string "MAGIC" appears at the start
      of the input. *)
</code></pre>
<h4 id="patternfieldreference">Pattern field reference</h4>
<p>The exact format of each pattern field is:</p>

<p><code class="code">pattern : length [: qualifier [,qualifier ...]]</code></p>

<p><code class="code">pattern</code> is the pattern, binding variable name, or constant to
   match.  <code class="code">length</code> is the length in bits which may be either a
   constant or an expression.  The length expression is just an OCaml
   expression and can use any values defined in the program, and refer
   back to earlier fields (but not to later fields).</p>

<p>Integers can only have lengths in the range [1..64] bits.  See the
   <a href="#integertypes">integer types</a> section below for how these are
   mapped to the OCaml int/int32/int64 types.  This is checked
   at compile time if the length expression is constant, otherwise it is
   checked at runtime and you will get a runtime exception eg. in
   the case of a computed length expression.</p>

<p>A bitstring field of length -1 matches all the rest of the
   bitstring (thus this is only useful as the last field in a
   pattern).</p>

<p>A bitstring field of length 0 matches an empty bitstring
   (occasionally useful when matching optional subfields).</p>

<p>Qualifiers are a list of identifiers/expressions which control the type,
   signedness and endianness of the field.  Permissible qualifiers are:</p>

<ul>
<li><code class="code">int</code>: field has an integer type</li>
<li><code class="code">string</code>: field is a string type</li>
<li><code class="code">bitstring</code>: field is a bitstring type</li>
<li><code class="code">signed</code>: field is signed</li>
<li><code class="code">unsigned</code>: field is unsigned</li>
<li><code class="code">bigendian</code>: field is big endian - a.k.a network byte order</li>
<li><code class="code">littleendian</code>: field is little endian - a.k.a Intel byte order</li>
<li><code class="code">nativeendian</code>: field is same endianness as the machine</li>
<li><code class="code">endian (expr)</code>: <code class="code">expr</code> should be an expression which evaluates to
       a <a href="Bitstring.html#TYPEendian"><code class="code">Bitstring.endian</code></a> type, ie. <code class="code">LittleEndian</code>, <code class="code">BigEndian</code> or <code class="code">NativeEndian</code>.
       The expression is an arbitrary OCaml expression and can use the
       value of earlier fields in the bitmatch.</li>
<li><code class="code">offset (expr)</code>: see <a href="#computedoffsets">computed offsets</a> below.</li>
</ul>
<p>The default settings are <code class="code">int</code>, <code class="code">unsigned</code>, <code class="code">bigendian</code>, no offset.</p>

<p>Note that many of these qualifiers cannot be used together,
   eg. bitstrings do not have endianness.  The syntax extension should
   give you a compile-time error if you use incompatible qualifiers.</p>

<h4 id="3_Othercasesinbitmatch">Other cases in bitmatch</h4>
<p>As well as a list of fields, it is possible to name the
   bitstring and/or have a default match case:</p>

<pre class="codepre"><code class="code">| { _ } -&gt; ...

   (* Default match case. *)

| { _ } as pkt -&gt; ...

   (* Default match case, with 'pkt' bound to the whole bitstring. *)
</code></pre>
<h3 id="2_Constructingbitstrings">Constructing bitstrings</h3>
<p>Bitstrings may be constructed using the <code class="code">BITSTRING</code> operator (as an
   expression).  The <code class="code">BITSTRING</code> operator takes a list of fields,
   similar to the list of fields for matching:</p>

<pre class="codepre"><code class="code">let version = 1 ;;
let data = 10 ;;
let bits =
  BITSTRING {
    version : 4;
    data : 12
  } ;;

   (* Constructs a 16-bit bitstring with the first four bits containing
      the integer 1, and the following 12 bits containing the integer 10,
      arranged in network byte order. *)

Bitstring.hexdump_bitstring stdout bits ;;

   (* Prints:

      00000000  10 0a         |..              |
    *)
</code></pre>
<p>The format of each field is the same as for pattern fields (see
   <a href="#patternfieldreference">Pattern field reference section</a>), and
   things like computed length fields, fixed value fields, insertion
   of bitstrings within bitstrings, etc. are all supported.</p>

<h4 id="3_Constructionexception">Construction exception</h4>
<p>The <code class="code">BITSTRING</code> operator may throw a <a href="Bitstring.html#EXCEPTIONConstruct_failure"><code class="code">Bitstring.Construct_failure</code></a>
   exception at runtime.</p>

<p>Runtime errors include:</p>

<ul>
<li>int field length not in the range [1..64]</li>
<li>a bitstring with a length declared which doesn't have the
     same length at runtime</li>
<li>trying to insert an out of range value into an int field
     (eg. an unsigned int field which is 2 bits wide can only
     take values in the range [0..3]).</li>
</ul>
<h3 id="integertypes">Integer types</h3>
<p>Integer types are mapped to OCaml types <code class="code">bool</code>, <code class="code">int</code>, <code class="code">int32</code> or
   <code class="code">int64</code> using a system which tries to ensure that (a) the types are
   reasonably predictable and (b) the most efficient type is
   preferred.</p>

<p>The rules are slightly different depending on whether the bit
   length expression in the field is a compile-time constant or a
   computed expression.</p>

<p>Detection of compile-time constants is quite simplistic so only
   simple integer literals and simple expressions (eg. <code class="code">5*8</code>) are
   recognized as constants.</p>

<p>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
   possible, otherwise a runtime check is added which can throw an
   <code class="code">Invalid_argument</code> exception.</p>

<p>The mapping is thus:</p>

<pre class="verbatim">   Bit size	    ---- OCaml type ----
                Constant	Computed expression

   1		bool		int64
   2..31	int		int64
   32		int32		int64
   33..64	int64		int64
  </pre>
<p>A possible future extension may allow people with 64 bit computers
   to specify a more optimal <code class="code">int</code> type for bit sizes in the range
   <code class="code">32..63</code>.  If this was implemented then such code <i>could not even
   be compiled</i> on 32 bit platforms, so it would limit portability.</p>

<p>Another future extension may be to allow computed
   expressions to assert min/max range for the bit size,
   allowing a more efficient data type than int64 to be
   used.  (Of course under such circumstances there would
   still need to be a runtime check to enforce the
   size).</p>

<h3 id="2_Advancedpatternmatchingfeatures">Advanced pattern-matching features</h3>
<h4 id="computedoffsets">Computed offsets</h4>
<p>You can add an <code class="code">offset(..)</code> qualifier to bitmatch patterns in order
   to move the current offset within the bitstring forwards.</p>

<p>For example:</p>

<pre class="codepre"><code class="code">bitmatch bits with
| { field1 : 8;
    field2 : 8 : offset(160) } -&gt; ...
</code></pre>
<p>matches <code class="code">field1</code> at the start of the bitstring and <code class="code">field2</code>
   at 160 bits into the bitstring.  The middle 152 bits go
   unmatched (ie. can be anything).</p>

<p>The generated code is efficient.  If field lengths and offsets
   are known to be constant at compile time, then almost all
   runtime checks are avoided.  Non-constant field lengths and/or
   non-constant offsets can result in more runtime checks being added.</p>

<p>Note that moving the offset backwards, and moving the offset in
   <code class="code">BITSTRING</code> constructors, are both not supported at present.</p>

<h4 id="3_Checkexpressions">Check expressions</h4>
<p>You can add a <code class="code">check(expr)</code> qualifier to bitmatch patterns.
   If the expression evaluates to false then the current match case
   fails to match (in other words, we fall through to the next
   match case - there is no error).</p>

<p>For example:</p>
<pre class="codepre"><code class="code">bitmatch bits with
| { field : 16 : check (field &gt; 100) } -&gt; ...
</code></pre>
<p>Note the difference between a check expression and a when-clause
   is that the when-clause is evaluated after all the fields have
   been matched.  On the other hand a check expression is evaluated
   after the individual field has been matched, which means it is
   potentially more efficient (if the check expression fails then
   we don't waste any time matching later fields).</p>

<p>We wanted to use the notation <code class="code">when(expr)</code> here, but because
   <code class="code">when</code> is a reserved word we could not do this.</p>

<h4 id="3_Bindexpressions">Bind expressions</h4>
<p>A bind expression is used to change the value of a matched
   field.  For example:</p>
<pre class="codepre"><code class="code">bitmatch bits with
| { len : 16 : bind (len * 8);
    field : len : bitstring } -&gt; ...
</code></pre>
<p>In the example, after 'len' has been matched, its value would
   be multiplied by 8, so the width of 'field' is the matched
   value multiplied by 8.</p>

<p>In the general case:</p>
<pre class="codepre"><code class="code">| { field : ... : bind (expr) } -&gt; ...
</code></pre><p>evaluates the following after the field has been matched:</p>
<pre class="codepre"><code class="code">   let field = expr in
   (* remaining fields *)
</code></pre>
<h4 id="3_Orderofevaluationofcheckandbind">Order of evaluation of check() and bind()</h4>
<p>The choice is arbitrary, but we have chosen that check expressions
   are evaluated first, and bind expressions are evaluated after.</p>

<p>This means that the result of bind() is <i>not</i> available in
   the check expression.</p>

<p>Note that this rule applies regardless of the order of check()
   and bind() in the source code.</p>

<h4 id="3_saveoffsetto">save_offset_to</h4>
<p>Use <code class="code">save_offset_to(variable)</code> to save the current bit offset
   within the match to a variable (strictly speaking, to a pattern).
   This variable is then made available in any <code class="code">check()</code> and <code class="code">bind()</code>
   clauses in the current field, <i>and</i> to any later fields, and
   to the code after the <code class="code">-&gt;</code>.</p>

<p>For example:</p>
<pre class="codepre"><code class="code">bitmatch bits with
| { len : 16;
    _ : len : bitstring;
    field : 16 : save_offset_to (field_offset) } -&gt;
      printf "field is at bit offset %d in the match\n" field_offset
</code></pre>
<p>(In that example, <code class="code">field_offset</code> should always have the value
   <code class="code">len+16</code>).</p>

<h3 id="2_Namedpatternsandpersistentpatterns">Named patterns and persistent patterns</h3>
<p>Please see <a href="Bitstring_persistent.html"><code class="code">Bitstring_persistent</code></a> for documentation on this subject.</p>

<h3 id="2_Compiling">Compiling</h3>
<p>Using the compiler directly you can do:</p>

<pre class="verbatim">   ocamlc -I +bitstring \
     -pp "camlp4of bitstring.cma bitstring_persistent.cma \
            `ocamlc -where`/bitstring/pa_bitstring.cmo" \
     unix.cma bitstring.cma test.ml -o test
  </pre>
<p>Simpler method using findlib:</p>

<pre class="verbatim">   ocamlfind ocamlc \
     -package bitstring,bitstring.syntax -syntax bitstring.syntax \
     -linkpkg test.ml -o test
  </pre>
<h3 id="2_Securityandtypesafety">Security and type safety</h3>
<h4 id="3_Securityoninput">Security on input</h4>
<p>The main concerns for input are buffer overflows and denial
   of service.</p>

<p>It is believed that this library is robust against attempted buffer
   overflows.  In addition to OCaml's normal bounds checks, we check
   that field lengths are &gt;= 0, and many additional checks.</p>

<p>Denial of service attacks are more problematic.  We only work
   forwards through the bitstring, thus computation will eventually
   terminate.  As for computed lengths, code such as this is thought
   to be secure:</p>

<pre class="codepre"><code class="code">   bitmatch bits with
   | { len : 64;
       buffer : Int64.to_int len : bitstring } -&gt;
   </code></pre>
<p>The <code class="code">len</code> field can be set arbitrarily large by an attacker, but
   when pattern-matching against the <code class="code">buffer</code> field this merely causes
   a test such as <code class="code">if len &lt;= remaining_size</code> to fail.  Even if the
   length is chosen so that <code class="code">buffer</code> bitstring is allocated, the
   allocation of sub-bitstrings is efficient and doesn't involve an
   arbitary-sized allocation or any copying.</p>

<p>However the above does not necessarily apply to strings used in
   matching, since they may cause the library to use the
   <a href="Bitstring.html#VALstring_of_bitstring"><code class="code">Bitstring.string_of_bitstring</code></a> function, which allocates a string.
   So you should take care if you use the <code class="code">string</code> type particularly
   with a computed length that is derived from external input.</p>

<p>The main protection against attackers should be to ensure that the
   main program will only read input bitstrings up to a certain
   length, which is outside the scope of this library.</p>

<h4 id="3_Securityonoutput">Security on output</h4>
<p>As with the input side, computed lengths are believed to be
   safe.  For example:</p>

<pre class="codepre"><code class="code">   let len = read_untrusted_source () in
   let buffer = allocate_bitstring () in
   BITSTRING {
     buffer : len : bitstring
   }
   </code></pre>
<p>This code merely causes a check that buffer's length is the same as
   <code class="code">len</code>.  However the program function <code class="code">allocate_bitstring</code> must
   refuse to allocate an oversized buffer (but that is outside the
   scope of this library).</p>

<h4 id="3_Orderofevaluation">Order of evaluation</h4>
<p>In <code class="code">bitmatch</code> statements, fields are evaluated left to right.</p>

<p>Note that the when-clause is evaluated <i>last</i>, 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.  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 <code class="code">check()</code> qualifier within fields.</p>

<h4 id="3_Safety">Safety</h4>
<p>The current implementation is believed to be fully type-safe,
   and makes compile and run-time checks where appropriate.  If
   you find a case where a check is missing please submit a
   bug report or a patch.</p>

<h3 id="2_Limits">Limits</h3>
<p>These are thought to be the current limits:</p>

<p>Integers: [1..64] bits.</p>

<p>Bitstrings (32 bit platforms): maximum length is limited
   by the string size, ie. 16 MBytes.</p>

<p>Bitstrings (64 bit platforms): maximum length is thought to be
   limited by the string size, ie. effectively unlimited.</p>

<p>Bitstrings must be loaded into memory before we can match against
   them.  Thus available memory may be considered a limit for some
   applications.</p>

<h3 id="reference">Reference</h3><h4 id="3_Types">Types</h4>
<pre><code><span id="TYPEendian"><span class="keyword">type</span> <code class="type"></code>endian</span> = </code></pre><table class="typetable">
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span id="TYPEELTendian.BigEndian"><span class="constructor">BigEndian</span></span></code></td>

</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span id="TYPEELTendian.LittleEndian"><span class="constructor">LittleEndian</span></span></code></td>

</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span id="TYPEELTendian.NativeEndian"><span class="constructor">NativeEndian</span></span></code></td>

</tr></table>



<pre><span id="VALstring_of_endian"><span class="keyword">val</span> string_of_endian</span> : <code class="type"><a href="Bitstring.html#TYPEendian">endian</a> -> string</code></pre><div class="info ">
<div class="info-desc">
<p>Endianness.</p>
</div>
</div>

<pre><span id="TYPEbitstring"><span class="keyword">type</span> <code class="type"></code>bitstring</span> = <code class="type">string * int * int</code> </pre>
<div class="info ">
<div class="info-desc">
<p><code class="code">bitstring</code> is the basic type used to store bitstrings.</p>

<p>The type contains the underlying data (a string),
    the current bit offset within the string and the
    current bit length of the string (counting from the
    bit offset).  Note that the offset and length are
    in <b>bits</b>, not bytes.</p>

<p>Normally you don't need to use the bitstring type
    directly, since there are functions and syntax
    extensions which hide the details.</p>

<p>See also <a href="Bitstring.html#VALbitstring_of_string"><code class="code">Bitstring.bitstring_of_string</code></a>, <a href="Bitstring.html#VALbitstring_of_file"><code class="code">Bitstring.bitstring_of_file</code></a>,
    <a href="Bitstring.html#VALhexdump_bitstring"><code class="code">Bitstring.hexdump_bitstring</code></a>, <a href="Bitstring.html#VALbitstring_length"><code class="code">Bitstring.bitstring_length</code></a>.</p>
</div>
</div>


<pre><span id="TYPEt"><span class="keyword">type</span> <code class="type"></code>t</span> = <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a></code> </pre>
<div class="info ">
<div class="info-desc">
<p><code class="code">t</code> is a synonym for the <a href="Bitstring.html#TYPEbitstring"><code class="code">Bitstring.bitstring</code></a> type.</p>

<p>This allows you to use this module with functors like
    <code class="code">Set</code> and <code class="code">Map</code> from the stdlib.</p>
</div>
</div>

<h4 id="3_Exceptions">Exceptions</h4>
<pre><span id="EXCEPTIONConstruct_failure"><span class="keyword">exception</span> Construct_failure</span> <span class="keyword">of</span> <code class="type">string * string * int * int</code></pre>
<div class="info ">
<div class="info-desc">
<p><code class="code">Construct_failure (message, file, line, char)</code> may be
    raised by the <code class="code">BITSTRING</code> constructor.</p>

<p>Common reasons are that values are out of range of
    the fields that contain them, or that computed lengths
    are impossible (eg. negative length bitfields).</p>

<p><code class="code">message</code> is the error message.</p>

<p><code class="code">file</code>, <code class="code">line</code> and <code class="code">char</code> point to the original source
    location of the <code class="code">BITSTRING</code> constructor that failed.</p>
</div>
</div>
<h4 id="3_Bitstringcomparison">Bitstring comparison</h4>
<pre><span id="VALcompare"><span class="keyword">val</span> compare</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> <a href="Bitstring.html#TYPEbitstring">bitstring</a> -> int</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">compare bs1 bs2</code> compares two bitstrings and returns zero
    if they are equal, a negative number if <code class="code">bs1 &lt; bs2</code>, or a
    positive number if <code class="code">bs1 &gt; bs2</code>.</p>

<p>This tests "semantic equality" which is not affected by
    the offset or alignment of the underlying representation
    (see <a href="Bitstring.html#TYPEbitstring"><code class="code">Bitstring.bitstring</code></a>).</p>

<p>The ordering is total and lexicographic.</p>
</div>
</div>

<pre><span id="VALequals"><span class="keyword">val</span> equals</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> <a href="Bitstring.html#TYPEbitstring">bitstring</a> -> bool</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">equals</code> returns true if and only if the two bitstrings are
    semantically equal.  It is the same as calling <code class="code">compare</code> and
    testing if the result is <code class="code">0</code>, but usually more efficient.</p>
</div>
</div>

<pre><span id="VALis_zeroes_bitstring"><span class="keyword">val</span> is_zeroes_bitstring</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> bool</code></pre><div class="info ">
<div class="info-desc">
<p>Tests if the bitstring is all zero bits (cf. <a href="Bitstring.html#VALzeroes_bitstring"><code class="code">Bitstring.zeroes_bitstring</code></a>)</p>
</div>
</div>

<pre><span id="VALis_ones_bitstring"><span class="keyword">val</span> is_ones_bitstring</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> bool</code></pre><div class="info ">
<div class="info-desc">
<p>Tests if the bitstring is all one bits (cf. <a href="Bitstring.html#VALones_bitstring"><code class="code">Bitstring.ones_bitstring</code></a>).</p>
</div>
</div>
<h4 id="3_Bitstringmanipulation">Bitstring manipulation</h4>
<pre><span id="VALbitstring_length"><span class="keyword">val</span> bitstring_length</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> int</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">bitstring_length bitstring</code> returns the length of
    the bitstring in bits.</p>

<p>Note this just returns the third field in the <a href="Bitstring.html#TYPEbitstring"><code class="code">Bitstring.bitstring</code></a> tuple.</p>
</div>
</div>

<pre><span id="VALsubbitstring"><span class="keyword">val</span> subbitstring</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> int -> int -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">subbitstring bits off len</code> returns a sub-bitstring
    of the bitstring, starting at offset <code class="code">off</code> bits and
    with length <code class="code">len</code> bits.</p>

<p>If the original bitstring is not long enough to do this
    then the function raises <code class="code">Invalid_argument "subbitstring"</code>.</p>

<p>Note that this function just changes the offset and length
    fields of the <a href="Bitstring.html#TYPEbitstring"><code class="code">Bitstring.bitstring</code></a> tuple, so is very efficient.</p>
</div>
</div>

<pre><span id="VALdropbits"><span class="keyword">val</span> dropbits</span> : <code class="type">int -> <a href="Bitstring.html#TYPEbitstring">bitstring</a> -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p>Drop the first n bits of the bitstring and return a new
    bitstring which is shorter by n bits.</p>

<p>If the length of the original bitstring is less than n bits,
    this raises <code class="code">Invalid_argument "dropbits"</code>.</p>

<p>Note that this function just changes the offset and length
    fields of the <a href="Bitstring.html#TYPEbitstring"><code class="code">Bitstring.bitstring</code></a> tuple, so is very efficient.</p>
</div>
</div>

<pre><span id="VALtakebits"><span class="keyword">val</span> takebits</span> : <code class="type">int -> <a href="Bitstring.html#TYPEbitstring">bitstring</a> -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p>Take the first n bits of the bitstring and return a new
    bitstring which is exactly n bits long.</p>

<p>If the length of the original bitstring is less than n bits,
    this raises <code class="code">Invalid_argument "takebits"</code>.</p>

<p>Note that this function just changes the offset and length
    fields of the <a href="Bitstring.html#TYPEbitstring"><code class="code">Bitstring.bitstring</code></a> tuple, so is very efficient.</p>
</div>
</div>

<pre><span id="VALconcat"><span class="keyword">val</span> concat</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> list -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p>Concatenate a list of bitstrings together into a single
    bitstring.</p>
</div>
</div>
<h4 id="3_Constructingbitstrings">Constructing bitstrings</h4>
<pre><span id="VALempty_bitstring"><span class="keyword">val</span> empty_bitstring</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">empty_bitstring</code> is the empty, zero-length bitstring.</p>
</div>
</div>

<pre><span id="VALcreate_bitstring"><span class="keyword">val</span> create_bitstring</span> : <code class="type">int -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">create_bitstring n</code> creates an <code class="code">n</code> bit bitstring
    containing all zeroes.</p>
</div>
</div>

<pre><span id="VALmake_bitstring"><span class="keyword">val</span> make_bitstring</span> : <code class="type">int -> char -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">make_bitstring n c</code> creates an <code class="code">n</code> bit bitstring
    containing the repeated 8 bit pattern in <code class="code">c</code>.</p>

<p>For example, <code class="code">make_bitstring 16 '\x5a'</code> will create
    the bitstring <code class="code">0x5a5a</code> or in binary <code class="code">0101 1010 0101 1010</code>.</p>

<p>Note that the length is in bits, not bytes.  The length does NOT
    need to be a multiple of 8.</p>
</div>
</div>

<pre><span id="VALzeroes_bitstring"><span class="keyword">val</span> zeroes_bitstring</span> : <code class="type">int -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">zeroes_bitstring</code> creates an <code class="code">n</code> bit bitstring of all 0's.</p>

<p>Actually this is the same as <a href="Bitstring.html#VALcreate_bitstring"><code class="code">Bitstring.create_bitstring</code></a>.</p>
</div>
</div>

<pre><span id="VALones_bitstring"><span class="keyword">val</span> ones_bitstring</span> : <code class="type">int -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">ones_bitstring</code> creates an <code class="code">n</code> bit bitstring of all 1's.</p>
</div>
</div>

<pre><span id="VALbitstring_of_string"><span class="keyword">val</span> bitstring_of_string</span> : <code class="type">string -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">bitstring_of_string str</code> creates a bitstring
    of length <code class="code">String.length str * 8</code> (bits) containing the
    bits in <code class="code">str</code>.</p>

<p>Note that the bitstring uses <code class="code">str</code> as the underlying
    string (see the representation of <a href="Bitstring.html#TYPEbitstring"><code class="code">Bitstring.bitstring</code></a>) so you
    should not change <code class="code">str</code> after calling this.</p>
</div>
</div>

<pre><span id="VALbitstring_of_file"><span class="keyword">val</span> bitstring_of_file</span> : <code class="type">string -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">bitstring_of_file filename</code> loads the named file
    into a bitstring.</p>
</div>
</div>

<pre><span id="VALbitstring_of_chan"><span class="keyword">val</span> bitstring_of_chan</span> : <code class="type">Stdlib.in_channel -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">bitstring_of_chan chan</code> loads the contents of
    the input channel <code class="code">chan</code> as a bitstring.</p>

<p>The length of the final bitstring is determined
    by the remaining input in <code class="code">chan</code>, but will always
    be a multiple of 8 bits.</p>

<p>See also <a href="Bitstring.html#VALbitstring_of_chan_max"><code class="code">Bitstring.bitstring_of_chan_max</code></a>.</p>
</div>
</div>

<pre><span id="VALbitstring_of_chan_max"><span class="keyword">val</span> bitstring_of_chan_max</span> : <code class="type">Stdlib.in_channel -> int -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">bitstring_of_chan_max chan max</code> works like
    <a href="Bitstring.html#VALbitstring_of_chan"><code class="code">Bitstring.bitstring_of_chan</code></a> but will only read up to
    <code class="code">max</code> bytes from the channel (or fewer if the end of input
    occurs before that).</p>
</div>
</div>

<pre><span id="VALbitstring_of_file_descr"><span class="keyword">val</span> bitstring_of_file_descr</span> : <code class="type">Unix.file_descr -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">bitstring_of_file_descr fd</code> loads the contents of
    the file descriptor <code class="code">fd</code> as a bitstring.</p>

<p>See also <a href="Bitstring.html#VALbitstring_of_chan"><code class="code">Bitstring.bitstring_of_chan</code></a>, <a href="Bitstring.html#VALbitstring_of_file_descr_max"><code class="code">Bitstring.bitstring_of_file_descr_max</code></a>.</p>
</div>
</div>

<pre><span id="VALbitstring_of_file_descr_max"><span class="keyword">val</span> bitstring_of_file_descr_max</span> : <code class="type">Unix.file_descr -> int -> <a href="Bitstring.html#TYPEbitstring">bitstring</a></code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">bitstring_of_file_descr_max fd max</code> works like
    <a href="Bitstring.html#VALbitstring_of_file_descr"><code class="code">Bitstring.bitstring_of_file_descr</code></a> but will only read up to
    <code class="code">max</code> bytes from the channel (or fewer if the end of input
    occurs before that).</p>
</div>
</div>
<h4 id="3_Convertingbitstrings">Converting bitstrings</h4>
<pre><span id="VALstring_of_bitstring"><span class="keyword">val</span> string_of_bitstring</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> string</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">string_of_bitstring bitstring</code> converts a bitstring to a string
    (eg. to allow comparison).</p>

<p>This function is inefficient.  In the best case when the bitstring
    is nicely byte-aligned we do a <code class="code">String.sub</code> operation.  If the
    bitstring isn't aligned then this involves a lot of bit twiddling
    and is particularly inefficient.</p>

<p>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.</p>
</div>
</div>

<pre><span id="VALbitstring_to_file"><span class="keyword">val</span> bitstring_to_file</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> string -> unit</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">bitstring_to_file bits filename</code> writes the bitstring <code class="code">bits</code>
    to the file <code class="code">filename</code>.  It overwrites the output file.</p>

<p>Some restrictions apply, see <a href="Bitstring.html#VALbitstring_to_chan"><code class="code">Bitstring.bitstring_to_chan</code></a>.</p>
</div>
</div>

<pre><span id="VALbitstring_to_chan"><span class="keyword">val</span> bitstring_to_chan</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> Stdlib.out_channel -> unit</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">bitstring_to_file bits filename</code> writes the bitstring <code class="code">bits</code>
    to the channel <code class="code">chan</code>.</p>

<p>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 <code class="code">Invalid_argument "bitstring_to_chan"</code>).</p>

<p>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 <a href="Bitstring.html#VALstring_of_bitstring"><code class="code">Bitstring.string_of_bitstring</code></a>.</p>

<p>In the common case where the bitstring was generated by the
    <code class="code">BITSTRING</code> operator and is an exact multiple of 8 bits wide,
    then this function will always work efficiently.</p>
</div>
</div>
<h4 id="3_Printingbitstrings">Printing bitstrings</h4>
<pre><span id="VALhexdump_bitstring"><span class="keyword">val</span> hexdump_bitstring</span> : <code class="type">Stdlib.out_channel -> <a href="Bitstring.html#TYPEbitstring">bitstring</a> -> unit</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">hexdump_bitstring chan bitstring</code> prints the bitstring
    to the output channel in a format similar to the
    Unix command <code class="code">hexdump -C</code>.</p>
</div>
</div>
<h4 id="3_Bitstringbuffer">Bitstring buffer</h4>
<pre><span id="MODULEBuffer"><span class="keyword">module</span> <a href="Bitstring.Buffer.html">Buffer</a></span>: <code class="code">sig</code> <a href="Bitstring.Buffer.html">..</a> <code class="code">end</code></pre><div class="info">
<p>Buffers are mainly used by the <code class="code">BITSTRING</code> constructor, but
    may also be useful for end users.</p>

</div>
<h4 id="3_Getsetbits">Get/set bits</h4>
<p>These functions let you manipulate individual bits in the
    bitstring.  However they are not particularly efficient and you
    should generally use the <code class="code">bitmatch</code> and <code class="code">BITSTRING</code> operators when
    building and parsing bitstrings.</p>

<p>These functions all raise <code class="code">Invalid_argument "index out of bounds"</code>
    if the index is out of range of the bitstring.</p>

<pre><span id="VALset"><span class="keyword">val</span> set</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> int -> unit</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">set bits n</code> sets the <code class="code">n</code>th bit in the bitstring to 1.</p>
</div>
</div>

<pre><span id="VALclear"><span class="keyword">val</span> clear</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> int -> unit</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">clear bits n</code> sets the <code class="code">n</code>th bit in the bitstring to 0.</p>
</div>
</div>

<pre><span id="VALis_set"><span class="keyword">val</span> is_set</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> int -> bool</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">is_set bits n</code> is true if the <code class="code">n</code>th bit is set to 1.</p>
</div>
</div>

<pre><span id="VALis_clear"><span class="keyword">val</span> is_clear</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> int -> bool</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">is_clear bits n</code> is true if the <code class="code">n</code>th bit is set to 0.</p>
</div>
</div>

<pre><span id="VALput"><span class="keyword">val</span> put</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> int -> int -> unit</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">put bits n v</code> sets the <code class="code">n</code>th bit in the bitstring to 1
      if <code class="code">v</code> is not zero, or to 0 if <code class="code">v</code> is zero.</p>
</div>
</div>

<pre><span id="VALget"><span class="keyword">val</span> get</span> : <code class="type"><a href="Bitstring.html#TYPEbitstring">bitstring</a> -> int -> int</code></pre><div class="info ">
<div class="info-desc">
<p><code class="code">get bits n</code> returns the <code class="code">n</code>th bit (returns non-zero or 0).</p>
</div>
</div>
<h4 id="3_Miscellaneous">Miscellaneous</h4>
<pre><span id="VALpackage"><span class="keyword">val</span> package</span> : <code class="type">string</code></pre><div class="info ">
<div class="info-desc">
<p>The package name, always <code class="code">"ocaml-bitstring"</code></p>
</div>
</div>

<pre><span id="VALversion"><span class="keyword">val</span> version</span> : <code class="type">string</code></pre><div class="info ">
<div class="info-desc">
<p>The package version as a string.</p>
</div>
</div>

<pre><span id="VALdebug"><span class="keyword">val</span> debug</span> : <code class="type">bool Stdlib.ref</code></pre><div class="info ">
<div class="info-desc">
<p>Set this variable to true to enable extended debugging.
    This only works if debugging was also enabled in the
    <code class="code">pa_bitstring.ml</code> file at compile time, otherwise it
    does nothing.</p>
</div>
</div>
</body></html>