-
Notifications
You must be signed in to change notification settings - Fork 5
/
Main.mm
168 lines (130 loc) · 3.77 KB
/
Main.mm
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
// TODO: use a Makefile already, this is ridiculous
#import "Extern.h"
void trace(NSString* format,...)
{
va_list args;
va_start(args,format);
NSString* message=[NSString.alloc initWithFormat:format arguments:args].autorelease;
va_end(args);
printf("\e[%dm%s\e[0m\n",31+DSCE_VERSION%6,message.UTF8String);
}
BOOL flagPad=false;
#import "LocationBase.h"
#import "Location.h"
@class CacheImage;
#import "CacheFile.h"
@class CacheSet;
#import "ImageHeader.h"
#import "Address.h"
#import "CacheImage.h"
#import "CacheSet.h"
#import "Selector.h"
#import "Output.h"
#import "Location.m"
#import "CacheFile.m"
#import "ImageHeader.m"
#import "Address.m"
#import "CacheImage.m"
#import "CacheSet.m"
#import "Selector.m"
#import "Output.m"
void process(NSMutableArray<NSString*>* args)
{
NSString* cachePath=args[0];
[args removeObjectAtIndex:0];
CacheSet* cache=[CacheSet.alloc initWithPathPrefix:cachePath].autorelease;
assert(cache);
if([args containsObject:@"list"])
{
assert(args.count==1);
NSArray<CacheImage*>* images=[cache imagesWithPathPrefix:@"/"];
trace(@"list %x images",images.count);
for(CacheImage* image in images)
{
trace(@"%@",image.path);
}
return;
}
if([args containsObject:@"search"])
{
assert(args.count==2);
NSData* target=[args[1] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableSet* seen=NSMutableSet.alloc.init.autorelease;
trace(@"search %@",target);
for(CacheFile* file in cache.files)
{
long offset=0;
while(true)
{
NSRange range=[file.data rangeOfData:target options:0 range:NSMakeRange(offset,file.data.length-offset)];
if(range.location==NSNotFound)
{
break;
}
else
{
CacheImage* image=[cache imageWithAddress:wrapOffset(file,range.location).address];
if(![seen containsObject:image.path])
{
trace(@"%@",image.path);
if(image.path)
{
[seen addObject:image.path];
}
}
offset=range.location+range.length;
}
}
}
return;
}
flagPad=[args containsObject:@"pad"];
[args removeObject:@"pad"];
NSMutableArray<CacheImage*>* images=NSMutableArray.alloc.init.autorelease;
for(NSString* arg in args)
{
NSArray<CacheImage*>* subset=[cache imagesWithPathPrefix:arg];
if(subset.count==0)
{
trace(@"no images found for %@*",arg);
abort();
}
[images addObjectsFromArray:subset];
}
for(int index=0;index<images.count;index++)
{
trace(@"extract %@ (%x/%x)",images[index].path,index+1,images.count);
// TODO: check for leaks between images
@autoreleasepool
{
double startTime=NSDate.date.timeIntervalSince1970;
NSString* outPath=[@"Out" stringByAppendingString:images[index].path];
NSString* outFolder=outPath.stringByDeletingLastPathComponent;
assert([NSFileManager.defaultManager createDirectoryAtPath:outFolder withIntermediateDirectories:true attributes:nil error:nil]);
[Output runWithCache:cache image:images[index] outPath:outPath];
trace(@"image took %.2lf seconds",NSDate.date.timeIntervalSince1970-startTime);
}
}
}
int main(int argc,char** argv)
{
NSString* edge=[@"" stringByPaddingToLength:9+log10(DSCE_VERSION) withString:@"─" startingAtIndex:0];
trace(@"┌%@┐",edge);
trace(@"│ dsce v%d │",DSCE_VERSION);
trace(@"└%@┘",edge);
if(argc<3)
{
trace(@"usage: dsce <first cache file> ( list | search <string> | [pad] <extraction path prefix> ... )");
return 1;
}
double startTime=NSDate.date.timeIntervalSince1970;
NSMutableArray<NSString*>* args=NSMutableArray.alloc.init.autorelease;
for(int index=1;index<argc;index++)
{
NSString* arg=[NSString stringWithUTF8String:argv[index]];
[args addObject:arg];
}
process(args);
trace(@"total %.2lf seconds",NSDate.date.timeIntervalSince1970-startTime);
return 0;
}