Base64.m
changeset 0 d84d25d6cdbb
child 1 e55a17cdabd2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Base64.m	Sat Mar 08 21:04:41 2008 -0800
     1.3 @@ -0,0 +1,165 @@
     1.4 +//
     1.5 +//  Base64.m
     1.6 +//  MYUtilities
     1.7 +//
     1.8 +//  Created by Jens Alfke on 1/27/08.
     1.9 +//  Copyright 2008 Jens Alfke. All rights reserved.
    1.10 +//
    1.11 +//  Adapted from SSCrypto.m by Ed Silva;
    1.12 +//  Copyright (c) 2003-2006 Septicus Software. All rights reserved.
    1.13 +//
    1.14 +
    1.15 +#import "Base64.h"
    1.16 +#import <openssl/bio.h>
    1.17 +#import <openssl/evp.h>
    1.18 +
    1.19 +
    1.20 +@implementation NSData (Base64)
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + * Encodes the current data in base64, and creates and returns an NSString from the result.
    1.25 + * This is the same as piping data through "... | openssl enc -base64" on the command line.
    1.26 + *
    1.27 + * Code courtesy of DaveDribin (http://www.dribin.org/dave/)
    1.28 + * Taken from http://www.cocoadev.com/index.pl?BaseSixtyFour
    1.29 + **/
    1.30 +- (NSString *)base64String
    1.31 +{
    1.32 +    return [self base64StringWithNewlines: YES];
    1.33 +}
    1.34 +
    1.35 +/**
    1.36 + * Encodes the current data in base64, and creates and returns an NSString from the result.
    1.37 + * This is the same as piping data through "... | openssl enc -base64" on the command line.
    1.38 + *
    1.39 + * Code courtesy of DaveDribin (http://www.dribin.org/dave/)
    1.40 + * Taken from http://www.cocoadev.com/index.pl?BaseSixtyFour
    1.41 + **/
    1.42 +- (NSString *)base64StringWithNewlines:(BOOL)encodeWithNewlines
    1.43 +{
    1.44 +    // Create a memory buffer which will contain the Base64 encoded string
    1.45 +    BIO * mem = BIO_new(BIO_s_mem());
    1.46 +    
    1.47 +    // Push on a Base64 filter so that writing to the buffer encodes the data
    1.48 +    BIO * b64 = BIO_new(BIO_f_base64());
    1.49 +    if (!encodeWithNewlines)
    1.50 +        BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    1.51 +    mem = BIO_push(b64, mem);
    1.52 +    
    1.53 +    // Encode all the data
    1.54 +    BIO_write(mem, [self bytes], [self length]);
    1.55 +    BIO_flush(mem);
    1.56 +    
    1.57 +    // Create a new string from the data in the memory buffer
    1.58 +    char * base64Pointer;
    1.59 +    long base64Length = BIO_get_mem_data(mem, &base64Pointer);
    1.60 +    NSString * base64String = [NSString stringWithCString:base64Pointer length:base64Length];
    1.61 +    
    1.62 +    // Clean up and go home
    1.63 +    BIO_free_all(mem);
    1.64 +    return base64String;
    1.65 +}
    1.66 +
    1.67 +- (NSData *)decodeBase64
    1.68 +{
    1.69 +    return [self decodeBase64WithNewLines:YES];
    1.70 +}
    1.71 +
    1.72 +- (NSData *)decodeBase64WithNewLines:(BOOL)encodedWithNewlines
    1.73 +{
    1.74 +    // Create a memory buffer containing Base64 encoded string data
    1.75 +    BIO * mem = BIO_new_mem_buf((void *) [self bytes], [self length]);
    1.76 +    
    1.77 +    // Push a Base64 filter so that reading from the buffer decodes it
    1.78 +    BIO * b64 = BIO_new(BIO_f_base64());
    1.79 +    if (!encodedWithNewlines)
    1.80 +        BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    1.81 +    mem = BIO_push(b64, mem);
    1.82 +    
    1.83 +    // Decode into an NSMutableData
    1.84 +    NSMutableData * data = [NSMutableData data];
    1.85 +    char inbuf[512];
    1.86 +    int inlen;
    1.87 +    while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0)
    1.88 +        [data appendBytes: inbuf length: inlen];
    1.89 +    
    1.90 +    // Clean up and go home
    1.91 +    BIO_free_all(mem);
    1.92 +    return data;
    1.93 +}
    1.94 +
    1.95 +
    1.96 +- (NSString *)hexString
    1.97 +{
    1.98 +    const UInt8 *bytes = self.bytes;
    1.99 +    NSUInteger length = self.length;
   1.100 +    char out[2*length+1];
   1.101 +    char *dst = &out[0];
   1.102 +    for( int i=0; i<length; i+=1 )
   1.103 +        dst += sprintf(dst,"%02X",*(bytes++));
   1.104 +    return [[[NSString alloc] initWithBytes: out length: 2*length encoding: NSASCIIStringEncoding]
   1.105 +            autorelease];
   1.106 +}
   1.107 +
   1.108 +- (NSString *)hexDump
   1.109 +{
   1.110 +    NSMutableString *ret=[NSMutableString stringWithCapacity:[self length]*2];
   1.111 +    /* dumps size bytes of *data to string. Looks like:
   1.112 +     * [0000] 75 6E 6B 6E 6F 77 6E 20
   1.113 +     *                  30 FF 00 00 00 00 39 00 unknown 0.....9.
   1.114 +     * (in a single line of course)
   1.115 +     */
   1.116 +    unsigned int size= [self length];
   1.117 +    const unsigned char *p = [self bytes];
   1.118 +    unsigned char c;
   1.119 +    int n;
   1.120 +    char bytestr[4] = {0};
   1.121 +    char addrstr[10] = {0};
   1.122 +    char hexstr[ 16*3 + 5] = {0};
   1.123 +    char charstr[16*1 + 5] = {0};
   1.124 +    for(n=1;n<=size;n++) {
   1.125 +        if (n%16 == 1) {
   1.126 +            /* store address for this line */
   1.127 +            snprintf(addrstr, sizeof(addrstr), "%.4x",
   1.128 +                     ((unsigned int)p-(unsigned int)self) );
   1.129 +        }
   1.130 +        
   1.131 +        c = *p;
   1.132 +        if (isalnum(c) == 0) {
   1.133 +            c = '.';
   1.134 +        }
   1.135 +        
   1.136 +        /* store hex str (for left side) */
   1.137 +        snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
   1.138 +        strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
   1.139 +        
   1.140 +        /* store char str (for right side) */
   1.141 +        snprintf(bytestr, sizeof(bytestr), "%c", c);
   1.142 +        strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
   1.143 +        
   1.144 +        if(n%16 == 0) {
   1.145 +            /* line completed */
   1.146 +            //printf("[%4.4s]   %-50.50s  %s\n", addrstr, hexstr, charstr);
   1.147 +            [ret appendString:[NSString stringWithFormat:@"[%4.4s]   %-50.50s  %s\n",
   1.148 +                               addrstr, hexstr, charstr]];
   1.149 +            hexstr[0] = 0;
   1.150 +            charstr[0] = 0;
   1.151 +        } else if(n%8 == 0) {
   1.152 +            /* half line: add whitespaces */
   1.153 +            strncat(hexstr, "  ", sizeof(hexstr)-strlen(hexstr)-1);
   1.154 +            strncat(charstr, " ", sizeof(charstr)-strlen(charstr)-1);
   1.155 +        }
   1.156 +        p++; /* next byte */
   1.157 +    }
   1.158 +    
   1.159 +    if (strlen(hexstr) > 0) {
   1.160 +        /* print rest of buffer if not empty */
   1.161 +        //printf("[%4.4s]   %-50.50s  %s\n", addrstr, hexstr, charstr);
   1.162 +        [ret appendString:[NSString stringWithFormat:@"[%4.4s]   %-50.50s  %s\n",
   1.163 +                           addrstr, hexstr, charstr]];
   1.164 +    }
   1.165 +    return ret;
   1.166 +}
   1.167 +
   1.168 +@end