Skip to content

Instantly share code, notes, and snippets.

@michal-kapala
Created August 4, 2023 14:43
Show Gist options
  • Save michal-kapala/41cdb44787dcad23d009c92150b7fab7 to your computer and use it in GitHub Desktop.
Save michal-kapala/41cdb44787dcad23d009c92150b7fab7 to your computer and use it in GitHub Desktop.
LZSS compression example
package main
import (
"bytes"
"fmt"
"github.com/bovarysme/lzss"
)
func compressAndDisplay(name string, input []byte) {
buffer := new(bytes.Buffer)
writer := lzss.NewWriter(buffer)
fmt.Println(fmt.Sprintf("%s:", name))
fmt.Println(fmt.Sprintf("%x", input))
writer.Write(input)
writer.Close()
// get rid of the null-termination byte
hex := buffer.Bytes()
hex = hex[0 : len(hex)-1]
fmt.Println(fmt.Sprintf("%s compressed:", name))
fmt.Println(fmt.Sprintf("%x", hex))
}
func main() {
input := []byte{0x00, 0x0A, 0x7C, 0x8D, 0x00, 0x00, 0x00, 0x03, 0x00, 0x5B}
compressAndDisplay("1st part", input)
input = []byte{0x00, 0x5C, 0x00, 0x5D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xE0}
compressAndDisplay("2nd part", input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment