1 /* Capstone testing regression */
2 /* By Do Minh Tuan <tuanit96@gmail.com>, 02-2019 */
3
4
5 #include "helper.h"
6
split(char * str,char * delim,int * size)7 char **split(char *str, char *delim, int *size)
8 {
9 char **result;
10 char *token, *src;
11 int cnt;
12
13 cnt = 0;
14 src = str;
15 result = NULL;
16
17 while ((token = strstr(src, delim)) != NULL) {
18 result = (char **)realloc(result, sizeof(char *) * (cnt + 1));
19 result[cnt] = (char *)calloc(1, sizeof(char) * (int)(token - src + 10));
20 memcpy(result[cnt], src, token - src);
21 result[cnt][token - src] = '\0';
22 src = token + strlen(delim);
23 cnt ++;
24 }
25
26 if (strlen(src) > 0) {
27 result = (char **)realloc(result, sizeof(char *) * (cnt + 1));
28 result[cnt] = strdup(src);
29 cnt ++;
30 }
31
32 *size = cnt;
33 return result;
34 }
35
print_strs(char ** list_str,int size)36 void print_strs(char **list_str, int size)
37 {
38 int i;
39
40 printf("[+] Debug %d strings:\n", size);
41 for (i = 0; i < size; ++i)
42 printf("String %d'th: %s\n", i+1, list_str[i]);
43 }
44
free_strs(char ** list_str,int size)45 void free_strs(char **list_str, int size)
46 {
47 int i;
48 for (i = 0; i < size; ++i)
49 free(list_str[i]);
50
51 free(list_str);
52 }
53
get_filename_ext(const char * filename)54 const char *get_filename_ext(const char *filename)
55 {
56 const char *dot;
57
58 dot = strrchr(filename, '.');
59 if (!dot || dot == filename)
60 return "";
61
62 return dot + 1;
63 }
64
readfile(const char * filename)65 char *readfile(const char *filename)
66 {
67 char *result;
68 FILE *fp;
69 int size;
70
71 fp = fopen(filename, "r");
72 if (fp == NULL) {
73 puts("No such file");
74 exit(-1);
75 }
76
77 fseek(fp, 0, SEEK_END);
78 size = ftell(fp);
79 rewind(fp);
80
81 result = (char *)calloc(1, sizeof(char) * size + 1);
82 fread(result, size, 1, fp);
83 result[size] = '\0';
84
85 fclose(fp);
86 return result;
87 }
88
add_str(char ** src,const char * format,...)89 void add_str(char **src, const char *format, ...)
90 {
91 char *tmp;
92 size_t len1, len2;
93 va_list args;
94
95 tmp = (char *)malloc(sizeof(char) * 1000);
96 va_start(args, format);
97 vsprintf(tmp, format, args);
98 va_end(args);
99
100 len1 = strlen(*src);
101 len2 = strlen(tmp);
102
103 *src = (char *)realloc(*src, sizeof(char) * (len1 + len2 + 10));
104 memcpy(*src + len1, tmp, len2 + 1);
105 free(tmp);
106 }
107
replace_hex(char * src)108 void replace_hex(char *src)
109 {
110 char *tmp, *result, *found, *origin, *orig_found;
111 int i, valid;
112 unsigned long long int value;
113 char *tmp_tmp;
114
115 result = (char *)malloc(sizeof(char));
116 result[0] = '\0';
117 tmp = strdup(src);
118 origin = tmp;
119
120 while ((found = strstr(tmp, "0x")) != NULL) {
121 orig_found = found;
122 found += 2;
123 value = 0;
124 valid = 0;
125
126 tmp_tmp = strndup(tmp, orig_found - tmp);
127 while (*found != '\0' && isxdigit(*found)) {
128 valid = 1;
129 if (*found >= 'a' && *found <='f')
130 value = value*0x10 + (*found - 'a' + 10);
131 else
132 value = value*0x10 + (*found - '0');
133 found++;
134 }
135
136 if (valid == 1) add_str(&result, "%s%llu", tmp_tmp, value);
137 else add_str(&result, "%s0x", tmp_tmp);
138 tmp = found;
139 free(tmp_tmp);
140 }
141
142 add_str(&result, "%s", tmp);
143 if (strlen(result) >= MAXMEM) {
144 fprintf(stderr, "[ Error ] --- Buffer Overflow in replace_hex()\n");
145 free(result);
146 free(origin);
147 _fail(__FILE__, __LINE__);
148 }
149
150 strcpy(src, result);
151 free(result);
152 free(origin);
153 }
154
replace_negative(char * src,int mode)155 void replace_negative(char *src, int mode)
156 {
157 char *tmp, *result, *found, *origin, *orig_found;
158 int i, cnt, valid;
159 char *value, *tmp_tmp;
160 unsigned short int tmp_short;
161 unsigned int tmp_int;
162 unsigned long int tmp_long;
163
164 result = (char *)malloc(sizeof(char));
165 result[0] = '\0';
166 tmp = strdup(src);
167 origin = tmp;
168
169 while ((found = strstr(tmp, "-")) != NULL) {
170 orig_found = found;
171 found ++;
172 valid = 0;
173
174 value = strdup("-");
175 cnt = 2;
176
177 while (*found != '\0' && isdigit(*found)) {
178 valid = 1;
179 value = (char *)realloc(value, cnt + 1);
180 value[cnt - 1] = *found;
181 value[cnt] = '\0';
182 cnt ++;
183 found++;
184 }
185
186 tmp_tmp = strndup(tmp, orig_found - tmp);
187 if (valid == 1) {
188 *orig_found = '\0';
189 if (mode == X86_16) {
190 sscanf(value, "%hu", &tmp_short);
191 add_str(&result, "%s%hu", tmp_tmp, tmp_short);
192 } else if (mode == X86_32) {
193 sscanf(value, "%u", &tmp_int);
194 add_str(&result, "%s%u", tmp_tmp, tmp_int);
195 } else if (mode == X86_64) {
196 sscanf(value, "%lu", &tmp_long);
197 add_str(&result, "%s%lu", tmp_tmp, tmp_long);
198 }
199 }
200 else add_str(&result, "%s-", tmp_tmp);
201
202 tmp = found;
203 free(value);
204 free(tmp_tmp);
205 }
206
207 add_str(&result, "%s", tmp);
208 if (strlen(result) >= MAXMEM) {
209 fprintf(stderr, "[ Error ] --- Buffer Overflow in replace_negative()\n");
210 free(result);
211 free(origin);
212 _fail(__FILE__, __LINE__);
213 }
214
215 strcpy(src, result);
216 free(result);
217 free(origin);
218 }
219
listdir(const char * name,char *** files,int * num_files)220 void listdir(const char *name, char ***files, int *num_files)
221 {
222 DIR *dir;
223 struct dirent *entry;
224 int cnt;
225
226 if (!(dir = opendir(name)))
227 return;
228
229 while ((entry = readdir(dir)) != NULL) {
230 if (entry->d_type == DT_DIR) {
231 char path[1024];
232 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
233 continue;
234 snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
235 listdir(path, files, num_files);
236 } else {
237 cnt = *num_files;
238 *files = (char **)realloc(*files, sizeof(char *) * (cnt + 1));
239 (*files)[cnt] = (char *)malloc(sizeof(char) * ( strlen(name) + 1 + strlen(entry->d_name) + 10));
240 sprintf((*files)[cnt], "%s/%s", name, entry->d_name);
241 cnt ++;
242 *num_files = cnt;
243 }
244 }
245
246 closedir(dir);
247 }
248
trim_str(char * str)249 void trim_str(char *str)
250 {
251 char tmp[MAXMEM];
252 int start, end, j, i;
253
254 start = 0;
255 end = strlen(str) - 1;
256 j = 0;
257 while (start < strlen(str) && isspace(str[start])) start++;
258 while (end >= 0 && isspace(str[end])) end--;
259
260 for (i = start; i <= end; ++i)
261 tmp[j++] = str[i];
262
263 tmp[j] = '\0';
264 strcpy(str, tmp);
265
266 return;
267 }
268