Added convenience methods

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@13884 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2002-06-14 09:48:15 +00:00
parent 9e2e6d6d64
commit 0d02de5d7c
4 changed files with 64 additions and 18 deletions

View file

@ -6,6 +6,8 @@
Patch by e.sammer <eric@linuxstep.org>
* Source/NSObject.m: Added some compatibility methods in a
categpory of Object.
* Source/Additions/GSMime.m: Add a couple of convenience methods.
* Source/GSHTTPURLHandle.m: Use one of them.
2002-06-13 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -87,7 +87,9 @@
}
+ (NSData*) decodeBase64: (NSData*)source;
+ (NSString*) decodeBase64String: (NSString*)source;
+ (NSData*) encodeBase64: (NSData*)source;
+ (NSString*) encodeBase64String: (NSString*)source;
- (void) addContent: (id)newContent;
- (void) addHeader: (GSMimeHeader*)info;

View file

@ -2678,19 +2678,28 @@ static NSCharacterSet *tokenSet = nil;
*/
+ (NSData*) decodeBase64: (NSData*)source
{
int length = [source length];
int declen = ((length + 3) * 3)/4;
const signed char *src = (const char*)[source bytes];
const signed char *end = &src[length];
int length;
int declen ;
const signed char *src;
const signed char *end;
unsigned char *result;
unsigned char *dst;
unsigned char buf[4];
unsigned pos = 0;
if (source == nil)
{
return nil;
}
length = [source length];
if (length == 0)
{
return [NSData data];
}
declen = ((length + 3) * 3)/4;
src = (const char*)[source bytes];
end = &src[length];
result = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), declen);
dst = result;
@ -2757,22 +2766,47 @@ static NSCharacterSet *tokenSet = nil;
initWithBytesNoCopy: result length: dst - result]);
}
/**
* Converts the base64 encoded data in source to a decoded ASCII string
* using the +decodeBase64: method. If the encoded data does not represent
* an ASCII string, you should use the +decodeBase64: method directly.
*/
+ (NSString*) decodeBase64String: (NSString*)source
{
NSData *d = [source dataUsingEncoding: NSASCIIStringEncoding];
NSString *r = nil;
d = [self decodeBase64: d];
if (d != nil)
{
r = [[NSString alloc] initWithData: d encoding: NSASCIIStringEncoding];
AUTORELEASE(r);
}
return r;
}
/**
* Encode the source data to base64 encoding and return the result.
*/
+ (NSData*) encodeBase64: (NSData*)source
{
int length = [source length];
int destlen = 4 * ((length - 1) / 3) + 5;
int length;
int destlen;
unsigned char *sBuf;
unsigned char *dBuf;
int sIndex = 0;
int dIndex = 0;
if (source == nil)
{
return nil;
}
length = [source length];
if (length == 0)
{
return [NSData data];
}
destlen = 4 * ((length - 1) / 3) + 5;
sBuf = (unsigned char*)[source bytes];
dBuf = NSZoneMalloc(NSDefaultMallocZone(), destlen);
dBuf[destlen - 1] = '\0';
@ -2809,6 +2843,25 @@ static NSCharacterSet *tokenSet = nil;
initWithBytesNoCopy: dBuf length: dIndex]);
}
/**
* Converts the ASCII string source into base64 encoded data using the
* +encodeBase64: method. If the original data is not an ASCII string,
* you should use the +encodeBase64: method directly.
*/
+ (NSString*) encodeBase64String: (NSString*)source
{
NSData *d = [source dataUsingEncoding: NSASCIIStringEncoding];
NSString *r = nil;
d = [self encodeBase64: d];
if (d != nil)
{
r = [[NSString alloc] initWithData: d encoding: NSASCIIStringEncoding];
AUTORELEASE(r);
}
return r;
}
+ (void) initialize
{
if (self == [GSMimeDocument class])

View file

@ -78,7 +78,6 @@ char emp[64] = {
reading,
} connectionState;
}
- (NSString*) encodebase64: (NSString*)s;
- (void) setDebug: (BOOL)flag;
@end
@ -701,7 +700,7 @@ static void debugWrite(NSData *data)
auth = [NSString stringWithFormat: @"%@", [url user]];
}
auth = [NSString stringWithFormat: @"Basic %@",
[self encodebase64: auth]];
[GSMimeDocument encodeBase64String: auth]];
[wProperties setObject: auth
forKey: @"authorization"];
}
@ -935,15 +934,5 @@ static void debugWrite(NSData *data)
return YES;
}
- (NSString*) encodebase64: (NSString*)s
{
NSData *d;
d = [s dataUsingEncoding: NSASCIIStringEncoding
allowLossyConversion: YES];
d = [GSMimeDocument encodeBase64: d];
s = [[NSString alloc] initWithData: d encoding: NSASCIIStringEncoding];
return AUTORELEASE(s);
}
@end