github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/util/reader.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"bytes"
     9  	"compress/gzip"
    10  	"io"
    11  	"io/ioutil"
    12  
    13  	"github.com/u-root/u-root/pkg/uio"
    14  )
    15  
    16  func readGzip(r io.Reader) ([]byte, error) {
    17  	z, err := gzip.NewReader(r)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	defer z.Close()
    22  	return ioutil.ReadAll(z)
    23  }
    24  
    25  // TryGzipFilter tries to read from an io.ReaderAt to see if it is a Gzip. If it is not, the
    26  // io.ReaderAt is returned.
    27  // TODO: could be tryCompressionFilters, grub support gz,xz and lzop
    28  // TODO: do we want to keep the filter inside multiboot? This could be the responsibility of the caller...
    29  func TryGzipFilter(r io.ReaderAt) io.ReaderAt {
    30  	b, err := readGzip(uio.Reader(r))
    31  	if err == nil {
    32  		return bytes.NewReader(b)
    33  	}
    34  	return r
    35  }