Documentation
¶
Overview ¶
Package compression provides zero-cgo Go bindings to Apple's /usr/lib/libcompression.dylib via purego.
It supports buffer-level compression and decompression (Encode, Decode) for LZ4, LZ4Raw, LZFSE, Zlib, LZMA, and LZBitmap, as well as streaming io.WriteCloser and io.ReadCloser adapters (NewWriter, NewReader) for the stream-capable algorithms (LZ4, LZFSE, Zlib, LZMA).
Basic usage:
encoded, err := compression.Encode(compression.LZFSE, data)
if err != nil {
return err
}
decoded, err := compression.Decode(compression.LZFSE, encoded)
if err != nil {
return err
}
Example ¶
package main
import (
"bytes"
"fmt"
"github.com/tmc/apple/x/compression"
)
func main() {
src := bytes.Repeat([]byte("apple-libcompression-"), 64)
encoded, err := compression.Encode(compression.LZFSE, src)
if err != nil {
panic(err)
}
decoded, err := compression.Decode(compression.LZFSE, encoded)
if err != nil {
panic(err)
}
fmt.Printf("equal=%v compressed=%v\n", bytes.Equal(src, decoded), len(encoded) < len(src))
}
Output: equal=true compressed=true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupported reports that libcompression or the requested operation // (such as streaming with LZ4Raw or LZBitmap) is not supported. ErrUnsupported = errors.New("compression: unsupported") // ErrInvalidAlgorithm reports an unrecognized [Algorithm] value. ErrInvalidAlgorithm = errors.New("compression: invalid algorithm") // ErrCorrupt reports malformed, truncated, or un-decodable compressed input. ErrCorrupt = errors.New("compression: corrupt input") // ErrClosed reports an operation attempted on a closed [Writer] or [Reader]. ErrClosed = errors.New("compression: closed") // ErrFailed reports an internal libcompression encoding or decoding failure. ErrFailed = errors.New("compression: operation failed") )
Functions ¶
Types ¶
type Algorithm ¶
type Algorithm uint32
Algorithm identifies a libcompression codec.
const ( // LZ4 is Apple's framed LZ4 format (high speed, moderate ratio). LZ4 Algorithm = 0x100 // LZ4Raw is unframed raw LZ4 (buffer API only). LZ4Raw Algorithm = 0x101 // Zlib is raw DEFLATE (RFC 1951, level 5 encoder). Zlib Algorithm = 0x205 // LZMA is LZMA (level 6 encoder, high ratio). LZMA Algorithm = 0x306 // LZBitmap is Apple's LZBitmap codec (buffer API only, macOS 12+). LZBitmap Algorithm = 0x702 // LZFSE is Apple's LZFSE codec (balanced speed and ratio). LZFSE Algorithm = 0x801 )
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader decompresses data read from an underlying io.Reader. Callers must call Reader.Close to release native libcompression stream state.
The zero value is not usable; create a Reader with NewReader.
func NewReader ¶
NewReader returns a streaming decompressor reading from r using alg. Only stream-capable algorithms (LZ4, LZFSE, Zlib, LZMA) are supported; LZ4Raw and LZBitmap return an error wrapping ErrUnsupported.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer compresses data written to it and writes the compressed stream to an underlying io.Writer. Callers must call Writer.Close to flush the final frame and release native libcompression stream state.
The zero value is not usable; create a Writer with NewWriter.
func NewWriter ¶
NewWriter returns a streaming compressor writing to w using alg. Only stream-capable algorithms (LZ4, LZFSE, Zlib, LZMA) are supported; LZ4Raw and LZBitmap return an error wrapping ErrUnsupported.
Example ¶
package main
import (
"bytes"
"fmt"
"io"
"github.com/tmc/apple/x/compression"
)
func main() {
var buf bytes.Buffer
w, err := compression.NewWriter(&buf, compression.LZ4)
if err != nil {
panic(err)
}
if _, err := io.WriteString(w, "streaming lz4 payload"); err != nil {
panic(err)
}
if err := w.Close(); err != nil {
panic(err)
}
r, err := compression.NewReader(&buf, compression.LZ4)
if err != nil {
panic(err)
}
defer r.Close()
out, err := io.ReadAll(r)
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
Output: streaming lz4 payload