github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/extractor/file/internal/sniff/sniff.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package sniff
    10  
    11  import (
    12  	"errors"
    13  	"os"
    14  )
    15  
    16  type Data struct {
    17  	Format   string `json:"format"`
    18  	Type     string `json:"type"`
    19  	Platform string `json:"platform"`
    20  	Arch     string `json:"arch"`
    21  	X64      bool   `json:"x64"`
    22  }
    23  
    24  func (d Data) ContentType() string {
    25  	switch true {
    26  	case d.Platform == Platform_MachO:
    27  		return "application/x-mach-binary"
    28  	case d.Platform == Platform_PE:
    29  		return "application/x-dosexec"
    30  	case d.Format == "ELF":
    31  		return "application/x-executable"
    32  	}
    33  	return "application/octet-stream"
    34  }
    35  
    36  var sniffers = []func(*os.File) (*Data, error){
    37  	ELF,
    38  	PE,
    39  	MachO,
    40  }
    41  
    42  func File(file *os.File) (*Data, error) {
    43  
    44  	for _, sniffer := range sniffers {
    45  		if d, e := sniffer(file); e == nil {
    46  			return d, nil
    47  		}
    48  	}
    49  
    50  	return nil, errors.New("Nothing found")
    51  }