* Made Test.h work with regular C99 without GCC extensions.
* Copied the necessary Google Toolbox source files into the project, so users don't have to download an additional library.
5 // Created by Jens Alfke on 1/27/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
8 // Portions adapted from SSCrypto.m by Ed Silva;
9 // Copyright (c) 2003-2006 Septicus Software. All rights reserved.
10 // Portions taken from uncopyrighted code posted by Dave Dribin.
15 //NOTE: Using this requires linking against /usr/lib/libcrypto.dylib.
16 #import <openssl/bio.h>
17 #import <openssl/evp.h>
20 @implementation NSData (MYBase64)
24 * Encodes the current data in base64, and creates and returns an NSString from the result.
25 * This is the same as piping data through "... | openssl enc -base64" on the command line.
27 * Code courtesy of DaveDribin (http://www.dribin.org/dave/)
28 * Taken from http://www.cocoadev.com/index.pl?BaseSixtyFour
30 - (NSString *)my_base64String
32 return [self my_base64StringWithNewlines: YES];
36 * Encodes the current data in base64, and creates and returns an NSString from the result.
37 * This is the same as piping data through "... | openssl enc -base64" on the command line.
39 * Code courtesy of DaveDribin (http://www.dribin.org/dave/)
40 * Taken from http://www.cocoadev.com/index.pl?BaseSixtyFour
42 - (NSString *)my_base64StringWithNewlines:(BOOL)encodeWithNewlines
44 // Create a memory buffer which will contain the Base64 encoded string
45 BIO * mem = BIO_new(BIO_s_mem());
47 // Push on a Base64 filter so that writing to the buffer encodes the data
48 BIO * b64 = BIO_new(BIO_f_base64());
49 if (!encodeWithNewlines)
50 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
51 mem = BIO_push(b64, mem);
53 // Encode all the data
54 BIO_write(mem, [self bytes], [self length]);
57 // Create a new string from the data in the memory buffer
59 long base64Length = BIO_get_mem_data(mem, &base64Pointer);
60 NSString * base64String = [NSString stringWithCString:base64Pointer length:base64Length];
62 // Clean up and go home
67 - (NSData *)my_decodeBase64
69 return [self my_decodeBase64WithNewLines:YES];
72 - (NSData *)my_decodeBase64WithNewLines:(BOOL)encodedWithNewlines
74 // Create a memory buffer containing Base64 encoded string data
75 BIO * mem = BIO_new_mem_buf((void *) [self bytes], [self length]);
77 // Push a Base64 filter so that reading from the buffer decodes it
78 BIO * b64 = BIO_new(BIO_f_base64());
79 if (!encodedWithNewlines)
80 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
81 mem = BIO_push(b64, mem);
83 // Decode into an NSMutableData
84 NSMutableData * data = [NSMutableData data];
87 while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0)
88 [data appendBytes: inbuf length: inlen];
90 // Clean up and go home
96 - (NSString *)my_hexString
98 // Adapted from SSCrypto.m by Ed Silva:
99 // Copyright (c) 2003-2006 Septicus Software. All rights reserved.
100 const UInt8 *bytes = self.bytes;
101 NSUInteger length = self.length;
102 char out[2*length+1];
104 for( int i=0; i<length; i+=1 )
105 dst += sprintf(dst,"%02X",*(bytes++));
106 return [[[NSString alloc] initWithBytes: out length: 2*length encoding: NSASCIIStringEncoding]
110 - (NSString *)my_hexDump
112 // Adapted from SSCrypto.m by Ed Silva:
113 // Copyright (c) 2003-2006 Septicus Software. All rights reserved.
114 NSMutableString *ret=[NSMutableString stringWithCapacity:[self length]*2];
115 /* dumps size bytes of *data to string. Looks like:
116 * [0000] 75 6E 6B 6E 6F 77 6E 20
117 * 30 FF 00 00 00 00 39 00 unknown 0.....9.
118 * (in a single line of course)
120 unsigned int size= [self length];
121 const unsigned char *p = [self bytes];
124 char bytestr[4] = {0};
125 char addrstr[10] = {0};
126 char hexstr[ 16*3 + 5] = {0};
127 char charstr[16*1 + 5] = {0};
128 for(n=1;n<=size;n++) {
130 /* store address for this line */
131 snprintf(addrstr, sizeof(addrstr), "%.4x",
132 ((unsigned int)p-(unsigned int)self) );
136 if (isalnum(c) == 0) {
140 /* store hex str (for left side) */
141 snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
142 strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
144 /* store char str (for right side) */
145 snprintf(bytestr, sizeof(bytestr), "%c", c);
146 strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
150 //printf("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
151 [ret appendString:[NSString stringWithFormat:@"[%4.4s] %-50.50s %s\n",
152 addrstr, hexstr, charstr]];
155 } else if(n%8 == 0) {
156 /* half line: add whitespaces */
157 strncat(hexstr, " ", sizeof(hexstr)-strlen(hexstr)-1);
158 strncat(charstr, " ", sizeof(charstr)-strlen(charstr)-1);
163 if (strlen(hexstr) > 0) {
164 /* print rest of buffer if not empty */
165 //printf("[%4.4s] %-50.50s %s\n", addrstr, hexstr, charstr);
166 [ret appendString:[NSString stringWithFormat:@"[%4.4s] %-50.50s %s\n",
167 addrstr, hexstr, charstr]];
176 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
178 Redistribution and use in source and binary forms, with or without modification, are permitted
179 provided that the following conditions are met:
181 * Redistributions of source code must retain the above copyright notice, this list of conditions
182 and the following disclaimer.
183 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
184 and the following disclaimer in the documentation and/or other materials provided with the
187 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
188 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
189 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
190 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
191 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
192 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
193 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
194 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.