-
Notifications
You must be signed in to change notification settings - Fork 2
/
scdbinsearch
executable file
·156 lines (141 loc) · 4.69 KB
/
scdbinsearch
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
#!/usr/bin/perl
###############################################################################
# Copyright (c) 2014 by bgvanbur
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
###############################################################################
use strict;
use warnings;
my $offset = 0;
my $end = 0;
my $length = 0;
my @files;
sub ArgNumber {
my ($type,$num) = @_;
my $value = 0;
if ( $num =~ /^(\d+)$/i ) {
$value = $1;
} elsif ( $num =~ /^(0x|\$)([0-9A-F]+)$/i ) {
$value = hex($2);
} else {
die "Bad num logic: ($type,$num)\n";
}
if ( $type eq '' || $type eq 'byte' ) {
# do nothing
} elsif ( $type eq 'word' ) {
$value *= 2;
} elsif ( $type eq 'long' ) {
$value *= 4;
} elsif ( $type eq 'pal' || $type eq 'tile' ) {
$value *= 32;
} else {
die "Bad type logic: ($type,$num)\n";
}
return $value;
}
my $keywordData = '';
# parse args
foreach my $arg (@ARGV) {
if ( $arg =~ /^-(|byte|word|long|pal|tile)offset=([\d+|(0x|\$)[0-9A-F]+)$/i ) {
$offset = &ArgNumber($1,$2);
} elsif ( $arg =~ /^-(|byte|word|long|pal|tile)length=([\d+|(0x|\$)[0-9A-F]+)$/i ) {
$length = &ArgNumber($1,$2);
} elsif ( $arg =~ /^-(|byte|word|long|pal|tile)end=([\d+|(0x|\$)[0-9A-F]+)$/i ) {
$end = &ArgNumber($1,$2);
} elsif ( $arg =~ /^-keyword=(0x|\$)([0-9A-F]+)$/i ) {
$keywordData = pack("H*",$2);
} elsif ( -e $arg ) {
push @files, $arg;
} else {
print STDERR "\nCould not parse argument: $arg\n";
&Help();
}
}
if ( $#files < 0 ||
( $keywordData eq '' && $#files < 1 ) ) {
print STDERR "Need to specify some more files\n";
&Help();
}
if ( $keywordData eq '' ) {
my $keywordFile = shift @files;
my $keywordFileSize = -s $keywordFile;
if ( $end > 0 ) {
if ( $offset >= $end ) {
die "start before end...\n";
}
my $lengthNew = $end - $offset;
if ( $length > 0 && $length != $lengthNew ) {
die "specified length ($length) doesn't match specified offset ($offset) and specified end ($end)\n";
}
$length = $lengthNew;
}
if ( $offset + $length > $keywordFileSize ) {
die "Keyword offset/length exceeds keyword file length\n";
}
if ( $length <= 0 ) {
$length = $keywordFileSize - $offset;
}
if ( ! open(KEYWORD, $keywordFile) ) {
die "Could not properly open keyword file\n";
}
binmode KEYWORD;
$keywordData = chr(0x00) x $length;
if ( ! seek(KEYWORD,$offset,0) ) {
die "Could not properly find start of keyword data\n";
}
if ( read(KEYWORD, $keywordData, $length) != $length ) {
die "Could not properly read keyword data\n";
}
close KEYWORD;
}
foreach my $searchFile (<@files>) {
my $searchFileLength = -s $searchFile;
if ( $searchFileLength ) {
my $searchData = chr(0x00) x $searchFileLength;
open(SEARCH, $searchFile);
binmode SEARCH;
read(SEARCH,$searchData,$searchFileLength);
close SEARCH;
my $index = index($searchData,$keywordData);
while ( $index >= 0 ) {
printf("$searchFile @ \$%4.4X\n",$index,$searchFileLength);
$index = index($searchData,$keywordData,$index+1);
}
}
}
sub Help {
die '
scdbinsearch [options] <keywordfile> <searchfiles>
scdbinsearch -keyword=<#> <searchfiles>
[description]
Search for a binary keyword chunk in binary search files(s).
[options]
-offset=<#> byte offset in keyword file (default 0)
-length=<#> byte length of keyword (default remainder of keyword file)
-end=<#> byte end in keyword file
-wordoffset=<#> word offset in keyword file
-wordlength=<#> word length of keyword
-wordend=<#> word end in keyword file
-longoffset=<#> long (4 byte) offset in keyword file
-longlength=<#> long (4 byte) length of keyword
-longend=<#> long (4 byte) end of keyword
-tileoffset=<#> tile (32 byte) offset in keyword file
-tilelength=<#> tile (32 byte) length of keyword
-tileend=<#> tile (32 byte) end of keyword
-paloffset=<#> pal (32 byte) offset in keyword file
-pallength=<#> pal (32 byte) length of keyword
-palend=<#> pal (32 byte) end of keyword
';
}