File: XADXZHandle.m

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (296 lines) | stat: -rw-r--r-- 6,798 bytes parent folder | download
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
#import "XADXZHandle.h"
#import "XADLZMA2Handle.h"
#import "XAD7ZipBranchHandles.h"
#import "XADDeltaHandle.h"
#import "XADException.h"
#import "CRC.h"
#import "Progress.h"


#define StreamHeaderState 0
#define BlockHeaderState 1
#define BlockDataState 2
#define BlockPaddingState 3
#define BlockChecksumState 4
#define StreamIndexState 5
#define StreamFooterState 6
#define StreamPaddingState 7
#define EndState 10

static uint64_t ParseInteger(CSHandle *fh);

@implementation XADXZHandle

-(id)initWithHandle:(CSHandle *)handle
{
	return [self initWithHandle:handle length:CSHandleMaxLength];
}

-(id)initWithHandle:(CSHandle *)handle length:(off_t)length
{
	if((self=[super initWithName:[handle name] length:length]))
	{
		parent=[handle retain];
		startoffs=[parent offsetInFile];
		currhandle=nil;
	}
	return self;
}

-(void)dealloc
{
	[parent release];
	[currhandle release];
	[super dealloc];
}

-(void)resetStream
{
	[parent seekToFileOffset:startoffs];

	[currhandle release];
	currhandle=nil;

	state=StreamHeaderState;
	checksumscorrect=YES;
	checksumflags=0;
}

-(int)streamAtMost:(int)num toBuffer:(void *)buffer
{
	int bytesread=0;
	uint8_t *bytebuf=buffer;

	while(bytesread<num && state!=EndState) switch(state)
	{
		 case StreamHeaderState:
		 {
			uint8_t head[6];
			[parent readBytes:6 toBuffer:head];
			if(head[0]!=0xfd||head[1]!='7'||head[2]!='z'||head[3]!='X'||head[4]!='Z'||head[5]!=0)
			[XADException raiseIllegalDataException];

			int version=[parent readUInt8];
			if(version!=0) [XADException raiseIllegalDataException];

			checksumflags=[parent readUInt8];
			if(checksumflags&0xf0) [XADException raiseIllegalDataException];
			[parent skipBytes:4]; // skip CRC

			state=BlockHeaderState;
		}
		break;

		case BlockHeaderState:
		{
			int blockheadsize=[parent readUInt8];
			if(blockheadsize==0)
			{
				state=StreamIndexState;
				break;
			}

			off_t streamstart=[parent offsetInFile]+blockheadsize*4+3;

			int blockflags=[parent readUInt8];
			if(blockflags&0x3c) [XADException raiseIllegalDataException];

			int numfilters=(blockflags&3)+1;

			if(blockflags&0x40) ParseInteger(parent);
			if(blockflags&0x80) ParseInteger(parent);

			uint64_t ids[4];
			NSData *properties[4];

			for(int i=0;i<numfilters;i++)
			{
				ids[i]=ParseInteger(parent);
				uint64_t size=ParseInteger(parent);
				properties[i]=[parent readDataOfLength:size];
			}

			[parent seekToFileOffset:streamstart];

			CSHandle *handle=parent;
			for(int i=numfilters-1;i>=0;i--)
			{
				switch(ids[i])
				{
					case 3: handle=[[[XADDeltaHandle alloc] initWithHandle:handle propertyData:properties[i]] autorelease]; break;

					case 4: handle=[[[XAD7ZipBCJHandle alloc] initWithHandle:handle propertyData:properties[i]] autorelease]; break;
					case 5: handle=[[[XAD7ZipPPCHandle alloc] initWithHandle:handle propertyData:properties[i]] autorelease]; break;
					case 6: handle=[[[XAD7ZipIA64Handle alloc] initWithHandle:handle propertyData:properties[i]] autorelease]; break;
					case 7: handle=[[[XAD7ZipARMHandle alloc] initWithHandle:handle propertyData:properties[i]] autorelease]; break;
					case 8: handle=[[[XAD7ZipThumbHandle alloc] initWithHandle:handle propertyData:properties[i]] autorelease]; break;
					case 9: handle=[[[XAD7ZipSPARCHandle alloc] initWithHandle:handle propertyData:properties[i]] autorelease]; break;

					case 33:
					{
						XADLZMA2Handle *lh=[[[XADLZMA2Handle alloc] initWithHandle:handle propertyData:properties[i]] autorelease];
						[lh setSeekBackAtEOF:YES];
						handle=lh;
					}
					break;
				}
			}

			currhandle=[handle retain];

			//currhandle=[[self decompressHandleForHandleAtBlockHeader:parent] retain];

			switch(checksumflags)
			{
				case 1: crc=0xffffffff; break;
				case 4: crc=0xffffffffffffffff; break;
			}

			state=BlockDataState;
		}
		break;

		case BlockDataState:
		{
			int actual=[currhandle readAtMost:num-bytesread toBuffer:&bytebuf[bytesread]];

			switch(checksumflags)
			{
				case 1:
					crc=XADCalculateCRC(crc,&bytebuf[bytesread],actual,XADCRCTable_edb88320);
				break;

				case 4:
					crc=XADCalculateCRC64(crc,&bytebuf[bytesread],actual,XADCRCTable_c96c5795d7870f42);
				break;
			}

			bytesread+=actual;

			if([currhandle atEndOfFile])
			{
				[currhandle release];
				currhandle=nil;
				state=BlockPaddingState;
			}
		}
		break;

		case BlockPaddingState:
			[parent skipBytes:(-([parent offsetInFile]-startoffs))&3];
			state=BlockChecksumState;
		break;

		case BlockChecksumState:
			switch(checksumflags)
			{
				case 0: break; // none

				case 1: // crc32
				{
					uint32_t correctcrc=[parent readUInt32LE];
					if((crc^0xffffffff)!=correctcrc) checksumscorrect=NO;
				}
				break;

				case 4: // crc64
				{
					uint64_t correctcrc=[parent readUInt64LE];
					if((crc^0xffffffffffffffff)!=correctcrc) checksumscorrect=NO;
				}
				break;

				case 2: case 3: [parent skipBytes:4]; break;
				case 5: case 6: [parent skipBytes:8]; break;
				case 7: case 8: case 9: [parent skipBytes:16]; break;
				case 10: case 11: case 12: [parent skipBytes:32]; break;
				case 13: case 14: case 15: [parent skipBytes:64]; break;
			}
			state=BlockHeaderState;
		break;

		case StreamIndexState:
		{
			uint64_t numrecords=ParseInteger(parent);
			for(uint64_t i=0;i<numrecords;i++)
			{
				// Just skip the index records
				ParseInteger(parent);
				ParseInteger(parent);
			}

			[parent skipBytes:(-([parent offsetInFile]-startoffs))&3];
			[parent skipBytes:4]; // skip CRC

			state=StreamFooterState;
		}
		break;

		case StreamFooterState:
			[parent skipBytes:8]; // skip CRC and backwards size
			if([parent readUInt8]!=0) [XADException raiseIllegalDataException];
			if([parent readUInt8]!=checksumflags) [XADException raiseIllegalDataException];
			if([parent readUInt8]!='Y') [XADException raiseIllegalDataException];
			if([parent readUInt8]!='Z') [XADException raiseIllegalDataException];

			state=StreamPaddingState;
		break;

		case StreamPaddingState:
			for(;;)
			{
				if([parent atEndOfFile])
				{
					state=EndState;
					break;
				}
				uint32_t pad=[parent readUInt32BE];
				if(pad=='\3757zX')
				{
					[parent skipBytes:-4];
					state=StreamHeaderState;
					break;
				}
				else if(pad!=0) [XADException raiseIllegalDataException];
			}
		break;
	}

	if(state==EndState) [self endStream];

	return bytesread;
}

-(BOOL)hasChecksum
{
	return checksumflags==1||checksumflags==4;
}

-(BOOL)isChecksumCorrect
{
	return checksumscorrect;
}

-(double)estimatedProgress { return [parent estimatedProgress]; } // TODO: better estimation using buffer?

@end




static uint64_t ParseInteger(CSHandle *fh)
{
	uint64_t res=0;
	int pos=0;
	uint8_t b;

	do
	{
		b=[fh readUInt8];
		res|=(b&0x7f)<<pos;
		pos+=7;
	}
	while(b&0x80);

	return res;
}