BSV-ex v0.2.3 BSV.Util.VarBin View Source

Module for parsing and serializing variable length binary data as integers, binaries and structs.

Link to this section Summary

Functions

Parses the given binary into a chunk of binary data, using the first byte(s) to determing the size of the chunk. Returns a tuple containing the chunk and any remaining binary data.

Parses the given binary into an integer. Returns a tuple containing the decoded integer and any remaining binary data.

Parses the given binary into a list of parsed structs, using the first byte(s) to determing the number of items, and calling the given callback to parse each repsective chunk of data.

Prefixes the given binary with a variable length integer to indicate the size of the following binary,

Serializes the given integer into a binary.

Serializes the given list of items into a binary, first by prefixing the binary with a variable length integer to indicate the number of items, and then by calling the given callback to serialize each respective item.

Link to this section Functions

Link to this function

parse_bin(data)

View Source
parse_bin(binary()) :: {binary(), binary()}

Parses the given binary into a chunk of binary data, using the first byte(s) to determing the size of the chunk. Returns a tuple containing the chunk and any remaining binary data.

Examples

iex> BSV.Util.VarBin.parse_bin(<<5, 104, 101, 108, 108, 111>>)
{"hello", ""}
Link to this function

parse_int(arg)

View Source
parse_int(binary()) :: {integer(), binary()}

Parses the given binary into an integer. Returns a tuple containing the decoded integer and any remaining binary data.

Examples

iex> BSV.Util.VarBin.parse_int(<<253, 4, 1>>)
{260, ""}

iex> BSV.Util.VarBin.parse_int(<<254, 0, 225, 245, 5>>)
{100_000_000, ""}
Link to this function

parse_items(data, callback)

View Source
parse_items(binary(), function()) :: {list(), binary()}

Parses the given binary into a list of parsed structs, using the first byte(s) to determing the number of items, and calling the given callback to parse each repsective chunk of data.

Returns a tuple containing a list of parsed items and any remaining binary data.

Examples

BSV.Util.VarBin.parse_items(data, &BSV.Transaction.Input.parse/1)
{[
  %BSV.Trasaction.Input{},
  %BSV.Trasaction.Input{}
], ""}
Link to this function

serialize_bin(data)

View Source
serialize_bin(binary()) :: binary()

Prefixes the given binary with a variable length integer to indicate the size of the following binary,

Examples

iex> BSV.Util.VarBin.serialize_bin("hello")
<<5, 104, 101, 108, 108, 111>>
Link to this function

serialize_int(int)

View Source
serialize_int(integer()) :: binary()

Serializes the given integer into a binary.

Examples

iex> BSV.Util.VarBin.serialize_int(260)
<<253, 4, 1>>

iex> BSV.Util.VarBin.serialize_int(100_000_000)
<<254, 0, 225, 245, 5>>
Link to this function

serialize_items(items, callback)

View Source
serialize_items(list(), function()) :: binary()

Serializes the given list of items into a binary, first by prefixing the binary with a variable length integer to indicate the number of items, and then by calling the given callback to serialize each respective item.

Examples

[
  %BSV.Trasaction.Input{},
  %BSV.Trasaction.Input{}
]
|> BSV.Util.VarBin.serialize_items(data, &BSV.Transaction.Input.serialize/1)
<< data >>