lzfse

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

lzfse-go

Go bindings for lzfse (Apple's LZFSE compression) that require no cgo. The native code ships as a small shared library loaded and called through purego, so binaries build and run with CGO_ENABLED=0 — including fully static binaries on Linux.

The API mirrors blacktop/lzfse-cgo:

package main

import (
	"log"
	"os"

	"github.com/bitxeno/lzfse-go"
)

func main() {
	src, _ := os.ReadFile("file.raw")

	enc, err := lzfse.EncodeBuffer(src) // compress
	if err != nil {
		log.Fatal(err)
	}
	dec, err := lzfse.DecodeBuffer(enc) // decompress
	if err != nil {
		log.Fatal(err)
	}
	_ = dec
}
CGO_ENABLED=0 go build ./...   # works out of the box

How it works

The C sources of lzfse (plus libzbitmap, as vendored by lzfse-cgo) are compiled into a tiny shared library per platform (396 KB in total, prebuilt ones are committed under libs/). The libraries are embedded in the Go package with go:embed; on first use the copy for the current platform is extracted to the user cache directory and loaded with purego.Dlopen, and all calls go through purego.RegisterFunc. No cgo is involved anywhere: the Go compiler never sees C code, so the package builds with CGO_ENABLED=0.

Rebuild the embedded libraries after changing anything under native/:

make build-libs        # host platform only, uses the system C compiler
make build-libs-all    # all six targets below, cross-compiled with zig

Supported without cgo: darwin/amd64, darwin/arm64, linux/amd64, linux/arm64, windows/amd64, windows/arm64 (the same targets mygo's sqlite plugin ships). On other platforms the package compiles but returns ErrUnsupported.

Library lookup

Package-level functions load the native library on first use:

  1. $LZFSE_LIBRARY_PATH, when set, or the path given to SetLibraryPath;
  2. otherwise the embedded copy for the current platform, extracted to the user cache directory (~/Library/Caches/lzfse-go/<version>, $XDG_CACHE_HOME/lzfse-go/<version>, or %LocalAppData%\lzfse-go\<version>).

SetLibraryPath/LZFSE_LIBRARY_PATH accept either a shared library file or a path as produced by go run ./internal/libbuild -out <dir> -all.

For full control, load a library explicitly and use its methods — useful to pin the location in embedded or multi-tenant processes:

lib, err := lzfse.NewLibrary("/path/to/liblzfse-go.dylib")
if err != nil {
	log.Fatal(err)
}
dec, err := lib.DecodeBuffer(enc)

API

The package offers the same surface as lzfse-cgo, plus the LZBITMAP container used by lzfse-cgo's libzbitmap:

Function Purpose
EncodeBuffer(src) ([]byte, error) Compress with LZFSE into a new buffer.
EncodeBufferInto(src, dst) (int, error) Compress into dst; ErrShortBuffer when it cannot fit.
DecodeBuffer(src) ([]byte, error) Decompress into a right-sized new buffer.
DecodeBufferInto(src, dst) (int, error) Decompress into dst; ErrShortBuffer when it cannot fit.
EncodeLZVNBuffer(src, dst) (int, error) Compress with the LZVN block format.
DecodeLZVNBuffer(src, dst) (int, error) Decompress LZVN data.
EncodeZBMBuffer(src, dst) (int, error) Compress with the LZBITMAP container format.
DecodeZBMBuffer(src, dst) (int, error) Decompress LZBITMAP data.
EncodeScratchSize() / DecodeScratchSize() Scratch buffer sizes, for allocation control.

EncodeBuffer/DecodeBuffer allocate; the *Into variants write into caller-provided buffers and return ErrShortBuffer when it does not fit. For reusable buffers see DecodeBufferInto with a sync.Pool.

All functions are safe for concurrent use by multiple goroutines.

Notes on fidelity

  • The vendored C code is unmodified upstream lzfse; golden vectors from lzfse-cgo's test suite (testdata/enc.bin, testdata/dec.bin) are reproduced byte for byte, and the upstream CLI produces identical output for the same input.
  • Unlike cgo builds, the shared library is loaded at run time: keep the embedded copy working (the default) or ship it alongside the binary and point LZFSE_LIBRARY_PATH at it.

Development

make test          # CGO_ENABLED=0 go test ./...
make vet           # CGO_ENABLED=0 go vet ./...
make build-libs    # rebuild the embedded library for this platform

internal/libbuild compiles native/ into libs/<goos>-<goarch>/, with zig as the cross compiler for -all (the same approach mygo uses for its SQLite plugin) and the host C compiler for -host.

Credit

License

MIT — see LICENSE. The vendored C sources keep their upstream licenses (native/lzfse/LICENSE).

Documentation

Overview

Package lzfse provides Go bindings for the lzfse compression library (https://github.com/lzfse/lzfse) that require no cgo.

The native code ships as a small shared library that this package loads and calls through github.com/ebitengine/purego, so binaries build and run with CGO_ENABLED=0. By default the package uses an embedded copy of the shared library for the current platform, extracted to the user cache directory on first use. SetLibraryPath or the LZFSE_LIBRARY_PATH environment variable can point at a different shared library instead.

The API mirrors github.com/blacktop/lzfse-cgo:

out, err := lzfse.EncodeBuffer(src)
src2, err := lzfse.DecodeBuffer(out)

All functions are safe for concurrent use.

Index

Constants

View Source
const Version = "1.0.0"

Version of the vendored lzfse C sources.

Variables

View Source
var (
	// ErrShortBuffer is returned when the destination buffer cannot hold the
	// complete result. Compress with a buffer of at least
	// cap(src)+cap(src)/2+1024 bytes (what EncodeBuffer allocates), and
	// decompress into a buffer at least as large as the original input.
	ErrShortBuffer = errors.New("lzfse: destination buffer is too short")

	// ErrDecompress is returned when the native decoder rejects the input.
	ErrDecompress = errors.New("lzfse: decompression failed")
)
View Source
var ErrUnsupported = errors.New("lzfse: unsupported platform")

ErrUnsupported is returned on platforms where the package cannot call the native library.

Functions

func DecodeBuffer

func DecodeBuffer(src []byte) ([]byte, error)

DecodeBuffer decompresses LZFSE-encoded src into a newly allocated buffer, returning the decompressed bytes.

func DecodeBufferInto

func DecodeBufferInto(src, dst []byte) (int, error)

DecodeBufferInto decompresses LZFSE-encoded src into dst, returning the number of bytes written. It returns ErrShortBuffer if dst is too small.

func DecodeLZVNBuffer

func DecodeLZVNBuffer(src, dst []byte) (int, error)

DecodeLZVNBuffer decompresses LZVN-encoded src into dst, returning the number of bytes written, or 0 on error or if dst is too small.

func DecodeScratchSize

func DecodeScratchSize() (int, error)

DecodeScratchSize returns the required scratch buffer size to decompress using LZFSE.

func DecodeZBMBuffer

func DecodeZBMBuffer(src, dst []byte) (int, error)

DecodeZBMBuffer decompresses LZBITMAP-encoded src into dst, returning the number of bytes written, or 0 on error or if dst is too small.

func EncodeBuffer

func EncodeBuffer(src []byte) ([]byte, error)

EncodeBuffer compresses src using LZFSE into a newly allocated buffer, returning the compressed bytes.

func EncodeBufferInto

func EncodeBufferInto(src, dst []byte) (int, error)

EncodeBufferInto compresses src into dst and returns the number of bytes written. It returns ErrShortBuffer if dst is too small.

func EncodeLZVNBuffer

func EncodeLZVNBuffer(src, dst []byte) (int, error)

EncodeLZVNBuffer compresses src using the LZVN block format into dst, returning the number of bytes written, or 0 if dst is too small.

func EncodeScratchSize

func EncodeScratchSize() (int, error)

EncodeScratchSize returns the required scratch buffer size to compress using LZFSE.

func EncodeZBMBuffer

func EncodeZBMBuffer(src, dst []byte) (int, error)

EncodeZBMBuffer compresses src using the LZBITMAP container format into dst, returning the number of bytes written, or 0 on error or if dst is too small.

func SetLibraryPath

func SetLibraryPath(path string) error

SetLibraryPath sets the file path of the native lzfse library used by the package-level functions, bypassing the default search. It must be called before the first compression or decompression call.

Types

type Library

type Library struct {
	// contains filtered or unexported fields
}

Library is a loaded native lzfse library. The default Library is loaded lazily on first use; all package-level functions use it.

func DefaultLibrary

func DefaultLibrary() (*Library, error)

DefaultLibrary returns the process-wide library used by the package-level functions, loading it on first use. The location is decided by SetLibraryPath, then the search described in the package docs.

func NewLibrary

func NewLibrary(path string) (*Library, error)

NewLibrary loads the native lzfse library from path. The returned library stays loaded for the lifetime of the process.

func (*Library) DecodeBuffer

func (lib *Library) DecodeBuffer(src []byte) ([]byte, error)

DecodeBuffer decompresses LZFSE-encoded src into a newly allocated buffer, returning the decompressed bytes.

func (*Library) DecodeBufferInto

func (lib *Library) DecodeBufferInto(src, dst []byte) (int, error)

DecodeBufferInto decompresses LZFSE-encoded src into dst, returning the number of bytes written. It returns ErrShortBuffer if dst is too small.

func (*Library) DecodeLZVNBuffer

func (lib *Library) DecodeLZVNBuffer(src, dst []byte) int

DecodeLZVNBuffer decompresses LZVN-encoded src into dst, returning the number of bytes written, or 0 on error or if dst is too small.

func (*Library) DecodeScratchSize

func (lib *Library) DecodeScratchSize() int

DecodeScratchSize returns the required scratch buffer size to decompress using LZFSE. It is lzfse_decode_scratch_size.

func (*Library) DecodeZBMBuffer

func (lib *Library) DecodeZBMBuffer(src, dst []byte) int

DecodeZBMBuffer decompresses LZBITMAP-encoded src into dst, returning the number of bytes written, or 0 on error or if dst is too small.

func (*Library) EncodeBuffer

func (lib *Library) EncodeBuffer(src []byte) ([]byte, error)

EncodeBuffer compresses src using LZFSE into a newly allocated buffer, returning the compressed bytes.

func (*Library) EncodeBufferInto

func (lib *Library) EncodeBufferInto(src, dst []byte) (int, error)

EncodeBufferInto compresses src into dst and returns the number of bytes written. It returns ErrShortBuffer if dst is too small. The encoder adapts its block layout to the space available, so a slightly undersized dst may still yield a valid (different) encoding; only when the output cannot fit at all does it fail.

func (*Library) EncodeLZVNBuffer

func (lib *Library) EncodeLZVNBuffer(src, dst []byte) int

EncodeLZVNBuffer compresses src using the LZVN block format into dst, returning the number of bytes written, or 0 if dst is too small.

func (*Library) EncodeScratchSize

func (lib *Library) EncodeScratchSize() int

EncodeScratchSize returns the required scratch buffer size to compress using LZFSE. It is lzfse_encode_scratch_size.

func (*Library) EncodeZBMBuffer

func (lib *Library) EncodeZBMBuffer(src, dst []byte) int

EncodeZBMBuffer compresses src using the LZBITMAP container format into dst, returning the number of bytes written, or 0 on error or if dst is too small.

func (*Library) LZVNEncodeScratchSize

func (lib *Library) LZVNEncodeScratchSize() int

LZVNEncodeScratchSize returns the scratch size required by EncodeLZVNBuffer.

Directories

Path Synopsis
examples
demo command
Command demo shows the lzfse package: it compresses and decompresses a file with LZFSE and prints the sizes.
Command demo shows the lzfse package: it compresses and decompresses a file with LZFSE and prints the sizes.
internal
libbuild command
Command libbuild builds the native lzfse shared libraries that this package embeds under libs/, using the vendored C sources in native/.
Command libbuild builds the native lzfse shared libraries that this package embeds under libs/, using the vendored C sources in native/.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址