diff options
-rw-r--r-- | Source/SPCategoryAdditions.h | 1 | ||||
-rw-r--r-- | Source/SPDataAdditions.h | 1 | ||||
-rw-r--r-- | Source/SPDataAdditions.m | 73 | ||||
-rw-r--r-- | Source/SPDataBase64EncodingAdditions.h | 30 | ||||
-rw-r--r-- | Source/SPDataBase64EncodingAdditions.m | 108 | ||||
-rw-r--r-- | sequel-pro.xcodeproj/project.pbxproj | 6 |
6 files changed, 145 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 diff --git a/sequel-pro.xcodeproj/project.pbxproj b/sequel-pro.xcodeproj/project.pbxproj index 09038bf0..e5f1a578 100644 --- a/sequel-pro.xcodeproj/project.pbxproj +++ b/sequel-pro.xcodeproj/project.pbxproj @@ -216,6 +216,7 @@ 584D88AA1515034200F24774 /* NSNotificationCenterThreadingAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 584D88A81515034200F24774 /* NSNotificationCenterThreadingAdditions.m */; }; 584D88AB1515034200F24774 /* NSNotificationCenterThreadingAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 584D88A71515034200F24774 /* NSNotificationCenterThreadingAdditions.h */; }; 584D88AC1515034200F24774 /* NSNotificationCenterThreadingAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 584D88A81515034200F24774 /* NSNotificationCenterThreadingAdditions.m */; }; + 584D899D15162CBE00F24774 /* SPDataBase64EncodingAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 584D899C15162CBE00F24774 /* SPDataBase64EncodingAdditions.m */; }; 584F5F8F0F50ACD800036517 /* table-view-small.tiff in Resources */ = {isa = PBXBuildFile; fileRef = 584F5F8E0F50ACD800036517 /* table-view-small.tiff */; }; 586AAB1514FAD3AF007F82BF /* QueryKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 17E5955314F304000054EE08 /* QueryKit.framework */; }; 586AAB9314FAD40D007F82BF /* QueryKit.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = 17E5955314F304000054EE08 /* QueryKit.framework */; }; @@ -924,6 +925,8 @@ 584D87BE15141A4A00F24774 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = usr/lib/libz.dylib; sourceTree = SDKROOT; }; 584D88A71515034200F24774 /* NSNotificationCenterThreadingAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSNotificationCenterThreadingAdditions.h; sourceTree = "<group>"; }; 584D88A81515034200F24774 /* NSNotificationCenterThreadingAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSNotificationCenterThreadingAdditions.m; sourceTree = "<group>"; }; + 584D899B15162CBE00F24774 /* SPDataBase64EncodingAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPDataBase64EncodingAdditions.h; sourceTree = "<group>"; }; + 584D899C15162CBE00F24774 /* SPDataBase64EncodingAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPDataBase64EncodingAdditions.m; sourceTree = "<group>"; }; 584F5F8E0F50ACD800036517 /* table-view-small.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = "table-view-small.tiff"; sourceTree = "<group>"; }; 586EBD2311418D7C00B3DE45 /* FeedbackReporter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FeedbackReporter.framework; path = Frameworks/FeedbackReporter.framework; sourceTree = "<group>"; }; 586F432A0FD74CFC00B428D7 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/SSHQuestionDialog.xib; sourceTree = "<group>"; }; @@ -2496,6 +2499,8 @@ B57747D80F7A8990003B34F9 /* SPWindowAdditions.m */, BC2C16D20FEBEDF10003993B /* SPDataAdditions.h */, BC2C16D30FEBEDF10003993B /* SPDataAdditions.m */, + 584D899B15162CBE00F24774 /* SPDataBase64EncodingAdditions.h */, + 584D899C15162CBE00F24774 /* SPDataBase64EncodingAdditions.m */, 582A01E7107C0C170027D42B /* SPNotLoaded.h */, 582A01E8107C0C170027D42B /* SPNotLoaded.m */, 5870868210FA3E9C00D58E1C /* SPDataStorage.h */, @@ -3197,6 +3202,7 @@ 584D878B15140FEB00F24774 /* SPObjectAdditions.m in Sources */, 584D87921514101E00F24774 /* SPDatabaseStructure.m in Sources */, 584D88A91515034200F24774 /* NSNotificationCenterThreadingAdditions.m in Sources */, + 584D899D15162CBE00F24774 /* SPDataBase64EncodingAdditions.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; |