Skip to content

Commit

Permalink
Added Base64 functions modelled after Dave Dribin at http://www.dribi…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gleb Dolgich committed Mar 5, 2009
1 parent 1e19e2f commit 52dec5b
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions objc/CFobLicVerifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Licensed under CC Attribution License <http://creativecommons.org/licenses/by/3.0/>
//

#import "NSString-Base64Extensions.h"
#import "NSString+PECrypt.h"
#import "CFobLicVerifier.h"
#import "decoder.h"
Expand Down
5 changes: 5 additions & 0 deletions objc/NSString+PECrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
// Copyright (C) 2009 PixelEspresso. All rights reserved.
// Licensed under CC Attribution License <http://creativecommons.org/licenses/by/3.0/>
//
// Base64 functions based on Dave Dribin's code:
// http://www.dribin.org/dave/blog/archives/2006/03/12/base64_cocoa/
//

#import <Foundation/Foundation.h>

@interface NSString (PXCrypt)
- (NSData *)sha1;
- (NSString *)base64DecodeWithBreaks:(BOOL)lineBreaks;
- (NSString *)base64Decode;
@end
37 changes: 37 additions & 0 deletions objc/NSString+PECrypt.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#import "NSString+PECrypt.h"
#import <openssl/sha.h>
#import <openssl/bio.h>
#import <openssl/evp.h>

@implementation NSString (PXCrypt)

Expand All @@ -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
29 changes: 29 additions & 0 deletions objc/NSString-Base64Extensions.h
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>


@interface NSString (Base64)

- (NSData *) decodeBase64;
- (NSData *) decodeBase64WithNewlines: (BOOL) encodedWithNewlines;

@end
62 changes: 62 additions & 0 deletions objc/NSString-Base64Extensions.m
Original file line number Diff line number Diff line change
@@ -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 <openssl/bio.h>
#include <openssl/evp.h>


@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
14 changes: 14 additions & 0 deletions objc/cocoafob.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -38,6 +39,8 @@
32A70AAB03705E1F00C91783 /* cocoafob_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cocoafob_Prefix.pch; sourceTree = "<group>"; };
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 = "<group>"; };
C7A731670F5F42D500D40AFE /* NSString-Base64Extensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString-Base64Extensions.h"; sourceTree = "<group>"; };
C7A731680F5F42D500D40AFE /* NSString-Base64Extensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString-Base64Extensions.m"; sourceTree = "<group>"; };
C7E378410F59D737002061CD /* decoder.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = decoder.c; sourceTree = "<group>"; };
C7E378420F59D737002061CD /* decoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = decoder.h; sourceTree = "<group>"; };
C7E378430F59D737002061CD /* encoder.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = encoder.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -80,6 +83,7 @@
08FB7795FE84155DC02AAC07 /* Source */ = {
isa = PBXGroup;
children = (
C7A731660F5F42C000D40AFE /* Dave Dribin */,
C7E378540F59DB33002061CD /* Base32 */,
C7E378530F59DB1E002061CD /* PECategories */,
C7E378470F59DB15002061CD /* CFobLicGenerator.h */,
Expand Down Expand Up @@ -117,6 +121,15 @@
name = Documentation;
sourceTree = "<group>";
};
C7A731660F5F42C000D40AFE /* Dave Dribin */ = {
isa = PBXGroup;
children = (
C7A731670F5F42D500D40AFE /* NSString-Base64Extensions.h */,
C7A731680F5F42D500D40AFE /* NSString-Base64Extensions.m */,
);
name = "Dave Dribin";
sourceTree = "<group>";
};
C7E378530F59DB1E002061CD /* PECategories */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit 52dec5b

Please sign in to comment.