libdeflate

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 3 Imported by: 11

Documentation

Index

Constants

View Source
const (
	MinCompressionLevel        = native.MinCompressionLevel
	MaxStdZlibCompressionLevel = native.MaxStdZlibCompressionLevel
	MaxCompressionLevel        = native.MaxCompressionLevel
	DefaultCompressionLevel    = native.DefaultCompressionLevel
)

These constants specify several special compression levels

Variables

This section is empty.

Functions

func Adler32

func Adler32(adler32 uint32, in []byte) uint32

Adler32 updates the running adler32 checksum by the contents of the slice in. This function returns the updated checksum. A new adler32-checksum requires 1 as initial value. This value is also returned if in == nil.

func Compress

func Compress(in, out []byte, m Mode) (int, []byte, error)

Compress compresses the data from in to out and returns the number of bytes written to out, out (sliced to written) or an error if the out buffer was too short. If you pass nil for out, this function will allocate a fitting buffer and return it (not preferred though).

m specifies which compression format should be used (e.g. ModeZlib). Uses default compression level.

IF YOU WANT TO COMPRESS MORE THAN ONCE, PLEASE REFER TO NewCompressor(), as this function creates a new Compressor (alloc 32KiB) which is then closed at the end of the function.

Notice that for extremely small or already highly compressed data, the compressed data could be larger than uncompressed. If out == nil: For a too large discrepancy (len(out) > 1000 + 2 * len(in)) Compress will error

func CompressLevel

func CompressLevel(in, out []byte, m Mode, level int) (int, []byte, error)

CompressLevel compresses the data from in to out using the specified compression level and returns the number of bytes written to out, out (sliced to written) or an error if the out buffer was too short. If you pass nil for out, this function will allocate a fitting buffer and return it (not preferred though).

m specifies which compression format should be used (e.g. ModeZlib). Level defines the compression level.

IF YOU WANT TO COMPRESS MORE THAN ONCE, PLEASE REFER TO NewCompressorLevel(), as this function creates a new Compressor (alloc 32KiB) which is then closed at the end of the function.

Notice that for extremely small or already highly compressed data, the compressed data could be larger than uncompressed. If out == nil: For a too large discrepancy (len(out) > 1000 + 2 * len(in)) Compress will error

func CompressZlib

func CompressZlib(in, out []byte) (int, []byte, error)

CompressZlib compresses the data from in to out (in zlib format) and returns the number of bytes written to out, out (sliced to written) or an error if the out buffer was too short. If you pass nil for out, this function will allocate a fitting buffer and return it (not preferred though).

IF YOU WANT TO COMPRESS MORE THAN ONCE, PLEASE REFER TO NewCompressor(), as this function creates a new Compressor (alloc 32KiB) which is then closed at the end of the function.

See Compress for further information.

func CompressZlibLevel

func CompressZlibLevel(in, out []byte, level int) (int, []byte, error)

CompressZlibLevel compresses the data from in to out (in zlib format) at the specified level and returns the number of bytes written to out, out (sliced to written) or an error if the out buffer was too short. If you pass nil for out, this function will allocate a fitting buffer and return it (not preferred though).

IF YOU WANT TO COMPRESS MORE THAN ONCE, PLEASE REFER TO NewCompressorLevel(), as this function creates a new Compressor (alloc 32KiB) which is then closed at the end of the function.

See CompressLevel for further information.

func Crc32

func Crc32(crc32 uint32, in []byte) uint32

Crc32 updates the running crc32 checksum by the contents of the slice in. This function returns the updated checksum. A new crc32-checksum requires 0 as initial value. This value is also returned if in == nil.

func Decompress

func Decompress(in, out []byte, m Mode) (int, []byte, error)

Decompress decompresses the given data from in to out and returns the number of consumed bytes c from 'in' and 'out' or an error if something went wrong. Mode m specifies the format (e.g. zlib) of the data within in.

c is the number of bytes that were read before the BFINAL flag was encountered, which indicates the end of the compressed data.

If you pass a buffer to out, the size of this buffer must exactly match the length of the decompressed data. If you pass nil to out, this function will allocate a sufficient buffer and return it.

IF YOU WANT TO DECOMPRESS MORE THAN ONCE, PLEASE REFER TO NewDecompressor(), as this function creates a new Decompressor (alloc 32KiB) which is then closed at the end of the function.

If error != nil, the data in out is undefined.

func DecompressZlib

func DecompressZlib(in, out []byte) (int, []byte, error)

DecompressZlib decompresses the given zlib data from in to out and returns the number of consumed bytes c from 'in' and 'out' or an error if something went wrong.

c is the number of bytes that were read before the BFINAL flag was encountered, which indicates the end of the compressed data.

If you pass a buffer to out, the size of this buffer must exactly match the length of the decompressed data. If you pass nil to out, this function will allocate a sufficient buffer and return it.

IF YOU WANT TO DECOMPRESS MORE THAN ONCE, PLEASE REFER TO NewDecompressor(), as this function creates a new Decompressor (alloc 32KiB) which is then closed at the end of the function.

If error != nil, the data in out is undefined.

Types

type Compressor

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

Compressor compresses data at the specified compression level.

A single compressor must not not be used across multiple threads concurrently. If you want to compress concurrently, create a compressor for each thread.

Always Close() the decompressor to free c memory or use the AutoClose constructors, which will automatically close the Compressor at garabage collection time of the underlying natvie equivalent. One Compressor allocates at least 32 KiB.

func NewCompressor

func NewCompressor() (Compressor, error)

NewCompressor returns a new Compressor used to compress data with compression level DefaultCompressionLevel. Errors if out of memory. Allocates 32KiB. See NewCompressorLevel for custom compression level

func NewCompressorAutoClose added in v2.2.0

func NewCompressorAutoClose() (Compressor, error)

NewCompressorAutoClose returns a Compressor used to compress data with compression level DefaultCompressionLevel and AutoClose functionality. This will close the Compressor automatically, when it is no longer used. Errors if out of memory. Allocates 32KiB. See NewCompressorLevelPointer for custom compression level

func NewCompressorLevel

func NewCompressorLevel(level int) (Compressor, error)

NewCompressorLevel returns a new Compressor used to compress data. Errors if out of memory or if an invalid compression level was passed. Allocates 32KiB.

The compression level is legal if and only if: MinCompressionLevel <= level <= MaxCompressionLevel

func NewCompressorLevelAutoClose added in v2.2.0

func NewCompressorLevelAutoClose(level int) (Compressor, error)

NewCompressorLevelAutoClose returns a new pointer to a Compressor used to compress data and AutoClose functionality. This will close the Compressor automatically, when it is no longer used. Errors if out of memory or if an invalid compression level was passed. Allocates 32KiB.

The compression level is legal if and only if: MinCompressionLevel <= level <= MaxCompressionLevel

func (Compressor) Close

func (c Compressor) Close()

Close closes the compressor and releases all occupied resources. It is the users responsibility to close compressors in order to free resources, as the underlying c objects are not subject to the go garbage collector. They have to be freed manually.

After closing, the compressor must not be used anymore, as the methods will panic (except for the c.Level() method).

func (Compressor) Compress

func (c Compressor) Compress(in, out []byte, m Mode) (int, []byte, error)

Compress compresses the data from in to out and returns the number of bytes written to out, out (sliced to written) or an error if the out buffer was too short. If you pass nil for out, this function will allocate a fitting buffer and return it (not preferred though).

m specifies which compression format should be used (e.g. ModeZlib)

Notice that for extremely small or already highly compressed data, the compressed data could be larger than uncompressed. If out == nil: For a too large discrepancy (len(out) > 1000 + 2 * len(in)) Compress will error

func (Compressor) CompressZlib

func (c Compressor) CompressZlib(in, out []byte) (int, []byte, error)

CompressZlib compresses the data from in to out (in zlib format) and returns the number of bytes written to out, out (sliced to written) or an error if the out buffer was too short. If you pass nil for out, this function will allocate a fitting buffer and return it (not preferred though).

See c.Compress for further information.

func (Compressor) Level

func (c Compressor) Level() int

Level returns the compression level at which this Compressor compresses. May be called after having closed a Compressor.

func (Compressor) WorstCaseCompressedSize

func (c Compressor) WorstCaseCompressedSize(size int, m Mode) (max int)

WorstCaseCompressedSize returns the maximum theoretical size of the data after compressing data of length 'size', using the given mode of compression. This prediction is a wild overestimate in most cases, for which holds true: max >= size. However, it gives a hard maximal bound of the size of compressed data, compressing with the given mode at the compression level of the this compressor, independent of the actual data. This method will always return the same max size for the same compressor, input size and mode.

type Decompressor

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

Decompressor decompresses any DEFLATE, zlib or gzip compressed data at any level

A single decompressor must not not be used across multiple threads concurrently. If you want to decompress concurrently, create a decompressor for each thread.

Always Close() the decompressor to free c memory or use the AutoClose constructors, which will automatically close the Compressor at garabage collection time of the underlying natvie equivalent. One Decompressor allocates at least 32KiB.

func NewDecompressor

func NewDecompressor() (Decompressor, error)

NewDecompressor returns a new Decompressor used to decompress data at any compression level and with any Mode. Errors if out of memory. Allocates 32KiB.

func NewDecompressorAutoClose added in v2.2.0

func NewDecompressorAutoClose() (Decompressor, error)

NewDecompressor returns a new Decompressor used to decompress data at any compression level and with any Mode. It has AutoClose functionality. This will close the Decompressor automatically, when it is no longer used. Errors if out of memory. Allocates 32KiB.

func NewDecompressorAutoCloseWithExtendedDecompression added in v2.2.0

func NewDecompressorAutoCloseWithExtendedDecompression(maxDecompressionFactor int) (Decompressor, error)

NewDecompressorWithExtendedDecompression returns a new Decompressor used to decompress data at any compression level and with any Mode. It has AutoClose functionality, which will close the Decompressor automatically, when it is no longer used. maxDecompressionFactor customizes how much larger your output than your input may be. This is set to a sensible default, however, it might need some tweaking if you have a huge compression factor. Usually, NewDecompressor should suffice. Errors if out of memory. Allocates 32KiB.

func NewDecompressorWithExtendedDecompression added in v2.0.3

func NewDecompressorWithExtendedDecompression(maxDecompressionFactor int) (Decompressor, error)

NewDecompressorWithExtendedDecompression returns a new Decompressor used to decompress data at any compression level and with any Mode. maxDecompressionFactor customizes how much larger your output than your input may be. This is set to a sensible default, however, it might need some tweaking if you have a huge compression factor. Usually, NewDecompressor should suffice. Errors if out of memory. Allocates 32KiB.

func (Decompressor) Close

func (dc Decompressor) Close()

Close closes the decompressor and releases all occupied resources. It is the users responsibility to close decompressors in order to free resources, as the underlying c objects are not subject to the go garbage collector. They have to be freed manually.

After closing, the decompressor must not be used anymore, as the methods will panic.

func (Decompressor) Decompress

func (dc Decompressor) Decompress(in, out []byte, m Mode) (int, []byte, error)

Decompress decompresses the given data from in to out and returns the number of consumed bytes c from 'in' and 'out' or an error if something went wrong. Mode m specifies the format (e.g. zlib) of the data within in.

c is the number of bytes that were read before the BFINAL flag was encountered, which indicates the end of the compressed data.

If you pass a buffer to out, the size of this buffer must exactly match the length of the decompressed data. If you pass nil to out, this function will allocate a sufficient buffer and return it.

If error != nil, the data in out is undefined.

func (Decompressor) DecompressZlib

func (dc Decompressor) DecompressZlib(in, out []byte) (int, []byte, error)

DecompressZlib decompresses the given zlib data from in to out and returns the number of consumed bytes c from 'in' and 'out' or an error if something went wrong.

c is the number of bytes that were read before the BFINAL flag was encountered, which indicates the end of the compressed data.

If you pass a buffer to out, the size of this buffer must exactly match the length of the decompressed data. If you pass nil to out, this function will allocate a sufficient buffer and return it.

If error != nil, the data in out is undefined.

type Mode

type Mode int

Mode specifies the type of compression/decompression such as zlib, gzip and raw DEFLATE

const (
	ModeDEFLATE Mode = iota
	ModeZlib
	ModeGzip
)

The constants that specify a certain mode of compression/decompression

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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