github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/ioutils/utils.go (about)

     1  package ioutils
     2  
     3  import (
     4  	"bytes"
     5  	"compress/gzip"
     6  )
     7  
     8  // Note: []byte can never be const as they are inherently mutable
     9  var (
    10  	// magic bytes to identify gzip.
    11  	// See https://www.ietf.org/rfc/rfc1952.txt
    12  	// and https://github.com/golang/go/blob/master/src/net/http/sniff.go#L186
    13  	gzipIdent = []byte("\x1F\x8B\x08")
    14  
    15  	wasmIdent = []byte("\x00\x61\x73\x6D")
    16  )
    17  
    18  // IsGzip returns checks if the file contents are gzip compressed
    19  func IsGzip(input []byte) bool {
    20  	return bytes.Equal(input[:3], gzipIdent)
    21  }
    22  
    23  // IsWasm checks if the file contents are of wasm binary
    24  func IsWasm(input []byte) bool {
    25  	return bytes.Equal(input[:4], wasmIdent)
    26  }
    27  
    28  // GzipIt compresses the input ([]byte)
    29  func GzipIt(input []byte) ([]byte, error) {
    30  	// Create gzip writer.
    31  	var b bytes.Buffer
    32  	w := gzip.NewWriter(&b)
    33  	_, err := w.Write(input)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	err = w.Close() // You must close this first to flush the bytes to the buffer.
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	return b.Bytes(), nil
    43  }