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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
|
/* MD5DEEP - helpers.c
*
* By Jesse Kornblum
*
* This is a work of the US Government. In accordance with 17 USC 105,
* copyright protection is not available for any work of the US Government.
*
* 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.
*
*/
#include "main.h"
/* Removes any newlines at the end of the string buf.
Works for both *nix and Windows styles of newlines.
Returns the new length of the string. */
unsigned int chop (char *buf)
{
/* Windows newlines are 0x0d 0x0a, *nix are 0x0a */
unsigned int len = strlen(buf);
if (buf[len - 1] == 0x0a)
{
if (buf[len - 2] == 0x0d)
{
buf[len - 2] = buf[len - 1];
}
buf[len - 1] = buf[len];
}
return strlen(buf);
}
char *units(unsigned int c)
{
switch (c)
{
case 0: return "B";
case 1: return "KB";
case 2: return "MB";
case 3: return "GB";
case 4: return "TB";
case 5: return "PB";
case 6: return "EB";
/* Steinbach's Guideline for Systems Programming:
Never test for an error condition you don't know how to handle.
Granted, given that no existing system can handle anything
more than 18 exabytes, this shouldn't be an issue. But how do we
communicate that 'this shouldn't happen' to the user? */
default: return "??";
}
}
char *human_readable(off_t size, char *buffer)
{
unsigned int count = 0;
while (size > 1024)
{
size /= 1024;
++count;
}
/* The size will be, at most, 1023, and the units will be
two characters no matter what. Thus, the maximum length of
this string is six characters. e.g. strlen("1023 EB") = 6 */
if (sizeof(off_t) == 4)
{
snprintf(buffer, 8, "%u %s", (unsigned int)size, units(count));
}
else if (sizeof(off_t) == 8)
{
snprintf(buffer, 8, "%llu %s", (u_int64_t) size, units(count));
}
return buffer;
}
char *current_time(void)
{
time_t now = time(NULL);
char *ascii_time = ctime(&now);
chop(ascii_time);
return ascii_time;
}
/* Shift the contents of a string so that the values after 'new_start'
will now begin at location 'start' */
void shift_string(char *fn, int start, int new_start)
{
if (start < 0 || start > strlen(fn) || new_start < 0 || new_start < start)
return;
while (new_start < strlen(fn))
{
fn[start] = fn[new_start];
new_start++;
start++;
}
fn[start] = 0;
}
void make_magic(void)
{
printf("%s%s",
"\x53\x41\x4E\x20\x44\x49\x4D\x41\x53\x20\x48\x49\x47\x48\x20\x53\x43\x48\x4F\x4F\x4C\x20\x46\x4F\x4F\x54\x42\x41\x4C\x4C\x20\x52\x55\x4C\x45\x53\x21",
NEWLINE);
}
#if defined(__UNIX)
/* Return the size, in bytes of an open file stream. On error, return 0 */
#if defined(__LINUX)
off_t find_file_size(FILE *f)
{
off_t num_sectors = 0;
int fd = fileno(f);
struct stat sb;
if (fstat(fd, &sb))
{
return 0;
}
if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
return sb.st_size;
else if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))
{
if (ioctl(fd, BLKGETSIZE, &num_sectors))
{
#if defined(__DEBUG)
fprintf(stderr, "%s: ioctl call to BLKGETSIZE failed.%s", __progname, NEWLINE);
#endif
}
else
return (num_sectors * 512);
}
return 0;
}
#elif defined(__MACOSX)
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/disk.h>
off_t find_file_size(FILE *f)
{
#ifdef DEBUG
printf(" FIND MAC file size\n");
#endif
return 0; /*FIX ME this function causes strange problems on MACOSX, so for now return 0*/
struct stat info;
off_t total = 0;
off_t original = ftello(f);
int ok = TRUE, fd = fileno(f);
/* I'd prefer not to use fstat as it will follow symbolic links. We don't
follow symbolic links. That being said, all symbolic links *should*
have been caught before we got here. */
fstat(fd, &info);
/* Block devices, like /dev/hda, don't return a normal filesize.
If we are working with a block device, we have to ask the operating
system to tell us the true size of the device.
The following only works on Linux as far as I know. If you know
how to port this code to another operating system, please contact
the current maintainer of this program! */
if (S_ISBLK(info.st_mode))
{
daddr_t blocksize = 0;
daddr_t blockcount = 0;
/* Get the block size */
if (ioctl(fd, DKIOCGETBLOCKSIZE, blocksize) < 0)
{
ok = FALSE;
#if defined(__DEBUG)
perror("DKIOCGETBLOCKSIZE failed");
#endif
}
/* Get the number of blocks */
if (ok)
{
if (ioctl(fd, DKIOCGETBLOCKCOUNT, blockcount) < 0)
{
#if defined(__DEBUG)
perror("DKIOCGETBLOCKCOUNT failed");
#endif
}
}
total = blocksize * blockcount;
}
else
{
/* I don't know why, but if you don't initialize this value you'll
get wildly innacurate results when you try to run this function */
if ((fseeko(f, 0, SEEK_END)))
return 0;
total = ftello(f);
if ((fseeko(f, original, SEEK_SET)))
return 0;
}
return (total - original);
}
#else
/* This is code for general UNIX systems
(e.g. NetBSD, FreeBSD, OpenBSD, etc) */
static off_t midpoint(off_t a, off_t b, long blksize)
{
off_t aprime = a / blksize;
off_t bprime = b / blksize;
off_t c, cprime;
cprime = (bprime - aprime) / 2 + aprime;
c = cprime * blksize;
return c;
}
off_t find_dev_size(int fd, int blk_size)
{
off_t curr = 0, amount = 0;
void *buf;
if (blk_size == 0)
return 0;
buf = malloc(blk_size);
for (;;)
{
ssize_t nread;
lseek(fd, curr, SEEK_SET);
nread = read(fd, buf, blk_size);
if (nread < blk_size)
{
if (nread <= 0)
{
if (curr == amount)
{
free(buf);
lseek(fd, 0, SEEK_SET);
return amount;
}
curr = midpoint(amount, curr, blk_size);
}
else
{ /* 0 < nread < blk_size */
free(buf);
lseek(fd, 0, SEEK_SET);
return amount + nread;
}
}
else
{
amount = curr + blk_size;
curr = amount * 2;
}
}
free(buf);
lseek(fd, 0, SEEK_SET);
return amount;
}
off_t find_file_size(FILE *f)
{
int fd = fileno(f);
struct stat sb;
return 0; /*FIX ME SOLARIS FILE SIZE CAUSES SEG FAULT, for now just return 0*/
if (fstat(fd, &sb))
return 0;
if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
return sb.st_size;
else if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))
return find_dev_size(fd, sb.st_blksize);
return 0;
}
#endif /* UNIX Flavors */
#endif /* ifdef __UNIX */
#if defined(__WIN32)
off_t find_file_size(FILE *f)
{
off_t total = 0, original = ftello(f);
if ((fseeko(f, 0, SEEK_END)))
return 0;
total = ftello(f);
if ((fseeko(f, original, SEEK_SET)))
return 0;
return total;
}
#endif /* ifdef __WIN32 */
void print_search_specs(f_state *s)
{
int i = 0;
int j = 0;
printf("\nDUMPING BUILTIN SEARCH INFO\n\t");
for (i = 0; i < s->num_builtin; i++)
{
printf("%s:\n\t footer_len:=%d, header_len:=%d, max_len:=%llu ",
search_spec[i].suffix,
search_spec[i].footer_len,
search_spec[i].header_len,
search_spec[i].max_len);
printf("\n\t header:\t");
printx(search_spec[i].header, 0, search_spec[i].header_len);
printf("\t footer:\t");
printx(search_spec[i].footer, 0, search_spec[i].footer_len);
for (j = 0; j < search_spec[i].num_markers; j++)
{
printf("\tmarker: \t");
printx(search_spec[i].markerlist[j].value, 0, search_spec[i].markerlist[j].len);
}
}
}
void print_stats(f_state *s)
{
int i = 0;
audit_msg(s, "\n%d FILES EXTRACTED\n\t", s->fileswritten);
for (i = 0; i < s->num_builtin; i++)
{
if (search_spec[i].found != 0)
{
if (search_spec[i].type == OLE)
search_spec[i].suffix = "ole";
else if (search_spec[i].type == RIFF)
search_spec[i].suffix = "rif";
else if (search_spec[i].type == ZIP)
search_spec[i].suffix = "zip";
audit_msg(s, "%s:= %d", search_spec[i].suffix, search_spec[i].found);
}
}
}
int charactersMatch(char a, char b, int caseSensitive)
{
//if(a==b) return 1;
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);
}
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);
}
void printx(unsigned char *buf, int start, int end)
{
int i = 0;
for (i = start; i < end; i++)
{
printf("%x ", buf[i]);
}
printf("\n");
}
char *reverse_string(char *to, char *from, int startLocation, int endLocation)
{
int i = endLocation;
int j = 0;
for (j = startLocation; j < endLocation; j++)
{
i--;
to[j] = from[i];
}
return to;
}
unsigned short htos(unsigned char s[], int endian)
{
unsigned char *bytes = (unsigned char *)malloc(sizeof(unsigned short) * sizeof(char));
unsigned short size = 0;
char temp = 'x';
bytes = memcpy(bytes, s, sizeof(short));
if (endian == FOREMOST_BIG_ENDIAN && BYTE_ORDER == LITTLE_ENDIAN)
{
//printf("switching the byte order\n");
temp = bytes[0];
bytes[0] = bytes[1];
bytes[1] = temp;
}
else if (endian == FOREMOST_LITTLE_ENDIAN && BYTE_ORDER == BIG_ENDIAN)
{
temp = bytes[0];
bytes[0] = bytes[1];
bytes[1] = temp;
}
size = *((unsigned short *)bytes);
free(bytes);
return size;
}
unsigned int htoi(unsigned char s[], int endian)
{
int length = sizeof(int);
unsigned char *bytes = (unsigned char *)malloc(length * sizeof(char));
unsigned int size = 0;
bytes = memcpy(bytes, s, length);
if (endian == FOREMOST_BIG_ENDIAN && BYTE_ORDER == LITTLE_ENDIAN)
{
bytes = (unsigned char *)reverse_string((char *)bytes, (char *)s, 0, length);
}
else if (endian == FOREMOST_LITTLE_ENDIAN && BYTE_ORDER == BIG_ENDIAN)
{
bytes = (unsigned char *)reverse_string((char *)bytes, (char *)s, 0, length);
}
size = *((unsigned int *)bytes);
free(bytes);
return size;
}
u_int64_t htoll(unsigned char s[], int endian)
{
int length = sizeof(u_int64_t);
unsigned char *bytes = (unsigned char *)malloc(length * sizeof(char));
u_int64_t size = 0;
bytes = memcpy(bytes, s, length);
#ifdef DEBUG
printf("htoll len=%d endian=%d\n",length,endian);
#endif
if (endian == FOREMOST_BIG_ENDIAN && BYTE_ORDER == LITTLE_ENDIAN)
{
#ifdef DEBUG
printf("reverse0\n");
#endif
bytes = (unsigned char *)reverse_string((char *)bytes, (char *)s, 0, length);
}
else if (endian == FOREMOST_LITTLE_ENDIAN && BYTE_ORDER == BIG_ENDIAN)
{
#ifdef DEBUG
printf("reverse1\n");
#endif
bytes = (unsigned char *)reverse_string((char *)bytes, (char *)s, 0, length);
}
size = *((u_int64_t *)bytes);
#ifdef DEBUG
printf("htoll size=%llu\n",size);
printx(bytes,0,length);
#endif
free(bytes);
return size;
}
/* display Position: Tell the user how far through the infile we are */
int displayPosition(f_state *s, f_info *i, u_int64_t pos)
{
int percentDone = 0;
static int last_val = 0;
int count;
int flag = FALSE;
int factor = 4;
int multiplier = 25;
int number_of_stars = 0;
char buffer[256];
long double skip = s->skip * s->block_size;
long double tot_bytes = (long double)((i->total_bytes));
tot_bytes -= skip;
if (i->total_bytes > 0)
{
percentDone = (((long double)pos) / ((long double)tot_bytes)) * 100;
if (percentDone != last_val)
flag = TRUE;
last_val = percentDone;
}
else
{
flag = TRUE;
factor = 4;
multiplier = 25;
}
if (flag)
{
number_of_stars = percentDone / factor;
printf("%s: |", s->input_file);
for (count = 0; count < number_of_stars; count++)
{
printf("*");
}
for (count = 0; count < (multiplier - number_of_stars); count++)
{
printf(" ");
}
if (i->total_bytes > 0)
{
printf("|\t %d%% done\n", percentDone);
}
else
{
printf("|\t %s done\n", human_readable(pos, buffer));
}
}
if (percentDone == 100)
{
last_val = 0;
}
return TRUE;
}
|