-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGPGData.m
528 lines (415 loc) · 17.3 KB
/
GPGData.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//
// GPGData.m
// MacGPGME
//
// Created by davelopper at users.sourceforge.net on Tue Aug 14 2001.
//
//
// Copyright (C) 2001-2006 Mac GPG Project.
//
// This code 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 code 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 program; if not, visit <http://www.gnu.org/> or write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.
//
// More info at <http://macgpg.sourceforge.net/>
//
#include <MacGPGME/GPGData.h>
#include <MacGPGME/GPGExceptions.h>
#include <MacGPGME/GPGInternals.h>
#include <Foundation/Foundation.h>
#include <gpgme.h>
#define _data ((gpgme_data_t)_internalRepresentation)
@implementation GPGData
- (id) init
{
gpgme_data_t aData;
gpgme_error_t anError = gpgme_data_new(&aData);
if(anError != GPG_ERR_NO_ERROR){
[self release];
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
self = [self initWithInternalRepresentation:aData];
return self;
}
- (id) initWithData:(NSData *)someData
{
gpgme_data_t aData;
gpgme_error_t anError = gpgme_data_new_from_mem(&aData, [someData bytes], [someData length], 1);
if(anError != GPG_ERR_NO_ERROR){
[self release];
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
self = [self initWithInternalRepresentation:aData];
return self;
}
- (id) initWithDataNoCopy:(NSData *)someData
{
gpgme_data_t aData;
gpgme_error_t anError = gpgme_data_new_from_mem(&aData, ([someData respondsToSelector:@selector(mutableBytes)] ? [(NSMutableData *)someData mutableBytes]:[someData bytes]), [someData length], 0);
if(anError != GPG_ERR_NO_ERROR){
[self release];
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
self = [self initWithInternalRepresentation:aData];
((GPGData *)self)->_objectReference = [someData retain];
return self;
}
static ssize_t readCallback(void *object, void *destinationBuffer, size_t destinationBufferSize)
{
// Returns the number of bytes read, or -1 on error. Sets errno in case of error.
NSData *readData = nil;
ssize_t readLength = 0;
NSCParameterAssert(destinationBufferSize != 0 && destinationBuffer != NULL);
NS_DURING
readData = [((GPGData *)object)->_objectReference data:((GPGData *)object) readDataOfLength:destinationBufferSize];
NS_HANDLER
if([[localException name] isEqualToString:GPGException]){
NSNumber *errorNumber = [[localException userInfo] objectForKey:GPGErrorKey];
int errorCodeAsErrno;
NSCAssert(errorNumber != nil, @"### GPGException raised by GPGData dataSource has no error");
errorCodeAsErrno = gpg_err_code_to_errno(gpgme_err_code([errorNumber intValue]));
NSCAssert2(errorCodeAsErrno != 0, @"### GPGException raised by GPGData dataSource has not a system error errorCode (%@: %@)", errorNumber, GPGErrorDescription([errorNumber intValue]));
errno = errorCodeAsErrno;
return -1;
}
else
[localException raise];
NS_ENDHANDLER
if(readData != nil){
readLength = [readData length];
if(readLength > 0){
NSCAssert(((size_t)readLength) <= destinationBufferSize, @"### GPGData dataSource may not return more bytes than given capacity!");
[readData getBytes:destinationBuffer];
}
}
return readLength;
}
static ssize_t writeCallback(void *object, const void *buffer, size_t size)
{
// Returns the number of bytes written, or -1 on error. Sets errno in case of error.
ssize_t writeLength = 0;
NSData *data = [NSData dataWithBytesNoCopy:(void *)buffer length:size freeWhenDone:NO];
NS_DURING
writeLength = [((GPGData *)object)->_objectReference data:((GPGData *)object) writeData:data];
NS_HANDLER
if([[localException name] isEqualToString:GPGException]){
NSNumber *errorNumber = [[localException userInfo] objectForKey:GPGErrorKey];
int errorCodeAsErrno;
NSCAssert(errorNumber != nil, @"### GPGException raised by GPGData dataSource has no error");
errorCodeAsErrno = gpg_err_code_to_errno(gpgme_err_code([errorNumber intValue]));
NSCAssert2(errorCodeAsErrno != 0, @"### GPGException raised by GPGData dataSource has not a system error errorCode (%@: %@)", errorNumber, GPGErrorDescription([errorNumber intValue]));
errno = errorCodeAsErrno;
return -1;
}
else
[localException raise];
NS_ENDHANDLER
return writeLength;
}
static off_t seekCallback(void *object, off_t offset, int whence)
{
// Returns the number of bytes written, or -1 on error. Sets errno in case of error.
off_t newPosition = 0;
NS_DURING
newPosition = [((GPGData *)object)->_objectReference data:((GPGData *)object) seekToFileOffset:offset offsetType:whence];
NS_HANDLER
if([[localException name] isEqualToString:GPGException]){
NSNumber *errorNumber = [[localException userInfo] objectForKey:GPGErrorKey];
int errorCodeAsErrno;
NSCAssert(errorNumber != nil, @"### GPGException raised by GPGData dataSource has no error");
errorCodeAsErrno = gpg_err_code_to_errno(gpgme_err_code([errorNumber intValue]));
NSCAssert2(errorCodeAsErrno != 0, @"### GPGException raised by GPGData dataSource has not a system error errorCode (%@: %@)", errorNumber, GPGErrorDescription([errorNumber intValue]));
errno = errorCodeAsErrno;
return -1;
}
else
[localException raise];
NS_ENDHANDLER
return newPosition;
}
static void releaseCallback(void *object)
{
[((GPGData *)object)->_objectReference dataRelease:((GPGData *)object)];
}
- (id) initWithDataSource:(id)dataSource
{
gpgme_data_t aData;
gpgme_error_t anError;
gpgme_data_cbs_t callbacks;
NSParameterAssert(dataSource != nil);
callbacks = (gpgme_data_cbs_t)NSZoneCalloc([self zone], 1, sizeof(struct gpgme_data_cbs));
if([dataSource respondsToSelector:@selector(data:readDataOfLength:)])
callbacks->read = readCallback;
if([dataSource respondsToSelector:@selector(data:writeData:)])
callbacks->write = writeCallback;
if([dataSource respondsToSelector:@selector(data:seekToFileOffset:offsetType:)])
callbacks->seek = seekCallback;
if([dataSource respondsToSelector:@selector(data:dataRelease:)])
callbacks->release = releaseCallback;
NSParameterAssert(callbacks->read != NULL || callbacks->write != NULL);
anError = gpgme_data_new_from_cbs(&aData, callbacks, self);
if(anError != GPG_ERR_NO_ERROR){
NSZoneFree([self zone], callbacks);
[self release];
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
NSAssert(self == [self initWithInternalRepresentation:aData], @"Tried to change self! Impossible due to callback registration.");
_objectReference = dataSource; // We don't retain dataSource
_callbacks = callbacks;
return self;
}
- (id) initWithContentsOfFile:(NSString *)filename
{
gpgme_data_t aData;
gpgme_error_t anError = gpgme_data_new_from_file(&aData, [filename fileSystemRepresentation], 1);
if(anError != GPG_ERR_NO_ERROR){
[self release];
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
self = [self initWithInternalRepresentation:aData];
[self setFilename:[filename lastPathComponent]];
return self;
}
- (id) initWithContentsOfFileNoCopy:(NSString *)filename
#warning Not yet supported as of 1.1.x
// Can raise a GPGException; in this case, a release is sent to self
{
gpgme_data_t aData;
gpgme_error_t anError = gpgme_data_new_from_file(&aData, [filename fileSystemRepresentation], 0);
if(anError != GPG_ERR_NO_ERROR){
[self release];
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
self = [self initWithInternalRepresentation:aData];
[self setFilename:[filename lastPathComponent]];
return self;
}
- (id) initWithContentsOfFile:(NSString *)filename atOffset:(off_t)offset length:(size_t)length
{
// We don't provide a method to match the case where filename is NULL
// and filePtr (FILE *) is not NULL (both arguments are exclusive),
// because we generally don't manipulate FILE * types in Cocoa.
gpgme_data_t aData;
gpgme_error_t anError = gpgme_data_new_from_filepart(&aData, [filename fileSystemRepresentation], NULL, offset, length);
if(anError != GPG_ERR_NO_ERROR){
[self release];
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
self = [self initWithInternalRepresentation:aData];
[self setFilename:[filename lastPathComponent]];
return self;
}
// We don't support gpgme_data_new_from_stream(), because there is
// no STREAM handling in Cocoa, yet(?).
// Maybe in Panther with NSStream?
- (id) initWithFileHandle:(NSFileHandle *)fileHandle
{
gpgme_data_t aData;
gpgme_error_t anError = gpgme_data_new_from_fd(&aData, [fileHandle fileDescriptor]);
if(anError != GPG_ERR_NO_ERROR){
[self release];
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
self = [self initWithInternalRepresentation:aData];
((GPGData *)self)->_objectReference = [fileHandle retain];
return self;
}
- (void) dealloc
{
gpgme_data_t cachedData = _data;
if(_callbacks != NULL)
NSZoneFree([self zone], _callbacks);
// If _callbacks is not NULL, it means that _objectReference was a non-retained dataSource
else if(_objectReference != nil)
[_objectReference release];
[super dealloc];
// We could have a problem here if we set ourself as callback
// and _data is deallocated later than us!!!
// This shouldn't happen, but who knows...
if(cachedData != NULL)
gpgme_data_release(cachedData);
}
#if 0
- (id) copyWithZone:(NSZone *)zone
{
GPGData *aCopy = nil;
switch([self type]){
case GPGDataTypeNone:
aCopy = [[[self class] allocWithZone:zone] init];
break;
case GPGDataTypeData:
if(_retainedData != nil){
NSMutableData *copiedData = [_retainedData mutableCopyWithZone:zone];
aCopy = [[[self class] allocWithZone:zone] initWithDataNoCopy:copiedData];
[copiedData release];
}
else
aCopy = [[[self class] allocWithZone:zone] initWithData:[self data]]; // WARNING: this rewinds myself and reads until EOF!
break;
case GPGDataTypeFileHandle:
// We don't provide a way in GPGME to create such data types
[NSException raise:NSInternalInconsistencyException format:@"### Unsupported GPGData type %d", [self type]];
break;
case GPGDataTypeFile:
// There is no way to know which inititializer was called!
[NSException raise:NSInternalInconsistencyException format:@"### Unsupported GPGData type %d", [self type]];
break;
case GPGDataTypeDataSource:
aCopy = [[[self class] allocWithZone:zone] initWithDataSource:_dataSource];
[aCopy rewind]; // This also rewinds myself!
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"### Unknown GPGData type %d", [self type]];
}
return aCopy;
}
#endif
- (GPGDataEncoding) encoding
{
GPGDataEncoding encoding = gpgme_data_get_encoding(_data);
return encoding;
}
- (void) setEncoding:(GPGDataEncoding)encoding
{
gpgme_error_t anError = gpgme_data_set_encoding(_data, encoding);
if(anError != GPG_ERR_NO_ERROR)
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
- (off_t) seekToFileOffset:(off_t)offset offsetType:(GPGDataOffsetType)offsetType
{
off_t newPosition = gpgme_data_seek(_data, offset, offsetType);
if(newPosition < 0)
[[NSException exceptionWithGPGError:gpgme_err_make_from_errno(GPG_MacGPGMEFrameworkErrorSource, errno) userInfo:nil] raise];
return newPosition;
}
- (NSData *) readDataOfLength:(size_t)length
{
NSMutableData *readData = [NSMutableData dataWithLength:length];
ssize_t aReadLength = gpgme_data_read(_data, [readData mutableBytes], length);
if(aReadLength == 0)
return nil;
if(aReadLength < 0)
[[NSException exceptionWithGPGError:gpgme_err_make_from_errno(GPG_MacGPGMEFrameworkErrorSource, errno) userInfo:nil] raise];
[readData setLength:aReadLength];
return readData;
}
- (ssize_t) writeData:(NSData *)data
{
ssize_t writtenByteCount = gpgme_data_write(_data, [data bytes], [data length]);
if(writtenByteCount < 0)
[[NSException exceptionWithGPGError:gpgme_err_make_from_errno(GPG_MacGPGMEFrameworkErrorSource, errno) userInfo:nil] raise];
return writtenByteCount;
}
- (NSString *) filename
{
const char *aCString = gpgme_data_get_file_name(_data); // Returns original string -> make a copy
return GPGStringFromChars(aCString);
}
- (void) setFilename:(NSString *)filename
{
const char *aCString = (filename != nil ? [filename fileSystemRepresentation] : NULL);
gpgme_error_t anError = gpgme_data_set_file_name(_data, aCString); // Will duplicate string
if(anError != GPG_ERR_NO_ERROR)
[[NSException exceptionWithGPGError:anError userInfo:nil] raise];
}
@end
@implementation GPGData(GPGExtensions)
- (id) initWithString:(NSString *)string
{
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
return [self initWithData:data];
}
- (NSString *) string
{
NSData *data = [self data];
unsigned dataLength = [data length];
if(dataLength > 0){
// Ensure byte buffer is 0-terminated
const char *dataBytes = [data bytes];
if(dataBytes[dataLength - 1] != 0){
NSMutableData *newData = [data mutableCopy];
NSString *aString;
[newData setLength:dataLength + 1];
((char *)[newData mutableBytes])[dataLength] = 0;
aString = GPGStringFromChars([newData bytes]);
[newData release];
return aString;
}
else
return GPGStringFromChars(dataBytes);
}
else
return @"";
}
- (off_t) length
{
off_t currentPos;
off_t length;
currentPos = gpgme_data_seek(_data, 0, GPGDataCurrentPosition);
if(currentPos < 0)
[[NSException exceptionWithGPGError:gpgme_err_make_from_errno(GPG_MacGPGMEFrameworkErrorSource, errno) userInfo:nil] raise];
length = gpgme_data_seek(_data, 0, GPGDataEndPosition);
if(length < 0)
[[NSException exceptionWithGPGError:gpgme_err_make_from_errno(GPG_MacGPGMEFrameworkErrorSource, errno) userInfo:nil] raise];
NSAssert(gpgme_data_seek(_data, currentPos, GPGDataStartPosition) == currentPos, @"Unable to go back to original position!");
return length;
}
- (BOOL) isAtEnd
{
off_t currentPos;
off_t length;
currentPos = gpgme_data_seek(_data, 0, GPGDataCurrentPosition);
if(currentPos < 0)
[[NSException exceptionWithGPGError:gpgme_err_make_from_errno(GPG_MacGPGMEFrameworkErrorSource, errno) userInfo:nil] raise];
length = gpgme_data_seek(_data, 0, GPGDataEndPosition);
if(length < 0)
[[NSException exceptionWithGPGError:gpgme_err_make_from_errno(GPG_MacGPGMEFrameworkErrorSource, errno) userInfo:nil] raise];
NSAssert(gpgme_data_seek(_data, currentPos, GPGDataStartPosition) == currentPos, @"Unable to go back to original position!");
return currentPos == length;
}
- (NSData *) availableData
{
size_t bufferSize = NSPageSize();
NSZone *aZone = NSDefaultMallocZone();
char *bufferPtr = (char *)NSZoneMalloc(aZone, bufferSize);
NSMutableData *readData = [NSMutableData dataWithCapacity:bufferSize];
ssize_t aReadLength;
do{
aReadLength = gpgme_data_read(_data, bufferPtr, bufferSize);
if(aReadLength > 0)
[readData appendBytes:bufferPtr length:aReadLength];
}while(aReadLength > 0);
NSZoneFree(aZone, bufferPtr);
if(aReadLength < 0)
[[NSException exceptionWithGPGError:gpgme_err_make_from_errno(GPG_MacGPGMEFrameworkErrorSource, errno) userInfo:nil] raise];
return readData;
}
- (NSData *) data
{
[self rewind];
return [self availableData];
}
- (void) rewind
{
off_t newPosition = gpgme_data_seek(_data, 0, GPGDataStartPosition);
if(newPosition < 0)
[[NSException exceptionWithGPGError:gpgme_err_make_from_errno(GPG_MacGPGMEFrameworkErrorSource, errno) userInfo:nil] raise];
}
@end
@implementation GPGData(GPGInternals)
- (gpgme_data_t) gpgmeData
{
return _data;
}
@end