aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorrowanbeentje <rowan@beent.je>2012-03-18 17:12:52 +0000
committerrowanbeentje <rowan@beent.je>2012-03-18 17:12:52 +0000
commitefd7982171996f7ba448fba269a728f6ff1afccc (patch)
treedb0573690c6ecedb7d697b56f3322f86f8ee8b4f /Source
parentf6de38fa5cc34461513670e52d5a9801ff8d8b32 (diff)
downloadsequelpro-efd7982171996f7ba448fba269a728f6ff1afccc.tar.gz
sequelpro-efd7982171996f7ba448fba269a728f6ff1afccc.tar.bz2
sequelpro-efd7982171996f7ba448fba269a728f6ff1afccc.zip
- Remove the Colloquy-derived, GPL -[NSData base64EncodingWithLineLength:] method, and replace with an equivalent method derived from QSUtilities/PHP, which is MIT/BSD-alike licensed. New method is also at least ten times faster, up to much faster for long strings.
Diffstat (limited to 'Source')
-rw-r--r--Source/SPCategoryAdditions.h1
-rw-r--r--Source/SPDataAdditions.h1
-rw-r--r--Source/SPDataAdditions.m73
-rw-r--r--Source/SPDataBase64EncodingAdditions.h30
-rw-r--r--Source/SPDataBase64EncodingAdditions.m108
5 files changed, 139 insertions, 74 deletions
diff --git a/Source/SPCategoryAdditions.h b/Source/SPCategoryAdditions.h
index b7581c44..eac323a2 100644
--- a/Source/SPCategoryAdditions.h
+++ b/Source/SPCategoryAdditions.h
@@ -35,6 +35,7 @@
#import "SPTextViewAdditions.h"
#import "SPWindowAdditions.h"
#import "SPDataAdditions.h"
+#import "SPDataBase64EncodingAdditions.h"
#import "SPMenuAdditions.h"
#import "SPNotLoaded.h"
#import "SPMainThreadTrampoline.h"
diff --git a/Source/SPDataAdditions.h b/Source/SPDataAdditions.h
index 4deb3f48..956e5b64 100644
--- a/Source/SPDataAdditions.h
+++ b/Source/SPDataAdditions.h
@@ -24,7 +24,6 @@
@interface NSData (SPDataAdditions)
-- (NSString *)base64EncodingWithLineLength:(NSUInteger)lineLength;
- (NSString *)dataToFormattedHexString;
- (NSString *)shortStringRepresentationUsingEncoding:(NSStringEncoding)encoding;
- (NSData *)dataEncryptedWithPassword:(NSString *)password;
diff --git a/Source/SPDataAdditions.m b/Source/SPDataAdditions.m
index 805692c6..0329395c 100644
--- a/Source/SPDataAdditions.m
+++ b/Source/SPDataAdditions.m
@@ -33,81 +33,8 @@
#include <openssl/aes.h>
#include <openssl/sha.h>
-static char base64encodingTable[64] = {
-'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
-'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
-'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
-'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' };
-
@implementation NSData (SPDataAdditions)
-/*
- * Derived from http://colloquy.info/project/browser/trunk/NSDataAdditions.m?rev=1576
- * Created by khammond on Mon Oct 29 2001.
- * Formatted by Timothy Hatcher on Sun Jul 4 2004.
- * Copyright (c) 2001 Kyle Hammond. All rights reserved.
- * Original development by Dave Winer.
- *
- * Convert self to a base64 encoded NSString
- */
-- (NSString *) base64EncodingWithLineLength:(NSUInteger)lineLength {
-
- const unsigned char *bytes = [self bytes];
- NSUInteger ixtext = 0;
- NSUInteger lentext = [self length];
- NSInteger ctremaining = 0;
- unsigned char inbuf[3], outbuf[4];
- NSUInteger i = 0;
- NSUInteger charsonline = 0, ctcopy = 0;
- NSUInteger ix = 0;
-
- NSMutableString *base64 = [NSMutableString stringWithCapacity:lentext];
-
- while(1) {
- ctremaining = lentext - ixtext;
- if( ctremaining <= 0 ) break;
-
- for( i = 0; i < 3; i++ ) {
- ix = ixtext + i;
- if( ix < lentext ) inbuf[i] = bytes[ix];
- else inbuf [i] = 0;
- }
-
- outbuf [0] = (inbuf [0] & 0xFC) >> 2;
- outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
- outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
- outbuf [3] = inbuf [2] & 0x3F;
- ctcopy = 4;
-
- switch( ctremaining ) {
- case 1:
- ctcopy = 2;
- break;
- case 2:
- ctcopy = 3;
- break;
- }
-
- for( i = 0; i < ctcopy; i++ )
- [base64 appendFormat:@"%c", base64encodingTable[outbuf[i]]];
-
- for( i = ctcopy; i < 4; i++ )
- [base64 appendFormat:@"%c",'='];
-
- ixtext += 3;
- charsonline += 4;
-
- if( lineLength > 0 ) {
- if (charsonline >= lineLength) {
- charsonline = 0;
- [base64 appendString:@"\n"];
- }
- }
- }
-
- return base64;
-}
-
- (NSData *)dataEncryptedWithPassword:(NSString *)password
{
// Create a random 128-bit initialization vector
diff --git a/Source/SPDataBase64EncodingAdditions.h b/Source/SPDataBase64EncodingAdditions.h
new file mode 100644
index 00000000..96474ee3
--- /dev/null
+++ b/Source/SPDataBase64EncodingAdditions.h
@@ -0,0 +1,30 @@
+//
+// $Id$
+//
+// SPDataBase64EncodingAdditions.m
+// sequel-pro
+//
+// Created by Rowan Beentje on March 18th, 2012
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// More info at <http://code.google.com/p/sequel-pro/>
+
+
+@interface NSData (SPDataBase64EncodingAdditions)
+
+- (NSString *)base64EncodingWithLineLength:(NSUInteger)lineLength;
+
+@end
diff --git a/Source/SPDataBase64EncodingAdditions.m b/Source/SPDataBase64EncodingAdditions.m
new file mode 100644
index 00000000..788184cf
--- /dev/null
+++ b/Source/SPDataBase64EncodingAdditions.m
@@ -0,0 +1,108 @@
+//
+// $Id$
+//
+// SPDataBase64EncodingAdditions.m
+// sequel-pro
+//
+// Created by Rowan Beentje on March 18th, 2012
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// More info at <http://code.google.com/p/sequel-pro/>
+
+
+#import "SPDataBase64EncodingAdditions.h"
+
+static const char _base64EncodingTable[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+@implementation NSData (SPDataBase64EncodingAdditions)
+
+/**
+ * Returns a base64-encoded representation of the NSData as an NSString.
+ * Takes an argument for the maximum output line length; supply 0 or NSNotFound
+ * to have the results on a single line.
+ *
+ * Derived from the MIT-licensed Quasidea Development QSUtilities implementation,
+ * available in its original form at https://github.com/mikeho/QSUtilities ;
+ * QSUtilities implementation author Mike Ho, Copyright (c) 2010 - 2011
+ * Quasidea Development, LLC .
+ *
+ * That implementation is itself an implementation ported from the PHP core;
+ * the PHP implementation is covered by the PHP license, a BSD-alike which
+ * is available at http://www.php.net/license/3_01.txt .
+ * PHP implementation author Jim Winstead <jimw@php.net>, Copyright (c) 1997-2012
+ * The PHP Group.
+ */
+- (NSString *)base64EncodingWithLineLength:(NSUInteger)lineLength
+{
+ const unsigned char *objRawData = [self bytes];
+ char *objPointer;
+ char *strResult;
+
+ // Line length details - record bool and tweak to account for 3-octet processing
+ BOOL hasMaxLineLength = (lineLength && lineLength != NSNotFound);
+ NSUInteger maxLineLengthChunks = hasMaxLineLength ? floorf(lineLength / 4) : 1;
+ if (!maxLineLengthChunks) maxLineLengthChunks++;
+
+ // Get the Raw Data length and ensure we actually have data
+ size_t intLength = [self length];
+ if (intLength == 0) return nil;
+
+ // Setup the String-based result placeholder and pointer within that placeholder
+ size_t encodedLength = ceilf((intLength + 2) / 3) * 4;
+ if (hasMaxLineLength) encodedLength += ceilf(encodedLength / (maxLineLengthChunks * 4)) - 1;
+ strResult = (char *)calloc(encodedLength, sizeof(char));
+ objPointer = strResult;
+
+ // Iterate through everything
+ NSUInteger octetsOnLine = 0;
+ while (intLength > 2) { // keep going until we have less than 24 bits
+ *objPointer++ = _base64EncodingTable[objRawData[0] >> 2];
+ *objPointer++ = _base64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)];
+ *objPointer++ = _base64EncodingTable[((objRawData[1] & 0x0f) << 2) + (objRawData[2] >> 6)];
+ *objPointer++ = _base64EncodingTable[objRawData[2] & 0x3f];
+
+ // we just handled 3 octets (24 bits) of data
+ objRawData += 3;
+ intLength -= 3;
+
+ if (hasMaxLineLength) {
+ octetsOnLine++;
+ if (octetsOnLine >= maxLineLengthChunks) {
+ *objPointer++ = '\n';
+ octetsOnLine = 0;
+ }
+ }
+ }
+
+ // now deal with the tail end of things
+ if (intLength != 0) {
+ *objPointer++ = _base64EncodingTable[objRawData[0] >> 2];
+ if (intLength > 1) {
+ *objPointer++ = _base64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)];
+ *objPointer++ = _base64EncodingTable[(objRawData[1] & 0x0f) << 2];
+ *objPointer++ = '=';
+ } else {
+ *objPointer++ = _base64EncodingTable[(objRawData[0] & 0x03) << 4];
+ *objPointer++ = '=';
+ *objPointer++ = '=';
+ }
+ }
+
+ NSString *strToReturn = [[NSString alloc] initWithBytesNoCopy:strResult length:objPointer - strResult encoding:NSASCIIStringEncoding freeWhenDone:YES];
+ return [strToReturn autorelease];
+}
+
+@end