-
Notifications
You must be signed in to change notification settings - Fork 6
/
extract.c
executable file
·2556 lines (2214 loc) · 61.9 KB
/
extract.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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* extract.c
* Copyright (c) 2005, Nick Mikus
* This file contains the file specific functions used to extract
* data from an image.
*
* Each has a similar structure
* f_state *s: state of the program.
* c_offset: offset that the header was recorded within the current chunk
* foundat: The location the header was "foundat"
* buflen: How much buffer is left until the end of the current chunk
* needle: Search specification
* f_offset: Offset that the current chunk is located within the file
*/
#include "main.h"
#include "extract.h"
#include "ole.h"
extern unsigned char buffer[OUR_BLK_SIZE];
extern int verbose;
extern int dir_count;
extern int block_list[OUR_BLK_SIZE / sizeof(int)];
extern int *FAT;
extern char *extract_name;
extern int extract;
extern int FATblk;
extern int highblk;
/********************************************************************************
*Function: extract_zip
*Description: Given that we have a ZIP header jump through the file headers
until we reach the EOF.
*Return: A pointer to where the EOF of the ZIP is in the current buffer
**********************************************************************************/
unsigned char *extract_zip(f_state *s, u_int64_t c_offset, unsigned char *foundat, u_int64_t buflen,
s_spec *needle, u_int64_t f_offset, char *type)
{
unsigned char *currentpos = NULL;
unsigned char *buf = foundat;
unsigned short comment_length = 0;
unsigned char *extractbuf = NULL;
struct zipLocalFileHeader localFH;
u_int64_t bytes_to_search = 50 * KILOBYTE;
u_int64_t file_size = 0;
int oOffice = FALSE;
int office2007 = FALSE;
char comment[32];
localFH.genFlag=0;
localFH.compressed=0;
localFH.uncompressed =0;
if (buflen < 100)
return NULL;
if (strncmp((char *) &foundat[30], "mimetypeapplication/vnd.sun.xml.", 32) == 0)
{
oOffice = TRUE;
if (strncmp((char *) &foundat[62], "calc", 4) == 0)
{
needle->suffix = "sxc";
}
else if (strncmp((char *) &foundat[62], "impress", 7) == 0)
{
needle->suffix = "sxi";
}
else if (strncmp((char *) &foundat[62], "writer", 6) == 0)
{
needle->suffix = "sxw";
}
else
{
sprintf(comment, " (OpenOffice Doc?)");
strcat(needle->comment, comment);
needle->suffix = "sx";
}
}
else
{
needle->suffix = "zip";
}
while (1) //Jump through each local file header until the central directory structure is reached, much faster than searching
{
if (foundat[2] == '\x03' && foundat[3] == '\x04') //Verfiy we are looking at a local file header//
{
localFH.compression=htos(&foundat[8], FOREMOST_LITTLE_ENDIAN);
localFH.compressed = htoi(&foundat[18], FOREMOST_LITTLE_ENDIAN);
localFH.uncompressed = htoi(&foundat[22], FOREMOST_LITTLE_ENDIAN);
localFH.filename_length = htos(&foundat[26], FOREMOST_LITTLE_ENDIAN);
localFH.extra_length = htos(&foundat[28], FOREMOST_LITTLE_ENDIAN);;
localFH.genFlag = htos(&foundat[6], FOREMOST_LITTLE_ENDIAN);
// Sanity checking
if (localFH.compressed > needle->max_len)
return foundat + needle->header_len;
if (localFH.filename_length > 100)
return foundat + needle->header_len;
//Check if we should grab more from the disk
if (localFH.compressed + 30 > buflen - (foundat - buf))
{
return NULL;
}
//Size of the local file header data structure
foundat += 30;
if (strcmp(needle->suffix,"zip")==0)
{
if (strncmp((char *)foundat, "content.xml", 11) == 0 && strcmp(needle->suffix,"zip")==0)
{
oOffice = TRUE;
sprintf(comment, " (OpenOffice Doc?)");
strcat(needle->comment, comment);
needle->suffix = "sx";
}
else if (strstr((char *)foundat, ".class") || strstr((char *)foundat, ".jar") ||
strstr((char *)foundat, ".java"))
{
needle->suffix = "jar";
}
else if(strncmp((char *)foundat, "[Content_Types].xml",19)==0)
{
office2007=TRUE;
}
else if(strncmp((char *)foundat, "ppt/slides",10)==0 && office2007==TRUE)
{
needle->suffix = "pptx";
}
else if(strncmp((char *)foundat, "word/document.xml",17)==0 && office2007==TRUE)
{
needle->suffix = "docx";
}
else if(strncmp((char *)foundat, "xl/workbook.xml",15)==0 && office2007==TRUE)
{
needle->suffix = "xlsx";
}
else
{
printf("foundat=%s\n",foundat);
}
}
foundat += localFH.compressed;
foundat += localFH.filename_length;
foundat += localFH.extra_length;
if (localFH.genFlag == 8)
{
#ifdef DEBUG
fprintf(stderr,"We have extra stuff!!!");
#endif
}
if(localFH.genFlag & 1<<3 && localFH.uncompressed==0 && localFH.compressed==0 )
{
#ifdef DEBUG
fprintf(stderr,"No data to jmp Just search for the next file Footer (localFH.genFlag:=%d)\n",localFH.genFlag);
#endif
break;
}
#ifdef DEBUG
printf("localFH.compressed:=%d localFH.uncompressed:=%d\n\t jumping %d bytes filename=%d bytes",
localFH.compressed,
localFH.uncompressed,localFH.filename_length+localFH.compressed+localFH.extra_length,localFH.filename_length);
printx(foundat, 0, 16);
#endif
}
else if (oOffice && localFH.genFlag == 8)
{
break;
}
else
{
break;
}
}//end while loop
if (oOffice)
{
//We have an OO doc how long should we search for?
bytes_to_search = 1 * MEGABYTE;
}
else if (localFH.genFlag & 1<<3 && localFH.uncompressed==0 && localFH.compressed==0 )
{
bytes_to_search = needle->max_len;
}
else
{
bytes_to_search = (buflen < (foundat - buf) ? buflen : buflen - (foundat - buf));
}
//Make sure we are not searching more than what he have
if (buflen <= (foundat - buf)) {
#ifdef DEBUG
printf("avoided bug in extract_zip!\n");
#endif
bytes_to_search = 0;
} else {
if (buflen - (foundat - buf) < bytes_to_search)
{
bytes_to_search = buflen - (foundat - buf);
}
}
currentpos = foundat;
#ifdef DEBUG
printf("Search for the footer bytes_to_search:=%lld buflen:=%lld\n", bytes_to_search, buflen);
#endif
foundat = bm_search(needle->footer,
needle->footer_len,
foundat,
bytes_to_search,
needle->footer_bm_table,
needle->case_sen,
SEARCHTYPE_FORWARD);
#ifdef DEBUG
printf("Search complete \n");
#endif
if (foundat) /*Found the end of the central directory structure, determine the exact length and extract*/
{
/*Jump to the comment length field*/
#ifdef DEBUG
printf("distance searched:=%lu\n", foundat - currentpos);
#endif
if (buflen - (foundat - buf) > 20)
{
foundat += 20;
}
else
{
return NULL;
}
comment_length = htos(foundat, FOREMOST_LITTLE_ENDIAN);
foundat += comment_length + 2;
file_size = (foundat - buf);
#ifdef DEBUG
printf("File size %lld\n", file_size);
printf("Found a %s type:=%s\n", needle->suffix, type);
#endif
extractbuf = buf;
if (strcmp(type,"all")==0 || strcmp(type,needle->suffix)==0)
{
#ifdef DEBUG
printf("Writing a %s to disk\n", needle->suffix);
#endif
write_to_disk(s, needle, file_size, extractbuf, c_offset + f_offset);
}
#ifdef DEBUG
printf("Found a %s\n", needle->suffix);
#endif
return foundat-2;
}
if (bytes_to_search > buflen - (currentpos - buf))
return NULL;
#ifdef DEBUG
printf("I give up \n");
#endif
return currentpos;
}
/********************************************************************************
*Function: extract_pdf
*Description: Given that we have a PDF header check if it is Linearized, if so
grab the file size and we are done, else search for the %%EOF
*Return: A pointer to where the EOF of the PDF is in the current buffer
**********************************************************************************/
unsigned char *extract_pdf(f_state *s, u_int64_t c_offset, unsigned char *foundat, u_int64_t buflen,
s_spec *needle, u_int64_t f_offset)
{
unsigned char *currentpos = NULL;
unsigned char *buf = foundat;
unsigned char *extractbuf = NULL;
unsigned char *tempsize;
unsigned long int size = 0;
int file_size = 0;
unsigned char *header = foundat;
int bytes_to_search = 0;
char comment[32];
foundat += needle->header_len; /* Jump Past the %PDF HEADER */
currentpos = foundat;
#ifdef DEBUG
printf("PDF SEARCH\n");
#endif
/*Determine when we have searched enough*/
if (buflen >= needle->max_len)
{
bytes_to_search = needle->max_len;
}
else
{
bytes_to_search = buflen;
}
/*Check if the buffer is less than 100 bytes, if so search what we have*/
if (buflen < 512)
return NULL;
else
{
currentpos = foundat;
/*Check for .obj in the first 100 bytes*/
foundat = bm_search(needle->markerlist[1].value,
needle->markerlist[1].len,
foundat,
100,
needle->markerlist[1].marker_bm_table,
needle->case_sen,
SEARCHTYPE_FORWARD);
if (!foundat)
{
#ifdef DEBUG
printf("no obj found\n");
#endif
return currentpos + 100;
}
foundat = currentpos;
/*Search for "./L " to see if the file is linearized*/
foundat = bm_search(needle->markerlist[2].value,
needle->markerlist[2].len,
foundat,
512,
needle->markerlist[2].marker_bm_table,
needle->case_sen,
SEARCHTYPE_FORWARD);
if (foundat)
{
foundat = bm_search(needle->markerlist[0].value,
needle->markerlist[0].len,
foundat,
512,
needle->markerlist[0].marker_bm_table,
needle->case_sen,
SEARCHTYPE_FORWARD);
}
else
{
#ifdef DEBUG
printf("not linearized\n");
#endif
}
}
if (foundat) /*The PDF is linearized extract the size and we are done*/
{
sprintf(comment, " (PDF is Linearized)");
strcat(needle->comment, comment);
foundat += needle->markerlist[0].len;
tempsize = (unsigned char *)malloc(8 * sizeof(char));
tempsize = memcpy(tempsize, foundat, 8);
size = atoi((char *)tempsize);
free(tempsize);
if (size <= 0)
return foundat;
if (size > buflen)
{
if (size > needle->max_len)
return foundat;
else
return NULL;
}
header += size;
foundat = header;
foundat -= needle->footer_len;
/*Jump back 10 bytes and see if we actually have and EOF there*/
foundat -= 10;
currentpos = foundat;
foundat = bm_search(needle->footer,
needle->footer_len,
foundat,
needle->footer_len + 9,
needle->footer_bm_table,
needle->case_sen,
SEARCHTYPE_FORWARD);
if (foundat) /*There is an valid EOF at the end, Write to disk*/
{
foundat += needle->footer_len + 1;
file_size = (foundat - buf);
extractbuf = buf;
write_to_disk(s, needle, file_size, extractbuf, c_offset + f_offset);
return foundat;
}
return NULL;
}
else /*Search for Linearized PDF failed, just look for %%EOF */
{
#ifdef DEBUG
printf(" Linearized search failed, searching %d bytes, buflen:=%lld\n",
bytes_to_search,
buflen - (header - buf));
#endif
foundat = currentpos;
foundat = bm_search(needle->footer,
needle->footer_len,
foundat,
bytes_to_search,
needle->footer_bm_table,
needle->case_sen,
SEARCHTYPE_FORWARD);
if (foundat) /*Write the non-linearized PDF to disk*/
{
foundat += needle->footer_len + 1;
file_size = (foundat - buf);
extractbuf = buf;
write_to_disk(s, needle, file_size, extractbuf, c_offset + f_offset);
return foundat;
}
return NULL;
}
}
/********************************************************************************
*Function: extract_cpp
*Description: Use keywords to attempt to find C/C++ source code
*Return: A pointer to where the EOF of the CPP file is in the current buffer
**********************************************************************************/
unsigned char *extract_cpp(f_state *s, u_int64_t c_offset, unsigned char *foundat, u_int64_t buflen,
s_spec *needle, u_int64_t f_offset)
{
unsigned char *header = foundat;
unsigned char *buf = foundat;
unsigned char *extractbuf = NULL;
int end = 0;
int start = 0;
int i = 0;
int marker_score = 0;
int ok = FALSE;
int file_size = 0;
unsigned char *footer = NULL;
/*Search for a " or a < within 20 bytes of a #include statement*/
for (i = 0; i < 20; i++)
{
if (foundat[i] == '\x22' || foundat[i] == '\x3C')
{
ok = TRUE;
}
}
if (!ok)
return foundat + needle->header_len;
/*Keep running through the buffer until an non printable character is reached*/
while (isprint(foundat[end]) || foundat[end] == '\x0a' || foundat[end] == '\x09')
{
end++;
}
foundat += end - 1;
footer = foundat;
if (end < 50)
return foundat;
/*Now lets go the other way and grab all those comments at the begining of the file*/
while (isprint(buf[start]) || buf[start] == '\x0a' || buf[start] == '\x09')
{
start--;
}
header = &buf[start + 1];
file_size = (footer - header);
foundat = header;
/*Now we have an ascii file to look for keywords in*/
foundat = bm_search(needle->footer,
needle->footer_len,
header,
file_size,
needle->footer_bm_table,
FALSE,
SEARCHTYPE_FORWARD);
if (foundat)
marker_score += 1;
foundat = header;
foundat = bm_search(needle->markerlist[0].value,
needle->markerlist[0].len,
header,
file_size,
needle->markerlist[0].marker_bm_table,
1,
SEARCHTYPE_FORWARD);
if (foundat)
marker_score += 1;
if (marker_score == 0)
return foundat;
if (foundat)
{
extractbuf = buf;
write_to_disk(s, needle, file_size, extractbuf, c_offset + f_offset + start + 1);
return footer;
}
return NULL;
}
/********************************************************************************
*Function: extract_htm
*Description: Given that we have a HTM header
search for the file EOF and check that the bytes areound the header are ascii
*Return: A pointer to where the EOF of the HTM is in the current buffer
**********************************************************************************/
unsigned char *extract_htm(f_state *s, u_int64_t c_offset, unsigned char *foundat, u_int64_t buflen,
s_spec *needle, u_int64_t f_offset)
{
unsigned char *buf = foundat;
unsigned char *extractbuf = NULL;
unsigned char *currentpos = NULL;
int bytes_to_search = 0;
int i = 0;
int file_size = 0;
/*Jump past the <HTML tag*/
foundat += needle->header_len;
/*Check the first 16 bytes to see if they are ASCII*/
for (i = 0; i < 16; i++)
{
if (!isprint(foundat[i]) && foundat[i] != '\x0a' && foundat[i] != '\x09')
{
return foundat + 16;
}
}
/*Determine if the buffer is large enough to encompass a reasonable search*/
if (buflen < needle->max_len)
{
bytes_to_search = buflen - (foundat - buf);
}
else
{
bytes_to_search = needle->max_len;
}
/*Store the current position and search for the HTML> tag*/
currentpos = foundat;
foundat = bm_search(needle->footer,
needle->footer_len,
foundat,
bytes_to_search,
needle->footer_bm_table,
needle->case_sen,
SEARCHTYPE_FORWARD);
if (foundat) //Found the footer, write to disk
{
file_size = (foundat - buf) + needle->footer_len;
extractbuf = buf;
write_to_disk(s, needle, file_size, extractbuf, c_offset + f_offset);
foundat += needle->footer_len;
return foundat;
}
else
{
return NULL;
}
}
/********************************************************************************
*Function: validOLEheader
*Description: run various tests aginst an OLE-HEADER to determine whether or not
it is valid.
*Return: TRUE/FALSE
**********************************************************************************/
int valid_ole_header(struct OLE_HDR *h)
{
if (htos((unsigned char *) &h->reserved, FOREMOST_LITTLE_ENDIAN) != 0 ||
htoi((unsigned char *) &h->reserved1, FOREMOST_LITTLE_ENDIAN) != 0 ||
htoi((unsigned char *) &h->reserved2, FOREMOST_LITTLE_ENDIAN) != 0)
{
return FALSE;
}
/*The minimum sector shift is usually 2^6(64) and the uSectorShift is 2^9(512))*/
if (htos((unsigned char *) &h->uMiniSectorShift, FOREMOST_LITTLE_ENDIAN) != 6 ||
htos((unsigned char *) &h->uSectorShift, FOREMOST_LITTLE_ENDIAN) != 9 ||
htoi((unsigned char *) &h->dir_flag, FOREMOST_LITTLE_ENDIAN) < 0)
{
return FALSE;
}
/*Sanity Checking*/
if (htoi((unsigned char *) &h->num_FAT_blocks, FOREMOST_LITTLE_ENDIAN) <= 0 ||
htoi((unsigned char *) &h->num_FAT_blocks, FOREMOST_LITTLE_ENDIAN) > 100)
{
return FALSE;
}
if (htoi((unsigned char *) &h->num_extra_FAT_blocks, FOREMOST_LITTLE_ENDIAN) < 0 ||
htoi((unsigned char *) &h->num_extra_FAT_blocks, FOREMOST_LITTLE_ENDIAN) > 100)
{
return FALSE;
}
return TRUE;
}
/********************************************************************************
*Function:checkOleName
*Description: Determine what type of file is stored in the OLE format based on the
names of DIRENT in the FAT table.
*Return: A char* consisting of the suffix of the appropriate file.
**********************************************************************************/
char *check_ole_name(char *name)
{
if (strstr(name, "WordDocument"))
{
return "doc";
}
else if (strstr(name, "Worksheet") || strstr(name, "Book") || strstr(name, "Workbook"))
{
return "xls";
}
else if (strstr(name, "Power"))
{
return "ppt";
}
else if (strstr(name, "Access") || strstr(name, "AccessObjSiteData"))
{
return "mbd";
}
else if (strstr(name, "Visio"))
{
return "vis";
}
else if (strstr(name, "Sfx"))
{
return "sdw";
}
else
{
return NULL;
}
return NULL;
}
int adjust_bs(int size, int bs)
{
int rem = (size % bs);
if (rem == 0)
{
return size;
}
#ifdef DEBUG
printf("\tnew size:=%d\n", size + (bs - rem));
#endif
return (size + (bs - rem));
}
/********************************************************************************
*Function: extract_ole
*Description: Given that we have a OLE header, jump through the OLE structure and
determine what type of file it is.
*Return: A pointer to where the EOF of the OLE is in the current buffer
**********************************************************************************/
unsigned char *extract_ole(f_state *s, u_int64_t c_offset, unsigned char *foundat, u_int64_t buflen,
s_spec *needle, u_int64_t f_offset, char *type)
{
unsigned char *buf = foundat;
unsigned char *extractbuf = NULL;
char *temp = NULL;
char *suffix = "ole";
int totalsize = 0;
int extrasize = 0;
int oldblk = 0;
int i, j;
int size = 0;
int blknum = 0;
int validblk = 512;
int file_size = 0;
int num_extra_FAT_blocks = 0;
unsigned char *htoi_c = NULL;
int extra_dir_blocks = 0;
int num_FAT_blocks = 0;
int next_FAT_block = 0;
unsigned char *p;
int fib = 1024;
struct OLE_HDR *h = NULL;
int result = 0;
int highblock = 0;
unsigned long miniSectorCutoff = 0;
unsigned long csectMiniFat = 0;
/*Deal with globals defined in the OLE API, ugly*/
if (dirlist != NULL)
free(dirlist);
if (FAT != NULL)
free(FAT);
init_ole();
if (buflen < validblk)
validblk = buflen;
h = (struct OLE_HDR *)foundat; /*cast the header block to point at foundat*/
#ifdef DEBUG
dump_header(h);
#endif
num_FAT_blocks = htoi((unsigned char *) &h->num_FAT_blocks, FOREMOST_LITTLE_ENDIAN);
if (!valid_ole_header(h))
return (buf + validblk);
miniSectorCutoff = htoi((unsigned char *) &h->miniSectorCutoff, FOREMOST_LITTLE_ENDIAN);
csectMiniFat = htoi((unsigned char *) &h->csectMiniFat, FOREMOST_LITTLE_ENDIAN);
next_FAT_block = htoi((unsigned char *) &h->FAT_next_block, FOREMOST_LITTLE_ENDIAN);
num_extra_FAT_blocks = htoi((unsigned char *) &h->num_extra_FAT_blocks, FOREMOST_LITTLE_ENDIAN);
FAT = (int *)Malloc(OUR_BLK_SIZE * (num_FAT_blocks + 1));
p = (unsigned char *)FAT;
memcpy(p, &h[1], OUR_BLK_SIZE - FAT_START);
if (next_FAT_block > 0)
{
p += (OUR_BLK_SIZE - FAT_START);
blknum = next_FAT_block;
for (i = 0; i < num_extra_FAT_blocks; i++)
{
if (!get_block(buf, blknum, p, buflen))
return buf + validblk;
validblk = (blknum + 1) * OUR_BLK_SIZE;
p += OUR_BLK_SIZE - sizeof(int);
blknum = htoi(p, FOREMOST_LITTLE_ENDIAN);
}
}
blknum = htoi((unsigned char *) &h->root_start_block, FOREMOST_LITTLE_ENDIAN);
if(blknum < 0)
{
return buf + 10;
}
highblock = htoi((unsigned char *) &h->dir_flag, FOREMOST_LITTLE_ENDIAN);
#ifdef DEBUG
printf("getting dir block\n");
#endif
//if(!get_dir_block (buf, blknum, buflen)) return buf+validblk;
if (!get_block(buf, blknum, buffer, buflen))
return buf + validblk; /*GET DIR BLOCK*/
#ifdef DEBUG
printf("done getting dir block\n");
#endif
validblk = (blknum + 1) * OUR_BLK_SIZE;
while (blknum != END_OF_CHAIN)
{
#ifdef DEBUG
printf("finding dir info extra_dir_blks:=%d\n", extra_dir_blocks);
#endif
if (extra_dir_blocks > 300)
return buf + validblk;
/**PROBLEMA**/
#ifdef DEBUG
printf("***blknum:=%d FATblk:=%d ourblksize=%d\n", blknum, FATblk,OUR_BLK_SIZE);
#endif
oldblk = blknum;
htoi_c = (unsigned char *) &FAT[blknum / (OUR_BLK_SIZE / sizeof(int))];
FATblk = htoi(htoi_c, FOREMOST_LITTLE_ENDIAN);
#ifdef DEBUG
printf("***blknum:=%d FATblk:=%d\n", blknum, FATblk);
#endif
if (!get_FAT_block(buf, blknum, block_list, buflen))
return buf + validblk;
blknum = htoi((unsigned char *) &block_list[blknum % 128], FOREMOST_LITTLE_ENDIAN);
#ifdef DEBUG
printf("**blknum:=%d FATblk:=%d\n", blknum, FATblk);
#endif
if (blknum == END_OF_CHAIN || oldblk == blknum)
{
#ifdef DEBUG
printf("EOC\n");
#endif
break;
}
extra_dir_blocks++;
result = get_dir_block(buf, blknum, buflen);
if (result == SHORT_BLOCK)
{
#ifdef DEBUG
printf("SHORT BLK\n");
#endif
break;
}
else if (!result)
return buf + validblk;
}
#ifdef DEBUG
printf("DONE WITH WHILE\n");
#endif
blknum = htoi((unsigned char *) &h->root_start_block, FOREMOST_LITTLE_ENDIAN);
size = OUR_BLK_SIZE * (extra_dir_blocks + 1);
dirlist = (struct DIRECTORY *)Malloc(size);
memset(dirlist, 0, size);
if (!get_block(buf, blknum, buffer, buflen))
return buf + validblk; /*GET DIR BLOCK*/
if (!get_dir_info(buffer))
{
return foundat + validblk;
}
for (i = 0; i < extra_dir_blocks; i++)
{
if (!get_FAT_block(buf, blknum, block_list, buflen))
return buf + validblk;
blknum = htoi((unsigned char *) &block_list[blknum % 128], FOREMOST_LITTLE_ENDIAN);
if (blknum == END_OF_CHAIN)
break;
#ifdef DEBUG
printf("getting dir blk blknum=%d\n", blknum);
#endif
if (!get_block(buf, blknum, buffer, buflen))
return buf + validblk; /*GET DIR BLOCK*/
if (!get_dir_info(buffer))
{
return buf + validblk;
}
}
#ifdef DEBUG
printf("dir count is %d\n", i);
#endif
for (dl = dirlist, i = 0; i < dir_count; i++, dl++)
{
memset(buffer, ' ', 75);
j = htoi((unsigned char *) &dl->level, FOREMOST_LITTLE_ENDIAN) * 4;
sprintf((char *) &buffer[j], "%-s", dl->name);
j = strlen((char *)buffer);
if (dl->name[0] == '@')
return foundat + validblk;
if (dl->type == STREAM)
{
buffer[j] = ' ';
sprintf((char *) &buffer[60], "%8d\n", dl->size);
if (temp == NULL) /*check if we have alread defined the type*/
{
temp = check_ole_name(dl->name);
if (temp)
suffix = temp;
}
if (dl->size > miniSectorCutoff)
{
totalsize += adjust_bs(dl->size, 512);
}
else
{
totalsize += adjust_bs(dl->size, 64);
}
#ifdef DEBUG
fprintf(stdout, buffer);
#endif
}
else
{
sprintf((char *) &buffer[j], "\n");
#ifdef DEBUG
printf("\tnot stream data \n");
fprintf(stdout, buffer);
#endif
extrasize += adjust_bs(dl->size, 512);
}
}
totalsize += fib;
#ifdef DEBUG
printf("DIR SIZE:=%d, numFATblks:=%d MiniFat:=%d\n",
adjust_bs(((dir_count) * 128), 512),
(num_FAT_blocks * 512),
adjust_bs((64 * csectMiniFat), 512));
#endif
totalsize += adjust_bs(((dir_count) * 128), 512);
totalsize += (num_FAT_blocks * 512);
totalsize += adjust_bs((64 * csectMiniFat), 512);
if ((highblk + 5) > highblock && highblk > 0)
{
highblock = highblk + 5;
}
highblock = highblock * 512;
#ifdef DEBUG
printf("\t highblock:=%d\n", highblock);
#endif
if (highblock > totalsize)
{
#ifdef DEBUG
printf(" Total size:=%d a difference of %lld\n", totalsize, buflen - totalsize);
printf(" Extra size:=%d \n", extrasize);
printf(" Highblock is greater than totalsize\n");
#endif
totalsize = highblock;
}
totalsize = adjust_bs(totalsize, 512);
#ifdef DEBUG
printf(" Total size:=%d a difference of %lld\n", totalsize, buflen - totalsize);
printf(" Extra size:=%d \n", extrasize);
#endif
if (buflen < totalsize)
{
#ifdef DEBUG
printf(" ***Error not enough left in the buffer left:=%lld needed=%d***\n",
buflen,
totalsize);
#endif
totalsize = buflen;
}
foundat = buf;
highblock -= 5 * 512;
if (highblock > 0 && highblock < buflen)
{
foundat += highblock;
}
else
{
foundat += totalsize;
}
/*Return to the highest blknum read in the file, that way we don't miss files that are close*/
file_size = totalsize;
extractbuf = buf;
if (suffix)
needle->suffix = suffix;
if (!strstr(needle->suffix, type) && strcmp(type,"all")!=0)
{
return foundat;