File: XADRPMParser.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 (186 lines) | stat: -rw-r--r-- 4,627 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
#import "XADRPMParser.h"

@implementation XADRPMParser

+(int)requiredHeaderSize { return 96; }

+(BOOL)recognizeFileWithHandle:(CSHandle *)handle firstBytes:(NSData *)data name:(NSString *)name
{
	const uint8_t *bytes=[data bytes];
	int length=[data length];

	if(length<6) return NO;
	if(bytes[0]==0xed&&bytes[1]==0xab&&bytes[2]==0xee&&bytes[3]==0xdb)
	{
		return YES;
	}
	return NO;
}

static int FindStringLength(const uint8_t *buffer,int size,int offset)
{
	int len=0;
	while(offset+len<size&&buffer[offset+len]) len++;
	return len;
}

static uint64_t ParseInt(const uint8_t *buffer,int size,int type,int offset)
{
	if(type<2||type>5) return 0;

	if(offset+(1<<type-2)>size) return 0;

	if(type==2) return buffer[offset];
	else if(type==3) return CSUInt16BE(&buffer[offset]);
	else if(type==4) return CSUInt32BE(&buffer[offset]);
	else if(type==5) return CSUInt64BE(&buffer[offset]);
	else return 0; // Can't happen
}

-(void)parse
{
	CSHandle *fh=[self handle];

	[fh skipBytes:4];
	int major=[fh readUInt8];
	/*int minor=*/[fh readUInt8];
	/*int type=*/[fh readUInt16BE];
	/*int archnum=*/[fh readUInt16BE];

	uint8_t name[66];
	[fh readBytes:66 toBuffer:name];
	int namelen=FindStringLength(name,66,0);
	NSMutableData *namedata=[NSMutableData dataWithBytes:name length:namelen];

	/*int osnum=*/[fh readUInt16BE];
	int sigtype=[fh readUInt16BE];
	[fh skipBytes:16];

	switch(sigtype)
	{
		case 0: break;

		case 1:
			[fh skipBytes:256];
		break;

		case 5:
		{
			if([fh readID]!=0x8eade801) [XADException raiseIllegalDataException];
			[fh skipBytes:4];
			int sigentries=[fh readUInt32BE];
			int sigbytes=[fh readUInt32BE];
			[fh skipBytes:sigentries*16+(sigbytes+7&~7)];
		}
		break;

		default:
			[self reportInterestingFileWithReason:@"Unsupported signature type %d",sigtype];
			[XADException raiseNotSupportedException];
	}

	const char *archiveext="cpio";
	const char *compressionext="gz";

	if(major>1)
	{
		if(major>2)
		{
			if([fh readID]!=0x8eade801) [XADException raiseIllegalDataException];
			[fh skipBytes:4];
		}

		int headentries=[fh readUInt32BE];
		int headbytes=[fh readUInt32BE];

		NSData *entrydata=[fh readDataOfLength:headentries*16];
		NSData *storagedata=[fh readDataOfLength:headbytes];

		const uint8_t *entries=[entrydata bytes];
		const uint8_t *storage=[storagedata bytes];


		for(int i=0;i<headentries;i++)
		{
			uint32_t tag=CSUInt32BE(entries+i*16+0);
			//uint32_t type=CSUInt32BE(entries+i*16+4);
			uint32_t offset=CSUInt32BE(entries+i*16+8);
			//uint32_t count=CSUInt32BE(entries+i*16+12);

			switch(tag)
			{
				case 1000:
				{
					int len=FindStringLength(storage,headbytes,offset);
					namedata=[NSMutableData dataWithBytes:storage+offset length:len];
				}
				break;

				/*case 1046:
				{
					int somelength=ParseInt(storage,headbytes,type,offset);
				}
				break;*/

				case 1124:
				{
					int len=FindStringLength(storage,headbytes,offset);
					if(len==4&&memcmp(storage+offset,"cpio",4)==0) archiveext="cpio";
					else archiveext=NULL;
				}
				break;

				case 1125:
				{
					int len=FindStringLength(storage,headbytes,offset);
					if(len==4&&memcmp(storage+offset,"gzip",4)==0) compressionext="gz";
					else if(len==5&&memcmp(storage+offset,"bzip2",5)==0) compressionext="bz2";
					else if(len==4&&memcmp(storage+offset,"lzma",4)==0) compressionext="lzma";
					else if(len==2&&memcmp(storage+offset,"xz",2)==0) compressionext="xz";
					else compressionext=NULL;
				}
				break;
			}
		}
	}

	if(archiveext)
	{
		[namedata appendBytes:"." length:1];
		[namedata appendBytes:archiveext length:strlen(archiveext)];
	}

	if(compressionext)
	{
		[namedata appendBytes:"." length:1];
		[namedata appendBytes:compressionext length:strlen(compressionext)];
	}

	NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithObjectsAndKeys:
		[self XADPathWithData:namedata separators:XADUnixPathSeparator],XADFileNameKey,
		[NSNumber numberWithLongLong:[fh offsetInFile]],XADDataOffsetKey,
		[NSNumber numberWithBool:YES],XADIsArchiveKey,
	nil];

	off_t filesize=[fh fileSize];
	if(filesize!=CSHandleMaxLength)
	{
		off_t length=filesize-[fh offsetInFile];
		[dict setObject:[NSNumber numberWithLongLong:length] forKey:XADFileSizeKey];
		[dict setObject:[NSNumber numberWithLongLong:length] forKey:XADCompressedSizeKey];
		[dict setObject:[NSNumber numberWithLongLong:length] forKey:XADDataLengthKey];
	}

	[self addEntryWithDictionary:dict];
}

-(CSHandle *)handleForEntryWithDictionary:(NSDictionary *)dict wantChecksum:(BOOL)checksum
{
	return [self handleAtDataOffsetForDictionary:dict];
}

-(NSString *)formatName { return @"RPM"; }

@end