# snappy.framing

> A module of snappy's API.

Snappy **framing format**: a `sNaPpY` stream identifier chunk followed by a sequence of length-prefixed chunks, each either compressed or uncompressed raw-format data guarded by a masked CRC-32C. See https://github.com/google/snappy/blob/main/framing_format.txt.

Chunk layout: 1-byte type, 3-byte little-endian data length, then the
data. Compressed/uncompressed data chunks additionally carry a 4-byte
little-endian masked CRC-32C of the *uncompressed* bytes as the first 4
bytes of their data.

## Structs

### `FramedWriter`

```mojo
struct FramedWriter
```

Incremental framing-format encoder: feed arbitrary-sized buffers to `write`, splitting into <=64 KiB chunks internally; `finish` returns the complete encoded stream.

Implements: `Movable`

#### `__init__`

```mojo
def __init__(out self)
```

#### `write`

```mojo
def write(mut self, data: Span[UInt8])
```

#### `finish`

```mojo
def finish(deinit self) -> List[UInt8]
```

### `FramedReader`

```mojo
struct FramedReader
```

Incremental framing-format decoder: `read_chunk` returns each data chunk's decompressed bytes in order (verifying its CRC-32C), or `None` once the stream is exhausted. Stream-identifier, padding, and skippable chunks are consumed transparently.

Implements: `Movable`

#### `__init__`

```mojo
def __init__(out self, data: Span[UInt8])
```

#### `read_chunk`

```mojo
def read_chunk(mut self) -> Optional[List[UInt8]]
```

## Functions

### `compress_framed`

```mojo
def compress_framed(data: Span[UInt8]) -> List[UInt8]
```

Compress `data` into a complete Snappy framing-format stream (stream identifier + one or more chunks, each <= 64 KiB uncompressed).

### `decompress_framed`

```mojo
def decompress_framed(data: Span[UInt8]) -> List[UInt8]
```

Decompress a full Snappy framing-format stream, verifying every chunk's masked CRC-32C. Raises on a missing/misplaced stream identifier, an unsupported unskippable chunk type, or a checksum mismatch.

## Aliases

### `CHUNK_COMPRESSED`

```mojo
comptime CHUNK_COMPRESSED = UInt8(0)
```

### `CHUNK_UNCOMPRESSED`

```mojo
comptime CHUNK_UNCOMPRESSED = UInt8(1)
```

### `CHUNK_PADDING`

```mojo
comptime CHUNK_PADDING = UInt8(254)
```

### `CHUNK_STREAM_IDENTIFIER`

```mojo
comptime CHUNK_STREAM_IDENTIFIER = UInt8(255)
```
