-
Notifications
You must be signed in to change notification settings - Fork 3
/
prefetch.go
65 lines (52 loc) · 1.77 KB
/
prefetch.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
65
package main
import (
"encoding/json"
"fmt"
"io"
"os"
cli "github.com/jawher/mow.cli"
"www.velocidex.com/golang/binparsergen"
"www.velocidex.com/golang/go-prefetch"
)
func main() {
// create an app
app := cli.App("prefetch", "parse pf files")
// Specify the action to execute when the app is invoked correctly
app.Command("parse", "Parse pf files", func(cmd *cli.Cmd) {
file := cmd.StringsArg("PF_FILE", nil, "Prefix .pf files")
cmd.Spec = "PF_FILE..."
cmd.Action = func() {
for _, arg := range *file {
fd, err := os.Open(arg)
binparsergen.FatalIfError(err, fmt.Sprintf("Open file: %v", err))
prefetch_obj, err := prefetch.LoadPrefetch(fd)
binparsergen.FatalIfError(err, fmt.Sprintf("Parsing Error: %v", err))
serialized_content, _ := json.MarshalIndent(prefetch_obj, " ", " ")
fmt.Println(string(serialized_content))
}
}
})
app.Command("extract", "Export a compressed pf file", func(cmd *cli.Cmd) {
file := cmd.StringArg("PF_FILE", "", "Prefix .pf files")
cmd.Action = func() {
profile := prefetch.NewPrefetchProfile()
reader, err := os.Open(*file)
binparsergen.FatalIfError(err, fmt.Sprintf("OpenFile: %v", err))
header := profile.MAMHeader(reader, 0)
if header.Signature() == "MAM\x04" {
// Need to decompress it in memory.
data := make([]byte, header.UncompressedSize())
n, err := reader.ReadAt(data, int64(header.Size()))
if err != io.EOF {
binparsergen.FatalIfError(err, fmt.Sprintf("Open file: %v", err))
}
decompressed, err := prefetch.LZXpressHuffmanDecompressWithFallback(
data[:n], int(header.UncompressedSize()))
binparsergen.FatalIfError(err, fmt.Sprintf("Open file: %v", err))
os.Stdout.Write(decompressed)
}
}
})
// Invoke the app passing in os.Args
app.Run(os.Args)
}