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