| 1 |
/* |
| 2 |
** CWUUFile.m |
| 3 |
** |
| 4 |
** Copyright (c) 2002-2004 Ludovic Marcotte |
| 5 |
** |
| 6 |
** Author: Ludovic Marcotte <[email protected]> |
| 7 |
** |
| 8 |
** This library is free software; you can redistribute it and/or |
| 9 |
** modify it under the terms of the GNU Lesser General Public |
| 10 |
** License as published by the Free Software Foundation; either |
| 11 |
** version 2.1 of the License, or (at your option) any later version. |
| 12 |
** |
| 13 |
** This library is distributed in the hope that it will be useful, |
| 14 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
** Lesser General Public License for more details. |
| 17 |
** |
| 18 |
** You should have received a copy of the GNU General Public License |
| 19 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#import <Pantomime/CWUUFile.h> |
| 23 |
|
| 24 |
#import <Pantomime/CWConstants.h> |
| 25 |
|
| 26 |
#import <Foundation/NSArray.h> |
| 27 |
#import <Foundation/NSFileManager.h> |
| 28 |
#import <Foundation/NSValue.h> |
| 29 |
|
| 30 |
#define UUDECODE(c) (((c) - ' ') & 077) |
| 31 |
|
| 32 |
// |
| 33 |
// |
| 34 |
// |
| 35 |
void uudecodeline(char *line, NSMutableData *data) |
| 36 |
{ |
| 37 |
int c, len; |
| 38 |
|
| 39 |
len = UUDECODE(*line++); |
| 40 |
|
| 41 |
while (len) |
| 42 |
{ |
| 43 |
c = UUDECODE(*line) << 2 | UUDECODE(line[1]) >> 4; |
| 44 |
|
| 45 |
[data appendBytes: &c |
| 46 |
length: 1]; |
| 47 |
|
| 48 |
if (--len) |
| 49 |
{ |
| 50 |
c = UUDECODE(line[1]) << 4 | UUDECODE(line[2]) >> 2; |
| 51 |
|
| 52 |
[data appendBytes: &c |
| 53 |
length: 1]; |
| 54 |
|
| 55 |
if (--len) |
| 56 |
{ |
| 57 |
c = UUDECODE(line[2]) << 6 | UUDECODE(line[3]); |
| 58 |
[data appendBytes: &c |
| 59 |
length: 1]; |
| 60 |
len--; |
| 61 |
} |
| 62 |
} |
| 63 |
line += 4; |
| 64 |
} |
| 65 |
|
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
// |
| 71 |
// |
| 72 |
// |
| 73 |
@implementation CWUUFile |
| 74 |
|
| 75 |
- (id) initWithName: (NSString *) theName |
| 76 |
data: (NSData *) theData |
| 77 |
attributes: (NSDictionary *) theAttributes |
| 78 |
{ |
| 79 |
self = [super init]; |
| 80 |
if (self) |
| 81 |
{ |
| 82 |
[self setName: theName]; |
| 83 |
[self setData: theData]; |
| 84 |
[self setAttributes: theAttributes]; |
| 85 |
} |
| 86 |
return self; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
// |
| 91 |
// |
| 92 |
// |
| 93 |
- (void) dealloc |
| 94 |
{ |
| 95 |
RELEASE(_name); |
| 96 |
RELEASE(_data); |
| 97 |
RELEASE(_attributes); |
| 98 |
[super dealloc]; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
// |
| 103 |
// access / mutation methods |
| 104 |
// |
| 105 |
- (NSString *) name |
| 106 |
{ |
| 107 |
return _name; |
| 108 |
} |
| 109 |
|
| 110 |
- (void) setName: (NSString *) theName |
| 111 |
{ |
| 112 |
ASSIGN(_name, theName); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
// |
| 117 |
// |
| 118 |
// |
| 119 |
- (NSData *) data |
| 120 |
{ |
| 121 |
return _data; |
| 122 |
} |
| 123 |
|
| 124 |
- (void) setData: (NSData *) theData |
| 125 |
{ |
| 126 |
ASSIGN(_data, theData); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
// |
| 131 |
// |
| 132 |
// |
| 133 |
- (NSDictionary *) attributes |
| 134 |
{ |
| 135 |
return _attributes; |
| 136 |
} |
| 137 |
|
| 138 |
- (void) setAttributes: (NSDictionary *) theAttributes |
| 139 |
{ |
| 140 |
ASSIGN(_attributes, theAttributes); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
// |
| 145 |
// |
| 146 |
// |
| 147 |
+ (CWUUFile *) fileFromUUEncodedString: (NSString *) theString |
| 148 |
{ |
| 149 |
NSString *aString, *aFilename; |
| 150 |
NSNumber *theFilePermissions; |
| 151 |
NSMutableData *aMutableData; |
| 152 |
NSArray *allLines; |
| 153 |
CWUUFile *aUUFile; |
| 154 |
int i, count; |
| 155 |
|
| 156 |
aMutableData = [NSMutableData dataWithCapacity: [theString length]]; |
| 157 |
|
| 158 |
allLines = [theString componentsSeparatedByString: @"\n"]; |
| 159 |
|
| 160 |
// We decode our filename and our mode |
| 161 |
aString = [allLines objectAtIndex: 0]; |
| 162 |
|
| 163 |
theFilePermissions = [NSNumber numberWithInt: [[[aString componentsSeparatedByString: @" "] objectAtIndex: 1] intValue]]; |
| 164 |
aFilename = [[aString componentsSeparatedByString: @" "] objectAtIndex: 2]; |
| 165 |
|
| 166 |
// We now get the data representing our uuencoding string |
| 167 |
count = [allLines count]-1; |
| 168 |
for (i = 1; i < count; i++) |
| 169 |
{ |
| 170 |
uudecodeline((char *)[[allLines objectAtIndex: i] cString], aMutableData); |
| 171 |
} |
| 172 |
|
| 173 |
// We finally initialize our file wrapper will all our informations |
| 174 |
aUUFile = [[CWUUFile alloc] initWithName: aFilename |
| 175 |
data: aMutableData |
| 176 |
attributes: [NSDictionary dictionaryWithObject: theFilePermissions |
| 177 |
forKey: NSFilePosixPermissions]]; |
| 178 |
|
| 179 |
return AUTORELEASE(aUUFile); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
// |
| 184 |
// FIXME, we currently ignore theRange |
| 185 |
// |
| 186 |
+ (NSRange) rangeOfUUEncodedStringFromString: (NSString *) theString |
| 187 |
range: (NSRange) theRange |
| 188 |
{ |
| 189 |
NSRange r1, r2; |
| 190 |
|
| 191 |
r1 = [theString rangeOfString: @"begin "]; |
| 192 |
|
| 193 |
if (r1.length == 0) |
| 194 |
{ |
| 195 |
return NSMakeRange(NSNotFound, 0); |
| 196 |
} |
| 197 |
|
| 198 |
r2 = [theString rangeOfString: @"\nend" |
| 199 |
options: 0 |
| 200 |
range: NSMakeRange(r1.location, [theString length] - r1.location)]; |
| 201 |
|
| 202 |
if (r2.length == 0) |
| 203 |
{ |
| 204 |
return NSMakeRange(NSNotFound, 0); |
| 205 |
} |
| 206 |
|
| 207 |
return NSMakeRange(r1.location, (r2.location + r2.length) - r1.location); |
| 208 |
} |
| 209 |
|
| 210 |
@end |