github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/readpe/pe.go (about)

     1  // Copyright 2017 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  // Dump the headers of a PE file.
     6  //
     7  // Synopsis:
     8  //     pe [FILENAME]
     9  //
    10  // Description:
    11  //     Windows and EFI executables are in the portable executable (PE) format.
    12  //     This command prints the headers in a JSON format.
    13  package main
    14  
    15  import (
    16  	"debug/pe"
    17  	"encoding/json"
    18  	"flag"
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  )
    23  
    24  func main() {
    25  	// Parse flags
    26  	flag.Parse()
    27  	var (
    28  		f   *pe.File
    29  		err error
    30  	)
    31  	switch flag.NArg() {
    32  	case 0:
    33  		f, err = pe.NewFile(os.Stdin)
    34  	case 1:
    35  		filename := flag.Arg(0)
    36  		f, err = pe.Open(filename)
    37  	default:
    38  		log.Fatal("Usage: pe [FILENAME]")
    39  	}
    40  	if err != nil {
    41  		log.Fatal(err)
    42  	}
    43  	defer f.Close()
    44  
    45  	// Convert to JSON
    46  	j, err := json.MarshalIndent(f, "", "\t")
    47  	if err != nil {
    48  		log.Fatal(err)
    49  	}
    50  	fmt.Println(string(j))
    51  }