-
Notifications
You must be signed in to change notification settings - Fork 43
/
XADAppleDouble.m
309 lines (256 loc) · 8.84 KB
/
XADAppleDouble.m
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
/*
* XADAppleDouble.m
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#import "XADAppleDouble.h"
#import "XADException.h"
// AppleDouble format referenced from:
// http://www.opensource.apple.com/source/Libc/Libc-391.2.3/darwin/copyfile.c
@implementation XADAppleDouble
+(BOOL)parseAppleDoubleWithHandle:(CSHandle *)fh resourceForkOffset:(off_t *)resourceoffsetptr
resourceForkLength:(off_t *)resourcelengthptr extendedAttributes:(NSDictionary **)extattrsptr
{
if([fh readUInt32BE]!=0x00051607) return NO;
if([fh readUInt32BE]!=0x00020000) return NO;
[fh skipBytes:16];
int num=[fh readUInt16BE];
uint32_t rsrcoffs=0,rsrclen=0;
uint32_t finderoffs=0,finderlen=0;
for(int i=0;i<num;i++)
{
uint32_t entryid=[fh readUInt32BE];
uint32_t entryoffs=[fh readUInt32BE];
uint32_t entrylen=[fh readUInt32BE];
switch(entryid)
{
case 2: // Resource fork
rsrcoffs=entryoffs;
rsrclen=entrylen;
break;
case 9: // Finder info
finderoffs=entryoffs;
finderlen=entrylen;
break;
}
}
if(!rsrcoffs&&!finderoffs) return NO;
// Load FinderInfo struct and extended attributes if available.
NSData *finderinfo=nil;
NSMutableDictionary *extattrs=nil;
if(finderoffs)
{
// First 32 bytes are the FinderInfo struct.
[fh seekToFileOffset:finderoffs];
if(finderlen>32) finderinfo=[fh readDataOfLength:32];
else finderinfo=[fh readDataOfLength:finderlen];
// Add FinderInfo to extended attributes only if it is not empty.
static const uint8_t zerobytes[32]={0x00};
if(memcmp([finderinfo bytes],zerobytes,[finderinfo length])!=0)
{
extattrs=[NSMutableDictionary dictionaryWithObject:finderinfo
forKey:@"com.apple.FinderInfo"];
}
// The FinderInfo struct is optionally followed by the extended attributes.
if(finderlen>70)
{
if(!extattrs) extattrs=[NSMutableDictionary dictionary];
[self parseAppleDoubleExtendedAttributesWithHandle:fh intoDictionary:extattrs];
}
}
if(resourceoffsetptr) *resourceoffsetptr=rsrcoffs;
if(resourcelengthptr) *resourcelengthptr=rsrclen;
if(extattrsptr) *extattrsptr=extattrs;
return YES;
}
+(void)parseAppleDoubleExtendedAttributesWithHandle:(CSHandle *)fh intoDictionary:(NSMutableDictionary *)extattrs
{
[fh skipBytes:2];
uint32_t magic=[fh readUInt32BE];
if(magic!=0x41545452) return;
/*uint32_t debug=*/[fh readUInt32BE];
/*uint32_t totalsize=*/[fh readUInt32BE];
/*uint32_t datastart=*/[fh readUInt32BE];
/*uint32_t datalength=*/[fh readUInt32BE];
[fh skipBytes:12];
/*int flags=*/[fh readUInt16BE];
int numattrs=[fh readUInt16BE];
struct
{
int offset,length,namelen;
uint8_t namebytes[256];
} entries[numattrs];
for(int i=0;i<numattrs;i++)
{
entries[i].offset=[fh readUInt32BE];
entries[i].length=[fh readUInt32BE];
/*int flags=*/[fh readUInt16BE];
entries[i].namelen=[fh readUInt8];
[fh readBytes:entries[i].namelen toBuffer:entries[i].namebytes];
int padbytes=(-(entries[i].namelen+11))&3;
[fh skipBytes:padbytes]; // Align to 4 bytes.
}
for(int i=0;i<numattrs;i++)
{
off_t curroffset=[fh offsetInFile];
// Find the entry that comes next in the file to avoid seeks.
int minoffset=INT_MAX;
int minindex=-1;
for(int j=0;j<numattrs;j++)
{
if(entries[j].offset>=curroffset && entries[j].offset<minoffset)
{
minoffset=entries[j].offset;
minindex=j;
}
}
if(minindex<0) break; // File structure was messed up, so give up.
if(minoffset!=curroffset) [fh seekToFileOffset:minoffset];
NSData *data=[fh readDataOfLength:entries[minindex].length];
NSString *name=[[[NSString alloc] initWithBytes:entries[minindex].namebytes
length:entries[minindex].namelen-1 encoding:NSUTF8StringEncoding] autorelease];
[extattrs setObject:data forKey:name];
}
}
+(void)writeAppleDoubleHeaderToHandle:(CSHandle *)fh resourceForkSize:(int)ressize
extendedAttributes:(NSDictionary *)extattrs
{
// AppleDouble header template.
uint8_t header[0x32]=
{
/* 0 */ 0x00,0x05,0x16,0x07, 0x00,0x02,0x00,0x00,
/* 8 */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* 24 */ 0x00,0x02,
/* 26 */ 0x00,0x00,0x00,0x09, 0x00,0x00,0x00,0x32, 0x00,0x00,0x00,0x00,
/* 38 */ 0x00,0x00,0x00,0x02, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
/* 50 */
};
// Calculate FinderInfo and extended attributes size field.
NSMutableDictionary *encodedkeys=[NSMutableDictionary dictionary];
int numattributes=0,attributeentrysize=0,attributedatasize=0;
// Sort keys and iterate over them.
NSArray *keys=[[extattrs allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSEnumerator *enumerator=[keys objectEnumerator];
NSString *key;
while((key=[enumerator nextObject]))
{
// Ignore FinderInfo.
if([key isEqual:@"com.apple.FinderInfo"]) continue;
NSData *data=[extattrs objectForKey:key];
NSData *keydata=[key dataUsingEncoding:NSUTF8StringEncoding];
int namelen=[keydata length]+1;
if(namelen>128) continue; // Skip entries with too long names.
numattributes++;
attributeentrysize+=(11+namelen+3)&~3; // Aligned to 4 bytes.
attributedatasize+=[data length];
[encodedkeys setObject:keydata forKey:key];
}
// Set FinderInfo size field and resource fork offset field.
if(numattributes)
{
CSSetUInt32BE(&header[34],32+38+attributeentrysize+attributedatasize);
CSSetUInt32BE(&header[42],50+32+38+attributeentrysize+attributedatasize);
}
else
{
CSSetUInt32BE(&header[34],32);
CSSetUInt32BE(&header[42],50+32);
}
// Set resource fork size field.
CSSetUInt32BE(&header[46],ressize);
// Write AppleDouble header.
[fh writeBytes:sizeof(header) fromBuffer:header];
// Write FinderInfo structure.
NSData *finderinfo=[extattrs objectForKey:@"com.apple.FinderInfo"];
if(finderinfo)
{
if([finderinfo length]<32) [XADException raiseUnknownException];
[fh writeBytes:32 fromBuffer:[finderinfo bytes]];
}
else
{
uint8_t emptyfinderinfo[32]={ 0x00 };
[fh writeBytes:32 fromBuffer:emptyfinderinfo];
}
// Write extended attributes if needed.
if(numattributes)
{
// Attributes section header template.
uint8_t attributesheader[38]=
{
/* 0 */ 0x00,0x00,
/* 2 */ 'A', 'T', 'T', 'R', 0x00,0x00,0x00,0x00,
/* 10 */ 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
/* 18 */ 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
/* 26 */ 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
/* 34 */ 0x00,0x00, 0x00,0x00,
/* 38 */
};
int datastart=50+32+38+attributeentrysize;
// Set header fields.
CSSetUInt32BE(&attributesheader[10],datastart+attributedatasize); // total_size
CSSetUInt32BE(&attributesheader[14],datastart); // data_start
CSSetUInt32BE(&attributesheader[18],attributedatasize); // data_length
CSSetUInt16BE(&attributesheader[36],numattributes); // num_attrs
// Write attributes section header.
[fh writeBytes:sizeof(attributesheader) fromBuffer:attributesheader];
// Write attribute entries.
int currdataoffset=datastart;
NSEnumerator *enumerator=[keys objectEnumerator];
NSString *key;
while((key=[enumerator nextObject]))
{
NSData *data=[extattrs objectForKey:key];
NSData *keydata=[encodedkeys objectForKey:key];
if(!keydata) continue;
int namelen=[keydata length]+1;
// Attribute entry header template.
uint8_t entryheader[11]=
{
/* 0 */ 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
/* 8 */ 0x00,0x00, namelen,
/* 11 */
};
// Set entry header fields.
CSSetUInt32BE(&entryheader[0],currdataoffset); // offset
CSSetUInt32BE(&entryheader[4],[data length]); // length
// Write entry header.
[fh writeBytes:sizeof(entryheader) fromBuffer:entryheader];
// Write name.
char namebytes[namelen];
[key getCString:namebytes maxLength:namelen encoding:NSUTF8StringEncoding];
[fh writeBytes:namelen fromBuffer:namebytes];
// Calculate and write padding.
int padbytes=(-(namelen+11))&3;
uint8_t zerobytes[4]={ 0x00 };
[fh writeBytes:padbytes fromBuffer:zerobytes];
// Update data pointer.
currdataoffset+=[data length];
}
// Write attribute data.
enumerator=[keys objectEnumerator];
while((key=[enumerator nextObject]))
{
NSData *data=[extattrs objectForKey:key];
NSData *keydata=[encodedkeys objectForKey:key];
if(!keydata) continue;
[fh writeData:data];
}
}
}
@end