-
Notifications
You must be signed in to change notification settings - Fork 0
/
implements.cc
2166 lines (1758 loc) · 65.1 KB
/
implements.cc
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
#include "rippers.cc"
#include <stdio.h>
// Implemented rippers.
// -- //
// Size rippers: LBM, ANM, PBM, HLP, WAV, WRI, EXE, FLI, MSP [50%]
// To add:
// IFF rippers are easy - FORM then size field (MSB) then the ID
class iff_ripper : public size_ripper {
private:
string ext;
int max_len;
public:
iff_ripper(string ID, string extension, int maxlen);
string get_extension() { return (ext); }
int suggest_max_length() { return(max_len); }
};
iff_ripper::iff_ripper(string ID, string extension, int maxlen) {
ext = extension;
max_len = maxlen;
set_size_pattern('?', "FORM????" + ID, true, 4, 8, 4, 1, true);
};
class lbm_ripper : public iff_ripper {
public:
lbm_ripper() : iff_ripper("ILBMBMHD", ".lbm", 76800) {}
// 320x240 < 76800
};
class anm_ripper : public iff_ripper {
public:
anm_ripper() : iff_ripper("ANIMFORM", ".anm", -1) {}
};
class pbm_ripper : public iff_ripper {
public:
pbm_ripper() : iff_ripper("PBM ", ".pbm", 76800) {}
};
/*class lbm_ripper : public size_ripper { // DeluxePaint pictures
public:
lbm_ripper() { set_size_pattern('?', "FORM????ILBMBMHD",
true, 4, 8, 4, 1, true); }
string get_extension() { return(".lbm"); }
int suggest_max_length() { return(76800); } // 320x240
};
class anm_ripper : public size_ripper { // DA animations
public:
anm_ripper() { set_size_pattern('?' , "FORM????ANIMFORM", true,
4, 8, 4, 1, true); }
string get_extension() { return(".anm"); }
};
class pbm_ripper : public size_ripper { // DA brushes or compressed images
public:
pbm_ripper() { set_size_pattern('?', "FORM????PBM ", true,
4, 8, 4, 1, true); }
string get_extension() { return(".pbm"); }
int suggest_max_length() { return(76800); }
};*/
// some other IFF formats (they don't cost much) - untested
class svx_ripper : public iff_ripper {
public:
svx_ripper() : iff_ripper("8SVX", ".svx", -1) {}
};
class abm_ripper : public iff_ripper {
public:
abm_ripper() : iff_ripper("ANBM", ".abm", -1) {}
};
// standarized Interactive Fiction (Z-code) saves
class quetzal_ripper : public iff_ripper {
public:
quetzal_ripper() : iff_ripper("IFZSIF", ".quetzal", -1) {}
};
class wav_ripper : public size_ripper { // MS Windows .WAV sound format
public:
wav_ripper() { set_size_pattern('?', "RIFF????WAVEfmt", true,
4, 8, 4, 1, false); }
string get_extension() { return(".wav"); }
};
class cdr_ripper : public size_ripper { // Corel draw .CDR format
public:
cdr_ripper() { set_size_pattern('?', "RIFF????CDR?vrsn",
true, 4, 8, 4, 1, false); }
string get_extension() { return(".cdr"); }
};
class hlp_ripper : public size_ripper { // MS Windows help file
public:
hlp_ripper();
string get_extension() { return(".hlp"); }
int suggest_max_length() { return(16777216); } //rounded up
};
hlp_ripper::hlp_ripper() {
// ? _ 0x3 0x0, four unknowns, then FF FF FF FF, then the size field.
string hdr(12, 0xFF);
hdr[0] = '?'; hdr[1] = '_'; hdr[2] = 3; hdr[4] = 'X';
hdr[5] = 'X'; hdr[6] = 'X'; hdr[7] = 'X';
set_size_pattern('X', hdr, true, 12, 0, 4, 1, false);
};
class wri_ripper : public size_ripper { // MS Windows 3.x Write documents
// 0x31BE (or 32BE) followed by 3 zeroes then 0xAB. Size field
// should be multiplied by 128.
public:
wri_ripper();
string get_extension() { return(".wri"); }
int suggest_max_length() { return(524288); }
};
wri_ripper::wri_ripper() {
string first_hdr(6, 0);
first_hdr[0] = 0x31;
first_hdr[1] = 0xBE;
first_hdr[5] = 0xAB;
string sec_hdr = first_hdr;
sec_hdr[0] = 0x32;
vector<string> headers;
headers.push_back(first_hdr);
headers.push_back(sec_hdr);
set_size_pattern('?', headers, true, 0x60, 0, 4, 128, false);
};
// can't be used yet (because of clue problems) (Now works.)
// Magic is either 0x44616ED9 (v1) or 0x4C696E53 (v2).
// Then two LSB u_shorts follow, width and height (in that order). Size is
// then equal to (Width / 8) * Height + 32, though that may overestimate for
// v2 files (which are RLE encoded). Note the /8 - that is because MSP is 2-
// color and thus 1 pixel = 1 bit.
class msp_ripper : public size_ripper {
private:
int get_integer(char_rope::const_iterator pos);
string get_extension() { return(".msp"); }
// very generous: 2 * (640x480 / 8)
int suggest_max_length() { return(65536); }
public:
msp_ripper();
};
int msp_ripper::get_integer(char_rope::const_iterator pos) {
// assumes we're at width.
int width = (u_char)*(pos) + ((u_char)*(pos+1) << 8);
int height = (u_char)*(pos+2) + ((u_char)*(pos+3) << 8);
return ((width >> 3) * height + 32);
};
// We can now have multiple headers!
msp_ripper::msp_ripper() {
string first = "xxxx"; vector<string> headers;
first[0] = 0x44; first[1] = 0x61; first[2] = 0x6E;
first[3] = 0xD9;
headers.push_back(first); // v1
first[0] = 0x4C; first[1] = 0x69; first[2] = 0x6E;
first[3] = 0x53;
headers.push_back(first);
set_size_pattern('?', headers, true, 4, 0, 4, 1, false);
}
// ?? ?? ?? ?? 11 AF xx xx : ?? is the size field, x is the number of frames
// all LSB.
class fli_ripper : public size_ripper {
private:
int get_header_here(char_rope::const_iterator pos,
char_rope::const_iterator end);
public:
fli_ripper();
string get_extension() { return(".fli"); }
};
int fli_ripper::get_header_here(char_rope::const_iterator pos,
char_rope::const_iterator end) {
// first check against the magic
int p = check_matches(header_targets, common_to_header, pos,
end);
if (p == -1) return(-1); // thumbs down
// Now get the number of frames
// This is from the POV of the size field, so we must add in its
// 4 bytes and the 2 bytes of the title.
int numframes = (u_char)*(pos+6) + ((u_char)*(pos+7) << 8);
// 0 frames make no sense, and the upper limit as given by Autodesk
// Anim is 4000.
if (numframes == 0 || numframes > 4000) return(-1);
// get width and height, shouldn't be > 320x200
int width = (u_char)*(pos+8) + ((u_char)*(pos+9) << 8);
if (width > 320 || width == 0) return(-1);
int height = (u_char)*(pos+10) + ((u_char)*(pos+11) << 8);
if (height > 200 || height == 0) return(-1);
// bpp - this is not a HDRI format
int bpp = (u_char)*(pos+12) + ((u_char)*(pos+13) << 8);
if (bpp == 0 || bpp > 8) return(-1);
return(0);
}
fli_ripper::fli_ripper() {
string hdr = "????";
hdr.push_back(0x11);
hdr.push_back(0xAF);
set_size_pattern('?', hdr, true, 0, 0, 4, 1, false);
};
// MS Windows bitmap. BM then u_long size. We add some header checks to
// keep the false positives from heaping up.
class bmp_ripper : public size_ripper {
public:
int get_header_here(char_rope::const_iterator position,
char_rope::const_iterator end);
string get_extension() { return(".bmp"); }
bmp_ripper() { set_size_pattern('?', "BM", true, 2, 0, 4, 1,
false); }
};
int bmp_ripper::get_header_here(char_rope::const_iterator position, char_rope::
const_iterator end) {
// Hack. TODO: Fix this. Check matches doesn't work for some odd
// reason.
// Now it does [I think]
/*if (*position != 'B') return(-1);
if (*(position+1) != 'M') return(-1);*/
// usual check
int p = check_matches(header_targets, common_to_header, position,
end);
if (p == -1) return(p);
// now check that index 0E is 0x28 and then three times 0.
// Also check that the file length > 0.
int len = get_integer(position+2);
if (len == 0) return(-1);
char_rope::const_iterator chk = position + 0x0E;
if (*chk++ != 0x28) return(-1);
if (*chk++ != 0) return(-1);
if (*chk++ != 0) return(-1);
return(0);
}
// MS-DOS executable (EXE) - note, does not work on PE / NE files:
// MZ then two bytes PartPag, then two bytes PagCnt, then size is
// PagCnt * 512 - (512 - PartPag).
// To match, PartPag < 512, and if PartPag == 0 then PagCnt > 0.
// Then at index 10, there's a min memory LSB u_short (<= 40960) and max memory
// just after (> 0 and >= min_memory, also <= 40960). Or does 0 mean "take all
// you can get"?
class exe_ripper : public size_ripper {
private:
int get_u_short(char_rope::const_iterator position);
int get_header_here(char_rope::const_iterator position,
char_rope::const_iterator end);
int get_integer(char_rope::const_iterator pos);
public:
exe_ripper();
string get_extension() { return(".exe"); }
int suggest_max_length() { return(2097152); }
};
// gets a LSB u_short size field
int exe_ripper::get_u_short(char_rope::const_iterator position) {
return ((unsigned char)*position + ( (unsigned char)*(position+1) <<
8));
}
int exe_ripper::get_header_here(char_rope::const_iterator position,
char_rope::const_iterator end) {
if (*position != 'M') return(-1);
if (*(position+1) != 'Z') return(-1);
int partpag = get_u_short(position+2);
if (partpag > 512) return(-1);
int pagcnt = get_u_short(position+4);
//cout << "Partpag: " << partpag << endl;
//cout << "Pagcnt: " << pagcnt << endl;
// Since the equation is pagcnt * 512 - (512 - partpag), having
// partpag != 512 would give negative results.
// Also assume that partpag is never > 512, because then DOS would
// have updated pagecount instead (sketchy, but I gotta do something
// about all these false positives)
if (pagcnt == 0 && partpag != 512) return(-1);
if (partpag > 512) return(-1);
int lin_size = pagcnt * 512 - (512 - partpag);
if (lin_size < 0) return(-1);
int min_mem = get_u_short(position + 10);
if (min_mem >= 40960) return(-1); // 40960 = 640k / 16
int max_mem = get_u_short(position + 12);
// allowing max_mem == min_mem == 0 gives too many false positives.
if (max_mem <= min_mem /* && max_mem != 0*/) return(-1);
//cout << max_mem << "\t" << min_mem << endl;
// Assume overlay is < 255, that is that there are a reasonable number
// of overlays.
int overlays = get_u_short(position + 0x1A);
if (overlays > 255) return(-1);
// Assume relocation offset < length of file and that it also isn't
// in the header
int reloc = get_u_short(position + 0x18);
if (reloc > lin_size) return(-1);
if (reloc < 0x1C) return(-1);
return(0);
}
int exe_ripper::get_integer(char_rope::const_iterator pos) {
// assumes pos is at partpag
int partpag = get_u_short(pos);
int pagcnt = get_u_short(pos+2);
return(pagcnt * 512 - (512 - partpag));
}
exe_ripper::exe_ripper() {
// most for the sake of the clue generator
set_size_pattern('?', "MZ", true, 2, 0, 2, 1, false);
}
//////////////////////////////////////////////////////////////////////////////
// TODO: For RTF, enforce that the byte after the {\rtf? must be printable.
// That gets rid of EXE files that contain an RTF renderer.
// Wildcard rippers: GIF, PNG, HTML, compressed BAS, RTF, ZIP, BAT (50%), JPG
// ARJ, ANS, ZZT, FRM, uncompressed BAS
// To add:
// MBBS callers' log.
class mbbs_callers_ripper : public text_ripper {
private:
int get_footer_length(int footer_num) { return(0); }
public:
mbbs_callers_ripper();
string get_extension() { return("_callers.bbs"); }
int should_skip() { return(count); } // since there may be
// multiple quits/restarts in a log
};
mbbs_callers_ripper::mbbs_callers_ripper() {
// using ? would create unintended trigraphs.
string header = "xx:xx xx-xx-xx System UP! ";
string footer(16, 0); // never match
set_patterns('x', header, footer, true);
};
class html_ripper : public text_ripper {
public:
html_ripper() { set_patterns('?', "<HTML>", "</HTML>", false);}
string get_extension() { return(".html"); }
int suggest_max_length() { return(4194304); }
};
// Visual Basic
class frm_ripper : public text_ripper {
public:
frm_ripper();
string get_extension() { return(".frm"); }
int suggest_max_length() { return(262144); }
};
frm_ripper::frm_ripper() {
string init = "VERSION ?.00";
string next = "xx" + init;
init.push_back(13); init.push_back(10);
next[0] = 13; next[1] = 10;
set_patterns('?', init, next, true);
}
// it's here but you can't use it yet (because of lousy clue generation)
// [Commented out the part that generates additional headers.]
// Now clue generation is a bit better.
class uncompressed_bas_ripper : public text_ripper {
private:
// same reason as in BAT ripper
int get_footer_length(int footer_num) { return(0); }
public:
int should_skip() { return(count); } // always skip
string get_extension() { return(".txt.bas"); }
uncompressed_bas_ripper();
};
uncompressed_bas_ripper::uncompressed_bas_ripper() {
string ftr(16, 0); // never match because we don't actually know what
// the end is like - no fixed footer.
vector<string> headers; // here we go
headers.push_back("DECLARE SUB ");
headers.push_back("DECLARE FUNCTION ");
headers.push_back("DEFDBL ");
headers.push_back("DEFSNG ");
headers.push_back("RANDOMIZE TIMER");
headers.push_back("SCREEN 12");
headers.push_back("SCREEN 13");
headers.push_back("DIM ");
headers.push_back("OPEN \"");
headers.push_back("PRINT USING #");
headers.push_back("COMMON SHARED");
vector<string> footers(1, ftr);
set_patterns('?', headers, footers, true);
}
class compressed_bas_ripper : public wildcard_ripper {
public:
compressed_bas_ripper();
string get_extension() { return(".bas"); }
int suggest_max_length() { return(262144); }
};
compressed_bas_ripper::compressed_bas_ripper() {
unsigned char hdr[17] = {0xFC, 0, 1, 0, 0x0C, 0, 0x81, 1, 0x82, 1, 6, 0,
1, 2, 3, 4};
string hdr_string;
for (int counter = 0; counter < 16; counter++)
hdr_string.push_back(hdr[counter]);
// 6 FFs then >0 then 3x 00 then ? then 00 then 02
// We use wildcards for the >0 part.
string footer(14, 0xFF);
footer[6] = '?';
footer[7] = 0;
footer[8] = 0;
footer[9] = 0;
footer[10] = '?';
footer[11] = 0;
footer[12] = 2;
footer[13] = '?';
set_patterns('?', hdr_string, footer, true);
}
class png_ripper : public wildcard_ripper {
public:
png_ripper();
string get_extension() { return(".png"); }
};
png_ripper::png_ripper() {
string header = "xPNGxxx";
header[0] = 0x89;
header[4] = 0x0D;
header[5] = 0x0A;
header[6] = 0x1A;
set_patterns('?', header, "IEND????", true);
}
class gif_ripper : public wildcard_ripper {
public:
gif_ripper();
string get_extension() { return(".gif"); }
};
gif_ripper::gif_ripper() {
// create vector of possible headers
vector<string> headers, footers;
headers.push_back("GIF89a");
headers.push_back("GIF87a");
// really just 00 3B but how are we going to match that without
// false positives?
string foot(3, 0);
foot[2] = 0x3B;
footers.push_back(foot);
set_patterns('?', headers, footers, true);
}
// Trying ANSi - 0x1B 0x5Bh, stop when < 0x20 and not 0x1B, 13, or 10, or
// smileys (1 or 2).
// We also check that ESC is suffixed with [, as it should be.
// Obviously you'll want to skip the area once done.
class ans_ripper : public wildcard_ripper {
private:
int get_header_here(char_rope::const_iterator pos,
char_rope::const_iterator end);
int get_footer_here(char_rope::const_iterator pos,
char_rope::const_iterator end);
public:
int should_skip() { return(count); }
ans_ripper();
string get_extension() { return(".ans"); }
};
int ans_ripper::get_header_here(char_rope::const_iterator pos,
char_rope::const_iterator end) {
// If the first is not ESC (0x1B), forget it
// Otherwise, if the three next are not printable, forget it
// Otherwise, if none of index 2, 3 are numbers, forget it
// Otherwise, if it ends in less than 20 bytes, forget it (removes
// false positives)
// Otherwise, OK!
if (*pos != 0x1B) return(-1);
if (isspace(*pos)) return(-1);
if (*(pos+1) != '[') return(-1);
if (!isprint(*(pos+1)) || !isprint(*(pos+2)) || !isprint(*(pos+3)))
return(-1);
if (!isdigit(*(pos+2)) && !isdigit(*(pos+3))) return(-1);
for (int counter = 1; counter < 20; counter++) {
if (pos + counter == end) return(-1);
if (is_footer_here(pos+counter, end)) return(-1);
}
return(0);
};
int ans_ripper::get_footer_here(char_rope::const_iterator pos,
char_rope::const_iterator end) {
// If it is 01 or 02 (smileys) or > 0x20, OK (not footer)
// If it is 13 and +1 is 10, OK (CRLF); if it is 10, also OK (LF part)
// If it is 0x1B (ESC) and +1 is 0x5B (opening bracket), OK
// else not OK.
unsigned char p = *pos;
if (p == 1 || p == 2 || p >= 0x20 || p == 10) return(-1);
if (p == 13 && *(pos+1) == 10) return(-1);
if (p == 0x1B && *(pos+1) == '[') return(-1);
return(0);
};
ans_ripper::ans_ripper() {
string hdr(2, '[');
hdr[0] = 0x1B;
set_patterns('?', hdr, "", true);
}
// now checks that the first byte after the "header" is printable or 13 (CR).
class rtf_ripper : public text_ripper {
private:
int get_header_here(char_rope::const_iterator pos,
char_rope::const_iterator end);
public:
rtf_ripper();
string get_extension() { return(".rtf"); }
};
int rtf_ripper::get_header_here(char_rope::const_iterator position,
char_rope::const_iterator end) {
int first_stage = check_matches(header_targets, common_to_header,
position, end);
if (first_stage == -1) return(-1);
char after = *(position+header_targets[first_stage].size()+1);
if (isprint(after) || after == 13 || after == 10) return(first_stage);
else return(-1);
}
rtf_ripper::rtf_ripper() {
// begins with {\rtf and ends in \par[CRLF]} or [LF]\par }
vector<string> headers, footers;
headers.push_back("{\\rtf");
footers.push_back("\\par??}");
footers.push_back("?\\par }");
set_patterns('?', headers, footers, false);
}
class bat_ripper : public wildcard_ripper {
private:
int get_footer_here(char_rope::const_iterator position,
char_rope::const_iterator end);
// the nonalphanumerics aren't really part of the file
int get_footer_length(int footer_num) { return(0); }
public:
bat_ripper();
string get_extension() { return(".bat"); }
int suggest_max_length() { return(16384); }
};
// A BAT file's "footer" is three nonprintable bytes. (Two would match CRLF)
// It's a bit of a hack, but it works.
int bat_ripper::get_footer_here(char_rope::const_iterator position,
char_rope::const_iterator end) {
if (!isprint(*position) && !isprint(*(position+1)) && !isprint(*
(position+2)))
return(0);
else return(-1);
}
bat_ripper::bat_ripper() {
vector<string> headers;
// we use the ? for alignment purposes. This will go away if I
// implement a real clue-finding system.
headers.push_back("?@echo off");
headers.push_back("@ echo off");
headers.push_back("@set ");
headers.push_back("@rem ");
vector<string> footers(1, "");
set_patterns('?', headers, footers, false);
};
// ARJ: 0x60 0xEA, then the sixth byte is >= 2 and <= 15, and eighth byte is
// < 9. Ends in 60 EA 00.
class arj_ripper : public wildcard_ripper {
private:
int get_header_here(char_rope::const_iterator
position, char_rope::const_iterator end);
public:
arj_ripper();
string get_extension() { return(".arj"); }
};
int arj_ripper::get_header_here(char_rope::const_iterator pos,
char_rope::const_iterator end) {
char_rope::const_iterator begin = pos;
if ((u_char)(*pos++) != 0x60) return(-1);
if ((u_char)(*pos++) != 0xEA) return(-1);
// the next two constitute the header size field. They must be
// greater than 0 and less than 2600 (decimal). LSB.
int hszfield = (u_char)(*pos++) + ( (u_char)(*pos++) << 8);
if (hszfield == 0 || hszfield > 2600) return(-1);
// next byte x, 2 <= x <= 15.
pos++;
if (*pos < 2 || *pos > 15) return(-1); // archiver used
pos++;
if (*pos++ > 15) return(-1); // min. archiver needed to extr.
if (*pos++ > 9) return(-1); // host OS
pos++;
//if (*pos++ == 0) return(-1); // arj flags - too defensive.
pos++; // security flag
// uncomment if you want ZIP-type file fragments.
// That may catch what would otherwise be lost, but as it is, it gives
// too many false positives.
// A ZIP-type state would be really nice here: if we've visited the
// area before, only extract *pos == 2 files, otherwise extract both.
if (*pos != 2 /* || *pos != 0 */) return(-1); // file type, must be 2
/* Checking
for (pos = begin; pos != begin + 0x10; pos++) {
printf("%.2x ", (unsigned char)*pos);
}
printf ("\n");*/
return(0);
}
arj_ripper::arj_ripper() {
string header = "XX";
string footer = "XXXX"; // 60 EA 00 00
header[0] = 0x60;
header[1] = 0xEA;
footer[0] = 0x60;
footer[1] = 0xEA;
footer[2] = 0;
footer[3] = 0;
set_patterns('?', header, footer, true);
};
// JPEG: FF D8 FF E0, two unknowns, then JFIF or Exif.
// The file ends in FF D9, but the last FF xx sequence before it must
// have been FF DA (handles Photoshop pictures that have more than one
// FF D9). Thus it is placed here, as it is a bit more advanced than a
// simple wildcard searcher.
class jpg_ripper : public wildcard_ripper {
private:
bool ff_seen;
bool last_da;
public:
jpg_ripper();
string get_extension() { return(".jpg"); }
int dump_from_header(char_rope::const_iterator position,
char_rope::const_iterator end, int max_length,
ostream & output);
};
jpg_ripper::jpg_ripper() {
// set header and footer
string first_hdr = "??????JFIF";
first_hdr[0] = 0xFF;
first_hdr[1] = 0xD8;
first_hdr[2] = 0xFF;
first_hdr[3] = 0xE0;
string sec_hdr = first_hdr;
sec_hdr[6] = 'E';
sec_hdr[7] = 'x';
sec_hdr[8] = 'i';
sec_hdr[9] = 'f';
vector<string> headers;
headers.push_back(first_hdr);
headers.push_back(sec_hdr);
vector<string> footers(1);
footers[0].resize(2);
footers[0][0] = 0xFF;
footers[0][1] = 0xD9;
set_patterns('?', headers, footers, true);
};
int jpg_ripper::dump_from_header(char_rope::const_iterator position,
char_rope::const_iterator end, int max_length, ostream &
output) {
if (!is_header_here(position, end)) return(0);
ff_seen = true;
last_da = false;
bool done = false;
count = 0;
while (count < max_length && position != end && !done) {
// Have we found a legitimate footer, and was the last FF
// sequence FF DA?
if (last_da)
if (is_footer_here(position, end))
done = true; // yes, we're done
if (done) continue;
// else, check for FF; if we had an FF at the previous
// byte, check for DA.
if (!ff_seen) {
if ((u_char)*position == 0xFF) ff_seen = true;
} else {
if ((u_char)*position == 0xDA) last_da = true;
ff_seen = false;
}
output << *position++;
count++;
}
int remaining = 0;
int footer_identified = get_footer_here(position, end);
if (footer_identified != -1)
remaining = footer_offset(position, end) +
get_footer_length(footer_identified);
for (int counter = 0; counter < remaining ; counter++)
output << *position++;
count += remaining;
return(count);
}
// MBBS "USERS.DAT" file. We match the postal code in order to find out
// that there may be an USERS.DAT here, and then we go backwards to find
// the first entry. Finally, we go forwards to the last, and dump all.
class mbbs_users_ripper : public wildcard_ripper {
private:
// is_userdat_esque - called by get_header and
// dump_from_header in different ways
// used to check if we have the ubiquitous "length strings"
// of MBBS
bool is_length_string(char_rope::const_iterator pos,
char_rope::const_iterator end, bool
accept_zero, int limit_above, bool
only_numbers);
bool is_userdat_struct(char_rope::const_iterator pos,
char_rope::const_iterator end);
int get_header_here(char_rope::const_iterator pos,
char_rope::const_iterator end);
int dump_from_header(char_rope::const_iterator pos,
char_rope::const_iterator end, int max_length,
ostream & output);
public:
int should_skip() { return(count); } //std fare for stream fmts
string get_extension() { return("_userdat.bbs"); }
mbbs_users_ripper();
};
// returns true if we have a length string with maximum length limit_above
// only_numbers also accepts punctuation, because people who don't want to
// divulge their phone numbers often used punctuation as a substitute.
bool mbbs_users_ripper::is_length_string(char_rope::const_iterator pos,
char_rope::const_iterator end, bool accept_zero, int limit_above,
bool only_numbers) {
// first get the length byte
unsigned char lb = *pos;
if (lb == 0 && !accept_zero) return(false);
if (lb > limit_above) return(false);
// now check that all the characters after it are printable
for (int counter = 0; counter < lb && pos < (end-1); counter++)
if (only_numbers) {
++pos;
if (!isdigit(*pos) && !ispunct(*pos)) return(false);
} else
if (!isprint(*(++pos))) return(false);
return(true);
}
bool mbbs_users_ripper::is_userdat_struct(char_rope::const_iterator pos,
char_rope::const_iterator end) {
if (pos >= end) return(false);
// MBBS USERS.DAT user field has the following length strings
// at 00: name, max length 30 chars (should have space also)
// at 0x1F: street address, max length 30 chars
// at 0x3E: postal code, max length 30 chars (who'da guessed it)
// at 0x5D: telephone number, max length 15 chars, all numbers or -
// at 0x6E: work telephone number, ditto
if (!is_length_string(pos, end, false, 30, false)) return(false);
if (!is_length_string(pos+0x1F, end, false, 30, false)) return(false);
if (!is_length_string(pos+0x3E, end, false, 30, false)) return(false);
if (!is_length_string(pos+0x5D, end, false, 15, true)) return(false);
// almost never used, so be a bit forgiving
if (!is_length_string(pos+0x6E, end, false, 15, false)) return(false);
return(true);
}
int mbbs_users_ripper::get_header_here(char_rope::const_iterator pos,
char_rope::const_iterator end) {
// standard code
int p = check_matches(header_targets, common_to_header, pos, end);
if (p == -1) return(p); // no? shoo!
if (!is_userdat_struct(pos, end)) return(-1);
return(p);
}
// need a begin() for this
int mbbs_users_ripper::dump_from_header(char_rope::const_iterator pos,
char_rope::const_iterator end, int max_length,
ostream & output) {
if (!is_header_here(pos, end)) return(0);
// First scan backwards -- there may be some people in the users.dat
// file who don't have the right postal code, and thus we passed them
// by.
// Note that count only counts the number of bytes *after* the header
// we found at first. This way should_skip synchronizes correctly.
char_rope::const_iterator incursion = pos;
pos -= 0x400;
while (is_userdat_struct(pos, end))
pos -= 0x400;
// now we're at the first that's not a header. Add 0x400 to get back
// on track.
char_rope::const_iterator very_begin = pos + 0x400;
pos = incursion + 0x400;
// now scan forwards
while (is_userdat_struct(pos, end))
pos += 0x400;
// we now are 0x400 past the last header.
char_rope::const_iterator very_end = pos; // inclusive
//cout << very_end - very_begin << endl;
// And now, the moment you've all been waiting for
count = 0;
for (pos = very_begin; pos != very_end && pos != end; pos++) {
if (pos >= incursion) count++;
output << *pos;
}
return(count);
}
mbbs_users_ripper::mbbs_users_ripper() {
// The postal code starts at 0x3F, but we ignore the first four digits
// in favor of the name of the location itself. The space is included
// in the match, thus there are 0x3F + 4 = 67 unknowns, a space, and
// then the name.
// Obviously only applicable to one country.
count = 0;
string prefix(67, '?');
prefix += ' ';
vector<string> headers;
headers.push_back(prefix + "Arendal");
headers.push_back(prefix + "Bergen");
headers.push_back(prefix + "Bodø");
headers.push_back(prefix + "Drammen");
headers.push_back(prefix + "Fredrikstad");
headers.push_back(prefix + "Hamar");
headers.push_back(prefix + "Kristiansand");
headers.push_back(prefix + "Leikanger");
headers.push_back(prefix + "Lillehammer");
headers.push_back(prefix + "Molde");
headers.push_back(prefix + "Oslo");
headers.push_back(prefix + "Porsgrunn");
headers.push_back(prefix + "Sandnes");
headers.push_back(prefix + "Sarpsborg");
headers.push_back(prefix + "Skien");
headers.push_back(prefix + "Stavanger");
headers.push_back(prefix + "Steinkjer");
headers.push_back(prefix + "Tønsberg");
headers.push_back(prefix + "Tromsø");
headers.push_back(prefix + "Trondheim");
headers.push_back(prefix + "Vadsø");
vector<string> footer(1, "");
set_patterns('?', headers, footer, false); // case-insensitive
};
// Hybrid size-ripper / wildcard-ripper. I use wildcard ripper for its
// automatic clue construction.
class zzt_ripper : public wildcard_ripper {
private:
int get_header_here(char_rope::const_iterator pos,
char_rope::const_iterator end);
public:
string get_extension() { return(".zzt"); }
int suggest_max_length() { return(524288); }
int dump_from_header(char_rope::const_iterator pos,
char_rope::const_iterator end, int max_length,
ostream & output);
zzt_ripper(); // lip service to wildcard_ripper
};
int zzt_ripper::get_header_here(char_rope::const_iterator pos, char_rope::
const_iterator end) {
// Extremely Permissive ZZT Ripper
// Done for quality checks.
if (pos+0x200 > end) return(-1); // no zzt file is this small
// first two FF?
if ((u_char)*(pos) != 0xFF) return(-1);
if ((u_char)*(pos+1) != 0xFF) return(-1);
// OK, is number three zero? MSB for number of boards, but number
// of boards is limited above by 0xA0
if (*(pos+3) != 0) return(-1);
// okay, does the LSB (byte 2) agree with limits?
int num_boards = (unsigned char)*(pos+2) + 1;
// counter excludes title screen, so we add one.
if (num_boards > 0xA0) return(-1);
// Check title length first to distinguish from zero runs
// Also do a sanity check
if (*(pos+0x1D) == 0 || *(pos+0x1D) >= 0x30) return(-1);
// Okay, now check the keys
char_rope::const_iterator beginning = pos;
for (pos = beginning + 8; pos < beginning + 0xD; pos++)