-
Notifications
You must be signed in to change notification settings - Fork 5
/
search.go
206 lines (157 loc) · 4.47 KB
/
search.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type SearchResultContext int
const (
SRC_UNKNOWN SearchResultContext = iota
SRC_FILE
SRC_DISK
)
type SearchResultItem struct {
DiskPath string
File *DiskFile
}
func searchForFilename(filename string, filter []string) {
fd := GetAllFiles("*_*_*_*.fgp", filter)
fmt.Printf("Filter: %s\n", filter)
fmt.Println()
fmt.Println()
fmt.Printf("SEARCH RESULTS FOR '%s'\n", filename)
fmt.Println()
for diskname, list := range fd {
//fmt.Printf("Checking: %s\n", diskname)
for _, f := range list {
if strings.Contains(strings.ToLower(f.Filename), strings.ToLower(filename)) {
fmt.Printf("%32s:\n %s (%s, %d bytes, sha: %s)\n\n", diskname, f.Filename, f.Type, f.Size, f.SHA256)
if *extract == "@" {
ExtractFile(diskname, f, *adornedCP, false)
} else if *extract == "#" {
ExtractDisk(diskname)
}
}
}
}
}
func searchForSHA256(sha string, filter []string) {
fd := GetAllFiles("*_*_*_*.fgp", filter)
fmt.Println()
fmt.Println()
fmt.Printf("SEARCH RESULTS FOR SHA256 '%s'\n", sha)
fmt.Println()
for diskname, list := range fd {
for _, f := range list {
if f.SHA256 == sha {
fmt.Printf("%32s:\n %s (%s, %d bytes, sha: %s)\n\n", diskname, f.Filename, f.Type, f.Size, f.SHA256)
if *extract == "@" {
ExtractFile(diskname, f, *adornedCP, false)
} else if *extract == "#" {
ExtractDisk(diskname)
}
}
}
}
}
func searchForTEXT(text string, filter []string) {
fd := GetAllFiles("*_*_*_*.fgp", filter)
fmt.Println()
fmt.Println()
fmt.Printf("SEARCH RESULTS FOR TEXT CONTENT '%s'\n", text)
fmt.Println()
for diskname, list := range fd {
for _, f := range list {
if strings.Contains(strings.ToLower(string(f.Text)), strings.ToLower(text)) {
fmt.Printf("%32s:\n %s (%s, %d bytes, sha: %s)\n\n", diskname, f.Filename, f.Type, f.Size, f.SHA256)
if *extract == "@" {
ExtractFile(diskname, f, *adornedCP, false)
} else if *extract == "#" {
ExtractDisk(diskname)
}
}
}
}
}
func directory(filter []string, format string) {
fd := GetAllFiles("*_*_*_*.fgp", filter)
fmt.Println()
fmt.Println()
fmt.Println()
for diskname, list := range fd {
fmt.Printf("CATALOG RESULTS FOR '%s'\n", diskname)
//fmt.Printf("Checking: %s\n", diskname)
out := ""
for _, file := range list {
tmp := format
// size
tmp = strings.Replace(tmp, "{size:blocks}", fmt.Sprintf("%3d Blocks", file.Size/256+1), -1)
tmp = strings.Replace(tmp, "{size:kb}", fmt.Sprintf("%4d Kb", file.Size/1024+1), -1)
tmp = strings.Replace(tmp, "{size:b}", fmt.Sprintf("%6d Bytes", file.Size), -1)
tmp = strings.Replace(tmp, "{size}", fmt.Sprintf("%6d", file.Size), -1)
// format
tmp = strings.Replace(tmp, "{filename}", fmt.Sprintf("%-36s", file.Filename), -1)
// type
tmp = strings.Replace(tmp, "{type}", fmt.Sprintf("%-20s", file.Type), -1)
// sha256
tmp = strings.Replace(tmp, "{sha256}", file.SHA256, -1)
out += tmp + "\n"
if *extract == "@" {
ExtractFile(diskname, file, *adornedCP, false)
} else if *extract == "#" {
ExtractDisk(diskname)
}
}
fmt.Println(out + "\n\n")
}
}
var fileExtractCounter int
func ExtractFile(diskname string, fd *DiskFile, adorned bool, local bool) error {
var name string
if adorned {
name = fd.GetNameAdorned()
} else {
name = fd.GetName()
}
path := binpath() + "/extract" + diskname
if local {
ext := filepath.Ext(diskname)
base := strings.Replace(filepath.Base(diskname), ext, "", -1)
path = "./" + base
}
if path != "." {
os.MkdirAll(path, 0755)
}
//fmt.Printf("FD.EXT=%s\n", fd.Ext)
f, err := os.Create(path + "/" + name)
if err != nil {
return err
}
defer f.Close()
f.Write(fd.Data)
os.Stderr.WriteString("Extracted file to " + path + "/" + name + "\n")
if strings.ToLower(fd.Ext) == "int" || strings.ToLower(fd.Ext) == "bas" || strings.ToLower(fd.Ext) == "txt" {
f, err := os.Create(path + "/" + name + ".ASC")
if err != nil {
return err
}
defer f.Close()
f.Write(fd.Text)
os.Stderr.WriteString("Extracted file to " + path + "/" + name + ".ASC\n")
}
//os.Stderr.WriteString("Extracted file to " + path + "/" + name)
fileExtractCounter++
return nil
}
func ExtractDisk(diskname string) error {
path := binpath() + "/extract" + diskname
os.MkdirAll(path, 0755)
data, err := ioutil.ReadFile(diskname)
if err != nil {
return err
}
target := path + "/" + filepath.Base(diskname)
return ioutil.WriteFile(target, data, 0755)
}