# `ASDF.Block`
[🔗](https://github.com/baoulo/asdf-elixir/blob/v0.1.0/lib/asdf/block.ex#L1)

One binary block header.

The payload is not stored here. `payload_offset` and the sizes are enough
to `pread` it later. `checksum` is nil when the header's 16 bytes are
zeros, which the spec defines as "do not check".

# `compression`

```elixir
@type compression() :: :none | :zlib
```

Compression stored in the 4-byte field. `:none` is four zero bytes.
`:zlib` is the ASCII bytes `zlib`. `bzp2` is rejected and is not a value
of this type.

# `t`

```elixir
@type t() :: %ASDF.Block{
  allocated_size: non_neg_integer(),
  checksum: binary() | nil,
  compression: compression(),
  data_size: non_neg_integer(),
  flags: non_neg_integer(),
  header_offset: non_neg_integer(),
  header_size: non_neg_integer(),
  index: non_neg_integer(),
  payload_offset: non_neg_integer(),
  streamed: boolean(),
  used_size: non_neg_integer()
}
```

One block header. The payload is not stored.

  * `:index` — zero-based position in the file
  * `:header_offset` — byte offset of the magic
  * `:header_size` — size of the header body after the 2-byte length
  * `:flags` — raw flag word; bit 0 means streamed
  * `:compression` — `:none` or `:zlib`
  * `:allocated_size` — bytes reserved for the stored payload
  * `:used_size` — stored bytes that are meaningful
  * `:data_size` — size after decompression; equals `:used_size` when uncompressed
  * `:checksum` — 16-byte MD5 of the used bytes, or `nil` when the field is zeros
  * `:payload_offset` — first byte of the stored payload
  * `:streamed` — `true` when the header is the last block and the file has no index

# `encode`

```elixir
@spec encode(keyword()) :: binary()
```

Encodes one block header.

`opts` is a keyword list.

  * `:used_size` — required non-negative integer, the stored payload size
  * `:allocated_size` — optional, defaults to `:used_size`
  * `:data_size` — optional, defaults to `:used_size`
  * `:compression` — `:none` (default) or `:zlib`
  * `:streamed` — `false` (default) or `true`, which sets flag bit 0
  * `:checksum` — `:skip` (default) writes 16 zero bytes; a 16-byte binary
    is stored as the checksum

Returns a binary. The first four bytes are `<<0xD3, "BLK">>`, then a
16-bit big-endian body size of 48, then the 48-byte body. This function
does not compress the payload and does not raise for a known option.
An unknown `:compression` raises `FunctionClauseError`.

## Examples

    iex> <<0xD3, "BLK", size::unsigned-big-16, rest::binary>> = ASDF.Block.encode(used_size: 4)
    ...> {size, byte_size(rest)}
    {48, 48}

    iex> ASDF.Block.encode(used_size: 0, compression: :zlib) |> binary_part(10, 4)
    "zlib"

# `read_payload`

```elixir
@spec read_payload(ASDF.Source.t(), t(), ASDF.Limits.t()) ::
  {:ok, binary()} | {:error, ASDF.Error.t()}
```

Reads and checks the used payload of `block`.

`source` is an `%ASDF.Source{}`. `block` is an `%ASDF.Block{}` from `scan/4`.
`limits` is an `%ASDF.Limits{}`. Returns `{:ok, bytes}`. Uncompressed bytes
are the used payload. `:zlib` bytes are inflated, and the result is capped
by `data_size`.

A checksum that does not match is `reason: :checksum_mismatch`. A stored
or inflated size above `max_block_bytes` is `reason: :decompression_bomb`.
A short read is `reason: :truncated_block`. Does not raise.

## Examples

    iex> header = ASDF.Block.encode(used_size: 0)
    ...> {:ok, source} = ASDF.Source.open({:binary, header})
    ...> {:ok, [block], _warnings} = ASDF.Block.scan(source, 0, ASDF.Limits.default(), :strict)
    ...> {:ok, payload} = ASDF.Block.read_payload(source, block, ASDF.Limits.default())
    ...> ASDF.Source.close(source)
    ...> payload
    ""

# `scan`

```elixir
@spec scan(ASDF.Source.t(), non_neg_integer(), ASDF.Limits.t(), :strict | :permissive) ::
  {:ok, [t()], [ASDF.Warning.t()]} | {:error, ASDF.Error.t()}
```

Scans block headers starting at `offset`.

`source` is an `%ASDF.Source{}`. `offset` is the first byte after the tree.
`limits` is an `%ASDF.Limits{}`. `mode` is `:strict` or `:permissive`.

Returns `{:ok, blocks, warnings}`. An empty range returns `{:ok, [], []}`.
The optional `#ASDF BLOCK INDEX` is checked against the scanned offsets.
A mismatch is `{:error, %ASDF.Error{reason: :invalid_block_index}}` in
`:strict` mode. In `:permissive` mode the scan is kept and a warning is
recorded. `bzp2` is `reason: :unsupported_compression`. A header that
claims more bytes than `max_block_bytes` is `reason: :decompression_bomb`.
Does not read payloads and does not raise.

## Examples

    iex> header = ASDF.Block.encode(used_size: 0)
    ...> {:ok, source} = ASDF.Source.open({:binary, header})
    ...> {:ok, [block], warnings} = ASDF.Block.scan(source, 0, ASDF.Limits.default(), :strict)
    ...> ASDF.Source.close(source)
    ...> {block.compression, block.used_size, warnings}
    {:none, 0, []}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
