-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
171 lines (156 loc) · 5.1 KB
/
main.c
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
/* Ariko, a relative search tool for the UNIX world.
* Copyright (C) 2013 Frederic Beaudet ([email protected])
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* Page: https://github.com/fbeaudet/ariko
* Repository: https://github.com/fbeaudet/ariko.git
*/
#include <ctype.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "relsearch.h"
int main(int argc, char* argv[])
{
char* helpString = "\
Usage: ariko [-b <NB>] [-e <END>] [-x <ID>] <FILEPATH> <SEARCHSTRING>\n\
Execute a relative search to find character encoding.\n\n\
-b bits per character (8 or 16)\n\
-e endianness: \"little\" OR \"big\"\n\
-x id of result to export to character map\n\
--help display this message\n";
int* result = NULL; //search result
int bits = 8; //bits per character: 8 or 16
int expId = -1; //charmap export result id
bool bigEndian = true; //endianness
//parsing options
static struct option longOptions[] = {
{"help", no_argument, 0, 'h'},
};
int opt;
int optindex = 0;
while((opt = getopt_long(argc, argv, "b:e:x:",longOptions,&optindex)) != -1) {
switch(opt) {
case 'b':
//control -b option
bits = strtol(optarg,NULL,10);
if(bits != 8 && bits != 16) {
printf("Error: -b option takes only 8 or 16.\n");
return 1;
}
break;
case 'e':
//control -e option
if(argc == 4 || strcmp(optarg,"big") == 0) {
bigEndian = false;
}
else {
bigEndian = true;
}
break;
case 'h':
printf("%s", helpString);
return 0;
case 'x':
expId = strtol(optarg,NULL,10);
break;
case '?':
return 1;
default:
abort();
}
}
//control number of required arguments
if(argc-optindex != 3) {
printf("Error: wrong arguments count. See --help.\n");
return 1;
}
//first required argument: file to read in
if(access(argv[optindex+1], F_OK) == -1 ) {
printf("Can't read specified file.\n");
return 1;
}
//second required argument : string to search for
int uppers = 0; int lowers = 0;
for(int x = 0; x < strlen(argv[optindex+2]); x++) {
if(isalpha(argv[optindex+2][x])) {
if(isupper(argv[optindex+2][x])) {
uppers++;
} else {
lowers++;
}
}
}
if(lowers < 2 && uppers < 2) {
printf("Error: string to search is badly formed.\n");
return 1;
}
//ok, execute the relative search
result = relSearch(argv[optindex+1], argv[optindex+2], bits, bigEndian);
//output result(s)
if(result[0] == 0) { // no results
printf("No results found.\n");
free(result);
return 1;
}
if(expId != -1) { //-x option specified
//result with specified id can't be exported to character map
if(expId > result[0]-1) {
printf("Error: can't export character map, id %d not found.\n", expId);
printf("-> %d result(s) found. ", result[0]);
free(result);
return 1;
}
//export character map of specified result
char temp[] = "x";
if(result[expId+2]!= -1) {
for(int i = 0; i < 26; i++) {
temp[0] = 65+i;
printf("%x=%s\n", result[expId+2]+i, temp);
}
}
if(result[expId+3] != -1) {
for(int i = 0; i < 26; i++) {
temp[0] = 97+i;
printf("%x=%s\n", result[expId+3]+i, temp);
}
}
free(result);
return 0;
}
//output all results in json format
printf("[");
for(int x = 0; x < result[0]*3; x+=3) {
printf("{'id':%d,'address':'0x%x', ", x/3, result[x+1]);
if(result[x+2] >= 0) {
printf("'A':'0x%x'", result[x+2]);
if(result[x+3] >= 0) {
printf(", 'a':'0x%x'", result[x+3]);
}
} else if(result[x+3] >= 0) {
printf("'a':'0x%x'", result[x+3]);
}
printf("}");
if(x != (result[0]-1)*3) {
printf(",\n");
}
}
printf("]\n");
free(result);
return 0;
}