-
Notifications
You must be signed in to change notification settings - Fork 11
/
NDResourceFork.m
executable file
·487 lines (409 loc) · 12.5 KB
/
NDResourceFork.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* NDResourceFork.m
*
* Created by nathan on Wed Dec 05 2001.
* Copyright (c) 2001 Nathan Day. All rights reserved.
*
* Currently ResourceFork will not add resource forks to files
* or create new files with resource forks
*
*/
#import "NDResourceFork.h"
#import "NSString+NDCarbonUtilities.h"
/*
* class implementation NDResourceFork
*/
@implementation NDResourceFork
NSData * dataFromResourceHandle( Handle aResourceHandle );
BOOL operateOnResourceUsingFunction( short int afileRef, ResType aType, short int anId, NSString * aName, BOOL (*aFunction)(Handle,ResType,NSString*,short int,void*), void * aContext );
/*
* resourceForkForReadingAtURL:
*/
+ (id)resourceForkForReadingAtURL:(NSURL *)aURL
{
return [[[self alloc] initForReadingAtURL:aURL] autorelease];
}
/*
* resourceForkForWritingAtURL:
*/
+ (id)resourceForkForWritingAtURL:(NSURL *)aURL
{
return [[[self alloc] initForWritingAtURL:aURL] autorelease];
}
/*
* resourceForkForReadingAtPath:
*/
+ (id)resourceForkForReadingAtPath:(NSString *)aPath
{
return [[[self alloc] initForReadingAtPath:aPath] autorelease];
}
/*
* resourceForkForWritingAtPath:
*/
+ (id)resourceForkForWritingAtPath:(NSString *)aPath
{
return [[[self alloc] initForWritingAtPath:aPath] autorelease];
}
/*
* initForReadingAtURL:
*/
- (id)initForReadingAtURL:(NSURL *)aURL
{
return [self initForPermission:fsRdPerm AtURL:aURL];
}
/*
* initForWritingAtURL:
*/
- (id)initForWritingAtURL:(NSURL *)aURL
{
return [self initForPermission:fsWrPerm AtURL:aURL];
}
/*
* initForPermission:AtURL:
*/
- (id)initForPermission:(char)aPermission AtURL:(NSURL *)aURL
{
return [self initForPermission:aPermission AtPath:[aURL path]];
}
/*
* -initForPermission:AtPath:
*/
- (id)initForPermission:(char)aPermission AtPath:(NSString *)aPath
{
OSErr theError = !noErr;
FSRef theFsRef,
theParentFsRef;
if( self = [self init] )
{
/*
* if write permission then create resource fork
*/
if( (aPermission & 0x06) != 0 ) // if write permission
{
if ( [[aPath stringByDeletingLastPathComponent] getFSRef:&theParentFsRef] )
{
unsigned int theNameLength;
unichar theUnicodeName[ PATH_MAX ];
NSString * theName;
theName = [aPath lastPathComponent];
theNameLength = [theName length];
if( theNameLength <= PATH_MAX )
{
[theName getCharacters:theUnicodeName range:NSMakeRange(0,theNameLength)];
FSCreateResFile( &theParentFsRef, theNameLength, theUnicodeName, 0, NULL, NULL, NULL ); // doesn't replace if already exists
theError = ResError( );
if( theError == noErr || theError == dupFNErr )
{
[aPath getFSRef:&theFsRef];
fileReference = FSOpenResFile ( &theFsRef, aPermission );
theError = fileReference > 0 ? ResError( ) : !noErr;
}
}
else
theError = !noErr;
}
}
else // dont have write permission
{
[aPath getFSRef:&theFsRef];
fileReference = FSOpenResFile ( &theFsRef, aPermission );
theError = fileReference > 0 ? ResError( ) : !noErr;
}
}
if( noErr != theError && theError != dupFNErr )
{
[self release];
self = nil;
}
return self;
}
/*
* initForReadingAtPath:
*/
- (id)initForReadingAtPath:(NSString *)aPath
{
if( [[NSFileManager defaultManager] fileExistsAtPath:aPath] )
return [self initForPermission:fsRdPerm AtURL:[NSURL fileURLWithPath:aPath]];
else
{
[self release];
return nil;
}
}
/*
* initForWritingAtPath:
*/
- (id)initForWritingAtPath:(NSString *)aPath
{
return [self initForPermission:fsWrPerm AtURL:[NSURL fileURLWithPath:aPath]];
}
/*
* dealloc
*/
- (void)dealloc
{
if( fileReference > 0 )
CloseResFile( fileReference );
[super dealloc];
}
/*
* -addData:type:Id:name:
*/
- (BOOL)addData:(NSData *)aData type:(ResType)aType Id:(short int)anId name:(NSString *)aName
{
Handle theResHandle;
if( [self removeType:aType Id:anId] )
{
short int thePreviousRefNum;
thePreviousRefNum = CurResFile(); // save current resource
UseResFile( fileReference ); // set this resource to be current
// copy NSData's bytes to a handle
if ( noErr == PtrToHand ( [aData bytes], &theResHandle, [aData length] ) )
{
Str255 thePName;
[aName getPascalString:(StringPtr)thePName length:sizeof(thePName)];
HLock( theResHandle );
AddResource( theResHandle, aType, anId, thePName );
HUnlock( theResHandle );
/* if( noErr == ResError() )
ChangedResource( theResHandle );
*/
UseResFile( thePreviousRefNum ); // reset back to resource previously set
return ( ResError( ) == noErr );
}
}
return NO;
}
/*
* -addData:type:name:
*/
- (BOOL)addData:(NSData *)aData type:(ResType)aType name:(NSString *)aName
{
if( aName == nil ) NSLog(@"Adding a resource without specifying the name of id.");
return [self addData:aData type:aType Id:Unique1ID(aType) name:aName];
}
/*
* dataForType:Id:
*/
static BOOL getDataFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext );
- (NSData *)dataForType:(ResType)aType Id:(short int)anId
{
NSData * theData = nil;
if( operateOnResourceUsingFunction( fileReference, aType, anId, nil, getDataFunction, (void*)&theData ) )
return theData;
else
return nil;
}
//static BOOL getDataFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext);
/*
* dataForType:named:
*/
- (NSData *)dataForType:(ResType)aType named:(NSString *)aName
{
NSData * theData = nil;
if( operateOnResourceUsingFunction( fileReference, aType, 0, aName, getDataFunction, (void*)&theData ) )
return theData;
else
return nil;
}
static BOOL getDataFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext )
{
NSData ** theData = (NSData**)aContext;
*theData = dataFromResourceHandle( aResHandle );
return *theData != nil;
}
/*
* -everyResourceType
* array of NSNumbers for every resource type
*/
- (NSArray *)everyResourceType
{
SInt16 theNumOfTypes;
NSMutableArray * theTypesArray = nil;
SInt16 theIndex;
NSAssert( sizeof(ResType) <= sizeof(unsigned long) ,@"WARNING: everyResourceType assumes that ResType is the same size as unsigned long" );
theNumOfTypes = Count1Types ();
if( noErr == ResError( ) )
{
theTypesArray = [NSMutableArray arrayWithCapacity:theNumOfTypes];
for( theIndex = 1; theIndex <= theNumOfTypes; theIndex++ )
{
ResType theResType;
Get1IndType ( &theResType, theIndex );
if( noErr == ResError( ) )
[theTypesArray addObject:[NSNumber numberWithUnsignedLong:theResType]];
else
NSLog( @"Could not get type for resource %i", theIndex);
}
}
else
{
NSLog( @"Could not get number of resource types" );
}
return theTypesArray;
}
/*
* removeType: Id:
*/
static BOOL removeResourceFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext );
- (BOOL)removeType:(ResType)aType Id:(short int)anId
{
return operateOnResourceUsingFunction( fileReference, aType, anId, nil, removeResourceFunction, NULL);
}
static BOOL removeResourceFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext )
{
if( aResHandle )
RemoveResource( aResHandle ); // Disposed of in current resource file
return !aResHandle || noErr == ResError( );
}
/*
* nameOfResourceType:Id:
*/
static BOOL getNameFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext );
- (NSString *)nameOfResourceType:(ResType)aType Id:(short int)anId
{
NSString * theString = nil;
if( operateOnResourceUsingFunction( fileReference, aType, anId, nil, getNameFunction, (void*)&theString ) )
return theString;
else
return nil;
}
static BOOL getNameFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext )
{
Str255 thePName;
NSString ** theString = (NSString **)aContext;
if( aResHandle )
{
GetResInfo( aResHandle, &anId, &aType, thePName );
if( noErr == ResError( ) )
*theString = [NSString stringWithPascalString:thePName];
}
return *theString != nil;
}
/*
* getId:OfResourceType:Id:
*/
static BOOL getIdFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext );
- (BOOL)getId:(short int *)anId ofResourceType:(ResType)aType named:(NSString *)aName
{
return operateOnResourceUsingFunction( fileReference, aType, 0, aName, getIdFunction, NULL );
}
static BOOL getIdFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext )
{
Str255 thePName;
if( aResHandle && [aName getPascalString:(StringPtr)thePName length:sizeof(thePName)] )
{
GetResInfo( aResHandle, &anId, &aType, thePName );
return noErr == ResError( );
}
else
return NO;
}
/*
* -attributeFlags:forResourceType:Id:
*/
static BOOL getAttributesFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext );
- (BOOL)getAttributeFlags:(short int*)attributes forResourceType:(ResType)aType Id:(short int)anId
{
return operateOnResourceUsingFunction( fileReference, aType, anId, nil, getAttributesFunction, (void*)attributes );
}
static BOOL getAttributesFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext )
{
short int * theAttributes = (short int*)aContext;
if( aResHandle )
{
*theAttributes = GetResAttrs( aResHandle );
return noErr == ResError( );
}
return NO;
}
static BOOL setAttributesFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext );
/*
* -setAttributeFlags:forResourceType:Id:
*/
- (BOOL)setAttributeFlags:(short int)attributes forResourceType:(ResType)aType Id:(short int)anId
{
BOOL theSuccess;
NSLog(@"WARRING: Currently the setAttributeFlags:forResourceType:Id: does not work");
theSuccess = operateOnResourceUsingFunction( fileReference, aType, anId, nil, setAttributesFunction, &attributes );
return theSuccess;
}
static BOOL setAttributesFunction( Handle aResHandle, ResType aType, NSString * aName, short int anId, void * aContext )
{
short int theAttributes = *(short int*)aContext;
if( aResHandle )
{
theAttributes &= ~(resPurgeable|resChanged); // these attributes should not be changed
SetResAttrs( aResHandle, theAttributes);
if( noErr == ResError( ) )
{
ChangedResource(aResHandle);
return noErr == ResError( );
}
}
return NO;
}
/*
* -dataForEntireResourceFork
*/
- (NSData *)dataForEntireResourceFork
{
NSMutableData * theData = nil;
ByteCount theByteCount;
signed long long theForkSize;
if( FSGetForkSize( fileReference, &theForkSize ) == noErr && theForkSize <= UINT_MAX )
{
theData = [NSMutableData dataWithLength:theForkSize];
if( FSReadFork( fileReference, fsFromStart, 0, theForkSize, [theData mutableBytes], &theByteCount ) != noErr || theByteCount != theForkSize )
theData = nil;
}
return theData;
}
/*
* -writeEntireResourceFork:
*/
- (BOOL)writeEntireResourceFork:(NSData *)aData
{
ByteCount theWrittenBytes;
unsigned int theDataLength;
theDataLength = [aData length];
// return true if aData exists, length not zero, write succeeds, write length equals data length
return aData && theDataLength != 0 && FSWriteFork( fileReference, fsFromStart, 0, theDataLength, [aData bytes], &theWrittenBytes ) == noErr && theDataLength == theWrittenBytes;
}
/*
* dataFromResourceHandle()
*/
NSData * dataFromResourceHandle( Handle aResourceHandle )
{
NSData * theData = nil;
if( aResourceHandle )
{
HLock(aResourceHandle);
theData = [NSData dataWithBytes:*aResourceHandle length:GetHandleSize( aResourceHandle )];
HUnlock(aResourceHandle);
}
return theData;
}
BOOL operateOnResourceUsingFunction( short int afileRef, ResType aType, short int anId, NSString * aName, BOOL (*aFunction)(Handle,ResType,NSString*,short int,void*), void * aContext )
{
Handle theResHandle = NULL;
short int thePreviousRefNum;
Str255 thePName;
BOOL theResult = NO;
thePreviousRefNum = CurResFile(); // save current resource
UseResFile( afileRef ); // set this resource to be current
if( noErr == ResError( ) && ((aName && [aName getPascalString:(StringPtr)thePName length:sizeof(thePName)]) || !aName ))
{
if( aName && [aName getPascalString:(StringPtr)thePName length:sizeof(thePName)] )
theResHandle = Get1NamedResource( aType, thePName );
else if( !aName )
theResHandle = Get1Resource( aType, anId );
if( noErr == ResError() )
theResult = aFunction( theResHandle, aType, aName, anId, aContext );
if ( theResHandle )
ReleaseResource( theResHandle );
}
UseResFile( thePreviousRefNum ); // reset back to resource previously set
return theResult;
}
@end