From 52dec5b1680d7b69b182620a988ea4023f3e5b3d Mon Sep 17 00:00:00 2001 From: Gleb Dolgich Date: Thu, 5 Mar 2009 20:55:06 +0000 Subject: [PATCH] Added Base64 functions modelled after Dave Dribin at http://www.dribin.org/dave/blog/archives/2006/03/12/base64_cocoa/ --- objc/CFobLicVerifier.m | 1 + objc/NSString+PECrypt.h | 5 ++ objc/NSString+PECrypt.m | 37 +++++++++++++++ objc/NSString-Base64Extensions.h | 29 ++++++++++++ objc/NSString-Base64Extensions.m | 62 +++++++++++++++++++++++++ objc/cocoafob.xcodeproj/project.pbxproj | 14 ++++++ 6 files changed, 148 insertions(+) create mode 100644 objc/NSString-Base64Extensions.h create mode 100644 objc/NSString-Base64Extensions.m diff --git a/objc/CFobLicVerifier.m b/objc/CFobLicVerifier.m index 07b5ba1..79ce4bc 100644 --- a/objc/CFobLicVerifier.m +++ b/objc/CFobLicVerifier.m @@ -8,6 +8,7 @@ // Licensed under CC Attribution License // +#import "NSString-Base64Extensions.h" #import "NSString+PECrypt.h" #import "CFobLicVerifier.h" #import "decoder.h" diff --git a/objc/NSString+PECrypt.h b/objc/NSString+PECrypt.h index d80e011..ede59ef 100644 --- a/objc/NSString+PECrypt.h +++ b/objc/NSString+PECrypt.h @@ -7,9 +7,14 @@ // Copyright (C) 2009 PixelEspresso. All rights reserved. // Licensed under CC Attribution License // +// Base64 functions based on Dave Dribin's code: +// http://www.dribin.org/dave/blog/archives/2006/03/12/base64_cocoa/ +// #import @interface NSString (PXCrypt) - (NSData *)sha1; +- (NSString *)base64DecodeWithBreaks:(BOOL)lineBreaks; +- (NSString *)base64Decode; @end diff --git a/objc/NSString+PECrypt.m b/objc/NSString+PECrypt.m index a192e73..f1256f2 100644 --- a/objc/NSString+PECrypt.m +++ b/objc/NSString+PECrypt.m @@ -10,6 +10,8 @@ #import "NSString+PECrypt.h" #import +#import +#import @implementation NSString (PXCrypt) @@ -28,4 +30,39 @@ - (NSData *)sha1 { return digest; } +// Based on Dave Dribin's code, http://www.dribin.org/dave/blog/archives/2006/03/12/base64_cocoa/ +- (NSString *)base64DecodeWithBreaks:(BOOL)lineBreaks { + // Create a memory buffer containing Base64-encoded string data. + const char *utf8 = [self UTF8String]; + if (!utf8) + return nil; + // Create an OpenSSL BIO buffer using UTF8 representation of the string. + BIO *mem = BIO_new_mem_buf((void *)utf8, strlen(utf8)); + // Push a Base64 filter so that reading from the buffer decodes it. + BIO *b64 = BIO_new(BIO_f_base64()); + if (!lineBreaks) + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + mem = BIO_push(b64, mem); + + // Decode into an NSMutableData + NSMutableData *data = [NSMutableData data]; + const int DECODE_BUF_SIZE = 512; + char inbuf[DECODE_BUF_SIZE]; + int inlen; + while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0) + [data appendBytes: inbuf length: inlen]; + unsigned char zeroByte[1] = {0}; + [data appendBytes:zeroByte length:1]; // zero-terminate the string + // Clean up. + BIO_free_all(mem); + // Use decoded data bytes to construct a new string. + NSString *decoded = [NSString stringWithUTF8String:[data bytes]]; + return decoded; +} + +- (NSString *)base64Decode { + return [self base64DecodeWithBreaks:NO]; +} + + @end diff --git a/objc/NSString-Base64Extensions.h b/objc/NSString-Base64Extensions.h new file mode 100644 index 0000000..3193854 --- /dev/null +++ b/objc/NSString-Base64Extensions.h @@ -0,0 +1,29 @@ +// Copyright (c) 2006 Dave Dribin (http://www.dribin.org/dave/) +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +#import + + +@interface NSString (Base64) + +- (NSData *) decodeBase64; +- (NSData *) decodeBase64WithNewlines: (BOOL) encodedWithNewlines; + +@end diff --git a/objc/NSString-Base64Extensions.m b/objc/NSString-Base64Extensions.m new file mode 100644 index 0000000..fa2fd0d --- /dev/null +++ b/objc/NSString-Base64Extensions.m @@ -0,0 +1,62 @@ +// Copyright (c) 2006 Dave Dribin (http://www.dribin.org/dave/) +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +// Modified by Gleb Dolgich @gbd +// PixelEspresso, http://www.pixelespressoapps.com/ + +#import "NSString-Base64Extensions.h" +#include +#include + + +@implementation NSString (Base64) + +- (NSData *) decodeBase64; +{ + return [self decodeBase64WithNewlines: YES]; +} + +- (NSData *) decodeBase64WithNewlines: (BOOL) encodedWithNewlines; +{ + // Create a memory buffer containing Base64 encoded string data + const char *utf8 = [self UTF8String]; + if (!utf8) + return nil; + BIO * mem = BIO_new_mem_buf((void *)utf8, strlen(utf8)); + + // Push a Base64 filter so that reading from the buffer decodes it + BIO * b64 = BIO_new(BIO_f_base64()); + if (!encodedWithNewlines) + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + mem = BIO_push(b64, mem); + + // Decode into an NSMutableData + NSMutableData * data = [NSMutableData data]; + char inbuf[512]; + int inlen; + while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0) + [data appendBytes: inbuf length: inlen]; + + // Clean up and go home + BIO_free_all(mem); + return data; +} + +@end diff --git a/objc/cocoafob.xcodeproj/project.pbxproj b/objc/cocoafob.xcodeproj/project.pbxproj index ffe431e..daa39b9 100644 --- a/objc/cocoafob.xcodeproj/project.pbxproj +++ b/objc/cocoafob.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 8DD76F9A0486AA7600D96B5E /* cocoafob.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB7796FE84155DC02AAC07 /* cocoafob.m */; settings = {ATTRIBUTES = (); }; }; 8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; }; 8DD76F9F0486AA7600D96B5E /* cocoafob.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6859EA3029092ED04C91782 /* cocoafob.1 */; }; + C7A731690F5F42D500D40AFE /* NSString-Base64Extensions.m in Sources */ = {isa = PBXBuildFile; fileRef = C7A731680F5F42D500D40AFE /* NSString-Base64Extensions.m */; }; C7E378450F59D737002061CD /* decoder.c in Sources */ = {isa = PBXBuildFile; fileRef = C7E378410F59D737002061CD /* decoder.c */; }; C7E378460F59D737002061CD /* encoder.c in Sources */ = {isa = PBXBuildFile; fileRef = C7E378430F59D737002061CD /* encoder.c */; }; C7E3784F0F59DB15002061CD /* CFobLicGenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = C7E378480F59DB15002061CD /* CFobLicGenerator.m */; }; @@ -38,6 +39,8 @@ 32A70AAB03705E1F00C91783 /* cocoafob_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cocoafob_Prefix.pch; sourceTree = ""; }; 8DD76FA10486AA7600D96B5E /* cocoafob */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = cocoafob; sourceTree = BUILT_PRODUCTS_DIR; }; C6859EA3029092ED04C91782 /* cocoafob.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = cocoafob.1; sourceTree = ""; }; + C7A731670F5F42D500D40AFE /* NSString-Base64Extensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString-Base64Extensions.h"; sourceTree = ""; }; + C7A731680F5F42D500D40AFE /* NSString-Base64Extensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString-Base64Extensions.m"; sourceTree = ""; }; C7E378410F59D737002061CD /* decoder.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = decoder.c; sourceTree = ""; }; C7E378420F59D737002061CD /* decoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = decoder.h; sourceTree = ""; }; C7E378430F59D737002061CD /* encoder.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = encoder.c; sourceTree = ""; }; @@ -80,6 +83,7 @@ 08FB7795FE84155DC02AAC07 /* Source */ = { isa = PBXGroup; children = ( + C7A731660F5F42C000D40AFE /* Dave Dribin */, C7E378540F59DB33002061CD /* Base32 */, C7E378530F59DB1E002061CD /* PECategories */, C7E378470F59DB15002061CD /* CFobLicGenerator.h */, @@ -117,6 +121,15 @@ name = Documentation; sourceTree = ""; }; + C7A731660F5F42C000D40AFE /* Dave Dribin */ = { + isa = PBXGroup; + children = ( + C7A731670F5F42D500D40AFE /* NSString-Base64Extensions.h */, + C7A731680F5F42D500D40AFE /* NSString-Base64Extensions.m */, + ); + name = "Dave Dribin"; + sourceTree = ""; + }; C7E378530F59DB1E002061CD /* PECategories */ = { isa = PBXGroup; children = ( @@ -189,6 +202,7 @@ C7E378500F59DB15002061CD /* CFobLicVerifier.m in Sources */, C7E378510F59DB15002061CD /* NSData+PECrypt.m in Sources */, C7E378520F59DB15002061CD /* NSString+PECrypt.m in Sources */, + C7A731690F5F42D500D40AFE /* NSString-Base64Extensions.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };