compress
Byte buffers, compression methods from RLE through DEFLATE, gzip and checksums.
import compress
This package is the base of all binary data work in Salam: ByteBuf and ByteReader are what packages such as encoding/binary, archive and encoding/msgpack pass around.
For compression, reach for gzip when the data has to be read by other programs. The other methods are mostly there to learn from and experiment with, and their output format belongs to this package alone. The examples show that for small data the overhead of Huffman and DEFLATE comes to more than the data itself.
Types
Method
enum Method| Name | Type | Description |
|---|---|---|
None | | No compression. |
RLE | | Run length encoding. |
Huffman | | Huffman. |
LZ77 | | LZ77. |
LZW | | LZW. |
Deflate | | LZ77 and Huffman. |
BWT | | Burrows Wheeler. |
import compress
func main:
text := "ababababab hello hello hello hello hello hello hello hello"
b := compress.BufFromStr(text)
each method in [compress.MethodNone, compress.MethodRLE, compress.MethodHuffman, compress.MethodLZ77, compress.MethodLZW, compress.MethodDeflate, compress.MethodBWT]:
mut c := compress.CompressBuf(method, b)
mut back := compress.DecompressBuf(method, c)
println method, compress.BufLen(c), compress.BufEquals(b, back)
compress.BufFree(c)
compress.BufFree(back)
end
mut best := 0
c := compress.CompressBest(b.data, b.len, best)
println best, compress.Ratio(c.len, b.len) < 1.0
s := compress.CompressStr(compress.MethodDeflate, "aaaaaaaaaa")
b3 := compress.Decompress(compress.MethodDeflate, s.data, s.len)
println compress.BufToStrUnsafe(b3)
b4 := compress.Compress(compress.MethodRLE, b.data, b.len)
println compress.BufLen(b4) > 0, compress.Method.Huffman
println compress.BufLen(compress.RLEDecompress(compress.RLECompress(b.data, b.len).data, compress.RLECompress(b.data, b.len).len)) == b.len
h := compress.HuffmanCompress(b.data, b.len)
println compress.BufEquals(compress.HuffmanDecompress(h.data, h.len), b)
l := compress.LZ77Compress(b.data, b.len)
println compress.BufEquals(compress.LZ77Decompress(l.data, l.len), b)
w := compress.LZWCompress(b.data, b.len)
println compress.BufEquals(compress.LZWDecompress(w.data, w.len), b)
bw := compress.BWTCompress(b.data, b.len)
println compress.BufEquals(compress.BWTDecompress(bw.data, bw.len), b)
mut primary := 0
t := compress.BWTTransform(b.data, 3, primary)
println primary, compress.BufToStrUnsafe(compress.BWTInverse(t.data, t.len, primary)) == "aba"
m := compress.MTFEncode(b)
println compress.BufEquals(compress.MTFDecode(m), b)
endByteBuf
struct ByteBuf| Name | Type | Description |
|---|---|---|
data | void* | The memory address of the bytes. |
len | int = 0 | The number of bytes. |
cap | int = 0 | The capacity. |
import compress
func main:
mut b := compress.NewByteBufCap(16)
compress.BufPush(b, 72)
compress.BufPush(b, 105)
mut other := compress.BufFromStr("!!")
compress.BufPushBuf(b, other)
compress.BufPushBytes(b, other.data, 1)
compress.BufPushU32(b, 10)
println compress.BufLen(b), compress.BufGet(b, 0), compress.BufToBase64(b)
compress.BufSet(b, 0, 104)
c := compress.BufClone(b)
println compress.BufEquals(b, c), compress.BufToStrUnsafe(compress.BufFromStr("hello"))
mut r := compress.ReaderFromBuf(b)
println compress.RdLeft(r), compress.RdByte(r), compress.RdByte(r)
three := compress.RdBytes(r, 3)
println compress.BufLen(three), compress.RdU32(r), compress.RdLeft(r), r.bad
_ := compress.RdByte(r)
println r.bad
mut r2 := compress.NewByteReader(c.data, 2)
println compress.RdByte(r2)
compress.BufReset(b)
println compress.BufLen(b)
compress.BufFree(b)
compress.BufFree(c)
compress.BufFree(other)
endByteReader
struct ByteReader| Name | Type | Description |
|---|---|---|
data | void* | The address of the data. |
len | int = 0 | The length of the data. |
pos | int = 0 | The current position. |
bad | bool = false | A read went past the end of the data. |
The example for this item is shown with the ones above: compress-Buffer.salam
BitWriter
struct BitWriter| Name | Type | Description |
|---|---|---|
buf | ByteBuf | The bytes written. |
acc | int = 0 | The bits waiting. |
nbits | int = 0 | The number of bits waiting. |
import compress
func main:
mut w := compress.NewBitWriter()
compress.BitWriteBit(w, 1)
compress.BitWriteBits(w, 5, 3)
compress.BitWriteBits(w, 255, 8)
compress.BitFlush(w)
println compress.BitWriterLen(w)
mut r := compress.BitReaderFromBuf(w.buf)
println compress.BitReadBit(r), compress.BitReadBits(r, 3), compress.BitReadBits(r, 8)
mut r2 := compress.NewBitReader(w.buf.data, 1)
println compress.BitReadBits(r2, 4)
compress.BitWriterFree(w)
endBitReader
struct BitReader| Name | Type | Description |
|---|---|---|
data | void* | The data. |
len | int = 0 | The length of the data. |
pos | int = 0 | The current byte position. |
acc | int = 0 | The bits read. |
nbits | int = 0 | The number of bits ready. |
bad | bool = false | A read went past the end of the data. |
The example for this item is shown with the ones above: compress-Bits.salam
Constants and variables
Method numbers Method…
Method enum cases as plain numbers, for Compress and Decompress.| Name | Value |
|---|---|
MethodNone | 0 |
MethodRLE | 1 |
MethodHuffman | 2 |
MethodLZ77 | 3 |
MethodLZW | 4 |
MethodDeflate | 5 |
MethodBWT | 6 |
The example for this item is shown with the ones above: compress-Methods.salam
Functions
LZWCompress
func LZWCompress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
LZWDecompress
func LZWDecompress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
RLECompress
func RLECompress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
RLEDecompress
func RLEDecompress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
GzipCompress
func GzipCompress(p: void*, n: int): ByteBuf.gz files.import compress
func main:
text := "hello gzip! hello gzip! hello gzip!"
g := compress.GzipCompressStr(text)
println compress.BufGet(g, 0), compress.BufGet(g, 1)
mut ok := false
b := compress.GzipDecompressBuf(g, ok)
println ok, compress.BufToStrUnsafe(b) == text
b2 := compress.BufFromStr(text)
g2 := compress.GzipCompressBuf(b2)
g3 := compress.GzipCompress(b2.data, b2.len)
println compress.BufEquals(g2, g3)
_ := compress.GzipDecompress(b2.data, b2.len, ok)
println ok
d := compress.DeflateCompress(b2.data, b2.len)
println compress.BufEquals(compress.DeflateDecompress(d.data, d.len), b2)
raw := compress.RawDeflate(b2.data, b2.len)
back := compress.RawInflate(raw.data, raw.len, b2.len, ok)
println ok, compress.BufEquals(back, b2)
endGzipDecompress
func GzipDecompress(p: void*, n: int, ok &: bool): ByteBufok becomes false when the data is not gzip. The checksum inside the file is not checked.The example for this item is shown with the ones above: compress-Gzip.salam
GzipCompressBuf
func GzipCompressBuf(b: ByteBuf): ByteBufThe example for this item is shown with the ones above: compress-Gzip.salam
GzipCompressStr
func GzipCompressStr(s: str): ByteBufThe example for this item is shown with the ones above: compress-Gzip.salam
GzipDecompressBuf
func GzipDecompressBuf(b: ByteBuf, ok &: bool): ByteBufThe example for this item is shown with the ones above: compress-Gzip.salam
RawInflate
func RawInflate(p: void*, n: int, expected_size: int, ok &: bool): ByteBufexpected_size is the rough size of the output.The example for this item is shown with the ones above: compress-Gzip.salam
NewBitWriter
func NewBitWriter(): BitWriterThe example for this item is shown with the ones above: compress-Bits.salam
BitWriteBit
func BitWriteBit(w &: BitWriter, bit: int)The example for this item is shown with the ones above: compress-Bits.salam
BitWriteBits
func BitWriteBits(w &: BitWriter, value: int, count: int)count bits of value.The example for this item is shown with the ones above: compress-Bits.salam
BitFlush
func BitFlush(w &: BitWriter)The example for this item is shown with the ones above: compress-Bits.salam
BitWriterLen
func BitWriterLen(w &: BitWriter): intThe example for this item is shown with the ones above: compress-Bits.salam
BitWriterFree
func BitWriterFree(w &: BitWriter)The example for this item is shown with the ones above: compress-Bits.salam
NewBitReader
func NewBitReader(p: void*, n: int): BitReaderThe example for this item is shown with the ones above: compress-Bits.salam
BitReaderFromBuf
func BitReaderFromBuf(b: ByteBuf): BitReaderThe example for this item is shown with the ones above: compress-Bits.salam
BitReadBit
func BitReadBit(r &: BitReader): intThe example for this item is shown with the ones above: compress-Bits.salam
BitReadBits
func BitReadBits(r &: BitReader, count: int): intcount bits.The example for this item is shown with the ones above: compress-Bits.salam
CRC32
func CRC32(p: void*, n: int): u32import compress
func main:
println compress.CRC32Str("123456789"), compress.Adler32Str("Wikipedia")
b := compress.BufFromStr("123456789")
println compress.CRC32(b.data, b.len) == compress.CRC32Buf(b)
println compress.Adler32(b.data, b.len) == compress.Adler32Buf(b)
println compress.CRC32Str("hello")
endCRC32Str
func CRC32Str(s: str): u32The example for this item is shown with the ones above: compress-Checksum.salam
CRC32Buf
func CRC32Buf(b: ByteBuf): u32The example for this item is shown with the ones above: compress-Checksum.salam
Adler32
func Adler32(p: void*, n: int): u32The example for this item is shown with the ones above: compress-Checksum.salam
Adler32Str
func Adler32Str(s: str): u32The example for this item is shown with the ones above: compress-Checksum.salam
Adler32Buf
func Adler32Buf(b: ByteBuf): u32The example for this item is shown with the ones above: compress-Checksum.salam
NewByteBuf
func NewByteBuf(): ByteBufThe example for this item is shown with the ones above: compress-Buffer.salam
NewByteBufCap
func NewByteBufCap(capacity: int): ByteBufThe example for this item is shown with the ones above: compress-Buffer.salam
BufPush
func BufPush(b &: ByteBuf, v: int)The example for this item is shown with the ones above: compress-Buffer.salam
BufPushBytes
func BufPushBytes(b &: ByteBuf, p: void*, n: int)n bytes from memory.The example for this item is shown with the ones above: compress-Buffer.salam
BufPushBuf
func BufPushBuf(b &: ByteBuf, src: ByteBuf)The example for this item is shown with the ones above: compress-Buffer.salam
BufPushU32
func BufPushU32(b &: ByteBuf, v: int)The example for this item is shown with the ones above: compress-Buffer.salam
BufGet
func BufGet(b: ByteBuf, i: int): inti.The example for this item is shown with the ones above: compress-Buffer.salam
BufSet
func BufSet(b &: ByteBuf, i: int, v: int)i.The example for this item is shown with the ones above: compress-Buffer.salam
BufLen
func BufLen(b: ByteBuf): intThe example for this item is shown with the ones above: compress-Buffer.salam
BufFree
func BufFree(b &: ByteBuf)The example for this item is shown with the ones above: compress-Buffer.salam
BufReset
func BufReset(b &: ByteBuf)The example for this item is shown with the ones above: compress-Buffer.salam
BufClone
func BufClone(b: ByteBuf): ByteBufThe example for this item is shown with the ones above: compress-Buffer.salam
BufEquals
func BufEquals(a: ByteBuf, b: ByteBuf): boolThe example for this item is shown with the ones above: compress-Buffer.salam
BufFromStr
func BufFromStr(s: str): ByteBufThe example for this item is shown with the ones above: compress-Buffer.salam
BufToStrUnsafe
func BufToStrUnsafe(b: ByteBuf): strThe example for this item is shown with the ones above: compress-Buffer.salam
BufToBase64
func BufToBase64(b: ByteBuf): strThe example for this item is shown with the ones above: compress-Buffer.salam
NewByteReader
func NewByteReader(p: void*, n: int): ByteReadern bytes of memory.The example for this item is shown with the ones above: compress-Buffer.salam
ReaderFromBuf
func ReaderFromBuf(b: ByteBuf): ByteReaderThe example for this item is shown with the ones above: compress-Buffer.salam
RdLeft
func RdLeft(r: ByteReader): intThe example for this item is shown with the ones above: compress-Buffer.salam
RdByte
func RdByte(r &: ByteReader): intbad becomes true.The example for this item is shown with the ones above: compress-Buffer.salam
RdU32
func RdU32(r &: ByteReader): intThe example for this item is shown with the ones above: compress-Buffer.salam
RdBytes
func RdBytes(r &: ByteReader, n: int): ByteBufn bytes into a new buffer.The example for this item is shown with the ones above: compress-Buffer.salam
DeflateCompress
func DeflateCompress(p: void*, n: int): ByteBufRawDeflate or gzip.The example for this item is shown with the ones above: compress-Gzip.salam
DeflateDecompress
func DeflateDecompress(p: void*, n: int): ByteBufDeflateCompress.The example for this item is shown with the ones above: compress-Gzip.salam
MTFEncode
func MTFEncode(inbuf: ByteBuf): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
MTFDecode
func MTFDecode(inbuf: ByteBuf): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
BWTTransform
func BWTTransform(p: void*, n: int, primary_out &: int): ByteBufprimary_out.The example for this item is shown with the ones above: compress-Methods.salam
BWTInverse
func BWTInverse(p: void*, n: int, primary: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
BWTCompress
func BWTCompress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
BWTDecompress
func BWTDecompress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
RawDeflate
func RawDeflate(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Gzip.salam
Compress
func Compress(method: int, p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
Decompress
func Decompress(method: int, p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
CompressStr
func CompressStr(method: int, s: str): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
CompressBuf
func CompressBuf(method: int, b: ByteBuf): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
DecompressBuf
func DecompressBuf(method: int, b: ByteBuf): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
CompressBest
func CompressBest(p: void*, n: int, method_out &: int): ByteBufmethod_out.The example for this item is shown with the ones above: compress-Methods.salam
Ratio
func Ratio(compressed_len: int, original_len: int): f64The example for this item is shown with the ones above: compress-Methods.salam
LZ77Compress
func LZ77Compress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
LZ77Decompress
func LZ77Decompress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
HuffmanCompress
func HuffmanCompress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam
HuffmanDecompress
func HuffmanDecompress(p: void*, n: int): ByteBufThe example for this item is shown with the ones above: compress-Methods.salam