File: helpers.c

package info (click to toggle)
scalpel 1.60-6
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 556 kB
  • sloc: ansic: 4,031; makefile: 61
file content (405 lines) | stat: -rw-r--r-- 10,999 bytes parent folder | download | duplicates (2)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Scalpel Copyright (C) 2005-6 by Golden G. Richard III.
// Written by Golden G. Richard III.
//
// 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.

// Scalpel is a complete rewrite of the foremost 0.69 file carver to
// increase speed and support execution on machines with minimal
// resources (e.g., < 256MB RAM).
//
// Thanks to Kris Kendall, Jesse Kornblum, et al for their work on
// foremost.


#include "scalpel.h"


void checkMemoryAllocation(struct scalpelState *state, void *ptr, int line, 
			   char *file, char *structure) {
  if (ptr) {
    return;
  }
  else {
    fprintf(stderr, "** MEMORY ALLOCATION FAILURE **\n");
    fprintf (stderr,
	     "ERROR: Memory exhausted at line %d in file %s. Scalpel was \n",
  	     line, file);
    fprintf(stderr, 
	    "allocating memory for %s when this condition occurred.\n", 
	    structure);

    fprintf (state->auditFile,
	     "ERROR: Memory exhausted at line %d in file %s. Scalpel was \n",
  	     line, file);
    fprintf(state->auditFile, 
	    "allocating memory for %s when this condition occurred.\n", 
	    structure);

    handleError(state, SCALPEL_GENERAL_ABORT);  // fatal
  }
}


#ifndef __GLIBC__
void setProgramName(char *s) {
  char *fn = (char *)malloc(sizeof(char) * PATH_MAX);
  if (realpath(s,fn) == NULL) {
    __progname = strdup("scalpel");
    return;
  }

  __progname = basename(fn);
}

#endif /* ifndef __GLIBC__ */


// write entry to both the screen and the audit file 
void scalpelLog(struct scalpelState *state, char *format, ...) {

  va_list argp;

  va_start(argp,format);
  vfprintf (stderr,format,argp);
  vfprintf (state->auditFile,format,argp);
  va_end(argp);
}

// determine if two characters match, with optional case 
// insensitivity.  If a is the Scalpel wildcard character,
// then a and b will always match.
int charactersMatch(char a, char b, int caseSensitive) {

  if (a == wildcard || a == b) {
    return 1;
  }
  if (caseSensitive || (a < 'A' || a > 'z' || b < 'A' || b > 'z')) {
    return 0;
  }

  /* This line is equivalent to (abs(a-b)) == 'a' - 'A' */
  return (abs(a-b) == 32);
}


// memwildcardcmp is a memcmp() clone, except that single
// character wildcards are supported.  The default wildcard character is '?',
// but this can be redefined in the configuration file.  A wildcard in s1 will 
// match any single character in s2.  
int memwildcardcmp(const void *s1, const void *s2, 
		   size_t n,int caseSensitive){
  if (n != 0) {
    register const unsigned char *p1 = s1, *p2 = s2;
    do {
      if (!charactersMatch(*p1++, *p2++, caseSensitive)) {
	return (*--p1 - *--p2);
      }
    } while (--n !=0);
  }
  return 0;
}

// initialize Boyer-Moore "jump table" for search. Dependence
// on search type (e.g., FORWARD, REVERSE, etc.) from Foremost 
// has been removed, because Scalpel always performs searches across
// a buffer in a forward direction.
void init_bm_table(char *needle, size_t table[UCHAR_MAX + 1], 
		   size_t len, int casesensitive) {

  size_t i=0,j=0,currentindex=0;
  
  for (i = 0; i <= UCHAR_MAX; i++) {
    table[i] = len;
  }

  for (i = 0; i < len; i++) {
    currentindex = len-i-1; //Count from the back of string
    //No skip entry can advance us past the last wildcard in the string
    if (needle[i] == wildcard) {           
      for (j=0; j<=UCHAR_MAX; j++) {
	table[j] = currentindex; 
      }
    }	
    table[(unsigned char)needle[i]] = currentindex;
    if (! casesensitive && needle[i] > 0) {
      table[tolower(needle[i])] = currentindex;
      table[toupper(needle[i])] = currentindex;
    }
  }	  
}

// Perform a modified Boyer-Moore string search, supporting wildcards,
// case-insensitive searches, and specifiable start locations in the buffer.
// Dependence on search type (e.g., FORWARD, REVERSe, etc.) from Foremost has 
// been removed, because Scalpel always performs forward searching.

char *bm_needleinhaystack_skipnchars(char *needle, size_t needle_len,
				     char *haystack, size_t haystack_len,
				     size_t table[UCHAR_MAX + 1], 
				     int casesensitive,
				     int start_pos) {
  register size_t shift=0;
  register size_t pos = start_pos;
  char *here; 
  
  if(needle_len == 0) {
    return haystack;
  }
  
  while (pos < haystack_len){
    while( pos < haystack_len && (shift = table[(unsigned char)haystack[pos]]) > 0) {
      pos += shift;
    }
    if (0 == shift) {
      if (0 == memwildcardcmp(needle,here = (char *)&haystack[pos-needle_len+1], needle_len, casesensitive)) {
	return(here);
      }
      else {
	pos++;
      }
    }
  }
  return NULL;
}


char *bm_needleinhaystack(char *needle, size_t needle_len,
                          char *haystack, size_t haystack_len,
                          size_t table[UCHAR_MAX + 1], int casesensitive) {

  return bm_needleinhaystack_skipnchars(needle, 
					needle_len, 
					haystack, 
					haystack_len, 
					table, 
					casesensitive, 
					needle_len - 1);
}


// find longest header OR footer
int findLongestNeedle(struct SearchSpecLine *SearchSpec) {
  int longest = 0;
  int i=0;
  for(i=0; SearchSpec[i].suffix != NULL; i++) {
    if (SearchSpec[i].beginlength > longest) {
      longest = SearchSpec[i].beginlength;
    }
    if (SearchSpec[i].endlength > longest) {
      longest = SearchSpec[i].endlength;
    }
  }
  return longest;  
}     


// decode strings with embedded escape sequences and return the total length of the
// translated string.

int translate(char *str) {
  char next;
  char *rd=str,*wr=str,*bad;
  char temp[1+3+1];
  char ch;
  
  if(!*rd) {  //If it's a null string just return
    return 0;
  }
  
  while (*rd) {
    // Is it an escaped character?
    if (*rd=='\\') {
      rd++;
      switch(*rd) {
      case '\\': rd++;*(wr++)='\\'; break;
      case 'a':  rd++;*(wr++)='\a'; break;
      case 's':  rd++;*(wr++)=' '; break;
      case 'n':  rd++;*(wr++)='\n'; break;
      case 'r':  rd++;*(wr++)='\r'; break;
      case 't':  rd++;*(wr++)='\t'; break;
      case 'v':  rd++;*(wr++)='\v'; break;
	// Hexadecimal/Octal values are treated in one place using strtoul() 
      case 'x':
      case '0': case '1': case '2': case '3':
	next = *(rd+1); 
	if (next < 48 || (57 < next && next < 65) || 
	    (70 < next && next < 97) || next > 102) 
	  break;  //break if not a digit or a-f, A-F 
	next = *(rd+2); 
	if (next < 48 || (57 < next && next < 65) || 
	    (70 < next && next < 97) || next > 102) 
	  break;  //break if not a digit or a-f, A-F 
	
	temp[0]='0'; bad=temp;
	strncpy(temp+1,rd,3);
	temp[4] = '\0';
	ch=strtoul(temp,&bad,0);
	if (*bad=='\0') {
	  *(wr++)=ch;
	  rd+=3;
	} // else INVALID CHARACTER IN INPUT ('\\' followed by *rd) 
	break;
      default: // INVALID CHARACTER IN INPUT (*rd)
	*(wr++)='\\';
	break;
      }
    }
    // just copy un-escaped characters
    else {
      *(wr++)=*(rd++);
    }
  }
  *wr = '\0';  // null termination
  return wr-str;
}

// skip leading whitespace
char *skipWhiteSpace(char* str) {
  while (isspace(str[0])) {
    str++;
  }
  return str;
}

// describe Scalpel error conditions.  Some errors are fatal, while
// others are advisory.
void handleError(struct scalpelState *state, int error) {
  
  switch(error) {
    
  case SCALPEL_ERROR_FILE_OPEN:
    // non-fatal
    scalpelLog(state,"Scalpel was unable to open the image file: %s\n"
		"Skipping...\n\n",
		state->imagefile);
    break;

  case SCALPEL_ERROR_FILE_READ:
    // non-fatal
    scalpelLog(state,"Scalpel was unable to read the image file: %s\n"
		"Skipping...\n\n",
		state->imagefile);
    break;

  case SCALPEL_ERROR_FATAL_READ:
    // fatal
    scalpelLog(state,"Scalpel was unable to read a needed file and will abort.\n");
    closeFile(state->auditFile);
    exit (-1);
    break;

  case SCALPEL_ERROR_FILE_WRITE:
    // fatal--unable to write files, which may mean that disk space is exhausted.
    fprintf(stderr,
	    "Scalpel was unable to write output files and will abort.\n");
    fprintf(stderr,
	    "This error generally indicates that disk space is exhausted.\n");
    closeFile(state->auditFile);
    exit (-1);

  case SCALPEL_ERROR_NO_SEARCH_SPEC:
    // fatal, configuration file didn't specify anything to carve
    scalpelLog(state,
		 "ERROR: The configuration file didn't specify any file types to carve.\n");
    scalpelLog(state, "(If you're using the default configuration file, you'll have to\n");
    scalpelLog(state, "uncomment some of the file types.)\n\n");
    scalpelLog(state, "See /etc/scalpel/scalpel.conf.\n");
    closeFile(state->auditFile);
    exit (-1);

  case SCALPEL_GENERAL_ABORT:
    // fatal
    scalpelLog(state,"Scalpel will abort.\n");
    closeFile(state->auditFile);
    exit (-1);
    break;
  
  default:
    // fatal
    scalpelLog(state,
		"Scalpel has encountered an error it doesn't know"
		"how to handle.\nError code: %d\n", error);
    closeFile(state->auditFile);
    exit (-1);
  }
}


void setttywidth(){
#if defined (__WIN32)
  CONSOLE_SCREEN_BUFFER_INFO csbi;  
  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  if(! GetConsoleScreenBufferInfo(hConsole, &csbi)){
    ttywidth = 80;
  }else{
    ttywidth = csbi.dwSize.X;
  }
#else
  //#if defined(__BSD)
  //  ttywidth = 80;
  //#else
  struct winsize winsize;
  if(ioctl(fileno(stdout),TIOCGWINSZ, &winsize) != -1) {
    ttywidth = winsize.ws_col;
  }
  else {
    // 80 is a reasonable default
    ttywidth = 80; 
  }
  //#endif
#endif
}


int skipInFile(struct scalpelState *state, FILE *infile) {

  int retries = 0;
  while(TRUE) {
    if ((fseeko(infile,state->skip,SEEK_SET))) {

#ifdef __WIN32      
      fprintf(stderr,
	      "ERROR: Couldn't skip %I64u bytes at the start of image file %s\n",
	      state->skip,state->imagefile);
#else
      fprintf(stderr,
	      "ERROR: Couldn't skip %lld bytes at the start of image file %s\n",
	      state->skip,state->imagefile);
#endif
	
      if (retries++ > 3) {
	fprintf(stderr,"Sorry, maximum retries exceeded...\n");
	return FALSE;
      } 
      else {
	fprintf (stderr,"Waiting to try again... \n");
	sleep(3);
      }
    }
    else {
#ifdef __WIN32
      fprintf(stderr,"Skipped the first %I64u bytes of %s...\n",state->skip,
	      state->imagefile);
#else
      fprintf(stderr,"Skipped the first %lld bytes of %s...\n",state->skip,
	      state->imagefile);
#endif


      return TRUE;
    }
  }
}