diff -r 000000000000 -r d84d25d6cdbb Base64.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Base64.m Sat Mar 08 21:04:41 2008 -0800 @@ -0,0 +1,165 @@ +// +// Base64.m +// MYUtilities +// +// Created by Jens Alfke on 1/27/08. +// Copyright 2008 Jens Alfke. All rights reserved. +// +// Adapted from SSCrypto.m by Ed Silva; +// Copyright (c) 2003-2006 Septicus Software. All rights reserved. +// + +#import "Base64.h" +#import +#import + + +@implementation NSData (Base64) + + +/** + * Encodes the current data in base64, and creates and returns an NSString from the result. + * This is the same as piping data through "... | openssl enc -base64" on the command line. + * + * Code courtesy of DaveDribin (http://www.dribin.org/dave/) + * Taken from http://www.cocoadev.com/index.pl?BaseSixtyFour + **/ +- (NSString *)base64String +{ + return [self base64StringWithNewlines: YES]; +} + +/** + * Encodes the current data in base64, and creates and returns an NSString from the result. + * This is the same as piping data through "... | openssl enc -base64" on the command line. + * + * Code courtesy of DaveDribin (http://www.dribin.org/dave/) + * Taken from http://www.cocoadev.com/index.pl?BaseSixtyFour + **/ +- (NSString *)base64StringWithNewlines:(BOOL)encodeWithNewlines +{ + // Create a memory buffer which will contain the Base64 encoded string + BIO * mem = BIO_new(BIO_s_mem()); + + // Push on a Base64 filter so that writing to the buffer encodes the data + BIO * b64 = BIO_new(BIO_f_base64()); + if (!encodeWithNewlines) + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + mem = BIO_push(b64, mem); + + // Encode all the data + BIO_write(mem, [self bytes], [self length]); + BIO_flush(mem); + + // Create a new string from the data in the memory buffer + char * base64Pointer; + long base64Length = BIO_get_mem_data(mem, &base64Pointer); + NSString * base64String = [NSString stringWithCString:base64Pointer length:base64Length]; + + // Clean up and go home + BIO_free_all(mem); + return base64String; +} + +- (NSData *)decodeBase64 +{ + return [self decodeBase64WithNewLines:YES]; +} + +- (NSData *)decodeBase64WithNewLines:(BOOL)encodedWithNewlines +{ + // Create a memory buffer containing Base64 encoded string data + BIO * mem = BIO_new_mem_buf((void *) [self bytes], [self length]); + + // Push a Base64 filter so that reading from the buffer decodes it + BIO * b64 = BIO_new(BIO_f_base64()); + if (!encodedWithNewlines) + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + mem = BIO_push(b64, mem); + + // Decode into an NSMutableData + NSMutableData * data = [NSMutableData data]; + char inbuf[512]; + int inlen; + while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0) + [data appendBytes: inbuf length: inlen]; + + // Clean up and go home + BIO_free_all(mem); + return data; +} + + +- (NSString *)hexString +{ + const UInt8 *bytes = self.bytes; + NSUInteger length = self.length; + char out[2*length+1]; + char *dst = &out[0]; + for( int i=0; i 0) { + /* print rest of buffer if not empty */ + //printf("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr); + [ret appendString:[NSString stringWithFormat:@"[%4.4s] %-50.50s %s\n", + addrstr, hexstr, charstr]]; + } + return ret; +} + +@end