adium 5408:4c8a9ac739cc: Fix Twitter account creation.

commits at adium.im commits at adium.im
Fri Mar 22 03:22:33 UTC 2013


details:	http://hg.adium.im/adium/rev/4c8a9ac739cc
revision:	5408:4c8a9ac739cc
branch:		adium-1.6
author:		Frank Dowsett <wixardy at adium.im>
date:		Thu Mar 21 21:38:33 2013 -0400

Fix Twitter account creation.
Subject: adium 5409:41c3eb54cdc6: Use Matt Gallagher's NSData+Base64 for 10.6 compat.

details:	http://hg.adium.im/adium/rev/41c3eb54cdc6
revision:	5409:41c3eb54cdc6
branch:		adium-1.6
author:		Frank Dowsett <wixardy at adium.im>
date:		Thu Mar 21 23:20:39 2013 -0400

Use Matt Gallagher's NSData+Base64 for 10.6 compat.
Subject: adium 5410:f7253bc48b5e: NSURLConnection blocks don't work on 10.6 either.

details:	http://hg.adium.im/adium/rev/f7253bc48b5e
revision:	5410:f7253bc48b5e
branch:		adium-1.6
author:		Frank Dowsett <wixardy at adium.im>
date:		Thu Mar 21 23:22:00 2013 -0400

NSURLConnection blocks don't work on 10.6 either.

diffs (668 lines):

diff -r 55fda25d96ca -r f7253bc48b5e Adium.xcodeproj/project.pbxproj
--- a/Adium.xcodeproj/project.pbxproj	Thu Mar 21 14:06:17 2013 +0100
+++ b/Adium.xcodeproj/project.pbxproj	Thu Mar 21 23:22:00 2013 -0400
@@ -1037,6 +1037,7 @@
 		5A3B4D7C16D878AC00903E40 /* STTwitterOAuth.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A3B4D7216D878AB00903E40 /* STTwitterOAuth.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
 		5A3B4D7E16D878AC00903E40 /* STHTTPRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A3B4D7816D878AC00903E40 /* STHTTPRequest.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
 		5A44595E169143130078AB0A /* AIPreferenceCVPrototypeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A445957169118C60078AB0A /* AIPreferenceCVPrototypeView.m */; };
+		5A4B77E916FBDDC700DF398C /* NSData+Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A4B77E716FBDDC600DF398C /* NSData+Base64.m */; };
 		5A4BD41D13F855B000A4D3F7 /* SearchTerms.plist in Resources */ = {isa = PBXBuildFile; fileRef = 5A4BD41B13F855B000A4D3F7 /* SearchTerms.plist */; };
 		5A4BD41E13F8568100A4D3F7 /* Preferences.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5ACF27331392C585004B6AEF /* Preferences.xib */; };
 		5A4BD43313F856E300A4D3F7 /* Preferences-AddressBookIntegration.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5AC7F26313B42952002D7265 /* Preferences-AddressBookIntegration.xib */; };
@@ -3895,6 +3896,8 @@
 		5A3B4D7816D878AC00903E40 /* STHTTPRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = STHTTPRequest.m; path = "Plugins/Twitter Plugin/STTwitter/Vendor/STHTTPRequest.m"; sourceTree = "<group>"; };
 		5A445956169118C60078AB0A /* AIPreferenceCVPrototypeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AIPreferenceCVPrototypeView.h; path = Source/AIPreferenceCVPrototypeView.h; sourceTree = "<group>"; };
 		5A445957169118C60078AB0A /* AIPreferenceCVPrototypeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AIPreferenceCVPrototypeView.m; path = Source/AIPreferenceCVPrototypeView.m; sourceTree = "<group>"; };
+		5A4B77E716FBDDC600DF398C /* NSData+Base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSData+Base64.m"; path = "Plugins/Twitter Plugin/STTwitter/NSData+Base64.m"; sourceTree = "<group>"; };
+		5A4B77E816FBDDC700DF398C /* NSData+Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSData+Base64.h"; path = "Plugins/Twitter Plugin/STTwitter/NSData+Base64.h"; sourceTree = "<group>"; };
 		5A4BD41C13F855B000A4D3F7 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = en; path = Resources/en.lproj/SearchTerms.plist; sourceTree = "<group>"; };
 		5A4BD47213F858EE00A4D3F7 /* Preferences-ContactList.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = "Preferences-ContactList.xib"; path = "Resources/Preferences-ContactList.xib"; sourceTree = "<group>"; };
 		5A4BD48813F859EF00A4D3F7 /* Preferences-WindowHandling.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = "Preferences-WindowHandling.xib"; path = "Resources/Preferences-WindowHandling.xib"; sourceTree = "<group>"; };
@@ -7324,6 +7327,8 @@
 		5A3B4D6A16D878AB00903E40 /* STTwitter */ = {
 			isa = PBXGroup;
 			children = (
+				5A4B77E716FBDDC600DF398C /* NSData+Base64.m */,
+				5A4B77E816FBDDC700DF398C /* NSData+Base64.h */,
 				5A0D236816F4C7BC005DF211 /* STTwitterAppOnly.m */,
 				5A0D236916F4C7BC005DF211 /* STTwitterAppOnly.h */,
 				5A3B4D6B16D878AB00903E40 /* NSString+STTwitter.h */,
@@ -10571,6 +10576,7 @@
 				5A3B4D7C16D878AC00903E40 /* STTwitterOAuth.m in Sources */,
 				5A3B4D7E16D878AC00903E40 /* STHTTPRequest.m in Sources */,
 				5A0D236A16F4C7BC005DF211 /* STTwitterAppOnly.m in Sources */,
+				5A4B77E916FBDDC700DF398C /* NSData+Base64.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
diff -r 55fda25d96ca -r f7253bc48b5e Plugins/Twitter Plugin/AITwitterAccount.m
--- a/Plugins/Twitter Plugin/AITwitterAccount.m	Thu Mar 21 14:06:17 2013 +0100
+++ b/Plugins/Twitter Plugin/AITwitterAccount.m	Thu Mar 21 23:22:00 2013 -0400
@@ -1041,23 +1041,25 @@
 		[listContact setValue:[NSNumber numberWithBool:YES] forProperty:TWITTER_PROPERTY_REQUESTED_USER_ICON notify:NotifyNever];
 		
 		// Grab the user icon and set it as their serverside icon.
-		NSString *imageURL = [url stringByReplacingOccurrencesOfString:@"_normal." withString:@"_bigger."];
-		NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]];
-		[NSURLConnection sendAsynchronousRequest:imageRequest
-										   queue:[NSOperationQueue currentQueue]
-							   completionHandler:^(NSURLResponse *resp, NSData *data, NSError *error) {
-								   NSImage *image = [[[NSImage alloc] initWithData:data] autorelease];
-								   
-								   if (image) {
-									   AILogWithSignature(@"%@ Updated user icon for %@", self, listContact);
-									   [listContact setServersideIconData:[image TIFFRepresentation]
-																   notify:NotifyLater];
-									   
-									   [listContact setValue:nil forProperty:TWITTER_PROPERTY_REQUESTED_USER_ICON notify:NotifyNever];
-								   } else {
-									   [self requestFailed:AITwitterUserIconPull withError:error userInfo:@{ @"ListContact" : listContact }];
-								   }
-							   }];
+		dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+			NSString *imageURL = [url stringByReplacingOccurrencesOfString:@"_normal." withString:@"_bigger."];
+			NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]];
+			NSError *error = nil;
+			NSData *data = [NSURLConnection sendSynchronousRequest:imageRequest returningResponse:nil error:&error];
+			NSImage *image = [[[NSImage alloc] initWithData:data] autorelease];
+			
+			if (image) {
+				dispatch_async(dispatch_get_main_queue(), ^{
+					AILogWithSignature(@"%@ Updated user icon for %@", self, listContact);
+					[listContact setServersideIconData:[image TIFFRepresentation]
+												notify:NotifyLater];
+					
+					[listContact setValue:nil forProperty:TWITTER_PROPERTY_REQUESTED_USER_ICON notify:NotifyNever];
+				});
+			} else {
+				[self requestFailed:AITwitterUserIconPull withError:error userInfo:@{ @"ListContact" : listContact }];
+			}
+		});
 	}
 }
 
@@ -2241,35 +2243,36 @@
 		[self setValue:[userInfo objectForKey:@"location"] forProperty:@"Profile Location" notify:NotifyLater];
 		[self setValue:[userInfo objectForKey:@"description"] forProperty:@"Profile Description" notify:NotifyLater];
 		
-		
-		NSString *imageURL = [userInfo objectForKey:TWITTER_INFO_ICON];
-		NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]];
-		[NSURLConnection sendAsynchronousRequest:imageRequest
-										   queue:[NSOperationQueue currentQueue]
-							   completionHandler:^(NSURLResponse *resp, NSData *data, NSError *error) {
-								   NSImage *image = [[[NSImage alloc] initWithData:data] autorelease];
-								   
-								   if (image) {
-									   AILogWithSignature(@"Updated self icon for %@", self);
-									   
-									   // Set a property so that we don't re-send the image we're just now downloading.
-									   [self setValue:[NSNumber numberWithBool:YES] forProperty:TWITTER_PROPERTY_REQUESTED_USER_ICON notify:NotifyNever];
-									   
-									   [self setPreference:[NSNumber numberWithBool:YES]
-													forKey:KEY_USE_USER_ICON
-													 group:GROUP_ACCOUNT_STATUS];
-									   
-									   [self setPreference:[image TIFFRepresentation]
-													forKey:KEY_USER_ICON
-													 group:GROUP_ACCOUNT_STATUS];
-									   
-									   [self notifyOfChangedPropertiesSilently:NO];
-									   
-									   [self setValue:nil forProperty:TWITTER_PROPERTY_REQUESTED_USER_ICON notify:NotifyNever];
-								   } else {
-									   [self requestFailed:AITwitterSelfUserIconPull withError:error userInfo:nil];
-								   }
-							   }];
+		dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+			NSString *imageURL = [userInfo objectForKey:TWITTER_INFO_ICON];
+			NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]];
+			NSError *error = nil;
+			NSData *data = [NSURLConnection sendSynchronousRequest:imageRequest returningResponse:nil error:&error];
+			NSImage *image = [[[NSImage alloc] initWithData:data] autorelease];
+			
+			if (image) {
+				dispatch_async(dispatch_get_main_queue(), ^{
+					AILogWithSignature(@"Updated self icon for %@", self);
+					
+					// Set a property so that we don't re-send the image we're just now downloading.
+					[self setValue:[NSNumber numberWithBool:YES] forProperty:TWITTER_PROPERTY_REQUESTED_USER_ICON notify:NotifyNever];
+					
+					[self setPreference:[NSNumber numberWithBool:YES]
+								 forKey:KEY_USE_USER_ICON
+								  group:GROUP_ACCOUNT_STATUS];
+					
+					[self setPreference:[image TIFFRepresentation]
+								 forKey:KEY_USER_ICON
+								  group:GROUP_ACCOUNT_STATUS];
+					
+					[self notifyOfChangedPropertiesSilently:NO];
+					
+					[self setValue:nil forProperty:TWITTER_PROPERTY_REQUESTED_USER_ICON notify:NotifyNever];
+				});
+			} else {
+				[self requestFailed:AITwitterSelfUserIconPull withError:error userInfo:nil];
+			}
+		});
 	}
 }
 
diff -r 55fda25d96ca -r f7253bc48b5e Plugins/Twitter Plugin/AITwitterAccountViewController.m
--- a/Plugins/Twitter Plugin/AITwitterAccountViewController.m	Thu Mar 21 14:06:17 2013 +0100
+++ b/Plugins/Twitter Plugin/AITwitterAccountViewController.m	Thu Mar 21 23:22:00 2013 -0400
@@ -131,9 +131,10 @@
 		};
 		
 		if ([textField_OAuthVerifier.stringValue isEqualToString:@""]) {
-			OAuthSetup = [STTwitterOAuth twitterServiceWithConsumerName:@"Adium"
-															consumerKey:[(AITwitterAccount *)account consumerKey]
-														 consumerSecret:[(AITwitterAccount *)account secretKey]];
+			[OAuthSetup release];
+			OAuthSetup = [[STTwitterOAuth twitterServiceWithConsumerName:@"Adium"
+															 consumerKey:[(AITwitterAccount *)account consumerKey]
+														  consumerSecret:[(AITwitterAccount *)account secretKey]] retain];
 			[OAuthSetup postTokenRequest:^(NSURL *url, NSString *oauthToken) {
 				// We have a request token, ask user to authorize.
 				[[NSWorkspace sharedWorkspace] openURL:url];
diff -r 55fda25d96ca -r f7253bc48b5e Plugins/Twitter Plugin/STTwitter/NSData+Base64.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Twitter Plugin/STTwitter/NSData+Base64.h	Thu Mar 21 23:22:00 2013 -0400
@@ -0,0 +1,42 @@
+//
+//  NSData+Base64.h
+//  base64
+//
+//  Created by Matt Gallagher on 2009/06/03.
+//  Copyright 2009 Matt Gallagher. All rights reserved.
+//
+//  This software is provided 'as-is', without any express or implied
+//  warranty. In no event will the authors be held liable for any damages
+//  arising from the use of this software. Permission is granted to anyone to
+//  use this software for any purpose, including commercial applications, and to
+//  alter it and redistribute it freely, subject to the following restrictions:
+//
+//  1. The origin of this software must not be misrepresented; you must not
+//     claim that you wrote the original software. If you use this software
+//     in a product, an acknowledgment in the product documentation would be
+//     appreciated but is not required.
+//  2. Altered source versions must be plainly marked as such, and must not be
+//     misrepresented as being the original software.
+//  3. This notice may not be removed or altered from any source
+//     distribution.
+//
+
+#import <Foundation/Foundation.h>
+
+void *NewBase64Decode(
+	const char *inputBuffer,
+	size_t length,
+	size_t *outputLength);
+
+char *NewBase64Encode(
+	const void *inputBuffer,
+	size_t length,
+	bool separateLines,
+	size_t *outputLength);
+
+ at interface NSData (Base64)
+
++ (NSData *)dataFromBase64String:(NSString *)aString;
+- (NSString *)base64EncodedString;
+
+ at end
diff -r 55fda25d96ca -r f7253bc48b5e Plugins/Twitter Plugin/STTwitter/NSData+Base64.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Twitter Plugin/STTwitter/NSData+Base64.m	Thu Mar 21 23:22:00 2013 -0400
@@ -0,0 +1,313 @@
+//
+//  NSData+Base64.m
+//  base64
+//
+//  Created by Matt Gallagher on 2009/06/03.
+//  Copyright 2009 Matt Gallagher. All rights reserved.
+//
+//  This software is provided 'as-is', without any express or implied
+//  warranty. In no event will the authors be held liable for any damages
+//  arising from the use of this software. Permission is granted to anyone to
+//  use this software for any purpose, including commercial applications, and to
+//  alter it and redistribute it freely, subject to the following restrictions:
+//
+//  1. The origin of this software must not be misrepresented; you must not
+//     claim that you wrote the original software. If you use this software
+//     in a product, an acknowledgment in the product documentation would be
+//     appreciated but is not required.
+//  2. Altered source versions must be plainly marked as such, and must not be
+//     misrepresented as being the original software.
+//  3. This notice may not be removed or altered from any source
+//     distribution.
+//
+
+#import "NSData+Base64.h"
+
+//
+// Mapping from 6 bit pattern to ASCII character.
+//
+static unsigned char base64EncodeLookup[65] =
+	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+//
+// Definition for "masked-out" areas of the base64DecodeLookup mapping
+//
+#define xx 65
+
+//
+// Mapping from ASCII character to 6 bit pattern.
+//
+static unsigned char base64DecodeLookup[256] =
+{
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, xx, 63, 
+    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx, 
+    xx,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 
+    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, xx, 
+    xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
+    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
+};
+
+//
+// Fundamental sizes of the binary and base64 encode/decode units in bytes
+//
+#define BINARY_UNIT_SIZE 3
+#define BASE64_UNIT_SIZE 4
+
+//
+// NewBase64Decode
+//
+// Decodes the base64 ASCII string in the inputBuffer to a newly malloced
+// output buffer.
+//
+//  inputBuffer - the source ASCII string for the decode
+//	length - the length of the string or -1 (to specify strlen should be used)
+//	outputLength - if not-NULL, on output will contain the decoded length
+//
+// returns the decoded buffer. Must be free'd by caller. Length is given by
+//	outputLength.
+//
+void *NewBase64Decode(
+	const char *inputBuffer,
+	size_t length,
+	size_t *outputLength)
+{
+	if (length == -1)
+	{
+		length = strlen(inputBuffer);
+	}
+	
+	size_t outputBufferSize =
+		((length+BASE64_UNIT_SIZE-1) / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE;
+	unsigned char *outputBuffer = (unsigned char *)malloc(outputBufferSize);
+	
+	size_t i = 0;
+	size_t j = 0;
+	while (i < length)
+	{
+		//
+		// Accumulate 4 valid characters (ignore everything else)
+		//
+		unsigned char accumulated[BASE64_UNIT_SIZE];
+		size_t accumulateIndex = 0;
+		while (i < length)
+		{
+			unsigned char decode = base64DecodeLookup[inputBuffer[i++]];
+			if (decode != xx)
+			{
+				accumulated[accumulateIndex] = decode;
+				accumulateIndex++;
+				
+				if (accumulateIndex == BASE64_UNIT_SIZE)
+				{
+					break;
+				}
+			}
+		}
+		
+		//
+		// Store the 6 bits from each of the 4 characters as 3 bytes
+		//
+		// (Uses improved bounds checking suggested by Alexandre Colucci)
+		//
+		if(accumulateIndex >= 2)  
+			outputBuffer[j] = (accumulated[0] << 2) | (accumulated[1] >> 4);  
+		if(accumulateIndex >= 3)  
+			outputBuffer[j + 1] = (accumulated[1] << 4) | (accumulated[2] >> 2);  
+		if(accumulateIndex >= 4)  
+			outputBuffer[j + 2] = (accumulated[2] << 6) | accumulated[3];
+		j += accumulateIndex - 1;
+	}
+	
+	if (outputLength)
+	{
+		*outputLength = j;
+	}
+	return outputBuffer;
+}
+
+//
+// NewBase64Encode
+//
+// Encodes the arbitrary data in the inputBuffer as base64 into a newly malloced
+// output buffer.
+//
+//  inputBuffer - the source data for the encode
+//	length - the length of the input in bytes
+//  separateLines - if zero, no CR/LF characters will be added. Otherwise
+//		a CR/LF pair will be added every 64 encoded chars.
+//	outputLength - if not-NULL, on output will contain the encoded length
+//		(not including terminating 0 char)
+//
+// returns the encoded buffer. Must be free'd by caller. Length is given by
+//	outputLength.
+//
+char *NewBase64Encode(
+	const void *buffer,
+	size_t length,
+	bool separateLines,
+	size_t *outputLength)
+{
+	const unsigned char *inputBuffer = (const unsigned char *)buffer;
+	
+	#define MAX_NUM_PADDING_CHARS 2
+	#define OUTPUT_LINE_LENGTH 64
+	#define INPUT_LINE_LENGTH ((OUTPUT_LINE_LENGTH / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE)
+	#define CR_LF_SIZE 2
+	
+	//
+	// Byte accurate calculation of final buffer size
+	//
+	size_t outputBufferSize =
+			((length / BINARY_UNIT_SIZE)
+				+ ((length % BINARY_UNIT_SIZE) ? 1 : 0))
+					* BASE64_UNIT_SIZE;
+	if (separateLines)
+	{
+		outputBufferSize +=
+			(outputBufferSize / OUTPUT_LINE_LENGTH) * CR_LF_SIZE;
+	}
+	
+	//
+	// Include space for a terminating zero
+	//
+	outputBufferSize += 1;
+
+	//
+	// Allocate the output buffer
+	//
+	char *outputBuffer = (char *)malloc(outputBufferSize);
+	if (!outputBuffer)
+	{
+		return NULL;
+	}
+
+	size_t i = 0;
+	size_t j = 0;
+	const size_t lineLength = separateLines ? INPUT_LINE_LENGTH : length;
+	size_t lineEnd = lineLength;
+	
+	while (true)
+	{
+		if (lineEnd > length)
+		{
+			lineEnd = length;
+		}
+
+		for (; i + BINARY_UNIT_SIZE - 1 < lineEnd; i += BINARY_UNIT_SIZE)
+		{
+			//
+			// Inner loop: turn 48 bytes into 64 base64 characters
+			//
+			outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
+			outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i] & 0x03) << 4)
+				| ((inputBuffer[i + 1] & 0xF0) >> 4)];
+			outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i + 1] & 0x0F) << 2)
+				| ((inputBuffer[i + 2] & 0xC0) >> 6)];
+			outputBuffer[j++] = base64EncodeLookup[inputBuffer[i + 2] & 0x3F];
+		}
+		
+		if (lineEnd == length)
+		{
+			break;
+		}
+		
+		//
+		// Add the newline
+		//
+		outputBuffer[j++] = '\r';
+		outputBuffer[j++] = '\n';
+		lineEnd += lineLength;
+	}
+	
+	if (i + 1 < length)
+	{
+		//
+		// Handle the single '=' case
+		//
+		outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
+		outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i] & 0x03) << 4)
+			| ((inputBuffer[i + 1] & 0xF0) >> 4)];
+		outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i + 1] & 0x0F) << 2];
+		outputBuffer[j++] =	'=';
+	}
+	else if (i < length)
+	{
+		//
+		// Handle the double '=' case
+		//
+		outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
+		outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0x03) << 4];
+		outputBuffer[j++] = '=';
+		outputBuffer[j++] = '=';
+	}
+	outputBuffer[j] = 0;
+	
+	//
+	// Set the output length and return the buffer
+	//
+	if (outputLength)
+	{
+		*outputLength = j;
+	}
+	return outputBuffer;
+}
+
+ at implementation NSData (Base64)
+
+//
+// dataFromBase64String:
+//
+// Creates an NSData object containing the base64 decoded representation of
+// the base64 string 'aString'
+//
+// Parameters:
+//    aString - the base64 string to decode
+//
+// returns the autoreleased NSData representation of the base64 string
+//
++ (NSData *)dataFromBase64String:(NSString *)aString
+{
+	NSData *data = [aString dataUsingEncoding:NSASCIIStringEncoding];
+	size_t outputLength;
+	void *outputBuffer = NewBase64Decode([data bytes], [data length], &outputLength);
+	NSData *result = [NSData dataWithBytes:outputBuffer length:outputLength];
+	free(outputBuffer);
+	return result;
+}
+
+//
+// base64EncodedString
+//
+// Creates an NSString object that contains the base 64 encoding of the
+// receiver's data. Lines are broken at 64 characters long.
+//
+// returns an autoreleased NSString being the base 64 representation of the
+//	receiver.
+//
+- (NSString *)base64EncodedString
+{
+	size_t outputLength;
+	char *outputBuffer =
+		NewBase64Encode([self bytes], [self length], true, &outputLength);
+	
+	NSString *result =
+		[[[NSString alloc]
+			initWithBytes:outputBuffer
+			length:outputLength
+			encoding:NSASCIIStringEncoding]
+		autorelease];
+	free(outputBuffer);
+	return result;
+}
+
+ at end
diff -r 55fda25d96ca -r f7253bc48b5e Plugins/Twitter Plugin/STTwitter/STTwitterAppOnly.m
--- a/Plugins/Twitter Plugin/STTwitter/STTwitterAppOnly.m	Thu Mar 21 14:06:17 2013 +0100
+++ b/Plugins/Twitter Plugin/STTwitter/STTwitterAppOnly.m	Thu Mar 21 23:22:00 2013 -0400
@@ -9,10 +9,7 @@
 #import "STTwitterAppOnly.h"
 #import "STHTTPRequest.h"
 #import "NSString+STTwitter.h"
-
- at interface NSData (Base64)
-- (NSString *)base64Encoding; // private API
- at end
+#import "NSData+Base64.h"
 
 @implementation STTwitterAppOnly
 
@@ -103,7 +100,7 @@
     NSString *encodedConsumerSecret = [consumerSecret st_stringByAddingRFC3986PercentEscapesUsingEncoding:NSUTF8StringEncoding];
     NSString *bearerTokenCredentials = [NSString stringWithFormat:@"%@:%@", encodedConsumerToken, encodedConsumerSecret];
     NSData *data = [bearerTokenCredentials dataUsingEncoding:NSUTF8StringEncoding];
-    return [data base64Encoding];
+    return [data base64EncodedString];
 }
 
 - (void)verifyCredentialsWithSuccessBlock:(void(^)(NSString *username))successBlock
diff -r 55fda25d96ca -r f7253bc48b5e Plugins/Twitter Plugin/STTwitter/STTwitterOAuth.m
--- a/Plugins/Twitter Plugin/STTwitter/STTwitterOAuth.m	Thu Mar 21 14:06:17 2013 +0100
+++ b/Plugins/Twitter Plugin/STTwitter/STTwitterOAuth.m	Thu Mar 21 23:22:00 2013 -0400
@@ -9,13 +9,11 @@
 #import "STTwitterOAuth.h"
 #import "STHTTPRequest.h"
 #import "NSString+STTwitter.h"
+#import "NSData+Base64.h"
+#import "JSONKit.h"
 
 #include <CommonCrypto/CommonHMAC.h>
 
- at interface NSData (Base64)
-- (NSString *)base64Encoding; // private API
- at end
-
 @interface STTwitterOAuth ()
 
 @property (nonatomic, retain) NSString *username;
@@ -438,7 +436,7 @@
     r.completionBlock = ^(NSDictionary *headers, NSString *body) {
         
         NSError *jsonError = nil;
-        id json = [NSJSONSerialization JSONObjectWithData:r.responseData options:NSJSONReadingMutableLeaves error:&jsonError];
+        id json = [r.responseData objectFromJSONDataWithParseOptions:JKParseOptionNone error:&jsonError];
         
         if(json == nil) {
             errorBlock(jsonError);
@@ -490,10 +488,10 @@
     r.completionBlock = ^(NSDictionary *headers, NSString *body) {
         
         NSError *jsonError = nil;
-        id json = [NSJSONSerialization JSONObjectWithData:r.responseData options:NSJSONReadingMutableLeaves error:&jsonError];
+		id json = [r.responseData objectFromJSONDataWithParseOptions:JKParseOptionNone error:&jsonError];
         
         if(json == nil) {
-            errorBlock(jsonError);
+            successBlock(body); // response is not necessarily json, eg. https://api.twitter.com/oauth/request_token
             return;
         }
         
@@ -600,7 +598,7 @@
     unsigned char buf[CC_SHA1_DIGEST_LENGTH];
     CCHmac(kCCHmacAlgSHA1, [key UTF8String], [key length], [self UTF8String], [self length], buf);
     NSData *data = [NSData dataWithBytes:buf length:CC_SHA1_DIGEST_LENGTH];
-    return [data base64Encoding];
+    return [data base64EncodedString];
 }
 
 - (NSDictionary *)parametersDictionary {
@@ -630,33 +628,3 @@
 
 @end
 
- at implementation NSData (STTwitterOAuth)
-
-- (NSString *)base64EncodedString {
-
-#if TARGET_OS_IPHONE
-    return [self base64Encoding]; // private API
-#else
-
-    CFDataRef retval = NULL;
-    SecTransformRef encodeTrans = SecEncodeTransformCreate(kSecBase64Encoding, NULL);
-    if (encodeTrans == NULL) return nil;
-    
-    if (SecTransformSetAttribute(encodeTrans, kSecTransformInputAttributeName, (CFDataRef)self, NULL)) {
-        retval = SecTransformExecute(encodeTrans, NULL);
-    }
-    CFRelease(encodeTrans);
-    
-    NSString *s = [[NSString alloc] initWithData:(NSData *)retval encoding:NSUTF8StringEncoding];
-    
-    if(retval) {
-        CFRelease(retval);
-    }
-    
-    return [s autorelease];
-    
-#endif
-
-}
- at end
-
diff -r 55fda25d96ca -r f7253bc48b5e Plugins/Twitter Plugin/STTwitter/Vendor/STHTTPRequest.m
--- a/Plugins/Twitter Plugin/STTwitter/Vendor/STHTTPRequest.m	Thu Mar 21 14:06:17 2013 +0100
+++ b/Plugins/Twitter Plugin/STTwitter/Vendor/STHTTPRequest.m	Thu Mar 21 23:22:00 2013 -0400
@@ -7,6 +7,7 @@
 //
 
 #import "STHTTPRequest.h"
+#import "NSData+Base64.h"
 
 #define DEBUG 0
 
@@ -30,10 +31,6 @@
 @property (nonatomic, retain) NSString *POSTFileParameter;
 @end
 
- at interface NSData (Base64)
-- (NSString *)base64Encoding; // private API
- at end
-
 @implementation STHTTPRequest
 
 @synthesize connection = _connection;
@@ -385,7 +382,7 @@
     if(credentialForHost) {
         NSString *authString = [NSString stringWithFormat:@"%@:%@", credentialForHost.user, credentialForHost.password];
         NSData *authData = [authString dataUsingEncoding:NSASCIIStringEncoding];
-        NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64Encoding]];
+        NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];
         [request addValue:authValue forHTTPHeaderField:@"Authorization"];
     }
     




More information about the commits mailing list