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
|
#import "XADNSAParser.h"
#import "CSMemoryHandle.h"
#import "CSBzip2Handle.h"
#import "XADRegex.h"
static NSMutableData *DecodeSPB(CSHandle *fh,uint32_t length);
static void SetSPBPixel(uint8_t *pixels,int bytesperrow,int width,int height,int channel,int n,uint8_t val);
static NSMutableData *MakeBMPContainer(int width,int height,uint32_t length,int *bytesperrowptr);
@implementation XADNSAParser
+(int)requiredHeaderSize { return 6; }
+(BOOL)recognizeFileWithHandle:(CSHandle *)handle firstBytes:(NSData *)data name:(NSString *)name
{
if(!name) return NO;
if(![[name lastPathComponent] matchedByPattern:@"^arc[0-9]*\\.nsa$" options:REG_ICASE]) return NO;
//const uint8_t *bytes=[data bytes];
//int length=[data length];
return YES;
}
-(void)parse
{
CSHandle *fh=[self handle];
int numfiles=[fh readUInt16BE];
if(numfiles==0) numfiles=[fh readUInt16BE];
uint32_t offset=[fh readUInt32BE];
for(int i=0;i<numfiles && [self shouldKeepParsing];i++)
{
NSMutableData *namedata=[NSMutableData data];
uint8_t c;
while((c=[fh readUInt8])) [namedata appendBytes:&c length:1];
int method=[fh readUInt8];
uint32_t dataoffs=[fh readUInt32BE];
uint32_t datalen=[fh readUInt32BE];
uint32_t filesize=[fh readUInt32BE];
NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithObjectsAndKeys:
[self XADPathWithData:namedata separators:XADWindowsPathSeparator],XADFileNameKey,
[NSNumber numberWithUnsignedLong:filesize],XADFileSizeKey,
[NSNumber numberWithUnsignedLong:datalen],XADCompressedSizeKey,
[NSNumber numberWithUnsignedLong:datalen],XADDataLengthKey,
[NSNumber numberWithUnsignedLong:dataoffs+offset],XADDataOffsetKey,
[NSNumber numberWithInt:method],@"NSAMethod",
nil];
NSString *methodname=nil;
switch(method)
{
case 0: methodname=@"None"; break;
case 1: methodname=@"SPB"; break;
case 2: methodname=@"LZSS"; break;
case 4: methodname=@"Bzip2"; break;
}
if(methodname) [dict setObject:[self XADStringWithString:methodname] forKey:XADCompressionNameKey];
[self addEntryWithDictionary:dict retainPosition:YES];
}
}
-(CSHandle *)handleForEntryWithDictionary:(NSDictionary *)dict wantChecksum:(BOOL)checksum
{
CSHandle *handle=[self handleAtDataOffsetForDictionary:dict];
int method=[[dict objectForKey:@"NSAMethod"] intValue];
uint32_t length=[[dict objectForKey:XADFileSizeKey] unsignedIntValue];
switch(method)
{
case 0:
return handle;
case 1:
return [CSMemoryHandle memoryHandleForReadingData:DecodeSPB(handle,length)];
case 2:
//return handle;
return [[[XADNSALZSSHandle alloc] initWithHandle:handle length:length] autorelease];
case 4:
return [CSBzip2Handle bzip2HandleWithHandle:handle length:length];
}
[self reportInterestingFileWithReason:@"Unsupported compression method %d",method];
return nil;
}
-(NSString *)formatName { return @"NSA"; }
@end
@implementation XADNSALZSSHandle
-(id)initWithHandle:(CSHandle *)handle length:(off_t)length
{
return [super initWithHandle:handle length:length windowSize:256];
}
-(void)resetLZSSHandle
{
uint8_t c1=CSInputNextByte(input);
uint8_t c2=CSInputNextByte(input);
if(c1==0xa1&&c2==0x53) CSInputRestart(input);
}
-(int)nextLiteralOrOffset:(int *)offset andLength:(int *)length atPosition:(off_t)pos
{
if(CSInputNextBit(input)) return CSInputNextBitString(input,8);
else
{
*offset=pos-CSInputNextBitString(input,8)-17;
*length=CSInputNextBitString(input,4)+2;
return XADLZSSMatch;
}
}
@end
static NSMutableData *DecodeSPB(CSHandle *fh,uint32_t length)
{
int width=[fh readUInt16BE];
int height=[fh readUInt16BE];
int bytesperrow;
NSMutableData *data=MakeBMPContainer(width,height,length,&bytesperrow);
uint8_t *bytes=[data mutableBytes];
uint8_t *pixels=&bytes[54];
CSInputBuffer *input=CSInputBufferAlloc(fh,[fh fileSize]);
@try
{
for(int channel=0;channel<3;channel++)
{
int n=0;
uint8_t val=CSInputNextBitString(input,8);
SetSPBPixel(pixels,bytesperrow,width,height,channel,n++,val);
while(n<width*height)
{
int nbits=CSInputNextBitString(input,3);
if(nbits==0)
{
for(int i=0;i<4 && n<=width*height;i++)
SetSPBPixel(pixels,bytesperrow,width,height,channel,n++,val);
continue;
}
int mask;
if(nbits==7) mask=CSInputNextBitString(input,1)+1;
else mask=nbits+2;
for(int i=0;i<4 && n<=width*height;i++)
{
if(mask==8) val=CSInputNextBitString(input,8);
else
{
int t=CSInputNextBitString(input,mask);
if(t&1) val+=(t>>1)+1;
else val-=(t>>1);
}
SetSPBPixel(pixels,bytesperrow,width,height,channel,n++,val);
}
}
}
}
@catch(id e)
{
}
CSInputBufferFree(input);
return data;
}
static void SetSPBPixel(uint8_t *pixels,int bytesperrow,int width,int height,int channel,int n,uint8_t val)
{
if(n>=width*height) return;
int x=n%width;
int y=n/width;
if(y&1) x=width-1-x;
y=height-1-y;
pixels[channel+x*3+y*bytesperrow]=val;
}
static NSMutableData *MakeBMPContainer(int width,int height,uint32_t length,int *bytesperrowptr)
{
int bytesperrow=((width*3)+3)&~3;
int bmpsize=54+bytesperrow*height;
if(length>bmpsize) bmpsize=length;
NSMutableData *data=[NSMutableData dataWithLength:bmpsize];
uint8_t *bytes=[data mutableBytes];
bytes[0]='B';
bytes[1]='M';
CSSetInt32LE(&bytes[2],bmpsize); // FILESIZE
CSSetInt32LE(&bytes[10],54); // OFFSET
CSSetInt32LE(&bytes[14],40); // BLOCKSIZE
CSSetInt32LE(&bytes[18],width); // WIDTH
CSSetInt32LE(&bytes[22],height); // HEIGHT
CSSetInt16LE(&bytes[26],1); // FIELDS = 1
CSSetInt16LE(&bytes[28],24); // BPP = 24
if(bytesperrowptr) *bytesperrowptr=bytesperrow;
return data;
}
|