package bitpack_serializer

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

The Buffer defines the methods for serializers & deserializers in a buffer. The compression algorithm used by the library works by writing bits after bits instead of bytes.

type 'dict dictionary =
  1. | NoDictionary
  2. | Dictionary of 'dict

A dictionary type. It is used as an argument for initialzing writers and readers.

Writer

type writer

A writer is a Bytes buffer where is written the data. It also contains an optional dictionary for string storage. It also holds statistics on the repartition of bits for each datatype (unsigned int, signed int and string).

val initialize_writer : ?with_dict:int option dictionary -> ?init_size:int -> unit -> writer

Initializes a new buffer as a writer. If with_dict is set to NoDictionary, strings will be written in the buffer. If with_dict is set to Dictionary None (by default), strings will be replaced by an unknown size integers; a disctionary will be saved and concatenated at the beginning of the buffer after finalization. If with_dict is set to Dictionary (Some size), it does the same than Dictionary None but it is assumed there will be no more than size words to save. registered in a dictionary (useful when the same string apprears at different places) and replaced by integers in the buffer. Variable init_size is the initial size of the writing bytes buffer (by default: 4096)

val finalize : writer -> Stdlib.Bytes.t

Returns the bytes representation of the buffer. If there is a dictionary, it is written at the beginning of the buffer.

val write : writer -> int64 -> int -> unit

write buff v size

Writes v on buff using size bits. Value must be positive. Fails with Invalid_argument if size is not in [0, 63] or if v is negative or greater than 2^size.

val write_bool : writer -> bool -> unit

Writes a boolean on 1 bit: 0 if false, 1 if true.

Write unsigned ints

val write_uint8 : writer -> int -> unit

Equivalent to write w v 8

val write_uint16 : writer -> int -> unit

Equivalent to write w v 16

val write_uint32 : writer -> int -> unit

Equivalent to write w v 32

val write_uint63 : writer -> int64 -> unit

Equivalent to write w v 63

val write_z : writer -> Z.t -> unit

Writes an unbounded integer.

val write_signed_int : writer -> int -> unit

Same as write_z w (Z.of_int i)

val write_bytes_known_length : len:int -> writer -> Stdlib.Bytes.t -> unit

write_bytes_known_size ~len w b Writes the first len bytes of b in argument.

val write_bytes : writer -> Stdlib.Bytes.t -> unit

Writes a bytes buffer of any size.

val write_str_repr : writer -> string -> unit

Write a string representation. If the buffer is associated to a dictionary, writes the its associated integer instead.

Reader

type reader

A reader buffer.

val initialize_reader : ?with_dict:int option dictionary -> Stdlib.Bytes.t -> reader

Initializes a buffer as a reader. The with_dict argument must be the same as the argument used for initializing the writer.

val read : reader -> int -> int64

read buff n reads n bits on the buffer as an unsigned value

val read_bool : reader -> bool

Reads 1 bit, returns true if 1, false otherwise

Reads unsigned integers

val read_uint8 : reader -> int

Reads 8 bits

val read_uint16 : reader -> int

Reads 16 bits

val read_uint32 : reader -> int

Reads 32 bits

val read_uint63 : reader -> int64

Reads 63 bits

val read_signed_int : reader -> int

Reads an unsigned int prefixed by its size.

val read_z : reader -> Z.t

Reads an unbounded integer

val read_bytes_known_length : len:int -> reader -> bytes

Reads len bytes and returns it as a bytes buffer.

val read_bytes : reader -> Stdlib.Bytes.t

Reads a integer representing the number of bytes to read, then reads and return these bytes.

val read_str_repr : reader -> string

Reads a string representation

Statistics

val print_stats : Stdlib.Format.formatter -> writer -> unit

Prints statistics of bits repartition of a buffer.

OCaml

Innovation. Community. Security.