Wired Foundation  2.0
A foundation framework for the Wired implementation on Mac OS X
NSData-WIFoundation.m
00001 /* $Id$ */
00002 
00003 /*
00004  *  Copyright (c) 2003-2009 Axel Andersson
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *  1. Redistributions of source code must retain the above copyright
00011  *     notice, this list of conditions and the following disclaimer.
00012  *  2. Redistributions in binary form must reproduce the above copyright
00013  *     notice, this list of conditions and the following disclaimer in the
00014  *     documentation and/or other materials provided with the distribution.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
00020  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00024  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00025  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #import <WiredFoundation/NSData-WIFoundation.h>
00030 
00031 @implementation NSData(WIDataChecksum)
00032 
00033 + (NSData *)dataWithBase64EncodedString:(NSString *)string {
00034         NSMutableData                   *mutableData;
00035         NSData                                  *data;
00036         const unsigned char             *buffer;
00037         unsigned char                   ch, inbuffer[4], outbuffer[3];
00038         NSUInteger                              i, length, count, position, offset;
00039         BOOL                                    ignore, stop, end;
00040         
00041         data = [string dataUsingEncoding:NSASCIIStringEncoding];
00042         length = [data length];
00043         position = offset = 0;
00044         buffer = [data bytes];
00045         mutableData = [NSMutableData dataWithCapacity:length];
00046         
00047         while(position < length) {
00048                 ignore = end = NO;
00049                 ch = buffer[position++];
00050                 
00051                 if(ch >= 'A' && ch <= 'Z')
00052                         ch = ch - 'A';
00053                 else if(ch >= 'a' && ch <= 'z')
00054                         ch = ch - 'a' + 26;
00055                 else if(ch >= '0' && ch <= '9')
00056                         ch = ch - '0' + 52;
00057                 else if(ch == '+')
00058                         ch = 62;
00059                 else if(ch == '=')
00060                         end = YES;
00061                 else if(ch == '/')
00062                         ch = 63;
00063                 else
00064                         ignore = YES;
00065                 
00066                 if(!ignore) {
00067                         count = 3;
00068                         stop = NO;
00069                         
00070                         if(end) {
00071                                 if(offset == 0)
00072                                         break;
00073                                 else if(offset == 1 || offset == 2)
00074                                         count = 1;
00075                                 else
00076                                         count = 2;
00077                                 
00078                                 offset = 3;
00079                                 stop = YES;
00080                         }
00081                         
00082                         inbuffer[offset++] = ch;
00083                         
00084                         if(offset == 4) {
00085                                 outbuffer[0] =  (inbuffer[0]         << 2) | ((inbuffer[1] & 0x30) >> 4);
00086                                 outbuffer[1] = ((inbuffer[1] & 0x0F) << 4) | ((inbuffer[2] & 0x3C) >> 2);
00087                                 outbuffer[2] = ((inbuffer[2] & 0x03) << 6) |  (inbuffer[3] & 0x3F);
00088                                 
00089                                 for(i = 0; i < count; i++)
00090                                         [mutableData appendBytes:&outbuffer[i] length:1];
00091 
00092                                 offset = 0;
00093                         }
00094                         
00095                         if(stop)
00096                                 break;
00097                 }
00098         }
00099         
00100         return mutableData;
00101 }
00102 
00103 
00104 
00105 - (NSString *)base64EncodedString {
00106         NSMutableString                 *string;
00107         const unsigned char             *buffer;
00108         unsigned char                   inbuffer[3], outbuffer[4];
00109         NSUInteger                              i, count, length, position, remaining;
00110         static char                             table[] =
00111                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00112         
00113         length = [self length];
00114         buffer = [self bytes];
00115         position = 0;
00116         string = [NSMutableString stringWithCapacity:(NSUInteger) (length * (4.0f / 3.0f)) + 4];
00117         
00118         while(position < length) {
00119                 for(i = 0; i < 3; i++) {
00120                         if(position + i < length)
00121                                 inbuffer[i] = buffer[position + i];
00122                         else
00123                                 inbuffer[i] = '\0';
00124                 }
00125                 
00126                 outbuffer[0] =  (inbuffer[0] & 0xFC) >> 2;
00127                 outbuffer[1] = ((inbuffer[0] & 0x03) << 4) | ((inbuffer[1] & 0xF0) >> 4);
00128                 outbuffer[2] = ((inbuffer[1] & 0x0F) << 2) | ((inbuffer[2] & 0xC0) >> 6);
00129                 outbuffer[3] =   inbuffer[2] & 0x3F;
00130                 
00131                 remaining = length - position;
00132                 
00133                 if(remaining == 1)
00134                         count = 2;
00135                 else if(remaining == 2)
00136                         count = 3;
00137                 else
00138                         count = 4;
00139                 
00140                 for(i = 0; i < count; i++)
00141                         [string appendFormat:@"%c", table[outbuffer[i]]];
00142                 
00143                 for(i = count; i < 4; i++)
00144                         [string appendFormat:@"%c", '='];
00145                 
00146                 position += 3;
00147         }
00148         
00149         return string;
00150 }
00151 
00152 
00153 
00154 #pragma mark -
00155 
00156 - (NSString *)SHA1 {
00157         CC_SHA1_CTX                             c;
00158         static unsigned char    hex[] = "0123456789abcdef";
00159         unsigned char                   sha[CC_SHA1_DIGEST_LENGTH];
00160         char                                    text[CC_SHA1_DIGEST_LENGTH * 2 + 1];
00161         NSUInteger                              i;
00162 
00163         CC_SHA1_Init(&c);
00164         CC_SHA1_Update(&c, [self bytes], (CC_LONG)[self length]);
00165         CC_SHA1_Final(sha, &c);
00166 
00167         for(i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
00168                 text[i + i]                     = hex[sha[i] >> 4];
00169                 text[i + i + 1]         = hex[sha[i] & 0x0F];
00170         }
00171 
00172         text[i + i] = '\0';
00173 
00174         return [NSString stringWithUTF8String:text];
00175 }
00176 
00177 @end
 All Classes