Skip to content

Commit a7161a7

Browse files
committed
updated Base64 module.
1 parent d49fa1c commit a7161a7

File tree

4 files changed

+23
-91
lines changed

4 files changed

+23
-91
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
path = xctool
33
url = https://github.com/facebook/xctool.git
44
ignore = dirty
5+
[submodule "submodules/Base64"]
6+
path = submodules/Base64
7+
url = https://github.com/nicklockwood/Base64.git

CocoaSecurity.xcodeproj/project.pbxproj

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
21540A04183DD7F000E4C15B /* CocoaSecurityEncoder_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 21540A00183DD7F000E4C15B /* CocoaSecurityEncoder_Tests.m */; };
1919
21540A05183DD7F000E4C15B /* CocoaSecurityResult_Test.m in Sources */ = {isa = PBXBuildFile; fileRef = 21540A01183DD7F000E4C15B /* CocoaSecurityResult_Test.m */; };
2020
21540A06183DD81A00E4C15B /* CocoaSecurity.m in Sources */ = {isa = PBXBuildFile; fileRef = 2113924116EAD6AF00AFDF87 /* CocoaSecurity.m */; };
21+
21FB346C189601A6002F38DE /* Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = 21FB346B189601A6002F38DE /* Base64.m */; };
22+
21FB346D18960347002F38DE /* Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = 21FB346B189601A6002F38DE /* Base64.m */; };
2123
/* End PBXBuildFile section */
2224

2325
/* Begin PBXContainerItemProxy section */
@@ -59,6 +61,8 @@
5961
21540A00183DD7F000E4C15B /* CocoaSecurityEncoder_Tests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CocoaSecurityEncoder_Tests.m; sourceTree = "<group>"; };
6062
21540A01183DD7F000E4C15B /* CocoaSecurityResult_Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CocoaSecurityResult_Test.m; sourceTree = "<group>"; };
6163
21BB8A2E17FEA0EC0032C38C /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
64+
21FB346A189601A6002F38DE /* Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Base64.h; path = submodules/Base64/Base64/Base64.h; sourceTree = SOURCE_ROOT; };
65+
21FB346B189601A6002F38DE /* Base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Base64.m; path = submodules/Base64/Base64/Base64.m; sourceTree = SOURCE_ROOT; };
6266
/* End PBXFileReference section */
6367

6468
/* Begin PBXFrameworksBuildPhase section */
@@ -115,6 +119,7 @@
115119
2113923C16EAD6AF00AFDF87 /* CocoaSecurity */ = {
116120
isa = PBXGroup;
117121
children = (
122+
21FB34691896019A002F38DE /* Submodules */,
118123
2113923F16EAD6AF00AFDF87 /* CocoaSecurity.h */,
119124
2113924116EAD6AF00AFDF87 /* CocoaSecurity.m */,
120125
);
@@ -143,6 +148,15 @@
143148
name = "Supporting Files";
144149
sourceTree = "<group>";
145150
};
151+
21FB34691896019A002F38DE /* Submodules */ = {
152+
isa = PBXGroup;
153+
children = (
154+
21FB346A189601A6002F38DE /* Base64.h */,
155+
21FB346B189601A6002F38DE /* Base64.m */,
156+
);
157+
name = Submodules;
158+
sourceTree = "<group>";
159+
};
146160
/* End PBXGroup section */
147161

148162
/* Begin PBXNativeTarget section */
@@ -228,6 +242,7 @@
228242
isa = PBXSourcesBuildPhase;
229243
buildActionMask = 2147483647;
230244
files = (
245+
21FB346C189601A6002F38DE /* Base64.m in Sources */,
231246
2113924216EAD6AF00AFDF87 /* CocoaSecurity.m in Sources */,
232247
);
233248
runOnlyForDeploymentPostprocessing = 0;
@@ -236,6 +251,7 @@
236251
isa = PBXSourcesBuildPhase;
237252
buildActionMask = 2147483647;
238253
files = (
254+
21FB346D18960347002F38DE /* Base64.m in Sources */,
239255
21540A06183DD81A00E4C15B /* CocoaSecurity.m in Sources */,
240256
21540A05183DD7F000E4C15B /* CocoaSecurityResult_Test.m in Sources */,
241257
21540A03183DD7F000E4C15B /* CocoaSecurityDecoder_Tests.m in Sources */,

CocoaSecurity/CocoaSecurity.m

+3-91
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "CocoaSecurity.h"
1010
#import <CommonCrypto/CommonHMAC.h>
1111
#import <CommonCrypto/CommonCryptor.h>
12+
#import "Base64.h"
1213

1314
#pragma mark - CocoaSecurity
1415
@implementation CocoaSecurity
@@ -419,51 +420,7 @@ @implementation CocoaSecurityEncoder
419420
// convert NSData to Base64
420421
- (NSString *)base64:(NSData *)data
421422
{
422-
static const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
423-
424-
long long inputLength = [data length];
425-
const unsigned char *inputBytes = [data bytes];
426-
427-
long long maxOutputLength = (inputLength / 3 + 1) * 4;
428-
unsigned char *outputBytes = (unsigned char *)malloc((unsigned long)maxOutputLength);
429-
430-
long long index;
431-
long long outputLength = 0;
432-
for (index = 0; index < inputLength - 2; index += 3)
433-
{
434-
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2];
435-
outputBytes[outputLength++] = lookup[((inputBytes[index] & 0x03) << 4) | ((inputBytes[index + 1] & 0xF0) >> 4)];
436-
outputBytes[outputLength++] = lookup[((inputBytes[index + 1] & 0x0F) << 2) | ((inputBytes[index + 2] & 0xC0) >> 6)];
437-
outputBytes[outputLength++] = lookup[inputBytes[index + 2] & 0x3F];
438-
}
439-
440-
//handle left-over data
441-
if (index == inputLength - 2)
442-
{
443-
// = terminator
444-
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2];
445-
outputBytes[outputLength++] = lookup[((inputBytes[index] & 0x03) << 4) | ((inputBytes[index + 1] & 0xF0) >> 4)];
446-
outputBytes[outputLength++] = lookup[(inputBytes[index + 1] & 0x0F) << 2];
447-
outputBytes[outputLength++] = '=';
448-
}
449-
else if (index == inputLength - 1)
450-
{
451-
// == terminator
452-
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2];
453-
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0x03) << 4];
454-
outputBytes[outputLength++] = '=';
455-
outputBytes[outputLength++] = '=';
456-
}
457-
458-
NSString *result;
459-
if (outputLength >= 4)
460-
{
461-
//truncate data to match actual output length
462-
outputBytes = realloc(outputBytes, (unsigned long)outputLength);
463-
result = [[NSString alloc] initWithBytes:outputBytes length:(unsigned long)outputLength encoding:NSASCIIStringEncoding];
464-
}
465-
free(outputBytes);
466-
return result;
423+
return [data base64EncodedString];
467424
}
468425

469426
// convert NSData to hex string
@@ -510,52 +467,7 @@ - (NSString *)hex:(NSData *)data useLower:(BOOL)isOutputLower
510467
@implementation CocoaSecurityDecoder
511468
- (NSData *)base64:(NSString *)string
512469
{
513-
static const char lookup[] =
514-
{
515-
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
516-
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
517-
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 62, 99, 99, 99, 63,
518-
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 99, 99, 99, 99, 99, 99,
519-
99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
520-
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 99, 99, 99, 99, 99,
521-
99, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
522-
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 99, 99, 99, 99, 99
523-
};
524-
525-
NSData *inputData = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
526-
NSUInteger inputLength = [inputData length];
527-
const unsigned char *inputBytes = [inputData bytes];
528-
529-
NSMutableData *outputData = [NSMutableData dataWithLength:(inputLength / 4 + 1) * 3];
530-
unsigned char *outputBytes = (unsigned char *)[outputData mutableBytes];
531-
532-
int accumulator = 0;
533-
long long outputLength = 0;
534-
unsigned char accumulated[] = {0, 0, 0, 0};
535-
for (NSUInteger index = 0; index < inputLength; index++)
536-
{
537-
unsigned char decoded = lookup[inputBytes[index] & 0x7F];
538-
if (decoded != 99)
539-
{
540-
accumulated[accumulator] = decoded;
541-
if (accumulator == 3)
542-
{
543-
outputBytes[outputLength++] = (accumulated[0] << 2) | (accumulated[1] >> 4);
544-
outputBytes[outputLength++] = (accumulated[1] << 4) | (accumulated[2] >> 2);
545-
outputBytes[outputLength++] = (accumulated[2] << 6) | accumulated[3];
546-
}
547-
accumulator = (accumulator + 1) % 4;
548-
}
549-
}
550-
551-
//handle left-over data
552-
if (accumulator > 0) outputBytes[outputLength] = (accumulated[0] << 2) | (accumulated[1] >> 4);
553-
if (accumulator > 1) outputBytes[++outputLength] = (accumulated[1] << 4) | (accumulated[2] >> 2);
554-
if (accumulator > 2) outputLength++;
555-
556-
//truncate data to match actual output length
557-
outputData.length = outputLength;
558-
return outputLength? outputData: nil;
470+
return [NSData dataWithBase64EncodedString:string];
559471
}
560472
- (NSData *)hex:(NSString *)data
561473
{

submodules/Base64

Submodule Base64 added at 613d17c

0 commit comments

Comments
 (0)