1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
# Reference
## Types
```ocaml
type endian =
| BigEndian
| LittleEndian
| NativeEndian
```
```ocaml
val string_of_endian : endian -> string
```
Endianness.
```ocaml
type bitstring = string * int * int
```
`bitstring` is the basic type used to store bitstrings.
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 bits, not bytes.
Normally you don't need to use the bitstring type directly, since there are
functions and syntax extensions which hide the details.
See also `Bitstring.bitstring_of_string`, `Bitstring.bitstring_of_file`,
`Bitstring.hexdump_bitstring`, `Bitstring.bitstring_length`.
```ocaml
type t = bitstring
```
`t` is a synonym for the `Bitstring.bitstring type`. This allows you to use this
module with functors like Set and Map from the stdlib.
## Exceptions
```ocaml
exception Construct_failure of string * string * int * int
```
`Construct_failure (message, file, line, char)` may be raised by the `BITSTRING`
constructor. 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).
`message` is the error message.
`file`, `line` and `char` point to the original source location of the `BITSTRING`
constructor that failed.
## Bitstring comparison
```ocaml
val compare : bitstring -> bitstring -> int
```
`compare bs1 bs2` compares two bitstrings and returns zero if they are equal, a
negative number if bs1 < bs2, or a positive number if bs1 > bs2. This tests
"semantic equality" which is not affected by the offset or alignment of the
underlying representation (see `Bitstring.bitstring`).
The ordering is total and lexicographic.
```ocaml
val equals : bitstring -> bitstring -> bool
```
`equals` returns true if and only if the two bitstrings are semantically equal.
It is the same as calling compare and testing if the result is 0, but usually more efficient.
## Bitstring manipulation
```ocaml
val bitstring_length : bitstring -> int
```
`bitstring_length bitstring` returns the length of the bitstring in bits.
Note this just returns the third field in the `Bitstring.bitstring` tuple.
```ocaml
val subbitstring : bitstring -> int -> int -> bitstring
```
`subbitstring bits off len` returns a sub-bitstring of the bitstring, starting
at offset off bits and with length len bits. If the original bitstring is not
long enough to do this then the function raises `Invalid_argument "subbitstring"`.
Note that this function just changes the offset and length fields of the
`Bitstring.bitstring` tuple, so is very efficient.
```ocaml
val dropbits : int -> bitstring -> bitstring
```
Drop the first n bits of the bitstring and return a new bitstring which is
shorter by n bits. If the length of the original bitstring is less than n
bits, this raises `Invalid_argument "dropbits"`.
Note that this function just changes the offset and length fields of the
`Bitstring.bitstring` tuple, so is very efficient.
```ocaml
val takebits : int -> bitstring -> bitstring
```
Take the first n bits of the bitstring and return a new bitstring which is
exactly n bits long. If the length of the original bitstring is less than n
bits, this raises `Invalid_argument "takebits"`.
Note that this function just changes the offset and length fields of the
`Bitstring.bitstring` tuple, so is very efficient.
```ocaml
val concat : bitstring list -> bitstring
```
Concatenate a list of bitstrings together into a single bitstring.
## Constructing bitstrings
```ocaml
val empty_bitstring : bitstring
```
`empty_bitstring` is the empty, zero-length bitstring.
```ocaml
val create_bitstring : int -> bitstring
```
`create_bitstring n` creates an n bit bitstring containing all zeroes.
```ocaml
val make_bitstring : int -> char -> bitstring
```
make_bitstring n c creates an n bit bitstring containing the repeated 8 bit
pattern in c. For example, `make_bitstring 16 '\x5a' `will create the bitstring
`0x5a5a` or in binary `0101 1010 0101 1010`.
Note that the length is in bits, not bytes. The length does NOT need to be a multiple of 8.
```ocaml
val zeroes_bitstring : int -> bitstring
```
zeroes_bitstring creates an n bit bitstring of all 0's. Actually this is the
same as `Bitstring.create_bitstring`.
```ocaml
val ones_bitstring : int -> bitstring
```
`ones_bitstring` creates an n bit bitstring of all 1's.
```ocaml
val bitstring_of_string : string -> bitstring
```
`bitstring_of_string str `creates a bitstring of length String.length str * 8
(bits) containing the bits in str.
Note that the bitstring uses str as the underlying string (see the
representation of Bitstring.bitstring) so you should not change str after
calling this.
```ocaml
val bitstring_of_file : string -> bitstring
```
bitstring_of_file filename loads the named file into a bitstring.
```ocaml
val bitstring_of_chan : Pervasives.in_channel -> bitstring
```
`bitstring_of_chan chan `loads the contents of the input channel chan as a
bitstring. The length of the final bitstring is determined by the remaining
input in chan, but will always be a multiple of 8 bits.
See also Bitstring.bitstring_of_chan_max.
```ocaml
val bitstring_of_chan_max : Pervasives.in_channel -> int -> bitstring
```
`bitstring_of_chan_max chan max` works like `Bitstring.bitstring_of_chan` but will
only read up to max bytes from the channel (or fewer if the end of input occurs
before that).
```ocaml
val bitstring_of_file_descr : Unix.file_descr -> bitstring
```
`bitstring_of_file_descr fd` loads the contents of the file descriptor fd as a
bitstring. See also `Bitstring.bitstring_of_chan`,
`Bitstring.bitstring_of_file_descr_max`.
```ocaml
val bitstring_of_file_descr_max : Unix.file_descr -> int -> bitstring
```
`bitstring_of_file_descr_max fd max` works like `Bitstring.bitstring_of_file_descr`
but will only read up to max bytes from the channel (or fewer if the end of
input occurs before that).
## Converting bitstrings
```ocaml
val string_of_bitstring : bitstring -> string
```
`string_of_bitstring bitstring` converts a bitstring to a string (eg. to allow
comparison). This function is inefficient. In the best case when the bitstring
is nicely byte-aligned we do a `String.sub` operation. If the bitstring isn't
aligned then this involves a lot of bit twiddling and is particularly
inefficient.
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.
```ocaml
val bitstring_to_file : bitstring -> string -> unit
```
`bitstring_to_file bits` filename writes the bitstring bits to the file
filename. It overwrites the output file. Some restrictions apply, see
`Bitstring.bitstring_to_chan`.
```ocaml
val bitstring_to_chan : bitstring -> Pervasives.out_channel -> unit
```
`bitstring_to_file bits` filename writes the bitstring bits to the channel chan.
Channels are made up of bytes, bitstrings can be any bit length including
fractions of bytes. So this function only works if the length of the bitstring
is an exact multiple of 8 bits (otherwise it raises `Invalid_argument "bitstring_to_chan"`).
Furthermore the function is efficient only in the case where the bitstring is
stored fully aligned, otherwise it has to do inefficient bit twiddling like
`Bitstring.string_of_bitstring`.
In the common case where the bitstring was generated by the `BITSTRING` operator
and is an exact multiple of 8 bits wide, then this function will always work
efficiently.
## Printing bitstrings
```ocaml
val hexdump_bitstring : Pervasives.out_channel -> bitstring -> unit
```
`hexdump_bitstring chan` bitstring prints the bitstring to the output channel in
a format similar to the Unix command `hexdump -C`.
## Bitstring buffer
```ocaml
module Buffer: sig .. end
```
Buffers are mainly used by the `BITSTRING` constructor, but may also be useful
for end users.
## Get/set bits
These functions let you manipulate individual bits in the bitstring. However
they are not particularly efficient and you should generally use the `bitmatch`
and `BITSTRING` operators when building and parsing bitstrings.
These functions all raise `Invalid_argument "index out of bounds"` if the index
is out of range of the bitstring.
```ocaml
val set : bitstring -> int -> unit
```
set bits n sets the nth bit in the bitstring to 1.
```ocaml
val clear : bitstring -> int -> unit
```
clear bits n sets the nth bit in the bitstring to 0.
```ocaml
val is_set : bitstring -> int -> bool
```
is_set bits n is true if the nth bit is set to 1.
```ocaml
val is_clear : bitstring -> int -> bool
```
is_clear bits n is true if the nth bit is set to 0.
```ocaml
val put : bitstring -> int -> int -> unit
```
put bits n v sets the nth bit in the bitstring to 1 if v is not zero, or to 0
if v is zero.
```ocaml
val get : bitstring -> int -> int
```
get bits n returns the nth bit (returns non-zero or 0).
|