2 * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
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.
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.
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
24 include Bitstring_types
25 include Bitstring_config
27 (* Enable runtime debug messages. Must also have been enabled
33 exception Construct_failure of string * string * int * int
35 (* A bitstring is simply the data itself (as a string), and the
36 * bitoffset and the bitlength within the string. Note offset/length
37 * are counted in bits, not bytes.
39 type bitstring = string * int * int
41 (* Functions to create and load bitstrings. *)
42 let empty_bitstring = "", 0, 0
44 let make_bitstring len c =
45 if len >= 0 then String.make ((len+7) lsr 3) c, 0, len
48 sprintf "make_bitstring/create_bitstring: len %d < 0" len
51 let create_bitstring len = make_bitstring len '\000'
53 let zeroes_bitstring = create_bitstring
55 let ones_bitstring len = make_bitstring len '\xff'
57 let bitstring_of_string str = str, 0, String.length str lsl 3
59 let bitstring_of_chan chan =
60 let tmpsize = 16384 in
61 let buf = Buffer.create tmpsize in
62 let tmp = String.create tmpsize in
64 while n := input chan tmp 0 tmpsize; !n > 0 do
65 Buffer.add_substring buf tmp 0 !n;
67 Buffer.contents buf, 0, Buffer.length buf lsl 3
69 let bitstring_of_chan_max chan max =
70 let tmpsize = 16384 in
71 let buf = Buffer.create tmpsize in
72 let tmp = String.create tmpsize in
76 let r = min tmpsize (max - !len) in
77 let n = input chan tmp 0 r in
79 Buffer.add_substring buf tmp 0 n;
86 Buffer.contents buf, 0, !len lsl 3
88 let bitstring_of_file_descr fd =
89 let tmpsize = 16384 in
90 let buf = Buffer.create tmpsize in
91 let tmp = String.create tmpsize in
93 while n := Unix.read fd tmp 0 tmpsize; !n > 0 do
94 Buffer.add_substring buf tmp 0 !n;
96 Buffer.contents buf, 0, Buffer.length buf lsl 3
98 let bitstring_of_file_descr_max fd max =
99 let tmpsize = 16384 in
100 let buf = Buffer.create tmpsize in
101 let tmp = String.create tmpsize in
105 let r = min tmpsize (max - !len) in
106 let n = Unix.read fd tmp 0 r in
108 Buffer.add_substring buf tmp 0 n;
115 Buffer.contents buf, 0, !len lsl 3
117 let bitstring_of_file fname =
118 let chan = open_in_bin fname in
120 let bs = bitstring_of_chan chan in
127 let bitstring_length (_, _, len) = len
129 let subbitstring (data, off, len) off' len' =
130 let off = off + off' in
131 if len < off' + len' then invalid_arg "subbitstring";
134 let dropbits n (data, off, len) =
137 if len < 0 then invalid_arg "dropbits";
140 let takebits n (data, off, len) =
141 if len < n then invalid_arg "takebits";
144 (*----------------------------------------------------------------------*)
145 (* Bitwise functions.
147 * We try to isolate all bitwise functions within these modules.
151 (* Bitwise operations on ints. Note that we assume int <= 31 bits. *)
152 external (<<<) : int -> int -> int = "%lslint"
153 external (>>>) : int -> int -> int = "%lsrint"
154 external to_int : int -> int = "%identity"
160 (* Create a mask 0-31 bits wide. *)
164 else if bits = 30 then
166 else if bits = 31 then
169 invalid_arg "Bitstring.I.mask"
171 (* Byte swap an int of a given size. *)
172 let byteswap v bits =
174 else if bits <= 16 then (
175 let shift = bits-8 in
176 let v1 = v >>> shift in
177 let v2 = ((v land (mask shift)) <<< 8) in
179 ) else if bits <= 24 then (
180 let shift = bits - 16 in
181 let v1 = v >>> (8+shift) in
182 let v2 = ((v >>> shift) land ff) <<< 8 in
183 let v3 = (v land (mask shift)) <<< 16 in
186 let shift = bits - 24 in
187 let v1 = v >>> (16+shift) in
188 let v2 = ((v >>> (8+shift)) land ff) <<< 8 in
189 let v3 = ((v >>> shift) land ff) <<< 16 in
190 let v4 = (v land (mask shift)) <<< 24 in
191 v4 lor v3 lor v2 lor v1
194 (* Check a value is in range 0 .. 2^bits-1. *)
195 let range_unsigned v bits =
196 let mask = lnot (mask bits) in
199 (* Call function g on the top bits, then f on each full byte
200 * (big endian - so start at top).
202 let rec map_bytes_be g f v bits =
204 map_bytes_be g f (v >>> 8) (bits-8);
205 let lsb = v land ff in
207 ) else if bits > 0 then (
208 let lsb = v land (mask bits) in
212 (* Call function g on the top bits, then f on each full byte
213 * (little endian - so start at root).
215 let rec map_bytes_le g f v bits =
217 let lsb = v land ff in
219 map_bytes_le g f (v >>> 8) (bits-8)
220 ) else if bits > 0 then (
221 let lsb = v land (mask bits) in
227 (* Bitwise operations on int32s. Note we try to keep it as similar
228 * as possible to the I module above, to make it easier to track
231 let (<<<) = Int32.shift_left
232 let (>>>) = Int32.shift_right_logical
233 let (land) = Int32.logand
234 let (lor) = Int32.logor
235 let lnot = Int32.lognot
236 let pred = Int32.pred
237 let max_int = Int32.max_int
238 let to_int = Int32.to_int
239 let zero = Int32.zero
241 let minus_one = Int32.minus_one
244 (* Create a mask so many bits wide. *)
248 else if bits = 31 then
250 else if bits = 32 then
253 invalid_arg "Bitstring.I32.mask"
255 (* Byte swap an int of a given size. *)
256 let byteswap v bits =
258 else if bits <= 16 then (
259 let shift = bits-8 in
260 let v1 = v >>> shift in
261 let v2 = (v land (mask shift)) <<< 8 in
263 ) else if bits <= 24 then (
264 let shift = bits - 16 in
265 let v1 = v >>> (8+shift) in
266 let v2 = ((v >>> shift) land ff) <<< 8 in
267 let v3 = (v land (mask shift)) <<< 16 in
270 let shift = bits - 24 in
271 let v1 = v >>> (16+shift) in
272 let v2 = ((v >>> (8+shift)) land ff) <<< 8 in
273 let v3 = ((v >>> shift) land ff) <<< 16 in
274 let v4 = (v land (mask shift)) <<< 24 in
275 v4 lor v3 lor v2 lor v1
278 (* Check a value is in range 0 .. 2^bits-1. *)
279 let range_unsigned v bits =
280 let mask = lnot (mask bits) in
283 (* Call function g on the top bits, then f on each full byte
284 * (big endian - so start at top).
286 let rec map_bytes_be g f v bits =
288 map_bytes_be g f (v >>> 8) (bits-8);
289 let lsb = v land ff in
291 ) else if bits > 0 then (
292 let lsb = v land (mask bits) in
296 (* Call function g on the top bits, then f on each full byte
297 * (little endian - so start at root).
299 let rec map_bytes_le g f v bits =
301 let lsb = v land ff in
303 map_bytes_le g f (v >>> 8) (bits-8)
304 ) else if bits > 0 then (
305 let lsb = v land (mask bits) in
311 (* Bitwise operations on int64s. Note we try to keep it as similar
312 * as possible to the I/I32 modules above, to make it easier to track
315 let (<<<) = Int64.shift_left
316 let (>>>) = Int64.shift_right_logical
317 let (land) = Int64.logand
318 let (lor) = Int64.logor
319 let lnot = Int64.lognot
320 let pred = Int64.pred
321 let max_int = Int64.max_int
322 let to_int = Int64.to_int
323 let zero = Int64.zero
325 let minus_one = Int64.minus_one
328 (* Create a mask so many bits wide. *)
332 else if bits = 63 then
334 else if bits = 64 then
337 invalid_arg "Bitstring.I64.mask"
339 (* Byte swap an int of a given size. *)
340 (* let byteswap v bits = *)
342 (* Check a value is in range 0 .. 2^bits-1. *)
343 let range_unsigned v bits =
344 let mask = lnot (mask bits) in
347 (* Call function g on the top bits, then f on each full byte
348 * (big endian - so start at top).
350 let rec map_bytes_be g f v bits =
352 map_bytes_be g f (v >>> 8) (bits-8);
353 let lsb = v land ff in
355 ) else if bits > 0 then (
356 let lsb = v land (mask bits) in
360 (* Call function g on the top bits, then f on each full byte
361 * (little endian - so start at root).
363 let rec map_bytes_le g f v bits =
365 let lsb = v land ff in
367 map_bytes_le g f (v >>> 8) (bits-8)
368 ) else if bits > 0 then (
369 let lsb = v land (mask bits) in
374 (*----------------------------------------------------------------------*)
375 (* Extraction functions.
377 * NB: internal functions, called from the generated macros, and
378 * the parameters should have been checked for sanity already).
381 (* Extract and convert to numeric. A single bit is returned as
382 * a boolean. There are no endianness or signedness considerations.
384 let extract_bit data off len _ = (* final param is always 1 *)
385 let byteoff = off lsr 3 in
386 let bitmask = 1 lsl (7 - (off land 7)) in
387 let b = Char.code data.[byteoff] land bitmask <> 0 in
390 (* Returns 8 bit unsigned aligned bytes from the string.
391 * If the string ends then this returns 0's.
393 let _get_byte data byteoff strlen =
394 if strlen > byteoff then Char.code data.[byteoff] else 0
395 let _get_byte32 data byteoff strlen =
396 if strlen > byteoff then Int32.of_int (Char.code data.[byteoff]) else 0l
397 let _get_byte64 data byteoff strlen =
398 if strlen > byteoff then Int64.of_int (Char.code data.[byteoff]) else 0L
400 (* Extract [2..8] bits. Because the result fits into a single
401 * byte we don't have to worry about endianness, only signedness.
403 let extract_char_unsigned data off len flen =
404 let byteoff = off lsr 3 in
406 (* Optimize the common (byte-aligned) case. *)
407 if off land 7 = 0 then (
408 let byte = Char.code data.[byteoff] in
409 byte lsr (8 - flen) (*, off+flen, len-flen*)
411 (* Extract the 16 bits at byteoff and byteoff+1 (note that the
412 * second byte might not exist in the original string).
414 let strlen = String.length data in
417 (_get_byte data byteoff strlen lsl 8) +
418 _get_byte data (byteoff+1) strlen in
420 (* Mask off the top bits. *)
421 let bitmask = (1 lsl (16 - (off land 7))) - 1 in
422 let word = word land bitmask in
423 (* Shift right to get rid of the bottom bits. *)
424 let shift = 16 - ((off land 7) + flen) in
425 let word = word lsr shift in
427 word (*, off+flen, len-flen*)
430 (* Extract [9..31] bits. We have to consider endianness and signedness. *)
431 let extract_int_be_unsigned data off len flen =
432 let byteoff = off lsr 3 in
434 let strlen = String.length data in
437 (* Optimize the common (byte-aligned) case. *)
438 if off land 7 = 0 then (
440 (_get_byte data byteoff strlen lsl 23) +
441 (_get_byte data (byteoff+1) strlen lsl 15) +
442 (_get_byte data (byteoff+2) strlen lsl 7) +
443 (_get_byte data (byteoff+3) strlen lsr 1) in
445 ) else if flen <= 24 then (
446 (* Extract the 31 bits at byteoff .. byteoff+3. *)
448 (_get_byte data byteoff strlen lsl 23) +
449 (_get_byte data (byteoff+1) strlen lsl 15) +
450 (_get_byte data (byteoff+2) strlen lsl 7) +
451 (_get_byte data (byteoff+3) strlen lsr 1) in
452 (* Mask off the top bits. *)
453 let bitmask = (1 lsl (31 - (off land 7))) - 1 in
454 let word = word land bitmask in
455 (* Shift right to get rid of the bottom bits. *)
456 let shift = 31 - ((off land 7) + flen) in
459 (* Extract the next 31 bits, slow method. *)
461 let c0 = extract_char_unsigned data off len 8
462 and off = off + 8 and len = len - 8 in
463 let c1 = extract_char_unsigned data off len 8
464 and off = off + 8 and len = len - 8 in
465 let c2 = extract_char_unsigned data off len 8
466 and off = off + 8 and len = len - 8 in
467 let c3 = extract_char_unsigned data off len 7 in
468 (c0 lsl 23) + (c1 lsl 15) + (c2 lsl 7) + c3 in
471 word (*, off+flen, len-flen*)
473 let extract_int_le_unsigned data off len flen =
474 let v = extract_int_be_unsigned data off len flen in
475 let v = I.byteswap v flen in
478 let extract_int_ne_unsigned =
479 if nativeendian = BigEndian
480 then extract_int_be_unsigned
481 else extract_int_le_unsigned
483 let extract_int_ee_unsigned = function
484 | BigEndian -> extract_int_be_unsigned
485 | LittleEndian -> extract_int_le_unsigned
486 | NativeEndian -> extract_int_ne_unsigned
488 let _make_int32_be c0 c1 c2 c3 =
492 (Int32.shift_left c0 24)
493 (Int32.shift_left c1 16))
494 (Int32.shift_left c2 8))
497 let _make_int32_le c0 c1 c2 c3 =
501 (Int32.shift_left c3 24)
502 (Int32.shift_left c2 16))
503 (Int32.shift_left c1 8))
506 (* Extract exactly 32 bits. We have to consider endianness and signedness. *)
507 let extract_int32_be_unsigned data off len flen =
508 let byteoff = off lsr 3 in
510 let strlen = String.length data in
513 (* Optimize the common (byte-aligned) case. *)
514 if off land 7 = 0 then (
516 let c0 = _get_byte32 data byteoff strlen in
517 let c1 = _get_byte32 data (byteoff+1) strlen in
518 let c2 = _get_byte32 data (byteoff+2) strlen in
519 let c3 = _get_byte32 data (byteoff+3) strlen in
520 _make_int32_be c0 c1 c2 c3 in
521 Int32.shift_right_logical word (32 - flen)
523 (* Extract the next 32 bits, slow method. *)
525 let c0 = extract_char_unsigned data off len 8
526 and off = off + 8 and len = len - 8 in
527 let c1 = extract_char_unsigned data off len 8
528 and off = off + 8 and len = len - 8 in
529 let c2 = extract_char_unsigned data off len 8
530 and off = off + 8 and len = len - 8 in
531 let c3 = extract_char_unsigned data off len 8 in
532 let c0 = Int32.of_int c0 in
533 let c1 = Int32.of_int c1 in
534 let c2 = Int32.of_int c2 in
535 let c3 = Int32.of_int c3 in
536 _make_int32_be c0 c1 c2 c3 in
537 Int32.shift_right_logical word (32 - flen)
539 word (*, off+flen, len-flen*)
541 let extract_int32_le_unsigned data off len flen =
542 let v = extract_int32_be_unsigned data off len flen in
543 let v = I32.byteswap v flen in
546 let extract_int32_ne_unsigned =
547 if nativeendian = BigEndian
548 then extract_int32_be_unsigned
549 else extract_int32_le_unsigned
551 let extract_int32_ee_unsigned = function
552 | BigEndian -> extract_int32_be_unsigned
553 | LittleEndian -> extract_int32_le_unsigned
554 | NativeEndian -> extract_int32_ne_unsigned
556 let _make_int64_be c0 c1 c2 c3 c4 c5 c6 c7 =
564 (Int64.shift_left c0 56)
565 (Int64.shift_left c1 48))
566 (Int64.shift_left c2 40))
567 (Int64.shift_left c3 32))
568 (Int64.shift_left c4 24))
569 (Int64.shift_left c5 16))
570 (Int64.shift_left c6 8))
573 let _make_int64_le c0 c1 c2 c3 c4 c5 c6 c7 =
574 _make_int64_be c7 c6 c5 c4 c3 c2 c1 c0
576 (* Extract [1..64] bits. We have to consider endianness and signedness. *)
577 let extract_int64_be_unsigned data off len flen =
578 let byteoff = off lsr 3 in
580 let strlen = String.length data in
583 (* Optimize the common (byte-aligned) case. *)
584 if off land 7 = 0 then (
586 let c0 = _get_byte64 data byteoff strlen in
587 let c1 = _get_byte64 data (byteoff+1) strlen in
588 let c2 = _get_byte64 data (byteoff+2) strlen in
589 let c3 = _get_byte64 data (byteoff+3) strlen in
590 let c4 = _get_byte64 data (byteoff+4) strlen in
591 let c5 = _get_byte64 data (byteoff+5) strlen in
592 let c6 = _get_byte64 data (byteoff+6) strlen in
593 let c7 = _get_byte64 data (byteoff+7) strlen in
594 _make_int64_be c0 c1 c2 c3 c4 c5 c6 c7 in
595 Int64.shift_right_logical word (64 - flen)
597 (* Extract the next 64 bits, slow method. *)
599 let c0 = extract_char_unsigned data off len 8
600 and off = off + 8 and len = len - 8 in
601 let c1 = extract_char_unsigned data off len 8
602 and off = off + 8 and len = len - 8 in
603 let c2 = extract_char_unsigned data off len 8
604 and off = off + 8 and len = len - 8 in
605 let c3 = extract_char_unsigned data off len 8
606 and off = off + 8 and len = len - 8 in
607 let c4 = extract_char_unsigned data off len 8
608 and off = off + 8 and len = len - 8 in
609 let c5 = extract_char_unsigned data off len 8
610 and off = off + 8 and len = len - 8 in
611 let c6 = extract_char_unsigned data off len 8
612 and off = off + 8 and len = len - 8 in
613 let c7 = extract_char_unsigned data off len 8 in
614 let c0 = Int64.of_int c0 in
615 let c1 = Int64.of_int c1 in
616 let c2 = Int64.of_int c2 in
617 let c3 = Int64.of_int c3 in
618 let c4 = Int64.of_int c4 in
619 let c5 = Int64.of_int c5 in
620 let c6 = Int64.of_int c6 in
621 let c7 = Int64.of_int c7 in
622 _make_int64_be c0 c1 c2 c3 c4 c5 c6 c7 in
623 Int64.shift_right_logical word (64 - flen)
625 word (*, off+flen, len-flen*)
627 let extract_int64_le_unsigned data off len flen =
628 let byteoff = off lsr 3 in
630 let strlen = String.length data in
633 (* Optimize the common (byte-aligned) case. *)
634 if off land 7 = 0 then (
636 let c0 = _get_byte64 data byteoff strlen in
637 let c1 = _get_byte64 data (byteoff+1) strlen in
638 let c2 = _get_byte64 data (byteoff+2) strlen in
639 let c3 = _get_byte64 data (byteoff+3) strlen in
640 let c4 = _get_byte64 data (byteoff+4) strlen in
641 let c5 = _get_byte64 data (byteoff+5) strlen in
642 let c6 = _get_byte64 data (byteoff+6) strlen in
643 let c7 = _get_byte64 data (byteoff+7) strlen in
644 _make_int64_le c0 c1 c2 c3 c4 c5 c6 c7 in
645 Int64.logand word (I64.mask flen)
647 (* Extract the next 64 bits, slow method. *)
649 let c0 = extract_char_unsigned data off len 8
650 and off = off + 8 and len = len - 8 in
651 let c1 = extract_char_unsigned data off len 8
652 and off = off + 8 and len = len - 8 in
653 let c2 = extract_char_unsigned data off len 8
654 and off = off + 8 and len = len - 8 in
655 let c3 = extract_char_unsigned data off len 8
656 and off = off + 8 and len = len - 8 in
657 let c4 = extract_char_unsigned data off len 8
658 and off = off + 8 and len = len - 8 in
659 let c5 = extract_char_unsigned data off len 8
660 and off = off + 8 and len = len - 8 in
661 let c6 = extract_char_unsigned data off len 8
662 and off = off + 8 and len = len - 8 in
663 let c7 = extract_char_unsigned data off len 8 in
664 let c0 = Int64.of_int c0 in
665 let c1 = Int64.of_int c1 in
666 let c2 = Int64.of_int c2 in
667 let c3 = Int64.of_int c3 in
668 let c4 = Int64.of_int c4 in
669 let c5 = Int64.of_int c5 in
670 let c6 = Int64.of_int c6 in
671 let c7 = Int64.of_int c7 in
672 _make_int64_le c0 c1 c2 c3 c4 c5 c6 c7 in
673 Int64.logand word (I64.mask flen)
675 word (*, off+flen, len-flen*)
677 let extract_int64_ne_unsigned =
678 if nativeendian = BigEndian
679 then extract_int64_be_unsigned
680 else extract_int64_le_unsigned
682 let extract_int64_ee_unsigned = function
683 | BigEndian -> extract_int64_be_unsigned
684 | LittleEndian -> extract_int64_le_unsigned
685 | NativeEndian -> extract_int64_ne_unsigned
687 external extract_fastpath_int16_be_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_be_unsigned" "noalloc"
689 external extract_fastpath_int16_le_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_le_unsigned" "noalloc"
691 external extract_fastpath_int16_ne_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_ne_unsigned" "noalloc"
693 external extract_fastpath_int16_be_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_be_signed" "noalloc"
695 external extract_fastpath_int16_le_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_le_signed" "noalloc"
697 external extract_fastpath_int16_ne_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int16_ne_signed" "noalloc"
700 external extract_fastpath_int24_be_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_be_unsigned" "noalloc"
702 external extract_fastpath_int24_le_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_le_unsigned" "noalloc"
704 external extract_fastpath_int24_ne_unsigned : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_ne_unsigned" "noalloc"
706 external extract_fastpath_int24_be_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_be_signed" "noalloc"
708 external extract_fastpath_int24_le_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_le_signed" "noalloc"
710 external extract_fastpath_int24_ne_signed : string -> int -> int = "ocaml_bitstring_extract_fastpath_int24_ne_signed" "noalloc"
713 external extract_fastpath_int32_be_unsigned : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_be_unsigned" "noalloc"
715 external extract_fastpath_int32_le_unsigned : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_le_unsigned" "noalloc"
717 external extract_fastpath_int32_ne_unsigned : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_ne_unsigned" "noalloc"
719 external extract_fastpath_int32_be_signed : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_be_signed" "noalloc"
721 external extract_fastpath_int32_le_signed : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_le_signed" "noalloc"
723 external extract_fastpath_int32_ne_signed : string -> int -> int32 -> int32 = "ocaml_bitstring_extract_fastpath_int32_ne_signed" "noalloc"
726 external extract_fastpath_int40_be_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_be_unsigned" "noalloc"
728 external extract_fastpath_int40_le_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_le_unsigned" "noalloc"
730 external extract_fastpath_int40_ne_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_ne_unsigned" "noalloc"
732 external extract_fastpath_int40_be_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_be_signed" "noalloc"
734 external extract_fastpath_int40_le_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_le_signed" "noalloc"
736 external extract_fastpath_int40_ne_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int40_ne_signed" "noalloc"
738 external extract_fastpath_int48_be_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_be_unsigned" "noalloc"
740 external extract_fastpath_int48_le_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_le_unsigned" "noalloc"
742 external extract_fastpath_int48_ne_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_ne_unsigned" "noalloc"
744 external extract_fastpath_int48_be_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_be_signed" "noalloc"
746 external extract_fastpath_int48_le_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_le_signed" "noalloc"
748 external extract_fastpath_int48_ne_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int48_ne_signed" "noalloc"
750 external extract_fastpath_int56_be_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_be_unsigned" "noalloc"
752 external extract_fastpath_int56_le_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_le_unsigned" "noalloc"
754 external extract_fastpath_int56_ne_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_ne_unsigned" "noalloc"
756 external extract_fastpath_int56_be_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_be_signed" "noalloc"
758 external extract_fastpath_int56_le_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_le_signed" "noalloc"
760 external extract_fastpath_int56_ne_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int56_ne_signed" "noalloc"
763 external extract_fastpath_int64_be_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_be_unsigned" "noalloc"
765 external extract_fastpath_int64_le_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_le_unsigned" "noalloc"
767 external extract_fastpath_int64_ne_unsigned : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_ne_unsigned" "noalloc"
769 external extract_fastpath_int64_be_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_be_signed" "noalloc"
771 external extract_fastpath_int64_le_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_le_signed" "noalloc"
773 external extract_fastpath_int64_ne_signed : string -> int -> int64 -> int64 = "ocaml_bitstring_extract_fastpath_int64_ne_signed" "noalloc"
775 (*----------------------------------------------------------------------*)
776 (* Constructor functions. *)
778 module Buffer = struct
781 mutable len : int; (* Length in bits. *)
782 (* Last byte in the buffer (if len is not aligned). We store
783 * it outside the buffer because buffers aren't mutable.
789 (* XXX We have almost enough information in the generator to
790 * choose a good initial size.
792 { buf = Buffer.create 128; len = 0; last = 0 }
794 let contents { buf = buf; len = len; last = last } =
796 if len land 7 = 0 then
799 Buffer.contents buf ^ (String.make 1 (Char.chr last)) in
802 (* Add exactly 8 bits. *)
803 let add_byte ({ buf = buf; len = len; last = last } as t) byte =
804 if byte < 0 || byte > 255 then invalid_arg "Bitstring.Buffer.add_byte";
805 let shift = len land 7 in
807 (* Target buffer is byte-aligned. *)
808 Buffer.add_char buf (Char.chr byte)
810 (* Target buffer is unaligned. 'last' is meaningful. *)
811 let first = byte lsr shift in
812 let second = (byte lsl (8 - shift)) land 0xff in
813 Buffer.add_char buf (Char.chr (last lor first));
818 (* Add exactly 1 bit. *)
819 let add_bit ({ buf = buf; len = len; last = last } as t) bit =
820 let shift = 7 - (len land 7) in
822 (* Somewhere in the middle of 'last'. *)
823 t.last <- last lor ((if bit then 1 else 0) lsl shift)
825 (* Just a single spare bit in 'last'. *)
826 let last = last lor if bit then 1 else 0 in
827 Buffer.add_char buf (Char.chr last);
832 (* Add a small number of bits (definitely < 8). This uses a loop
833 * to call add_bit so it's slow.
835 let _add_bits t c slen =
836 if slen < 1 || slen >= 8 then invalid_arg "Bitstring.Buffer._add_bits";
837 for i = slen-1 downto 0 do
838 let bit = c land (1 lsl i) <> 0 in
842 let add_bits ({ buf = buf; len = len } as t) str slen =
844 if len land 7 = 0 then (
845 if slen land 7 = 0 then
846 (* Common case - everything is byte-aligned. *)
847 Buffer.add_substring buf str 0 (slen lsr 3)
849 (* Target buffer is aligned. Copy whole bytes then leave the
850 * remaining bits in last.
852 let slenbytes = slen lsr 3 in
853 if slenbytes > 0 then Buffer.add_substring buf str 0 slenbytes;
854 let last = Char.code str.[slenbytes] in (* last char *)
855 let mask = 0xff lsl (8 - (slen land 7)) in
856 t.last <- last land mask
860 (* Target buffer is unaligned. Copy whole bytes using
861 * add_byte which knows how to deal with an unaligned
862 * target buffer, then call add_bit for the remaining < 8 bits.
864 * XXX This is going to be dog-slow.
866 let slenbytes = slen lsr 3 in
867 for i = 0 to slenbytes-1 do
868 let byte = Char.code str.[i] in
871 let bitsleft = slen - (slenbytes lsl 3) in
872 if bitsleft > 0 then (
873 let c = Char.code str.[slenbytes] in
874 for i = 0 to bitsleft - 1 do
875 let bit = c land (0x80 lsr i) <> 0 in
883 (* Construct a single bit. *)
884 let construct_bit buf b _ _ =
887 (* Construct a field, flen = [2..8]. *)
888 let construct_char_unsigned buf v flen exn =
889 let max_val = 1 lsl flen in
890 if v < 0 || v >= max_val then raise exn;
892 Buffer.add_byte buf v
894 Buffer._add_bits buf v flen
896 (* Construct a field of up to 31 bits. *)
897 let construct_int_be_unsigned buf v flen exn =
898 (* Check value is within range. *)
899 if not (I.range_unsigned v flen) then raise exn;
901 I.map_bytes_be (Buffer._add_bits buf) (Buffer.add_byte buf) v flen
903 (* Construct a field of up to 31 bits. *)
904 let construct_int_le_unsigned buf v flen exn =
905 (* Check value is within range. *)
906 if not (I.range_unsigned v flen) then raise exn;
908 I.map_bytes_le (Buffer._add_bits buf) (Buffer.add_byte buf) v flen
910 let construct_int_ne_unsigned =
911 if nativeendian = BigEndian
912 then construct_int_be_unsigned
913 else construct_int_le_unsigned
915 let construct_int_ee_unsigned = function
916 | BigEndian -> construct_int_be_unsigned
917 | LittleEndian -> construct_int_le_unsigned
918 | NativeEndian -> construct_int_ne_unsigned
920 (* Construct a field of exactly 32 bits. *)
921 let construct_int32_be_unsigned buf v flen _ =
923 (Int32.to_int (Int32.shift_right_logical v 24));
925 (Int32.to_int ((Int32.logand (Int32.shift_right_logical v 16) 0xff_l)));
927 (Int32.to_int ((Int32.logand (Int32.shift_right_logical v 8) 0xff_l)));
929 (Int32.to_int (Int32.logand v 0xff_l))
931 let construct_int32_le_unsigned buf v flen _ =
933 (Int32.to_int (Int32.logand v 0xff_l));
935 (Int32.to_int ((Int32.logand (Int32.shift_right_logical v 8) 0xff_l)));
937 (Int32.to_int ((Int32.logand (Int32.shift_right_logical v 16) 0xff_l)));
939 (Int32.to_int (Int32.shift_right_logical v 24))
941 let construct_int32_ne_unsigned =
942 if nativeendian = BigEndian
943 then construct_int32_be_unsigned
944 else construct_int32_le_unsigned
946 let construct_int32_ee_unsigned = function
947 | BigEndian -> construct_int32_be_unsigned
948 | LittleEndian -> construct_int32_le_unsigned
949 | NativeEndian -> construct_int32_ne_unsigned
951 (* Construct a field of up to 64 bits. *)
952 let construct_int64_be_unsigned buf v flen exn =
953 (* Check value is within range. *)
954 if not (I64.range_unsigned v flen) then raise exn;
956 I64.map_bytes_be (Buffer._add_bits buf) (Buffer.add_byte buf) v flen
958 (* Construct a field of up to 64 bits. *)
959 let construct_int64_le_unsigned buf v flen exn =
960 (* Check value is within range. *)
961 if not (I64.range_unsigned v flen) then raise exn;
963 I64.map_bytes_le (Buffer._add_bits buf) (Buffer.add_byte buf) v flen
965 let construct_int64_ne_unsigned =
966 if nativeendian = BigEndian
967 then construct_int64_be_unsigned
968 else (*construct_int64_le_unsigned*)
969 fun _ _ _ _ -> failwith "construct_int64_le_unsigned"
971 let construct_int64_ee_unsigned = function
972 | BigEndian -> construct_int64_be_unsigned
973 | LittleEndian -> (*construct_int64_le_unsigned*)
974 (fun _ _ _ _ -> failwith "construct_int64_le_unsigned")
975 | NativeEndian -> construct_int64_ne_unsigned
977 (* Construct from a string of bytes, exact multiple of 8 bits
978 * in length of course.
980 let construct_string buf str =
981 let len = String.length str in
982 Buffer.add_bits buf str (len lsl 3)
984 (* Construct from a bitstring. *)
985 let construct_bitstring buf (data, off, len) =
986 (* Add individual bits until we get to the next byte boundary of
987 * the underlying string.
989 let blen = 7 - ((off + 7) land 7) in
990 let blen = min blen len in
991 let rec loop off len blen =
992 if blen = 0 then (off, len)
994 let b = extract_bit data off len 1
995 and off = off + 1 and len = len + 1 in
996 Buffer.add_bit buf b;
997 loop off len (blen-1)
1000 let off, len = loop off len blen in
1001 assert (len = 0 || (off land 7) = 0);
1003 (* Add the remaining 'len' bits. *)
1005 let off = off lsr 3 in
1006 (* XXX dangerous allocation *)
1007 if off = 0 then data
1008 else String.sub data off (String.length data - off) in
1010 Buffer.add_bits buf data len
1012 (*----------------------------------------------------------------------*)
1013 (* Extract a string from a bitstring. *)
1015 let string_of_bitstring (data, off, len) =
1016 if off land 7 = 0 && len land 7 = 0 then
1017 (* Easy case: everything is byte-aligned. *)
1018 String.sub data (off lsr 3) (len lsr 3)
1020 (* Bit-twiddling case. *)
1021 let strlen = (len + 7) lsr 3 in
1022 let str = String.make strlen '\000' in
1023 let rec loop data off len i =
1025 let c = extract_char_unsigned data off len 8
1026 and off = off + 8 and len = len - 8 in
1027 str.[i] <- Char.chr c;
1028 loop data off len (i+1)
1029 ) else if len > 0 then (
1030 let c = extract_char_unsigned data off len len in
1031 str.[i] <- Char.chr (c lsl (8-len))
1034 loop data off len 0;
1040 let bitstring_to_chan ((data, off, len) as bits) chan =
1041 (* Fail if the bitstring length isn't a multiple of 8. *)
1042 if len land 7 <> 0 then invalid_arg "bitstring_to_chan";
1044 if off land 7 = 0 then
1045 (* Easy case: string is byte-aligned. *)
1046 output chan data (off lsr 3) (len lsr 3)
1048 (* Bit-twiddling case: reuse string_of_bitstring *)
1049 let str = string_of_bitstring bits in
1050 output_string chan str
1053 let bitstring_to_file bits filename =
1054 let chan = open_out_bin filename in
1056 bitstring_to_chan bits chan;
1062 (*----------------------------------------------------------------------*)
1063 (* Display functions. *)
1066 let c = Char.code c in
1069 let hexdump_bitstring chan (data, off, len) =
1070 let count = ref 0 in
1071 let off = ref off in
1072 let len = ref len in
1073 let linelen = ref 0 in
1074 let linechars = String.make 16 ' ' in
1076 fprintf chan "00000000 ";
1079 let bits = min !len 8 in
1080 let byte = extract_char_unsigned data !off !len bits in
1081 off := !off + bits; len := !len - bits;
1083 let byte = byte lsl (8-bits) in
1084 fprintf chan "%02x " byte;
1087 linechars.[!linelen] <-
1088 (let c = Char.chr byte in
1089 if isprint c then c else '.');
1091 if !linelen = 8 then fprintf chan " ";
1092 if !linelen = 16 then (
1093 fprintf chan " |%s|\n%08x " linechars !count;
1095 for i = 0 to 15 do linechars.[i] <- ' ' done
1099 if !linelen > 0 then (
1100 let skip = (16 - !linelen) * 3 + if !linelen < 8 then 1 else 0 in
1101 for i = 0 to skip-1 do fprintf chan " " done;
1102 fprintf chan " |%s|\n%!" linechars