github.com/linuxboot/fiano@v1.2.0/cmds/glzma/glzma.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  // glzma compresses and decompresses in the same manner as EDK2's LzmaCompress.
     6  //
     7  // Synopsis:
     8  //     glzma -o OUTPUT_FILE (-d|-e) [-f86] INPUT_FILE
     9  //
    10  // Options:
    11  //     -d: decode
    12  //     -e: encode
    13  //     -f86: Use the x86 branch/call/jump filter. See `man xz` for more information.
    14  //     -o OUTPUT_FILE: output file
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"os"
    20  
    21  	"github.com/linuxboot/fiano/pkg/compression"
    22  	"github.com/linuxboot/fiano/pkg/log"
    23  )
    24  
    25  var (
    26  	d   = flag.Bool("d", false, "decode")
    27  	e   = flag.Bool("e", false, "encode")
    28  	f86 = flag.Bool("f86", false, "use x86 extension")
    29  	o   = flag.String("o", "", "output file")
    30  )
    31  
    32  func main() {
    33  	flag.Parse()
    34  
    35  	if *d == *e {
    36  		log.Fatalf("either decode (-d) or encode (-e) must be set")
    37  	}
    38  	if *o == "" {
    39  		log.Fatalf("output file must be set")
    40  	}
    41  	if flag.NArg() != 1 {
    42  		log.Fatalf("expected one input file")
    43  	}
    44  
    45  	var compressor compression.Compressor
    46  	if *f86 {
    47  		compressor = compression.CompressorFromGUID(&compression.LZMAX86GUID)
    48  	} else {
    49  		compressor = compression.CompressorFromGUID(&compression.LZMAGUID)
    50  	}
    51  
    52  	var op func([]byte) ([]byte, error)
    53  	if *d {
    54  		op = compressor.Decode
    55  	} else {
    56  		op = compressor.Encode
    57  	}
    58  
    59  	in, err := os.ReadFile(flag.Args()[0])
    60  	if err != nil {
    61  		log.Fatalf("%v", err)
    62  	}
    63  	out, err := op(in)
    64  	if err != nil {
    65  		log.Fatalf("%v", err)
    66  	}
    67  	if err := os.WriteFile(*o, out, 0666); err != nil {
    68  		log.Fatalf("%v", err)
    69  	}
    70  }