From 3769c54b3819d2ae8dccce5c93c8e3c88801ecfb Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Fri, 4 Feb 2022 00:04:22 -0800 Subject: [PATCH] Add: squish --- .gitignore | 1 + squish/squish.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 .gitignore create mode 100644 squish/squish.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/squish/squish.go b/squish/squish.go new file mode 100644 index 0000000..299b6ce --- /dev/null +++ b/squish/squish.go @@ -0,0 +1,54 @@ +package squish + +import ( + "bytes" + "compress/gzip" + "encoding/base64" + "io" +) + +// Gzip compresses as slice of bytes using gzip compression. +func Gzip(data []byte) ([]byte, error) { + var b bytes.Buffer + gz := gzip.NewWriter(&b) + if _, err := gz.Write(data); err != nil { + return data, err + } + if err := gz.Close(); err != nil { + return data, err + } + return b.Bytes(), nil +} + + +// Gunzip decompresses a gzip compressed slice of bytes. +func Gunzip(data []byte) ([]byte, error) { + gz, err := gzip.NewReader(bytes.NewReader(data)) + if err != nil { + return nil, err + } + out, err := io.ReadAll(gz) + if err != nil { + return nil, err + } + return out, err +} + +// B64e encodes the given slice of bytes into base64 standard encoding. +func B64e(cytes []byte) (data string) { + data = base64.StdEncoding.EncodeToString(cytes) + return +} + +// B64d decodes the given string into the original slice of bytes. +// Do note that this is for non critical tasks, it has no error handling for purposes of clean code. +func B64d(str string) (data []byte) { + data, _ = base64.StdEncoding.DecodeString(str) + return data +} + +// UnpackStr UNsafely unpacks (usually banners) that have been base64'd and then gzip'd. +func UnpackStr(encoded string) string { + dcytes, _ := Gunzip(B64d(encoded)) + return string(dcytes) +}