This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.cpp
349 lines (270 loc) · 8.2 KB
/
helpers.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// ssdeep
// Copyright (C) 2012 Kyrus
// Copyright (C) 2006 ManTech International Corporation
//
// $Id: helpers.cpp 144 2012-04-24 14:59:33Z jessekornblum $
//
// 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 St, Fifth Floor, Boston, MA 02110-1301 USA
#include "ssdeep.h"
void try_msg(void)
{
fprintf (stderr,"Try `%s -h` for more information.%s", __progname, NEWLINE);
}
void sanity_check(state *s, int condition, const char *msg)
{
if (NULL == s)
exit(EXIT_FAILURE);
if (condition)
{
if (!(s->mode & (mode_silent || mode_full_silent)))
{
print_status("%s: %s", __progname, msg);
try_msg();
}
exit (EXIT_FAILURE);
}
}
// The basename function kept misbehaving on OS X, so I rewrote it.
// This function isn't perfect, nor is it designed to be. Because
// we're guaranteed to be working with a filename here, there's no way
// that s will end with a DIR_SEPARATOR (e.g. /foo/bar/). This function
// will not work properly for a string that ends in a DIR_SEPARATOR */
int my_basename(TCHAR *s)
{
size_t len;
TCHAR * tmp;
if (NULL == s)
return TRUE;
tmp = _tcsrchr(s,DIR_SEPARATOR);
if (NULL == tmp)
return FALSE;
len = _tcslen(tmp);
// We advance tmp one character to move us past the DIR_SEPARATOR
_tmemmove(s,tmp+1,len);
return FALSE;
}
int my_dirname(TCHAR *c)
{
TCHAR *tmp;
if (NULL == c)
return TRUE;
// If there are no DIR_SEPARATORs in the directory name, then the
// directory name should be the empty string
tmp = _tcsrchr(c,DIR_SEPARATOR);
if (NULL != tmp)
tmp[1] = 0;
else
c[0] = 0;
return FALSE;
}
void prepare_filename(state *s, TCHAR *fn)
{
if (s->mode & mode_barename)
{
if (my_basename(fn))
{
print_error_unicode(s,fn,"Unable to shorten filename");
return;
}
}
}
// Remove the newlines, if any. Works on both DOS and *nix newlines
void chop_line_tchar(TCHAR *s)
{
size_t pos = _tcslen(s);
while (pos > 0)
{
// We split up the two checks because we can never know which
// condition the computer will examine if first. If pos == 0, we
// don't want to be checking s[pos-1] under any circumstances!
if (!(s[pos-1] == _TEXT('\r') || s[pos-1] == _TEXT('\n')))
return;
s[pos-1] = 0;
--pos;
}
}
// Remove the newlines, if any. Works on both DOS and *nix newlines
void chop_line(char *s)
{
size_t pos = strlen(s);
while (pos > 0)
{
// We split up the two checks because we can never know which
// condition the computer will examine if first. If pos == 0, we
// don't want to be checking s[pos-1] under any circumstances!
if (!(s[pos-1] == _TEXT('\r') || s[pos-1] == _TEXT('\n')))
return;
s[pos-1] = 0;
--pos;
}
}
// Shift the contents of a string so that the values after 'new_start'
// will now begin at location 'start'
void shift_string_tchar(TCHAR *fn, unsigned int start, unsigned int new_start)
{
size_t sz = _tcslen(fn);
if (start > sz || new_start < start)
return;
while (new_start < sz)
{
fn[start] = fn[new_start];
new_start++;
start++;
}
fn[start] = 0;
}
// Find the index of the next comma in the string s starting at index start.
// If there is no next comma, returns -1.
int find_next_comma_tchar(TCHAR *s, unsigned int start)
{
size_t size = _tcslen(s);
unsigned int pos = start;
int in_quote = FALSE;
while (pos < size)
{
switch (s[pos]) {
case _TEXT('"'):
in_quote = !in_quote;
break;
case _TEXT(','):
if (in_quote)
break;
// Although it's potentially unwise to cast an unsigned int back
// to an int, problems will only occur when the value is beyond
// the range of int. Because we're working with the index of a
// string that is probably less than 32,000 characters, we should
// be okay.
return (int)pos;
}
++pos;
}
return -1;
}
void mm_magic(void){MM_INIT("%s\n","\x49\x20\x64\x6f\x20\x6e\x6f\x74\x20\x62\x65\x6c\x69\x65\x76\x65\x20\x77\x65\x20\x77\x69\x6c\x6c\x20\x67\x65\x74\x20\x45\x64\x64\x69\x65\x20\x56\x61\x6e\x20\x48\x61\x6c\x65\x6e\x20\x75\x6e\x74\x69\x6c\x20\x77\x65\x20\x68\x61\x76\x65\x20\x61\x20\x74\x72\x69\x75\x6d\x70\x68\x61\x6e\x74\x20\x76\x69\x64\x65\x6f\x2e");}
// Returns the string after the nth comma in the string s. If that
// string is quoted, the quotes are removed. If there is no valid
// string to be found, returns TRUE. Otherwise, returns FALSE
int find_comma_separated_string_tchar(TCHAR *s, unsigned int n)
{
int start = 0, end;
unsigned int count = 0;
while (count < n)
{
if ((start = find_next_comma_tchar(s,start)) == -1)
return TRUE;
++count;
// Advance the pointer past the current comma
++start;
}
// It's okay if there is no next comma, it just means that this is
// the last comma separated value in the string
if ((end = find_next_comma_tchar(s,start)) == -1)
end = _tcslen(s);
// Strip off the quotation marks, if necessary. We don't have to worry
// about uneven quotation marks (i.e quotes at the start but not the end
// as they are handled by the the find_next_comma function.
if (s[start] == _TEXT('"'))
++start;
if (s[end - 1] == _TEXT('"'))
end--;
s[end] = 0;
shift_string_tchar(s,0,start);
return FALSE;
}
// Shift the contents of a string so that the values after 'new_start'
// will now begin at location 'start'
void shift_string(char *fn, size_t start, size_t new_start)
{
// TODO: Can shift_string be replaced with memmove?
if (start > strlen(fn) || new_start < start)
return;
while (new_start < strlen(fn))
{
fn[start] = fn[new_start];
new_start++;
start++;
}
fn[start] = 0;
}
// Find the index of the next comma in the string s starting at index start.
// If there is no next comma, returns -1
int find_next_comma(char *s, unsigned int start)
{
size_t size=strlen(s);
unsigned int pos = start;
int in_quote = FALSE;
while (pos < size)
{
switch (s[pos]) {
case '"':
in_quote = !in_quote;
break;
case ',':
if (in_quote)
break;
// Although it's potentially unwise to cast an unsigned int back
// to an int, problems will only occur when the value is beyond
// the range of int. Because we're working with the index of a
// string that is probably less than 32,000 characters, we should
// be okay.
return (int)pos;
}
++pos;
}
return -1;
}
/// Returns the string after the nth comma in the string s. If that
/// string is quoted, the quotes are removed. If there is no valid
/// string to be found, returns TRUE. Otherwise, returns FALSE
int find_comma_separated_string(char *s, unsigned int n)
{
int start = 0, end;
unsigned int count = 0;
while (count < n)
{
if ((start = find_next_comma(s,start)) == -1)
return TRUE;
++count;
// Advance the pointer past the current comma
++start;
}
// It's okay if there is no next comma, it just means that this is
// the last comma separated value in the string
if ((end = find_next_comma(s,start)) == -1)
end = strlen(s);
// Strip off the quotation marks, if necessary. We don't have to worry
// about uneven quotation marks (i.e quotes at the start but not the end
// as they are handled by the the find_next_comma function.
if (s[start] == '"')
++start;
if (s[end - 1] == '"')
end--;
s[end] = 0;
shift_string(s,0,start);
return FALSE;
}
int remove_escaped_quotes(char * str)
{
if (NULL == str)
return TRUE;
size_t pos = 0;
while (str[pos] != 0)
{
if ('\\' == str[pos] && '"' == str[pos+1])
shift_string(str,pos,pos+1);
++pos;
}
return FALSE;
}