jens@0: // jens@0: // Base64.m jens@0: // MYUtilities jens@0: // jens@0: // Created by Jens Alfke on 1/27/08. jens@0: // Copyright 2008 Jens Alfke. All rights reserved. jens@0: // jens@11: // Portions adapted from SSCrypto.m by Ed Silva; jens@0: // Copyright (c) 2003-2006 Septicus Software. All rights reserved. jens@11: // Portions taken from uncopyrighted code posted by Dave Dribin. jens@0: // jens@0: jens@0: #import "Base64.h" jens@1: jens@1: //NOTE: Using this requires linking against /usr/lib/libcrypto.dylib. jens@0: #import jens@0: #import jens@0: jens@0: jens@1: @implementation NSData (MYBase64) jens@0: jens@0: jens@0: /** jens@0: * Encodes the current data in base64, and creates and returns an NSString from the result. jens@0: * This is the same as piping data through "... | openssl enc -base64" on the command line. jens@0: * jens@0: * Code courtesy of DaveDribin (http://www.dribin.org/dave/) jens@0: * Taken from http://www.cocoadev.com/index.pl?BaseSixtyFour jens@0: **/ jens@1: - (NSString *)my_base64String jens@0: { jens@1: return [self my_base64StringWithNewlines: YES]; jens@0: } jens@0: jens@0: /** jens@0: * Encodes the current data in base64, and creates and returns an NSString from the result. jens@0: * This is the same as piping data through "... | openssl enc -base64" on the command line. jens@0: * jens@0: * Code courtesy of DaveDribin (http://www.dribin.org/dave/) jens@0: * Taken from http://www.cocoadev.com/index.pl?BaseSixtyFour jens@0: **/ jens@1: - (NSString *)my_base64StringWithNewlines:(BOOL)encodeWithNewlines jens@0: { jens@0: // Create a memory buffer which will contain the Base64 encoded string jens@0: BIO * mem = BIO_new(BIO_s_mem()); jens@0: jens@0: // Push on a Base64 filter so that writing to the buffer encodes the data jens@0: BIO * b64 = BIO_new(BIO_f_base64()); jens@0: if (!encodeWithNewlines) jens@0: BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); jens@0: mem = BIO_push(b64, mem); jens@0: jens@0: // Encode all the data jens@0: BIO_write(mem, [self bytes], [self length]); jens@0: BIO_flush(mem); jens@0: jens@0: // Create a new string from the data in the memory buffer jens@0: char * base64Pointer; jens@0: long base64Length = BIO_get_mem_data(mem, &base64Pointer); jens@0: NSString * base64String = [NSString stringWithCString:base64Pointer length:base64Length]; jens@0: jens@0: // Clean up and go home jens@0: BIO_free_all(mem); jens@0: return base64String; jens@0: } jens@0: jens@1: - (NSData *)my_decodeBase64 jens@0: { jens@1: return [self my_decodeBase64WithNewLines:YES]; jens@0: } jens@0: jens@1: - (NSData *)my_decodeBase64WithNewLines:(BOOL)encodedWithNewlines jens@0: { jens@0: // Create a memory buffer containing Base64 encoded string data jens@0: BIO * mem = BIO_new_mem_buf((void *) [self bytes], [self length]); jens@0: jens@0: // Push a Base64 filter so that reading from the buffer decodes it jens@0: BIO * b64 = BIO_new(BIO_f_base64()); jens@0: if (!encodedWithNewlines) jens@0: BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); jens@0: mem = BIO_push(b64, mem); jens@0: jens@0: // Decode into an NSMutableData jens@0: NSMutableData * data = [NSMutableData data]; jens@0: char inbuf[512]; jens@0: int inlen; jens@0: while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0) jens@0: [data appendBytes: inbuf length: inlen]; jens@0: jens@0: // Clean up and go home jens@0: BIO_free_all(mem); jens@0: return data; jens@0: } jens@0: jens@0: jens@1: - (NSString *)my_hexString jens@0: { jens@11: // Adapted from SSCrypto.m by Ed Silva: jens@11: // Copyright (c) 2003-2006 Septicus Software. All rights reserved. jens@0: const UInt8 *bytes = self.bytes; jens@0: NSUInteger length = self.length; jens@0: char out[2*length+1]; jens@0: char *dst = &out[0]; jens@0: for( int i=0; i 0) { jens@0: /* print rest of buffer if not empty */ jens@0: //printf("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr); jens@0: [ret appendString:[NSString stringWithFormat:@"[%4.4s] %-50.50s %s\n", jens@0: addrstr, hexstr, charstr]]; jens@0: } jens@0: return ret; jens@0: } jens@0: jens@0: @end jens@11: jens@11: jens@11: /* jens@11: Copyright (c) 2008, Jens Alfke . All rights reserved. jens@11: jens@11: Redistribution and use in source and binary forms, with or without modification, are permitted jens@11: provided that the following conditions are met: jens@11: jens@11: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@11: and the following disclaimer. jens@11: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions jens@11: and the following disclaimer in the documentation and/or other materials provided with the jens@11: distribution. jens@11: jens@11: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@11: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@11: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@11: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@11: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@11: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@11: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@11: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@11: */