mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
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:
parent
9aa5d4cd04
commit
3bbe9ff957
6 changed files with 94 additions and 101 deletions
|
@ -139,17 +139,16 @@ decodebase64(unsigned char *dst, const unsigned char *src)
|
|||
dst[2] = ((src[2] & 0x03) << 6) | (src[3] & 0x3F);
|
||||
}
|
||||
|
||||
static char b64[]
|
||||
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
static int
|
||||
encodebase64(unsigned char *dst, const unsigned char *src, int length)
|
||||
void
|
||||
GSPrivateEncodeBase64(const uint8_t *src, NSUInteger length, uint8_t *dst)
|
||||
{
|
||||
int dIndex = 0;
|
||||
int sIndex;
|
||||
|
||||
for (sIndex = 0; sIndex < length; sIndex += 3)
|
||||
{
|
||||
static char b64[]
|
||||
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
int c0 = src[sIndex];
|
||||
int c1 = (sIndex+1 < length) ? src[sIndex+1] : 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 - 2] = '=';
|
||||
}
|
||||
return dIndex;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
encodeQuotedPrintable(NSMutableData *result,
|
||||
const unsigned char *src, unsigned length)
|
||||
|
@ -4442,7 +4439,7 @@ appendString(NSMutableData *m, NSUInteger offset, NSUInteger fold,
|
|||
dBuf = NSZoneMalloc(NSDefaultMallocZone(), destlen);
|
||||
#endif
|
||||
|
||||
destlen = encodebase64(dBuf, sBuf, length);
|
||||
GSPrivateEncodeBase64(sBuf, length, dBuf);
|
||||
|
||||
return AUTORELEASE([[NSData allocWithZone: NSDefaultMallocZone()]
|
||||
initWithBytesNoCopy: dBuf length: destlen]);
|
||||
|
@ -5727,14 +5724,12 @@ appendString(NSMutableData *m, NSUInteger offset, NSUInteger fold,
|
|||
- (NSString*) makeBoundary
|
||||
{
|
||||
static int count = 0;
|
||||
unsigned char output[20];
|
||||
unsigned char *ptr;
|
||||
NSMutableData *md;
|
||||
uint8_t output[20];
|
||||
uint8_t *ptr;
|
||||
NSString *result;
|
||||
NSData *source;
|
||||
NSData *digest;
|
||||
int sequence = ++count;
|
||||
int length;
|
||||
|
||||
source = [[[NSProcessInfo processInfo] globallyUniqueString]
|
||||
dataUsingEncoding: NSUTF8StringEncoding];
|
||||
|
@ -5746,16 +5741,15 @@ appendString(NSMutableData *m, NSUInteger offset, NSUInteger fold,
|
|||
output[18] = (sequence >> 8) & 0xff;
|
||||
output[19] = sequence & 0xff;
|
||||
|
||||
md = [NSMutableData allocWithZone: NSDefaultMallocZone()];
|
||||
md = [md initWithLength: 40];
|
||||
length = encodebase64([md mutableBytes], output, 20);
|
||||
[md setLength: length + 2];
|
||||
ptr = (unsigned char*)[md mutableBytes];
|
||||
ptr[length] = '=';
|
||||
ptr[length+1] = '_';
|
||||
ptr = (uint8_t*)NSZoneMalloc(NSDefaultMallocZone(), 30);
|
||||
GSPrivateEncodeBase64(output, 20, ptr);
|
||||
ptr[28] = '=';
|
||||
ptr[29] = '_';
|
||||
result = [NSStringClass allocWithZone: NSDefaultMallocZone()];
|
||||
result = [result initWithData: md encoding: NSASCIIStringEncoding];
|
||||
RELEASE(md);
|
||||
result = [result initWithBytesNoCopy: ptr
|
||||
length: 30
|
||||
encoding: NSASCIIStringEncoding
|
||||
freeWhenDone: YES];
|
||||
return AUTORELEASE(result);
|
||||
}
|
||||
|
||||
|
|
|
@ -231,46 +231,62 @@ static Class sslClass = 0;
|
|||
static void
|
||||
debugRead(GSHTTPURLHandle *handle, NSData *data)
|
||||
{
|
||||
unsigned len = (unsigned)[data length];
|
||||
const char *ptr = (const char*)[data bytes];
|
||||
NSUInteger len = [data length];
|
||||
const uint8_t *ptr = (const uint8_t*)[data bytes];
|
||||
uint8_t *hex;
|
||||
NSUInteger hl;
|
||||
int pos;
|
||||
|
||||
hl = ((len + 2) / 3) * 4;
|
||||
hex = malloc(hl + 1);
|
||||
hex[hl] = '\0';
|
||||
GSPrivateEncodeBase64(ptr, len, hex);
|
||||
for (pos = 0; pos < len; pos++)
|
||||
{
|
||||
if (0 == ptr[pos])
|
||||
{
|
||||
char *esc = [data escapedRepresentation: 0];
|
||||
|
||||
NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n%@",
|
||||
handle, len, esc, data);
|
||||
NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n<[%*.*s]>",
|
||||
handle, (unsigned)len, esc, hex);
|
||||
free(esc);
|
||||
free(hex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
NSLog(@"Read for %p of %d bytes - '%*.*s'\n%@",
|
||||
handle, len, len, len, ptr, data);
|
||||
NSLog(@"Read for %p of %d bytes - '%s'\n<[%*.*s]>",
|
||||
handle, (unsigned)len, ptr, hex);
|
||||
free(hex);
|
||||
}
|
||||
static void
|
||||
debugWrite(GSHTTPURLHandle *handle, NSData *data)
|
||||
{
|
||||
unsigned len = (unsigned)[data length];
|
||||
const char *ptr = (const char*)[data bytes];
|
||||
NSUInteger len = [data length];
|
||||
const uint8_t *ptr = (const uint8_t*)[data bytes];
|
||||
uint8_t *hex;
|
||||
NSUInteger hl;
|
||||
int pos;
|
||||
|
||||
hl = ((len + 2) / 3) * 4;
|
||||
hex = malloc(hl + 1);
|
||||
hex[hl] = '\0';
|
||||
GSPrivateEncodeBase64(ptr, len, hex);
|
||||
for (pos = 0; pos < len; pos++)
|
||||
{
|
||||
if (0 == ptr[pos])
|
||||
{
|
||||
char *esc = [data escapedRepresentation: 0];
|
||||
|
||||
NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n%@",
|
||||
handle, len, esc, data);
|
||||
NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n<[%s]>",
|
||||
handle, (unsigned)len, esc, hex);
|
||||
free(esc);
|
||||
free(hex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
NSLog(@"Write for %p of %d bytes - '%*.*s'\n%@",
|
||||
handle, len, len, len, ptr, data);
|
||||
NSLog(@"Write for %p of %d bytes - '%s'\n<[%s]>",
|
||||
handle, (unsigned)len, ptr, hex);
|
||||
free(hex);
|
||||
}
|
||||
|
||||
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl
|
||||
|
|
|
@ -593,5 +593,13 @@ NSUInteger
|
|||
GSPrivateThreadID()
|
||||
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_ */
|
||||
|
||||
|
|
|
@ -1475,57 +1475,19 @@ GSPropertyListFromStringsFormat(NSString *string)
|
|||
|
||||
#include <math.h>
|
||||
|
||||
static char base64[]
|
||||
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
static void
|
||||
encodeBase64(NSData *source, NSMutableData *dest)
|
||||
{
|
||||
int 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];
|
||||
NSUInteger length = [source length];
|
||||
|
||||
[dest setLength: dIndex + destlen];
|
||||
if (length > 0)
|
||||
{
|
||||
NSUInteger base = [dest length];
|
||||
NSUInteger destlen = 4 * ((length + 2) / 3);
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
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] = '=';
|
||||
[dest setLength: base + destlen];
|
||||
GSPrivateEncodeBase64((const uint8_t*)[source bytes],
|
||||
length, (uint8_t*)[dest mutableBytes]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -110,8 +110,6 @@
|
|||
|
||||
#import "GNUstepBase/Unicode.h"
|
||||
|
||||
#import "GSPrivate.h"
|
||||
|
||||
extern BOOL GSScanDouble(unichar*, unsigned, double*);
|
||||
|
||||
@class GSString;
|
||||
|
|
|
@ -72,55 +72,70 @@ static void
|
|||
debugRead(id handle, unsigned len, const unsigned char *ptr)
|
||||
{
|
||||
int pos;
|
||||
NSData *data;
|
||||
uint8_t *hex;
|
||||
NSUInteger hl;
|
||||
|
||||
data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
|
||||
length: len
|
||||
freeWhenDone: NO];
|
||||
hl = ((len + 2) / 3) * 4;
|
||||
hex = malloc(hl + 1);
|
||||
hex[hl] = '\0';
|
||||
GSPrivateEncodeBase64(ptr, len, hex);
|
||||
|
||||
for (pos = 0; pos < len; pos++)
|
||||
{
|
||||
if (0 == ptr[pos])
|
||||
{
|
||||
char *esc = [data escapedRepresentation: 0];
|
||||
NSData *data;
|
||||
char *esc;
|
||||
|
||||
NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n%@",
|
||||
handle, len, esc, data);
|
||||
data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
|
||||
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);
|
||||
RELEASE(data);
|
||||
free(hex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
NSLog(@"Read for %p of %d bytes - '%*.*s'\n%@",
|
||||
handle, len, len, len, ptr, data);
|
||||
RELEASE(data);
|
||||
NSLog(@"Read for %p of %d bytes - '%s'\n<[%s]>", handle, len, ptr, hex);
|
||||
free(hex);
|
||||
}
|
||||
static void
|
||||
debugWrite(id handle, unsigned len, const unsigned char *ptr)
|
||||
{
|
||||
int pos;
|
||||
NSData *data;
|
||||
uint8_t *hex;
|
||||
NSUInteger hl;
|
||||
|
||||
data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
|
||||
length: len
|
||||
freeWhenDone: NO];
|
||||
hl = ((len + 2) / 3) * 4;
|
||||
hex = malloc(hl + 1);
|
||||
hex[hl] = '\0';
|
||||
GSPrivateEncodeBase64(ptr, len, hex);
|
||||
|
||||
for (pos = 0; pos < len; pos++)
|
||||
{
|
||||
if (0 == ptr[pos])
|
||||
{
|
||||
char *esc = [data escapedRepresentation: 0];
|
||||
NSData *data;
|
||||
char *esc;
|
||||
|
||||
NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n%@",
|
||||
handle, len, esc, data);
|
||||
data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr
|
||||
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);
|
||||
RELEASE(data);
|
||||
free(hex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
NSLog(@"Write for %p of %d bytes - '%*.*s'\n%@",
|
||||
handle, len, len, len, ptr, data);
|
||||
RELEASE(data);
|
||||
NSLog(@"Write for %p of %d bytes - '%s'\n<[%s]>", handle, len, ptr, hex);
|
||||
free(hex);
|
||||
}
|
||||
|
||||
@interface GSSocketStreamPair : NSObject
|
||||
|
|
Loading…
Reference in a new issue