diff --git a/Additions/NSData+STAdditions.h b/Additions/NSData+STAdditions.h new file mode 100644 index 0000000..2b9c933 --- /dev/null +++ b/Additions/NSData+STAdditions.h @@ -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 + + +@interface NSData (STAdditions) + +- (NSString *)base64EncodedStringWithLineLength:(NSInteger)length; +- (NSString *)base64EncodedString; +- (NSString *)hexadecimalString; + +@end diff --git a/Additions/NSData+STAdditions.m b/Additions/NSData+STAdditions.m new file mode 100644 index 0000000..c1d3378 --- /dev/null +++ b/Additions/NSData+STAdditions.m @@ -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 diff --git a/Additions/NSDate+STAdditions.m b/Additions/NSDate+STAdditions.m index 6299f06..1b38dfb 100644 --- a/Additions/NSDate+STAdditions.m +++ b/Additions/NSDate+STAdditions.m @@ -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); diff --git a/Additions/NSDictionary+STAdditions.m b/Additions/NSDictionary+STAdditions.m index 222bf69..e305c7c 100644 --- a/Additions/NSDictionary+STAdditions.m +++ b/Additions/NSDictionary+STAdditions.m @@ -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) { @@ -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) { @@ -148,4 +163,4 @@ - (void)addUniqueEntriesFromDictionary:(NSDictionary *)inDictionary; } } -@end \ No newline at end of file +@end diff --git a/Additions/NSString+STAdditions.m b/Additions/NSString+STAdditions.m index 3a2c1d5..68061b6 100644 --- a/Additions/NSString+STAdditions.m +++ b/Additions/NSString+STAdditions.m @@ -28,6 +28,7 @@ #import "STUtils.h" +#import @implementation NSString (STAdditions) @@ -43,7 +44,7 @@ - (NSString *)stringByRemovingLastPathComponent; if (currentComponent == lastComponent) { break; } - + [returnString appendPathComponent:currentComponent]; } @@ -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 @@ -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; } diff --git a/STUtils.h b/STUtils.h index ec7f5ea..a50ba73 100644 --- a/STUtils.h +++ b/STUtils.h @@ -32,6 +32,7 @@ #import // Additions +#import "NSData+STAdditions.h" #import "NSArray+STAdditions.h" #import "NSDate+STAdditions.h" #import "NSDictionary+STAdditions.h"