Fix handling of OCAML_PKG_* macros for new OCaml autoconf.
[ocaml-bitstring.git] / bitstring.mli
1 (** Bitstring library. *)
2 (* Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version,
8  * with the OCaml linking exception described in COPYING.LIB.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * $Id$
20  *)
21
22 (**
23    {{:#reference}Jump straight to the reference section for
24    documentation on types and functions}.
25
26    {2 Introduction}
27
28    Bitstring adds Erlang-style bitstrings and matching over bitstrings
29    as a syntax extension and library for OCaml.  You can use
30    this module to both parse and generate binary formats, for
31    example, communications protocols, disk formats and binary files.
32
33    {{:http://code.google.com/p/bitstring/}OCaml bitstring website}
34
35    This library used to be called "bitmatch".
36
37    {2 Examples}
38
39    A function which can parse IPv4 packets:
40
41 {[
42 let display pkt =
43   bitmatch pkt with
44   (* IPv4 packet header
45     0                   1                   2                   3   
46     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 
47    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48    |   4   |  IHL  |Type of Service|          Total Length         |
49    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50    |         Identification        |Flags|      Fragment Offset    |
51    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52    |  Time to Live |    Protocol   |         Header Checksum       |
53    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54    |                       Source Address                          |
55    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56    |                    Destination Address                        |
57    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58    |                    Options                    |    Padding    |
59    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60   *)
61   | { 4 : 4; hdrlen : 4; tos : 8;   length : 16;
62       identification : 16;          flags : 3; fragoffset : 13;
63       ttl : 8; protocol : 8;        checksum : 16;
64       source : 32;
65       dest : 32;
66       options : (hdrlen-5)*32 : bitstring;
67       payload : -1 : bitstring } ->
68
69     printf "IPv4:\n";
70     printf "  header length: %d * 32 bit words\n" hdrlen;
71     printf "  type of service: %d\n" tos;
72     printf "  packet length: %d bytes\n" length;
73     printf "  identification: %d\n" identification;
74     printf "  flags: %d\n" flags;
75     printf "  fragment offset: %d\n" fragoffset;
76     printf "  ttl: %d\n" ttl;
77     printf "  protocol: %d\n" protocol;
78     printf "  checksum: %d\n" checksum;
79     printf "  source: %lx  dest: %lx\n" source dest;
80     printf "  header options + padding:\n";
81     Bitstring.hexdump_bitstring stdout options;
82     printf "  packet payload:\n";
83     Bitstring.hexdump_bitstring stdout payload
84
85   | { version : 4 } ->
86     eprintf "unknown IP version %d\n" version;
87     exit 1
88
89   | { _ } as pkt ->
90     eprintf "data is smaller than one nibble:\n";
91     Bitstring.hexdump_bitstring stderr pkt;
92     exit 1
93 ]}
94
95    A program which can parse
96    {{:http://lxr.linux.no/linux/include/linux/ext3_fs.h}Linux EXT3 filesystem superblocks}:
97
98 {[
99 let bits = Bitstring.bitstring_of_file "tests/ext3_sb"
100
101 let () =
102   bitmatch bits with
103   | { s_inodes_count : 32 : littleendian;       (* Inodes count *)
104       s_blocks_count : 32 : littleendian;       (* Blocks count *)
105       s_r_blocks_count : 32 : littleendian;     (* Reserved blocks count *)
106       s_free_blocks_count : 32 : littleendian;  (* Free blocks count *)
107       s_free_inodes_count : 32 : littleendian;  (* Free inodes count *)
108       s_first_data_block : 32 : littleendian;   (* First Data Block *)
109       s_log_block_size : 32 : littleendian;     (* Block size *)
110       s_log_frag_size : 32 : littleendian;      (* Fragment size *)
111       s_blocks_per_group : 32 : littleendian;   (* # Blocks per group *)
112       s_frags_per_group : 32 : littleendian;    (* # Fragments per group *)
113       s_inodes_per_group : 32 : littleendian;   (* # Inodes per group *)
114       s_mtime : 32 : littleendian;              (* Mount time *)
115       s_wtime : 32 : littleendian;              (* Write time *)
116       s_mnt_count : 16 : littleendian;          (* Mount count *)
117       s_max_mnt_count : 16 : littleendian;      (* Maximal mount count *)
118       0xef53 : 16 : littleendian } ->           (* Magic signature *)
119
120     printf "ext3 superblock:\n";
121     printf "  s_inodes_count = %ld\n" s_inodes_count;
122     printf "  s_blocks_count = %ld\n" s_blocks_count;
123     printf "  s_free_inodes_count = %ld\n" s_free_inodes_count;
124     printf "  s_free_blocks_count = %ld\n" s_free_blocks_count
125
126   | { _ } ->
127     eprintf "not an ext3 superblock!\n%!";
128     exit 2
129 ]}
130
131    Constructing packets for a simple binary message
132    protocol:
133
134 {[
135 (*
136   +---------------+---------------+--------------------------+
137   | type          | subtype       | parameter                |
138   +---------------+---------------+--------------------------+
139    <-- 16 bits --> <-- 16 bits --> <------- 32 bits -------->
140
141   All fields are in network byte order.
142 *)
143
144 let make_message typ subtype param =
145   (BITSTRING {
146      typ : 16;
147      subtype : 16;
148      param : 32
149    }) ;;
150 ]}
151
152    {2 Loading, creating bitstrings}
153
154    The basic data type is the {!bitstring}, a string of bits of
155    arbitrary length.  Bitstrings can be any length in bits and
156    operations do not need to be byte-aligned (although they will
157    generally be more efficient if they are byte-aligned).
158
159    Internally a bitstring is stored as a normal OCaml [string]
160    together with an offset and length, where the offset and length are
161    measured in bits.  Thus one can efficiently form substrings of
162    bitstrings, overlay a bitstring on existing data, and load and save
163    bitstrings from files or other external sources.
164
165    To load a bitstring from a file use {!bitstring_of_file} or
166    {!bitstring_of_chan}.
167
168    There are also functions to create bitstrings from arbitrary data.
169    See the {{:#reference}reference} below.
170
171    {2 Matching bitstrings with patterns}
172
173    Use the [bitmatch] operator (part of the syntax extension) to break
174    apart a bitstring into its fields.  [bitmatch] works a lot like the
175    OCaml [match] operator.
176
177    The general form of [bitmatch] is:
178
179    [bitmatch] {i bitstring-expression} [with]
180
181    [| {] {i pattern} [} ->] {i code}
182
183    [| {] {i pattern} [} ->] {i code}
184
185    [|] ...
186
187    As with normal match, the statement attempts to match the
188    bitstring against each pattern in turn.  If none of the patterns
189    match then the standard library [Match_failure] exception is
190    thrown.
191
192    Patterns look a bit different from normal match patterns.  They
193    consist of a list of bitfields separated by [;] where each bitfield
194    contains a bind variable, the width (in bits) of the field, and
195    other information.  Some example patterns:
196
197 {[
198 bitmatch bits with
199
200 | { version : 8; name : 8; param : 8 } -> ...
201
202    (* Bitstring of at least 3 bytes.  First byte is the version
203       number, second byte is a field called name, third byte is
204       a field called parameter. *)
205
206 | { flag : 1 } ->
207    printf "flag is %b\n" flag
208
209    (* A single flag bit (mapped into an OCaml boolean). *)
210
211 | { len : 4; data : 1+len } ->
212    printf "len = %d, data = 0x%Lx\n" len data
213
214    (* A 4-bit length, followed by 1-16 bits of data, where the
215       length of the data is computed from len. *)
216
217 | { ipv6_source : 128 : bitstring;
218     ipv6_dest : 128 : bitstring } -> ...
219
220    (* IPv6 source and destination addresses.  Each is 128 bits
221       and is mapped into a bitstring type which will be a substring
222       of the main bitstring expression. *)
223 ]}
224
225    You can also add conditional when-clauses:
226
227 {[
228 | { version : 4 }
229     when version = 4 || version = 6 -> ...
230
231    (* Only match and run the code when version is 4 or 6.  If
232       it isn't we will drop through to the next case. *)
233 ]}
234
235    Note that the pattern is only compared against the first part of
236    the bitstring (there may be more data in the bitstring following
237    the pattern, which is not matched).  In terms of regular
238    expressions you might say that the pattern matches [^pattern], not
239    [^pattern$].  To ensure that the bitstring contains only the
240    pattern, add a length -1 bitstring to the end and test that its
241    length is zero in the when-clause:
242
243 {[
244 | { n : 4;
245     rest : -1 : bitstring }
246     when Bitstring.bitstring_length rest = 0 -> ...
247
248    (* Only matches exactly 4 bits. *)
249 ]}
250
251    Normally the first part of each field is a binding variable,
252    but you can also match a constant, as in:
253
254 {[
255 | { (4|6) : 4 } -> ...
256
257    (* Only matches if the first 4 bits contain either
258       the integer 4 or the integer 6. *)
259 ]}
260
261    One may also match on strings:
262
263 {[
264 | { "MAGIC" : 5*8 : string } -> ...
265
266    (* Only matches if the string "MAGIC" appears at the start
267       of the input. *)
268 ]}
269
270    {3:patternfieldreference Pattern field reference}
271
272    The exact format of each pattern field is:
273
274    [pattern : length [: qualifier [,qualifier ...]]]
275
276    [pattern] is the pattern, binding variable name, or constant to
277    match.  [length] is the length in bits which may be either a
278    constant or an expression.  The length expression is just an OCaml
279    expression and can use any values defined in the program, and refer
280    back to earlier fields (but not to later fields).
281
282    Integers can only have lengths in the range \[1..64\] bits.  See the
283    {{:#integertypes}integer types} section below for how these are
284    mapped to the OCaml int/int32/int64 types.  This is checked
285    at compile time if the length expression is constant, otherwise it is
286    checked at runtime and you will get a runtime exception eg. in
287    the case of a computed length expression.
288
289    A bitstring field of length -1 matches all the rest of the
290    bitstring (thus this is only useful as the last field in a
291    pattern).
292
293    A bitstring field of length 0 matches an empty bitstring
294    (occasionally useful when matching optional subfields).
295
296    Qualifiers are a list of identifiers/expressions which control the type,
297    signedness and endianness of the field.  Permissible qualifiers are:
298
299    - [int]: field has an integer type
300    - [string]: field is a string type
301    - [bitstring]: field is a bitstring type
302    - [signed]: field is signed
303    - [unsigned]: field is unsigned
304    - [bigendian]: field is big endian - a.k.a network byte order
305    - [littleendian]: field is little endian - a.k.a Intel byte order
306    - [nativeendian]: field is same endianness as the machine
307    - [endian (expr)]: [expr] should be an expression which evaluates to
308        a {!endian} type, ie. [LittleEndian], [BigEndian] or [NativeEndian].
309        The expression is an arbitrary OCaml expression and can use the
310        value of earlier fields in the bitmatch.
311    - [offset (expr)]: see {{:#computedoffsets}computed offsets} below.
312
313    The default settings are [int], [unsigned], [bigendian], no offset.
314
315    Note that many of these qualifiers cannot be used together,
316    eg. bitstrings do not have endianness.  The syntax extension should
317    give you a compile-time error if you use incompatible qualifiers.
318
319    {3 Other cases in bitmatch}
320
321    As well as a list of fields, it is possible to name the
322    bitstring and/or have a default match case:
323
324 {[
325 | { _ } -> ...
326
327    (* Default match case. *)
328
329 | { _ } as pkt -> ...
330
331    (* Default match case, with 'pkt' bound to the whole bitstring. *)
332 ]}
333
334    {2 Constructing bitstrings}
335
336    Bitstrings may be constructed using the [BITSTRING] operator (as an
337    expression).  The [BITSTRING] operator takes a list of fields,
338    similar to the list of fields for matching:
339
340 {[
341 let version = 1 ;;
342 let data = 10 ;;
343 let bits =
344   BITSTRING {
345     version : 4;
346     data : 12
347   } ;;
348
349    (* Constructs a 16-bit bitstring with the first four bits containing
350       the integer 1, and the following 12 bits containing the integer 10,
351       arranged in network byte order. *)
352
353 Bitstring.hexdump_bitstring stdout bits ;;
354
355    (* Prints:
356
357       00000000  10 0a         |..              |
358     *)
359 ]}
360
361    The format of each field is the same as for pattern fields (see
362    {{:#patternfieldreference}Pattern field reference section}), and
363    things like computed length fields, fixed value fields, insertion
364    of bitstrings within bitstrings, etc. are all supported.
365
366    {3 Construction exception}
367
368    The [BITSTRING] operator may throw a {!Construct_failure}
369    exception at runtime.
370
371    Runtime errors include:
372
373    - int field length not in the range \[1..64\]
374    - a bitstring with a length declared which doesn't have the
375      same length at runtime
376    - trying to insert an out of range value into an int field
377      (eg. an unsigned int field which is 2 bits wide can only
378      take values in the range \[0..3\]).
379
380    {2:integertypes Integer types}
381
382    Integer types are mapped to OCaml types [bool], [int], [int32] or
383    [int64] using a system which tries to ensure that (a) the types are
384    reasonably predictable and (b) the most efficient type is
385    preferred.
386
387    The rules are slightly different depending on whether the bit
388    length expression in the field is a compile-time constant or a
389    computed expression.
390
391    Detection of compile-time constants is quite simplistic so only
392    simple integer literals and simple expressions (eg. [5*8]) are
393    recognized as constants.
394
395    In any case the bit size of an integer is limited to the range
396    \[1..64\].  This is detected as a compile-time error if that is
397    possible, otherwise a runtime check is added which can throw an
398    [Invalid_argument] exception.
399
400    The mapping is thus:
401
402    {v
403    Bit size         ---- OCaml type ----
404                 Constant        Computed expression
405
406    1            bool            int64
407    2..31        int             int64
408    32           int32           int64
409    33..64       int64           int64
410    v}
411
412    A possible future extension may allow people with 64 bit computers
413    to specify a more optimal [int] type for bit sizes in the range
414    [32..63].  If this was implemented then such code {i could not even
415    be compiled} on 32 bit platforms, so it would limit portability.
416
417    Another future extension may be to allow computed
418    expressions to assert min/max range for the bit size,
419    allowing a more efficient data type than int64 to be
420    used.  (Of course under such circumstances there would
421    still need to be a runtime check to enforce the
422    size).
423
424    {2 Advanced pattern-matching features}
425
426    {3:computedoffsets Computed offsets}
427
428    You can add an [offset(..)] qualifier to bitmatch patterns in order
429    to move the current offset within the bitstring forwards.
430
431    For example:
432
433 {[
434 bitmatch bits with
435 | { field1 : 8;
436     field2 : 8 : offset(160) } -> ...
437 ]}
438
439    matches [field1] at the start of the bitstring and [field2]
440    at 160 bits into the bitstring.  The middle 152 bits go
441    unmatched (ie. can be anything).
442
443    The generated code is efficient.  If field lengths and offsets
444    are known to be constant at compile time, then almost all
445    runtime checks are avoided.  Non-constant field lengths and/or
446    non-constant offsets can result in more runtime checks being added.
447
448    Note that moving the offset backwards, and moving the offset in
449    [BITSTRING] constructors, are both not supported at present.
450
451    {3 Check expressions}
452
453    You can add a [check(expr)] qualifier to bitmatch patterns.
454    If the expression evaluates to false then the current match case
455    fails to match (in other words, we fall through to the next
456    match case - there is no error).
457
458    For example:
459 {[
460 bitmatch bits with
461 | { field : 16 : check (field > 100) } -> ...
462 ]}
463
464    Note the difference between a check expression and a when-clause
465    is that the when-clause is evaluated after all the fields have
466    been matched.  On the other hand a check expression is evaluated
467    after the individual field has been matched, which means it is
468    potentially more efficient (if the check expression fails then
469    we don't waste any time matching later fields).
470
471    We wanted to use the notation [when(expr)] here, but because
472    [when] is a reserved word we could not do this.
473
474    {3 Bind expressions}
475
476    A bind expression is used to change the value of a matched
477    field.  For example:
478 {[
479 bitmatch bits with
480 | { len : 16 : bind (len * 8);
481     field : len : bitstring } -> ...
482 ]}
483
484    In the example, after 'len' has been matched, its value would
485    be multiplied by 8, so the width of 'field' is the matched
486    value multiplied by 8.
487
488    In the general case:
489 {[
490 | { field : ... : bind (expr) } -> ...
491 ]}
492    evaluates the following after the field has been matched:
493 {[
494    let field = expr in
495    (* remaining fields *)
496 ]}
497
498    {3 Order of evaluation of check() and bind()}
499
500    The choice is arbitrary, but we have chosen that check expressions
501    are evaluated first, and bind expressions are evaluated after.
502
503    This means that the result of bind() is {i not} available in
504    the check expression.
505
506    Note that this rule applies regardless of the order of check()
507    and bind() in the source code.
508
509    {3 save_offset_to}
510
511    Use [save_offset_to(variable)] to save the current bit offset
512    within the match to a variable (strictly speaking, to a pattern).
513    This variable is then made available in any [check()] and [bind()]
514    clauses in the current field, {i and} to any later fields, and
515    to the code after the [->].
516
517    For example:
518 {[
519 bitmatch bits with
520 | { len : 16;
521     _ : len : bitstring;
522     field : 16 : save_offset_to (field_offset) } ->
523       printf "field is at bit offset %d in the match\n" field_offset
524 ]}
525
526    (In that example, [field_offset] should always have the value
527    [len+16]).
528
529    {2 Named patterns and persistent patterns}
530
531    Please see {!Bitstring_persistent} for documentation on this subject.
532
533    {2 Compiling}
534
535    Using the compiler directly you can do:
536
537    {v
538    ocamlc -I +bitstring \
539      -pp "camlp4of bitstring.cma bitstring_persistent.cma \
540             `ocamlc -where`/bitstring/pa_bitstring.cmo" \
541      unix.cma bitstring.cma test.ml -o test
542    v}
543
544    Simpler method using findlib:
545
546    {v
547    ocamlfind ocamlc \
548      -package bitstring,bitstring.syntax -syntax bitstring.syntax \
549      -linkpkg test.ml -o test
550    v}
551
552    {2 Security and type safety}
553
554    {3 Security on input}
555
556    The main concerns for input are buffer overflows and denial
557    of service.
558
559    It is believed that this library is robust against attempted buffer
560    overflows.  In addition to OCaml's normal bounds checks, we check
561    that field lengths are >= 0, and many additional checks.
562
563    Denial of service attacks are more problematic.  We only work
564    forwards through the bitstring, thus computation will eventually
565    terminate.  As for computed lengths, code such as this is thought
566    to be secure:
567
568    {[
569    bitmatch bits with
570    | { len : 64;
571        buffer : Int64.to_int len : bitstring } ->
572    ]}
573
574    The [len] field can be set arbitrarily large by an attacker, but
575    when pattern-matching against the [buffer] field this merely causes
576    a test such as [if len <= remaining_size] to fail.  Even if the
577    length is chosen so that [buffer] bitstring is allocated, the
578    allocation of sub-bitstrings is efficient and doesn't involve an
579    arbitary-sized allocation or any copying.
580
581    However the above does not necessarily apply to strings used in
582    matching, since they may cause the library to use the
583    {!Bitstring.string_of_bitstring} function, which allocates a string.
584    So you should take care if you use the [string] type particularly
585    with a computed length that is derived from external input.
586
587    The main protection against attackers should be to ensure that the
588    main program will only read input bitstrings up to a certain
589    length, which is outside the scope of this library.
590
591    {3 Security on output}
592
593    As with the input side, computed lengths are believed to be
594    safe.  For example:
595
596    {[
597    let len = read_untrusted_source () in
598    let buffer = allocate_bitstring () in
599    BITSTRING {
600      buffer : len : bitstring
601    }
602    ]}
603
604    This code merely causes a check that buffer's length is the same as
605    [len].  However the program function [allocate_bitstring] must
606    refuse to allocate an oversized buffer (but that is outside the
607    scope of this library).
608
609    {3 Order of evaluation}
610
611    In [bitmatch] statements, fields are evaluated left to right.
612
613    Note that the when-clause is evaluated {i last}, so if you are
614    relying on the when-clause to filter cases then your code may do a
615    lot of extra and unncessary pattern-matching work on fields which
616    may never be needed just to evaluate the when-clause.  Either
617    rearrange the code to do only the first part of the match,
618    followed by the when-clause, followed by a second inner bitmatch,
619    or use a [check()] qualifier within fields.
620
621    {3 Safety}
622
623    The current implementation is believed to be fully type-safe,
624    and makes compile and run-time checks where appropriate.  If
625    you find a case where a check is missing please submit a
626    bug report or a patch.
627
628    {2 Limits}
629
630    These are thought to be the current limits:
631
632    Integers: \[1..64\] bits.
633
634    Bitstrings (32 bit platforms): maximum length is limited
635    by the string size, ie. 16 MBytes.
636
637    Bitstrings (64 bit platforms): maximum length is thought to be
638    limited by the string size, ie. effectively unlimited.
639
640    Bitstrings must be loaded into memory before we can match against
641    them.  Thus available memory may be considered a limit for some
642    applications.
643
644    {2:reference Reference}
645    {3 Types}
646 *)
647
648 type endian = BigEndian | LittleEndian | NativeEndian
649
650 val string_of_endian : endian -> string
651 (** Endianness. *)
652
653 type bitstring = string * int * int
654 (** [bitstring] is the basic type used to store bitstrings.
655
656     The type contains the underlying data (a string),
657     the current bit offset within the string and the
658     current bit length of the string (counting from the
659     bit offset).  Note that the offset and length are
660     in {b bits}, not bytes.
661
662     Normally you don't need to use the bitstring type
663     directly, since there are functions and syntax
664     extensions which hide the details.
665
666     See also {!bitstring_of_string}, {!bitstring_of_file},
667     {!hexdump_bitstring}, {!bitstring_length}.
668 *)
669
670 type t = bitstring
671 (** [t] is a synonym for the {!bitstring} type.
672
673     This allows you to use this module with functors like
674     [Set] and [Map] from the stdlib. *)
675
676 (** {3 Exceptions} *)
677
678 exception Construct_failure of string * string * int * int
679 (** [Construct_failure (message, file, line, char)] may be
680     raised by the [BITSTRING] constructor.
681
682     Common reasons are that values are out of range of
683     the fields that contain them, or that computed lengths
684     are impossible (eg. negative length bitfields).
685
686     [message] is the error message.
687
688     [file], [line] and [char] point to the original source
689     location of the [BITSTRING] constructor that failed.
690 *)
691
692 (** {3 Bitstring comparison} *)
693
694 val compare : bitstring -> bitstring -> int
695 (** [compare bs1 bs2] compares two bitstrings and returns zero
696     if they are equal, a negative number if [bs1 < bs2], or a
697     positive number if [bs1 > bs2].
698
699     This tests "semantic equality" which is not affected by
700     the offset or alignment of the underlying representation
701     (see {!bitstring}).
702
703     The ordering is total and lexicographic. *)
704
705 val equals : bitstring -> bitstring -> bool
706 (** [equals] returns true if and only if the two bitstrings are
707     semantically equal.  It is the same as calling [compare] and
708     testing if the result is [0], but usually more efficient. *)
709
710 (** {3 Bitstring manipulation} *)
711
712 val bitstring_length : bitstring -> int
713 (** [bitstring_length bitstring] returns the length of
714     the bitstring in bits.
715
716     Note this just returns the third field in the {!bitstring} tuple. *)
717
718 val subbitstring : bitstring -> int -> int -> bitstring
719 (** [subbitstring bits off len] returns a sub-bitstring
720     of the bitstring, starting at offset [off] bits and
721     with length [len] bits.
722
723     If the original bitstring is not long enough to do this
724     then the function raises [Invalid_argument "subbitstring"].
725
726     Note that this function just changes the offset and length
727     fields of the {!bitstring} tuple, so is very efficient. *)
728
729 val dropbits : int -> bitstring -> bitstring
730 (** Drop the first n bits of the bitstring and return a new
731     bitstring which is shorter by n bits.
732
733     If the length of the original bitstring is less than n bits,
734     this raises [Invalid_argument "dropbits"].
735
736     Note that this function just changes the offset and length
737     fields of the {!bitstring} tuple, so is very efficient. *)
738
739 val takebits : int -> bitstring -> bitstring
740 (** Take the first n bits of the bitstring and return a new
741     bitstring which is exactly n bits long.
742
743     If the length of the original bitstring is less than n bits,
744     this raises [Invalid_argument "takebits"].
745
746     Note that this function just changes the offset and length
747     fields of the {!bitstring} tuple, so is very efficient. *)
748
749 val concat : bitstring list -> bitstring
750 (** Concatenate a list of bitstrings together into a single
751     bitstring. *)
752
753 (** {3 Constructing bitstrings} *)
754
755 val empty_bitstring : bitstring
756 (** [empty_bitstring] is the empty, zero-length bitstring. *)
757
758 val create_bitstring : int -> bitstring
759 (** [create_bitstring n] creates an [n] bit bitstring
760     containing all zeroes. *)
761
762 val make_bitstring : int -> char -> bitstring
763 (** [make_bitstring n c] creates an [n] bit bitstring
764     containing the repeated 8 bit pattern in [c].
765
766     For example, [make_bitstring 16 '\x5a'] will create
767     the bitstring [0x5a5a] or in binary [0101 1010 0101 1010].
768
769     Note that the length is in bits, not bytes.  The length does NOT
770     need to be a multiple of 8. *)
771
772 val zeroes_bitstring : int -> bitstring
773 (** [zeroes_bitstring] creates an [n] bit bitstring of all 0's.
774
775     Actually this is the same as {!create_bitstring}. *)
776
777 val ones_bitstring : int -> bitstring
778 (** [ones_bitstring] creates an [n] bit bitstring of all 1's. *)
779
780 val bitstring_of_string : string -> bitstring
781 (** [bitstring_of_string str] creates a bitstring
782     of length [String.length str * 8] (bits) containing the
783     bits in [str].
784
785     Note that the bitstring uses [str] as the underlying
786     string (see the representation of {!bitstring}) so you
787     should not change [str] after calling this. *)
788
789 val bitstring_of_file : string -> bitstring
790 (** [bitstring_of_file filename] loads the named file
791     into a bitstring. *)
792
793 val bitstring_of_chan : in_channel -> bitstring
794 (** [bitstring_of_chan chan] loads the contents of
795     the input channel [chan] as a bitstring.
796
797     The length of the final bitstring is determined
798     by the remaining input in [chan], but will always
799     be a multiple of 8 bits.
800
801     See also {!bitstring_of_chan_max}. *)
802
803 val bitstring_of_chan_max : in_channel -> int -> bitstring
804 (** [bitstring_of_chan_max chan max] works like
805     {!bitstring_of_chan} but will only read up to
806     [max] bytes from the channel (or fewer if the end of input
807     occurs before that). *)
808
809 val bitstring_of_file_descr : Unix.file_descr -> bitstring
810 (** [bitstring_of_file_descr fd] loads the contents of
811     the file descriptor [fd] as a bitstring.
812
813     See also {!bitstring_of_chan}, {!bitstring_of_file_descr_max}. *)
814
815 val bitstring_of_file_descr_max : Unix.file_descr -> int -> bitstring
816 (** [bitstring_of_file_descr_max fd max] works like
817     {!bitstring_of_file_descr} but will only read up to
818     [max] bytes from the channel (or fewer if the end of input
819     occurs before that). *)
820
821 (** {3 Converting bitstrings} *)
822
823 val string_of_bitstring : bitstring -> string
824 (** [string_of_bitstring bitstring] converts a bitstring to a string
825     (eg. to allow comparison).
826
827     This function is inefficient.  In the best case when the bitstring
828     is nicely byte-aligned we do a [String.sub] operation.  If the
829     bitstring isn't aligned then this involves a lot of bit twiddling
830     and is particularly inefficient.
831
832     If the bitstring is not a multiple of 8 bits wide then the
833     final byte of the string contains the high bits set to the
834     remaining bits and the low bits set to 0. *)
835
836 val bitstring_to_file : bitstring -> string -> unit
837 (** [bitstring_to_file bits filename] writes the bitstring [bits]
838     to the file [filename].  It overwrites the output file.
839
840     Some restrictions apply, see {!bitstring_to_chan}. *)
841
842 val bitstring_to_chan : bitstring -> out_channel -> unit
843 (** [bitstring_to_file bits filename] writes the bitstring [bits]
844     to the channel [chan].
845
846     Channels are made up of bytes, bitstrings can be any bit length
847     including fractions of bytes.  So this function only works
848     if the length of the bitstring is an exact multiple of 8 bits
849     (otherwise it raises [Invalid_argument "bitstring_to_chan"]).
850
851     Furthermore the function is efficient only in the case where
852     the bitstring is stored fully aligned, otherwise it has to
853     do inefficient bit twiddling like {!string_of_bitstring}.
854
855     In the common case where the bitstring was generated by the
856     [BITSTRING] operator and is an exact multiple of 8 bits wide,
857     then this function will always work efficiently.
858 *)
859
860 (** {3 Printing bitstrings} *)
861
862 val hexdump_bitstring : out_channel -> bitstring -> unit
863 (** [hexdump_bitstring chan bitstring] prints the bitstring
864     to the output channel in a format similar to the
865     Unix command [hexdump -C]. *)
866
867 (** {3 Bitstring buffer} *)
868
869 module Buffer : sig
870   type t
871   val create : unit -> t
872   val contents : t -> bitstring
873   val add_bits : t -> string -> int -> unit
874   val add_bit : t -> bool -> unit
875   val add_byte : t -> int -> unit
876 end
877 (** Buffers are mainly used by the [BITSTRING] constructor, but
878     may also be useful for end users.  They work much like the
879     standard library [Buffer] module. *)
880
881 (** {3 Get/set bits}
882
883     These functions let you manipulate individual bits in the
884     bitstring.  However they are not particularly efficient and you
885     should generally use the [bitmatch] and [BITSTRING] operators when
886     building and parsing bitstrings.
887
888     These functions all raise [Invalid_argument "index out of bounds"]
889     if the index is out of range of the bitstring.
890 *)
891
892 val set : bitstring -> int -> unit
893   (** [set bits n] sets the [n]th bit in the bitstring to 1. *)
894
895 val clear : bitstring -> int -> unit
896   (** [clear bits n] sets the [n]th bit in the bitstring to 0. *)
897
898 val is_set : bitstring -> int -> bool
899   (** [is_set bits n] is true if the [n]th bit is set to 1. *)
900
901 val is_clear : bitstring -> int -> bool
902   (** [is_clear bits n] is true if the [n]th bit is set to 0. *)
903
904 val put : bitstring -> int -> int -> unit
905   (** [put bits n v] sets the [n]th bit in the bitstring to 1
906       if [v] is not zero, or to 0 if [v] is zero. *)
907
908 val get : bitstring -> int -> int
909   (** [get bits n] returns the [n]th bit (returns non-zero or 0). *)
910
911 (** {3 Miscellaneous} *)
912
913 val package : string
914 (** The package name, always ["ocaml-bitstring"] *)
915
916 val version : string
917 (** The package version as a string. *)
918
919 val debug : bool ref
920 (** Set this variable to true to enable extended debugging.
921     This only works if debugging was also enabled in the
922     [pa_bitstring.ml] file at compile time, otherwise it
923     does nothing. *)
924
925 (**/**)
926
927 (* Private functions, called from generated code.  Do not use
928  * these directly - they are not safe.
929  *)
930
931 (* 'extract' functions are used in bitmatch statements. *)
932
933 val extract_bit : string -> int -> int -> int -> bool
934
935 val extract_char_unsigned : string -> int -> int -> int -> int
936
937 val extract_int_be_unsigned : string -> int -> int -> int -> int
938
939 val extract_int_le_unsigned : string -> int -> int -> int -> int
940
941 val extract_int_ne_unsigned : string -> int -> int -> int -> int
942
943 val extract_int_ee_unsigned : endian -> string -> int -> int -> int -> int
944
945 val extract_int32_be_unsigned : string -> int -> int -> int -> int32
946
947 val extract_int32_le_unsigned : string -> int -> int -> int -> int32
948
949 val extract_int32_ne_unsigned : string -> int -> int -> int -> int32
950
951 val extract_int32_ee_unsigned : endian -> string -> int -> int -> int -> int32
952
953 val extract_int64_be_unsigned : string -> int -> int -> int -> int64
954
955 val extract_int64_le_unsigned : string -> int -> int -> int -> int64
956
957 val extract_int64_ne_unsigned : string -> int -> int -> int -> int64
958
959 val extract_int64_ee_unsigned : endian -> string -> int -> int -> int -> int64
960
961 external extract_fastpath_int16_be_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_be_unsigned" "noalloc"
962
963 external extract_fastpath_int16_le_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_le_unsigned" "noalloc"
964
965 external extract_fastpath_int16_ne_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_ne_unsigned" "noalloc"
966
967 external extract_fastpath_int16_be_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_be_signed" "noalloc"
968
969 external extract_fastpath_int16_le_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_le_signed" "noalloc"
970
971 external extract_fastpath_int16_ne_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_ne_signed" "noalloc"
972
973 (*
974 external extract_fastpath_int24_be_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_be_unsigned" "noalloc"
975
976 external extract_fastpath_int24_le_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_le_unsigned" "noalloc"
977
978 external extract_fastpath_int24_ne_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_ne_unsigned" "noalloc"
979
980 external extract_fastpath_int24_be_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_be_signed" "noalloc"
981
982 external extract_fastpath_int24_le_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_le_signed" "noalloc"
983
984 external extract_fastpath_int24_ne_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_ne_signed" "noalloc"
985 *)
986
987 external extract_fastpath_int32_be_unsigned : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_be_unsigned" "noalloc"
988
989 external extract_fastpath_int32_le_unsigned : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_le_unsigned" "noalloc"
990
991 external extract_fastpath_int32_ne_unsigned : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_ne_unsigned" "noalloc"
992
993 external extract_fastpath_int32_be_signed : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_be_signed" "noalloc"
994
995 external extract_fastpath_int32_le_signed : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_le_signed" "noalloc"
996
997 external extract_fastpath_int32_ne_signed : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_ne_signed" "noalloc"
998
999 (*
1000 external extract_fastpath_int40_be_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_be_unsigned" "noalloc"
1001
1002 external extract_fastpath_int40_le_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_le_unsigned" "noalloc"
1003
1004 external extract_fastpath_int40_ne_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_ne_unsigned" "noalloc"
1005
1006 external extract_fastpath_int40_be_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_be_signed" "noalloc"
1007
1008 external extract_fastpath_int40_le_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_le_signed" "noalloc"
1009
1010 external extract_fastpath_int40_ne_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_ne_signed" "noalloc"
1011
1012 external extract_fastpath_int48_be_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_be_unsigned" "noalloc"
1013
1014 external extract_fastpath_int48_le_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_le_unsigned" "noalloc"
1015
1016 external extract_fastpath_int48_ne_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_ne_unsigned" "noalloc"
1017
1018 external extract_fastpath_int48_be_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_be_signed" "noalloc"
1019
1020 external extract_fastpath_int48_le_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_le_signed" "noalloc"
1021
1022 external extract_fastpath_int48_ne_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_ne_signed" "noalloc"
1023
1024 external extract_fastpath_int56_be_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_be_unsigned" "noalloc"
1025
1026 external extract_fastpath_int56_le_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_le_unsigned" "noalloc"
1027
1028 external extract_fastpath_int56_ne_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_ne_unsigned" "noalloc"
1029
1030 external extract_fastpath_int56_be_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_be_signed" "noalloc"
1031
1032 external extract_fastpath_int56_le_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_le_signed" "noalloc"
1033
1034 external extract_fastpath_int56_ne_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_ne_signed" "noalloc"
1035 *)
1036
1037 external extract_fastpath_int64_be_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_be_unsigned" "noalloc"
1038
1039 external extract_fastpath_int64_le_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_le_unsigned" "noalloc"
1040
1041 external extract_fastpath_int64_ne_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_ne_unsigned" "noalloc"
1042
1043 external extract_fastpath_int64_be_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_be_signed" "noalloc"
1044
1045 external extract_fastpath_int64_le_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_le_signed" "noalloc"
1046
1047 external extract_fastpath_int64_ne_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_ne_signed" "noalloc"
1048
1049 (* 'construct' functions are used in BITSTRING constructors. *)
1050 val construct_bit : Buffer.t -> bool -> int -> exn -> unit
1051
1052 val construct_char_unsigned : Buffer.t -> int -> int -> exn -> unit
1053
1054 val construct_int_be_unsigned : Buffer.t -> int -> int -> exn -> unit
1055
1056 val construct_int_le_unsigned : Buffer.t -> int -> int -> exn -> unit
1057
1058 val construct_int_ne_unsigned : Buffer.t -> int -> int -> exn -> unit
1059
1060 val construct_int_ee_unsigned : endian -> Buffer.t -> int -> int -> exn -> unit
1061
1062 val construct_int32_be_unsigned : Buffer.t -> int32 -> int -> exn -> unit
1063
1064 val construct_int32_le_unsigned : Buffer.t -> int32 -> int -> exn -> unit
1065
1066 val construct_int32_ne_unsigned : Buffer.t -> int32 -> int -> exn -> unit
1067
1068 val construct_int32_ee_unsigned : endian -> Buffer.t -> int32 -> int -> exn -> unit
1069
1070 val construct_int64_be_unsigned : Buffer.t -> int64 -> int -> exn -> unit
1071
1072 val construct_int64_le_unsigned : Buffer.t -> int64 -> int -> exn -> unit
1073
1074 val construct_int64_ne_unsigned : Buffer.t -> int64 -> int -> exn -> unit
1075
1076 val construct_int64_ee_unsigned : endian -> Buffer.t -> int64 -> int -> exn -> unit
1077
1078 val construct_string : Buffer.t -> string -> unit
1079
1080 val construct_bitstring : Buffer.t -> bitstring -> unit