Various tidyups

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@13853 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2002-06-12 15:00:02 +00:00
parent 156ea5249a
commit a850730753
5 changed files with 153 additions and 35 deletions

View file

@ -9,6 +9,8 @@
* Source/NSNotification.m: Made -description more informative.
* Testing/call.m: Trivial test program for tcp connections.
* SSL/GSUnixSSLHandle.m: Updated for socks.
* Additions/GSMime.m: Utilites to encode/decode base64
* Source/GSHTTPURLHandle.m: Use encoding.
2002-06-10 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -86,6 +86,9 @@
id content;
}
+ (NSData*) decodeBase64: (NSData*)source;
+ (NSData*) encodeBase64: (NSData*)source;
- (void) addContent: (id)newContent;
- (void) addHeader: (GSMimeHeader*)info;
- (NSArray*) allHeaders;

View file

@ -73,6 +73,9 @@ decodebase64(unsigned char *dst, const char *src)
dst[2] = ((src[2] & 0x03) << 6) | (src[3] & 0x3F);
}
static char b64[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
typedef enum {
WE_QUOTED,
WE_BASE64
@ -2670,6 +2673,142 @@ static NSCharacterSet *tokenSet = nil;
*/
@implementation GSMimeDocument
/**
* Decode the source data from base64 encoding and return the result.
*/
+ (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];
unsigned char *result;
unsigned char *dst;
unsigned char buf[4];
unsigned pos = 0;
if (length == 0)
{
return [NSData data];
}
result = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), declen);
dst = result;
while (*src && (src != end))
{
int c = *src++;
if (isupper(c))
{
c -= 'A';
}
else if (islower(c))
{
c = c - 'a' + 26;
}
else if (isdigit(c))
{
c = c - '0' + 52;
}
else if (c == '/')
{
c = 63;
}
else if (c == '+')
{
c = 62;
}
else if (c == '=')
{
c = -1;
}
else if (c == '-')
{
break; /* end */
}
else
{
c = -1; /* ignore */
}
if (c >= 0)
{
buf[pos++] = c;
if (pos == 4)
{
pos = 0;
decodebase64(dst, buf);
dst += 3;
}
}
}
if (pos > 0)
{
unsigned i;
for (i = pos; i < 4; i++)
buf[i] = '\0';
pos--;
}
decodebase64(dst, buf);
dst += pos;
return AUTORELEASE([[NSData allocWithZone: NSDefaultMallocZone()]
initWithBytesNoCopy: result length: dst - result]);
}
/**
* 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;
unsigned char *sBuf;
unsigned char *dBuf;
int sIndex = 0;
int dIndex = 0;
if (length == 0)
{
return [NSData data];
}
sBuf = (unsigned char*)[source bytes];
dBuf = NSZoneMalloc(NSDefaultMallocZone(), destlen);
dBuf[destlen - 1] = '\0';
for (sIndex = 0; sIndex < length; sIndex += 3)
{
dBuf[dIndex] = b64[sBuf[sIndex] >> 2];
dBuf[dIndex + 1]
= b64[((sBuf[sIndex] << 4) & 060)
| ((sBuf[sIndex + 1] >> 4) & 017)];
dBuf[dIndex + 2]
= b64[((sBuf[sIndex + 1] << 2) & 074)
| ((sBuf[sIndex + 2] >> 6) & 03)];
dBuf[dIndex + 3] = b64[sBuf[sIndex + 2] & 077];
dIndex += 3;
}
/* If len was not a multiple of 3, then we have encoded too
* many characters. Adjust appropriately.
*/
if (sIndex == length + 1)
{
/* There were only 2 bytes in that last group */
dBuf[dIndex - 1] = '=';
}
else if (sIndex == length + 2)
{
/* There was only 1 byte in that last group */
dBuf[dIndex - 1] = '=';
dBuf[dIndex - 2] = '=';
}
return AUTORELEASE([[NSData allocWithZone: NSDefaultMallocZone()]
initWithBytesNoCopy: dBuf length: dIndex]);
}
+ (void) initialize
{
if (self == [GSMimeDocument class])

View file

@ -78,7 +78,7 @@ char emp[64] = {
reading,
} connectionState;
}
- (NSString*) encodebase64: (NSString*) input;
- (NSString*) encodebase64: (NSString*)s;
- (void) setDebug: (BOOL)flag;
@end
@ -935,41 +935,15 @@ static void debugWrite(NSData *data)
return YES;
}
- (NSString*) encodebase64: (NSString*) input
- (NSString*) encodebase64: (NSString*)s
{
char *str = calloc([input length], sizeof(char));
char *sptr = str;
NSMutableString *nstr = [NSMutableString string];
int i;
NSData *d;
strcpy(str, [input cString]);
for (i=0; i < [input length]; i += 3)
{
[nstr appendFormat: @"%c", emp[*sptr >> 2]];
[nstr appendFormat: @"%c",
emp[((*sptr << 4) & 060) | ((sptr[1] >> 4) & 017)]];
[nstr appendFormat: @"%c",
emp[((sptr[1] << 2) & 074) | ((sptr[2] >> 6) & 03)]];
[nstr appendFormat: @"%c", emp[sptr[2] & 077]];
sptr += 3;
}
/* If len was not a multiple of 3, then we have encoded too
* many characters. Adjust appropriately.
*/
if (i == [input length] + 1)
{
/* There were only 2 bytes in that last group */
[nstr deleteCharactersInRange: NSMakeRange([nstr length] - 1, 1)];
}
else if (i == [input length] + 2)
{
/* There was only 1 byte in that last group */
[nstr deleteCharactersInRange: NSMakeRange([nstr length] - 2, 2)];
}
free (str);
return (nstr);
d = [s dataUsingEncoding: NSASCIIStringEncoding
allowLossyConversion: YES];
d = [GSMimeDocument encodeBase64: d];
s = [[NSString alloc] initWithData: d encoding: NSASCIIStringEncoding];
return AUTORELEASE(s);
}
@end

View file

@ -47,7 +47,7 @@
* Keys for NSURLHandle
*/
NSString * const NSHTTPPropertyStatusCodeKey
= "@NSHTTPPropertyStatusCodeKey";
= @"NSHTTPPropertyStatusCodeKey";
NSString * const NSHTTPPropertyStatusReasonKey
= @"NSHTTPPropertyStatusReasonKey";