Base types
type buffer = (char, Bigarray_compat.int8_unsigned_elt, Bigarray_compat.c_layout) Bigarray_compat.Array1.tType of a buffer. A cstruct is composed of an underlying buffer and position/length within this buffer.
Type of a cstruct.
val byte : int -> bytebyte v convert v to a single byte.
- raises Invalid_argument
if
vis negative or greater than 255.
Creation and conversion
val empty : tempty is the cstruct of length 0.
of_bigarray ~off ~len b is the cstruct contained in b starting at off, of length len.
val create : int -> tcreate len is a fresh cstruct of size len with an offset of 0, filled with zero bytes.
val create_unsafe : int -> tcreate_unsafe len is a cstruct of size len with an offset of 0.
Note that the returned cstruct will contain arbitrary data, likely including the contents of previously-deallocated cstructs.
Beware!
Forgetting to replace this data could cause your application to leak sensitive information.
of_string ~allocator ~off ~len str is the cstruct representation of str slice located at off offset and of len length, with the underlying buffer allocated by alloc. If allocator is not provided, create is used.
of_bytes ~allocator byt is the cstruct representation of byt slice located at off offset and of len length, with the underlying buffer allocated by alloc. If allocator is not provided, create is used.
val of_hex : string -> tof_hex str is the cstruct cs. Every pair of hex-encoded characters in str are converted to one byte in cs. Whitespaces (space, newline, tab, carriage return) in str are skipped. The resulting cstruct is exactly half the size of the non-skipped characters of str.
- raises Invalid_argument
if the input string contains invalid characters or has an odd numbers of non-whitespace characters.
Comparison
equal t1 t2 is true iff t1 and t2 correspond to the same sequence of bytes.
Getters and Setters
val byte_to_int : byte -> intConvert a byte to an integer
val check_bounds : t -> int -> boolcheck_bounds cstr len is true if len is a non-negative integer and cstr.buffer's size is greater or equal than len false otherwise.
val check_alignment : t -> int -> boolcheck_alignment cstr alignment is true if the first byte stored within cstr is at a memory address where address mod alignment = 0, false otherwise. Typical uses are to check a buffer is aligned to a page or disk sector boundary.
- raises Invalid_argument
if
alignmentis not a positive integer.
val get_char : t -> int -> charget_char t off returns the character contained in the cstruct at offset off.
- raises Invalid_argument
if the offset exceeds cstruct length.
get_uint8 t off returns the byte contained in the cstruct at offset off.
- raises Invalid_argument
if the offset exceeds cstruct length.
val set_char : t -> int -> char -> unitset_char t off c sets the byte contained in the cstruct at offset off to character c.
- raises Invalid_argument
if the offset exceeds cstruct length.
set_uint8 t off c sets the byte contained in the cstruct at offset off to byte c.
- raises Invalid_argument
if the offset exceeds cstruct length.
sub cstr off len is { t with off = t.off + off; len }
- raises Invalid_argument
if the offset exceeds cstruct length.
shift cstr len is { cstr with off=t.off+len; len=t.len-len }
- raises Invalid_argument
if the offset exceeds cstruct length.
val copy : t -> int -> int -> stringcopy cstr off len is the string representation of the segment of t starting at off of size len.
- raises Invalid_argument
if
offandlendo not designate a valid segment oft.
blit src srcoff dst dstoff len copies len characters from cstruct src, starting at index srcoff, to cstruct dst, starting at index dstoff. It works correctly even if src and dst are the same string, and the source and destination intervals overlap.
- raises Invalid_argument
if
srcoffandlendo not designate a valid segment ofsrc, or ifdstoffandlendo not designate a valid segment ofdst.
val blit_from_string : string -> int -> t -> int -> int -> unitblit_from_string src srcoff dst dstoff len copies len characters from string src, starting at index srcoff, to cstruct dst, starting at index dstoff.
- raises Invalid_argument
if
srcoffandlendo not designate a valid substring ofsrc, or ifdstoffandlendo not designate a valid segment ofdst.
val blit_from_bytes : bytes -> int -> t -> int -> int -> unitblit_from_bytes src srcoff dst dstoff len copies len characters from bytes src, starting at index srcoff, to cstruct dst, starting at index dstoff.
- raises Invalid_argument
if
srcoffandlendo not designate a valid subsequence ofsrc, or ifdstoffandlendo not designate a valid segment ofdst.
val blit_to_bytes : t -> int -> bytes -> int -> int -> unitblit_to_bytes src srcoff dst dstoff len copies len characters from cstruct src, starting at index srcoff, to the dst buffer, starting at index dstoff.
- raises Invalid_argument
if
srcoffandlendo not designate a valid segment ofsrc, or ifdstoffandlendo not designate a valid segment ofdst.
val blit_to_string : t -> int -> bytes -> int -> int -> unitblit_to_string is a deprecated alias of blit_to_bytes.
val memset : t -> int -> unitmemset t x sets all the bytes of t to x land 0xff.
val len : t -> intReturns the length of the current cstruct view. Note that this length is potentially smaller than the actual size of the underlying buffer, as the sub or set_len functions can construct a smaller view.
set_len t len sets the length of the cstruct t to a new absolute value, and returns a fresh cstruct with these settings.
- raises Invalid_argument
if
lenexceeds the size of the buffer.
add_len t l will add l bytes to the length of the buffer, and return a fresh cstruct with these settings.
- raises Invalid_argument
if
lenexceeds the size of the buffer.
split ~start cstr len is a tuple containing the cstruct extracted from cstr at offset start (default: 0) of length len as first element, and the rest of cstr as second element.
- raises Invalid_argument
if
startexceeds the cstruct length, or if there is a bounds violation of the cstruct vialen+start.
val to_string : t -> stringto_string t will allocate a fresh OCaml string and copy the contents of the cstruct into it, and return that string copy.
val to_bytes : t -> bytesto_bytes t will allocate a fresh OCaml bytes and copy the contents of the cstruct into it, and return that byte copy.
Debugging
val hexdump : t -> unitWhen the going gets tough, the tough hexdump their cstructs and peer at it until the bug disappears. This will directly prettyprint the contents of the cstruct to the standard output.
val hexdump_to_buffer : Stdlib.Buffer.t -> t -> unithexdump_to_buffer buf c will append the pretty-printed hexdump of the cstruct c to the buffer buf.
val hexdump_pp : Stdlib.Format.formatter -> t -> unithexdump_pp f c pretty-prints a hexdump of c to f.
val debug : t -> stringdebug t will print out the internal details of a cstruct such as its base offset and the length, and raise an assertion failure if invariants have been violated. Not intended for casual use.
module BE : sig ... endmodule LE : sig ... endList of buffers
val lenv : t list -> intlenv cstrs is the combined length of all cstructs in cstrs.
- raises Invalid_argument
if computing the sum overflows.
val copyv : t list -> stringcopyv cstrs is the string representation of the concatenation of all cstructs in cstrs.
- raises Invalid_argument
if the length of the result would exceed
Sys.max_string_length.
fillv ~src ~dst copies from src to dst until src is exhausted or dst is full. Returns the number of bytes copied and the remaining data from src, if any. This is useful if you want buffer data into fixed-sized chunks.
Iterations
iter lenf of_cstr cstr is an iterator over cstr that returns elements of size lenf cstr and type of_cstr cstr.
val fold : ('b -> 'a -> 'b) -> 'a iter -> 'b -> 'bfold f iter acc is (f iterN accN ... (f iter acc)...).
concat ts is the concatenation of all the ts. It is not guaranteed that * the result is a newly created t in the zero- and one-element cases.