github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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  
    12  	"github.com/mvdan/u-root-coreutils/pkg/uio"
    13  )
    14  
    15  func readGzip(r io.Reader) ([]byte, error) {
    16  	z, err := gzip.NewReader(r)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	defer z.Close()
    21  	return io.ReadAll(z)
    22  }
    23  
    24  // TryGzipFilter tries to read from an io.ReaderAt to see if it is a Gzip. If it is not, the
    25  // io.ReaderAt is returned.
    26  // TODO: could be tryCompressionFilters, grub support gz,xz and lzop
    27  // TODO: do we want to keep the filter inside multiboot? This could be the responsibility of the caller...
    28  func TryGzipFilter(r io.ReaderAt) io.ReaderAt {
    29  	b, err := readGzip(uio.Reader(r))
    30  	if err == nil {
    31  		return bytes.NewReader(b)
    32  	}
    33  	return r
    34  }