From 8482d3c1035095c89d112c75bfcc2e4095b486bf Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sat, 17 Dec 2022 11:59:24 +0100 Subject: pkg/image: factor out from prog Move image compression-related function to a separate package. In preperation for subsequent changes that make decompression more complex. Prog package is already large and complex. Also makes running compression tests/benchmarks much faster. --- pkg/image/compression.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 pkg/image/compression.go (limited to 'pkg/image/compression.go') diff --git a/pkg/image/compression.go b/pkg/image/compression.go new file mode 100644 index 000000000..9878b460d --- /dev/null +++ b/pkg/image/compression.go @@ -0,0 +1,71 @@ +// Copyright 2022 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package image + +import ( + "bytes" + "compress/zlib" + "encoding/base64" + "fmt" + "io" +) + +func Compress(rawData []byte) []byte { + var buffer bytes.Buffer + zlibWriter := zlib.NewWriter(&buffer) + + _, err := zlibWriter.Write(rawData) + if err != nil { + panic(fmt.Sprintf("could not compress with zlib: %v", err)) + } + + err = zlibWriter.Close() + if err != nil { + panic(fmt.Sprintf("could not finalize compression with zlib: %v", err)) + } + + return buffer.Bytes() +} + +func Decompress(compressedData []byte) ([]byte, error) { + buf := new(bytes.Buffer) + err := DecompressWriter(buf, compressedData) + return buf.Bytes(), err +} + +func DecompressWriter(w io.Writer, compressedData []byte) error { + zlibReader, err := zlib.NewReader(bytes.NewReader(compressedData)) + if err != nil { + return fmt.Errorf("could not initialise zlib: %v", err) + } + + if _, err := io.Copy(w, zlibReader); err != nil { + return fmt.Errorf("could not read data with zlib: %v", err) + } + + return zlibReader.Close() +} + +func DecodeB64(b64Data []byte) ([]byte, error) { + decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(b64Data)) + rawData, err := io.ReadAll(decoder) + if err != nil { + return nil, fmt.Errorf("could not decode Base64: %v", err) + } + return rawData, nil +} + +func EncodeB64(rawData []byte) []byte { + var buf bytes.Buffer + encoder := base64.NewEncoder(base64.StdEncoding, &buf) + _, err := encoder.Write(rawData) + if err != nil { + panic(fmt.Sprintf("could not encode Base64: %v", err)) + } + err = encoder.Close() + if err != nil { + panic(fmt.Sprintf("could not finalize encoding to Base64: %v", err)) + } + return buf.Bytes() +} -- cgit mrf-deployment