Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Additions/NSData+STAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// NSData+STAdditions.h
//
// Created by Florent Morin on 06/24/12.
// Copyright 2012 Kaeli Soft. All rights reserved.
//
// 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 NSData (STAdditions)

- (NSString *)base64EncodedStringWithLineLength:(NSInteger)length;
- (NSString *)base64EncodedString;
- (NSString *)hexadecimalString;

@end
123 changes: 123 additions & 0 deletions Additions/NSData+STAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// NSURL+STAdditions.m
//
// Created by Florent Morin on 06/24/12.
// Copyright 2012 Kaeli Soft. All rights reserved.
//
// 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 "STUtils.h"

@implementation NSData (STAdditions)

- (NSString *)base64EncodedString {
return [self base64EncodedStringWithLineLength:0];
}

- (NSString *)base64EncodedStringWithLineLength:(NSInteger)length
{
static char *encodingTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

const unsigned char *bytes = [self bytes];

NSMutableString *result = [[NSMutableString alloc] init];

NSInteger idxSource = 0;
NSInteger idx = 0;
NSInteger lengthSource = [self length];
NSInteger idxCharOnLine = 0;

while (1) {
NSInteger remaining = lengthSource - idx;
unsigned char inBuffer[3];
unsigned char outBuffer[4];
NSInteger toCopy = 4;

if (remaining <= 0) {
break;
}

for (NSInteger i = 0; i < 3; i++) {
idx = idxSource + i;
if (idx < lengthSource) {
inBuffer[i] = bytes[idx];
} else {
inBuffer[i] = '\0';
}
}

outBuffer[0] = (inBuffer[0] & 0xfc) >> 2;
outBuffer[1] = ((inBuffer[0] & 0x03) << 4) | ((inBuffer[1] & 0xf0) >> 4);
outBuffer[2] = ((inBuffer[1] & 0x0f) << 2) | ((inBuffer[2] & 0xc0) >> 6);
outBuffer[3] = inBuffer[2] & 0x3f;

toCopy = 4;

if (remaining < 3) {
toCopy = remaining + 1;
}

for (NSInteger i = 0;i < toCopy;i++) {
unsigned char c = outBuffer[i];
[result appendFormat:@"%c", encodingTable[c]];
}

for (NSInteger i = toCopy;i < 4;i++) {
[result appendFormat:@"%c", '='];
}

idxSource += 3;
idxCharOnLine += 4;

if (length > 0 && idxCharOnLine >= length) {
idxCharOnLine = 0;
[result appendString:@"\n"];
}
}

return (NSString *)result;
}

- (NSString *)hexadecimalString
{
static const char hexValues[] = "0123456789abcdef";
const size_t len = [self length];
const unsigned char *data = [self bytes];
char *buffer = (char *)calloc(len * 2 + 1, sizeof(char));
char *hex = buffer;
NSString *hexBytes = nil;

for (int i = 0; i < len; i++) {
const unsigned char c = data[i];
*hex++ = hexValues[(c >> 4) & 0xF];
*hex++ = hexValues[(c ) & 0xF];
}

hexBytes = [NSString stringWithUTF8String:buffer];

free(buffer);

return hexBytes;
}

@end
2 changes: 1 addition & 1 deletion Additions/NSDate+STAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ - (NSString *)ISO8601StringForTimeZone:(NSTimeZone *)inTimeZone;
struct tm *timeinfo;
char buffer[80];

time_t rawtime = [self timeIntervalSince1970] - [inTimeZone secondsFromGMT];
time_t rawtime = [self timeIntervalSince1970];
timeinfo = localtime(&rawtime);

strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S%z", timeinfo);
Expand Down
21 changes: 18 additions & 3 deletions Additions/NSDictionary+STAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ - (NSString *)URLEncodedStringValue;

while ((currentKey = (NSString *)[keyEnum nextObject]) != nil) {
id currentValue = [self objectForKey:currentKey];
NSString *stringValue = [currentValue URLParameterStringValue];
NSString *stringValue = nil;

if ([currentValue isKindOfClass:[NSString class]]) {
stringValue = currentValue;
} else if ([currentValue isKindOfClass:[NSData class]]) {
stringValue = [NSString stringWithCString:[(NSData *)currentValue bytes]
encoding:NSUTF8StringEncoding];
}

if (stringValue != nil) {
if (appendAmpersand) {
Expand Down Expand Up @@ -97,7 +104,15 @@ - (NSString *)URLEncodedQuotedKeyValueListValue;

while ((currentKey = (NSString *)[keyEnum nextObject]) != nil) {
id currentValue = [self objectForKey:currentKey];
NSString *stringValue = [currentValue URLParameterStringValue];

NSString *stringValue = nil;

if ([currentValue isKindOfClass:[NSString class]]) {
stringValue = currentValue;
} else if ([currentValue isKindOfClass:[NSData class]]) {
stringValue = [NSString stringWithCString:[(NSData *)currentValue bytes]
encoding:NSUTF8StringEncoding];
}

if (stringValue != nil) {
if (appendComma) {
Expand Down Expand Up @@ -148,4 +163,4 @@ - (void)addUniqueEntriesFromDictionary:(NSDictionary *)inDictionary;
}
}

@end
@end
7 changes: 4 additions & 3 deletions Additions/NSString+STAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#import "STUtils.h"

#import <CommonCrypto/CommonDigest.h>

@implementation NSString (STAdditions)

Expand All @@ -43,7 +44,7 @@ - (NSString *)stringByRemovingLastPathComponent;
if (currentComponent == lastComponent) {
break;
}

[returnString appendPathComponent:currentComponent];
}

Expand Down Expand Up @@ -262,7 +263,7 @@ - (NSDate *)ISO8601DateValue;
tm.tm_isdst = -1;
t = mktime(&tm);

return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]];
return [NSDate dateWithTimeIntervalSince1970:t];
}

#pragma mark Drawing
Expand All @@ -287,7 +288,7 @@ - (CGSize)drawInRect:(CGRect)inRect withFont:(UIFont *)inFont lineBreakMode:(UIL
CGContextSetFillColorWithColor(context, inColor.CGColor);
CGSize renderedSize = [self drawInRect:inRect withFont:inFont lineBreakMode:inLineBreakMode];
CGContextRestoreGState(context);

return renderedSize;
}

Expand Down
1 change: 1 addition & 0 deletions STUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#import <Security/Security.h>

// Additions
#import "NSData+STAdditions.h"
#import "NSArray+STAdditions.h"
#import "NSDate+STAdditions.h"
#import "NSDictionary+STAdditions.h"
Expand Down