more tweaks ... move base64 encoding to shared private function

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38954 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2015-08-30 09:23:27 +00:00
parent 8e3bc2f56b
commit afa642aa1f
6 changed files with 94 additions and 101 deletions

View file

@ -139,17 +139,16 @@ decodebase64(unsigned char *dst, const unsigned char *src)
dst[2] = ((src[2] & 0x03) << 6) | (src[3] & 0x3F); dst[2] = ((src[2] & 0x03) << 6) | (src[3] & 0x3F);
} }
static char b64[] void
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; GSPrivateEncodeBase64(const uint8_t *src, NSUInteger length, uint8_t *dst)
static int
encodebase64(unsigned char *dst, const unsigned char *src, int length)
{ {
int dIndex = 0; int dIndex = 0;
int sIndex; int sIndex;
for (sIndex = 0; sIndex < length; sIndex += 3) for (sIndex = 0; sIndex < length; sIndex += 3)
{ {
static char b64[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int c0 = src[sIndex]; int c0 = src[sIndex];
int c1 = (sIndex+1 < length) ? src[sIndex+1] : 0; int c1 = (sIndex+1 < length) ? src[sIndex+1] : 0;
int c2 = (sIndex+2 < length) ? src[sIndex+2] : 0; int c2 = (sIndex+2 < length) ? src[sIndex+2] : 0;
@ -174,10 +173,8 @@ encodebase64(unsigned char *dst, const unsigned char *src, int length)
dst[dIndex - 1] = '='; dst[dIndex - 1] = '=';
dst[dIndex - 2] = '='; dst[dIndex - 2] = '=';
} }
return dIndex;
} }
static void static void
encodeQuotedPrintable(NSMutableData *result, encodeQuotedPrintable(NSMutableData *result,
const unsigned char *src, unsigned length) const unsigned char *src, unsigned length)
@ -4442,7 +4439,7 @@ appendString(NSMutableData *m, NSUInteger offset, NSUInteger fold,
dBuf = NSZoneMalloc(NSDefaultMallocZone(), destlen); dBuf = NSZoneMalloc(NSDefaultMallocZone(), destlen);
#endif #endif
destlen = encodebase64(dBuf, sBuf, length); GSPrivateEncodeBase64(sBuf, length, dBuf);
return AUTORELEASE([[NSData allocWithZone: NSDefaultMallocZone()] return AUTORELEASE([[NSData allocWithZone: NSDefaultMallocZone()]
initWithBytesNoCopy: dBuf length: destlen]); initWithBytesNoCopy: dBuf length: destlen]);
@ -5727,14 +5724,12 @@ appendString(NSMutableData *m, NSUInteger offset, NSUInteger fold,
- (NSString*) makeBoundary - (NSString*) makeBoundary
{ {
static int count = 0; static int count = 0;
unsigned char output[20]; uint8_t output[20];
unsigned char *ptr; uint8_t *ptr;
NSMutableData *md;
NSString *result; NSString *result;
NSData *source; NSData *source;
NSData *digest; NSData *digest;
int sequence = ++count; int sequence = ++count;
int length;
source = [[[NSProcessInfo processInfo] globallyUniqueString] source = [[[NSProcessInfo processInfo] globallyUniqueString]
dataUsingEncoding: NSUTF8StringEncoding]; dataUsingEncoding: NSUTF8StringEncoding];
@ -5746,16 +5741,15 @@ appendString(NSMutableData *m, NSUInteger offset, NSUInteger fold,
output[18] = (sequence >> 8) & 0xff; output[18] = (sequence >> 8) & 0xff;
output[19] = sequence & 0xff; output[19] = sequence & 0xff;
md = [NSMutableData allocWithZone: NSDefaultMallocZone()]; ptr = (uint8_t*)NSZoneMalloc(NSDefaultMallocZone(), 30);
md = [md initWithLength: 40]; GSPrivateEncodeBase64(output, 20, ptr);
length = encodebase64([md mutableBytes], output, 20); ptr[28] = '=';
[md setLength: length + 2]; ptr[29] = '_';
ptr = (unsigned char*)[md mutableBytes];
ptr[length] = '=';
ptr[length+1] = '_';
result = [NSStringClass allocWithZone: NSDefaultMallocZone()]; result = [NSStringClass allocWithZone: NSDefaultMallocZone()];
result = [result initWithData: md encoding: NSASCIIStringEncoding]; result = [result initWithBytesNoCopy: ptr
RELEASE(md); length: 30
encoding: NSASCIIStringEncoding
freeWhenDone: YES];
return AUTORELEASE(result); return AUTORELEASE(result);
} }

View file

@ -231,46 +231,62 @@ static Class sslClass = 0;
static void static void
debugRead(GSHTTPURLHandle *handle, NSData *data) debugRead(GSHTTPURLHandle *handle, NSData *data)
{ {
unsigned len = (unsigned)[data length]; NSUInteger len = [data length];
const char *ptr = (const char*)[data bytes]; const uint8_t *ptr = (const uint8_t*)[data bytes];
uint8_t *hex;
NSUInteger hl;
int pos; int pos;
hl = ((len + 2) / 3) * 4;
hex = malloc(hl + 1);
hex[hl] = '\0';
GSPrivateEncodeBase64(ptr, len, hex);
for (pos = 0; pos < len; pos++) for (pos = 0; pos < len; pos++)
{ {
if (0 == ptr[pos]) if (0 == ptr[pos])
{ {
char *esc = [data escapedRepresentation: 0]; char *esc = [data escapedRepresentation: 0];
NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n%@", NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n<[%*.*s]>",
handle, len, esc, data); handle, (unsigned)len, esc, hex);
free(esc); free(esc);
free(hex);
return; return;
} }
} }
NSLog(@"Read for %p of %d bytes - '%*.*s'\n%@", NSLog(@"Read for %p of %d bytes - '%s'\n<[%*.*s]>",
handle, len, len, len, ptr, data); handle, (unsigned)len, ptr, hex);
free(hex);
} }
static void static void
debugWrite(GSHTTPURLHandle *handle, NSData *data) debugWrite(GSHTTPURLHandle *handle, NSData *data)
{ {
unsigned len = (unsigned)[data length]; NSUInteger len = [data length];
const char *ptr = (const char*)[data bytes]; const uint8_t *ptr = (const uint8_t*)[data bytes];
uint8_t *hex;
NSUInteger hl;
int pos; int pos;
hl = ((len + 2) / 3) * 4;
hex = malloc(hl + 1);
hex[hl] = '\0';
GSPrivateEncodeBase64(ptr, len, hex);
for (pos = 0; pos < len; pos++) for (pos = 0; pos < len; pos++)
{ {
if (0 == ptr[pos]) if (0 == ptr[pos])
{ {
char *esc = [data escapedRepresentation: 0]; char *esc = [data escapedRepresentation: 0];
NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n%@", NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n<[%s]>",
handle, len, esc, data); handle, (unsigned)len, esc, hex);
free(esc); free(esc);
free(hex);
return; return;
} }
} }
NSLog(@"Write for %p of %d bytes - '%*.*s'\n%@", NSLog(@"Write for %p of %d bytes - '%s'\n<[%s]>",
handle, len, len, len, ptr, data); handle, (unsigned)len, ptr, hex);
free(hex);
} }
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl + (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl

View file

@ -593,5 +593,13 @@ NSUInteger
GSPrivateThreadID() GSPrivateThreadID()
GS_ATTRIB_PRIVATE; GS_ATTRIB_PRIVATE;
/** Function to base64 encode data. The destination buffer must be of
* size (((length + 2) / 3) * 4) or more.
*/
void
GSPrivateEncodeBase64(const uint8_t *src, NSUInteger length, uint8_t *dst)
GS_ATTRIB_PRIVATE;
#endif /* _GSPrivate_h_ */ #endif /* _GSPrivate_h_ */

View file

@ -1475,57 +1475,19 @@ GSPropertyListFromStringsFormat(NSString *string)
#include <math.h> #include <math.h>
static char base64[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static void static void
encodeBase64(NSData *source, NSMutableData *dest) encodeBase64(NSData *source, NSMutableData *dest)
{ {
int length = [source length]; NSUInteger length = [source length];
int enclen = length / 3;
int remlen = length - 3 * enclen;
int destlen = 4 * ((length + 2) / 3);
unsigned char *sBuf;
unsigned char *dBuf;
int sIndex = 0;
int dIndex = [dest length];
[dest setLength: dIndex + destlen]; if (length > 0)
{
NSUInteger base = [dest length];
NSUInteger destlen = 4 * ((length + 2) / 3);
if (length == 0) [dest setLength: base + destlen];
{ GSPrivateEncodeBase64((const uint8_t*)[source bytes],
return; length, (uint8_t*)[dest mutableBytes]);
}
sBuf = (unsigned char*)[source bytes];
dBuf = [dest mutableBytes];
for (sIndex = 0; sIndex < length - 2; sIndex += 3, dIndex += 4)
{
dBuf[dIndex] = base64[sBuf[sIndex] >> 2];
dBuf[dIndex + 1]
= base64[((sBuf[sIndex] << 4) | (sBuf[sIndex + 1] >> 4)) & 0x3f];
dBuf[dIndex + 2]
= base64[((sBuf[sIndex + 1] << 2) | (sBuf[sIndex + 2] >> 6)) & 0x3f];
dBuf[dIndex + 3] = base64[sBuf[sIndex + 2] & 0x3f];
}
if (remlen == 1)
{
dBuf[dIndex] = base64[sBuf[sIndex] >> 2];
dBuf[dIndex + 1] = (sBuf[sIndex] << 4) & 0x30;
dBuf[dIndex + 1] = base64[dBuf[dIndex + 1]];
dBuf[dIndex + 2] = '=';
dBuf[dIndex + 3] = '=';
}
else if (remlen == 2)
{
dBuf[dIndex] = base64[sBuf[sIndex] >> 2];
dBuf[dIndex + 1] = (sBuf[sIndex] << 4) & 0x30;
dBuf[dIndex + 1] |= sBuf[sIndex + 1] >> 4;
dBuf[dIndex + 1] = base64[dBuf[dIndex + 1]];
dBuf[dIndex + 2] = (sBuf[sIndex + 1] << 2) & 0x3c;
dBuf[dIndex + 2] = base64[dBuf[dIndex + 2]];
dBuf[dIndex + 3] = '=';
} }
} }

View file

@ -110,8 +110,6 @@
#import "GNUstepBase/Unicode.h" #import "GNUstepBase/Unicode.h"
#import "GSPrivate.h"
extern BOOL GSScanDouble(unichar*, unsigned, double*); extern BOOL GSScanDouble(unichar*, unsigned, double*);
@class GSString; @class GSString;

View file

@ -72,55 +72,70 @@ static void
debugRead(id handle, unsigned len, const unsigned char *ptr) debugRead(id handle, unsigned len, const unsigned char *ptr)
{ {
int pos; int pos;
NSData *data; uint8_t *hex;
NSUInteger hl;
data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr hl = ((len + 2) / 3) * 4;
length: len hex = malloc(hl + 1);
freeWhenDone: NO]; hex[hl] = '\0';
GSPrivateEncodeBase64(ptr, len, hex);
for (pos = 0; pos < len; pos++) for (pos = 0; pos < len; pos++)
{ {
if (0 == ptr[pos]) if (0 == ptr[pos])
{ {
char *esc = [data escapedRepresentation: 0]; NSData *data;
char *esc;
NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n%@", data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
handle, len, esc, data); length: len
freeWhenDone: NO];
esc = [data escapedRepresentation: 0];
NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n<[%s]>",
handle, len, esc, hex);
free(esc); free(esc);
RELEASE(data); RELEASE(data);
free(hex);
return; return;
} }
} }
NSLog(@"Read for %p of %d bytes - '%*.*s'\n%@", NSLog(@"Read for %p of %d bytes - '%s'\n<[%s]>", handle, len, ptr, hex);
handle, len, len, len, ptr, data); free(hex);
RELEASE(data);
} }
static void static void
debugWrite(id handle, unsigned len, const unsigned char *ptr) debugWrite(id handle, unsigned len, const unsigned char *ptr)
{ {
int pos; int pos;
NSData *data; uint8_t *hex;
NSUInteger hl;
data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr hl = ((len + 2) / 3) * 4;
length: len hex = malloc(hl + 1);
freeWhenDone: NO]; hex[hl] = '\0';
GSPrivateEncodeBase64(ptr, len, hex);
for (pos = 0; pos < len; pos++) for (pos = 0; pos < len; pos++)
{ {
if (0 == ptr[pos]) if (0 == ptr[pos])
{ {
char *esc = [data escapedRepresentation: 0]; NSData *data;
char *esc;
NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n%@", data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
handle, len, esc, data); length: len
freeWhenDone: NO];
esc = [data escapedRepresentation: 0];
NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n<[%s]>",
handle, len, esc, hex);
free(esc); free(esc);
RELEASE(data); RELEASE(data);
free(hex);
return; return;
} }
} }
NSLog(@"Write for %p of %d bytes - '%*.*s'\n%@", NSLog(@"Write for %p of %d bytes - '%s'\n<[%s]>", handle, len, ptr, hex);
handle, len, len, len, ptr, data); free(hex);
RELEASE(data);
} }
@interface GSSocketStreamPair : NSObject @interface GSSocketStreamPair : NSObject