-
Notifications
You must be signed in to change notification settings - Fork 3
/
probe.go
64 lines (53 loc) · 1.35 KB
/
probe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package pelican
import (
"github.com/itchio/pelican/pe"
"github.com/itchio/headway/state"
"github.com/itchio/httpkit/eos"
"github.com/pkg/errors"
)
type ProbeParams struct {
Consumer *state.Consumer
// Return errors instead of printing warnings when
// we can't parse some parts of the file
Strict bool
}
// Probe retrieves information about an PE file
func Probe(file eos.File, params ProbeParams) (*PeInfo, error) {
consumer := params.Consumer
stats, err := file.Stat()
if err != nil {
return nil, errors.WithStack(err)
}
pf, err := pe.NewFile(file, stats.Size())
if err != nil {
return nil, errors.WithStack(err)
}
info := &PeInfo{
VersionProperties: make(map[string]string),
}
switch pf.Machine {
case pe.IMAGE_FILE_MACHINE_I386:
info.Arch = "386"
case pe.IMAGE_FILE_MACHINE_AMD64:
info.Arch = "amd64"
}
imports, err := pf.ImportedLibraries()
if err != nil {
if params.Strict {
return nil, errors.WithMessage(err, "while parsing imported libraries")
}
consumer.Warnf("Could not parse imported libraries: %+v", err)
}
info.Imports = imports
sect := pf.Section(".rsrc")
if sect != nil {
err = params.parseResources(info, sect)
if err != nil {
if params.Strict {
return nil, errors.WithMessage(err, "while parsing resources")
}
consumer.Warnf("Could not parse resources: %+v", err)
}
}
return info, nil
}