github.com/linuxboot/fiano@v1.2.0/pkg/compression/compression.go (about)

     1  // Copyright 2018 the LinuxBoot 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 compression implements reading and writing of compressed files.
     6  //
     7  // This package is specifically designed for the LZMA formats used by popular UEFI
     8  // implementations.
     9  package compression
    10  
    11  import (
    12  	"flag"
    13  	"os/exec"
    14  
    15  	"github.com/linuxboot/fiano/pkg/guid"
    16  )
    17  
    18  var xzPath = flag.String("xzPath", "xz", "Path to system xz command used for lzma encoding. If unset, an internal lzma implementation is used.")
    19  
    20  // Compressor defines a single compression scheme (such as LZMA).
    21  type Compressor interface {
    22  	// Name is typically the name of a class.
    23  	Name() string
    24  
    25  	// Decode and Encode obey "x == Decode(Encode(x))".
    26  	Decode(encodedData []byte) ([]byte, error)
    27  	Encode(decodedData []byte) ([]byte, error)
    28  }
    29  
    30  // Well-known GUIDs for GUIDed sections containing compressed data.
    31  var (
    32  	LZMAGUID    = *guid.MustParse("EE4E5898-3914-4259-9D6E-DC7BD79403CF")
    33  	LZMAX86GUID = *guid.MustParse("D42AE6BD-1352-4BFB-909A-CA72A6EAE889")
    34  )
    35  
    36  // CompressorFromGUID returns a Compressor for the corresponding GUIDed Section.
    37  func CompressorFromGUID(guid *guid.GUID) Compressor {
    38  	// Default to system xz command for lzma encoding; if not found, use an
    39  	// internal lzma implementation.
    40  	var lzma Compressor
    41  	if _, err := exec.LookPath(*xzPath); err == nil {
    42  		lzma = &SystemLZMA{*xzPath}
    43  	} else {
    44  		lzma = &LZMA{}
    45  	}
    46  	switch *guid {
    47  	case LZMAGUID:
    48  		return lzma
    49  	case LZMAX86GUID:
    50  		// Alternatively, the -f86 argument could be passed
    51  		// into xz. It does not make much difference because
    52  		// the x86 filter is not the bottleneck.
    53  		return &LZMAX86{lzma}
    54  	}
    55  	return nil
    56  }