2007-01-31 18:12:33 +01:00
|
|
|
/* NSString+Utilities.m - this file is part of SOGo
|
2006-07-29 00:43:28 +02:00
|
|
|
*
|
2014-02-04 21:03:02 +01:00
|
|
|
* Copyright (C) 2006-2014 Inverse inc.
|
2006-07-29 00:43:28 +02:00
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-04-11 21:51:45 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
2007-05-15 22:52:01 +02:00
|
|
|
#import <Foundation/NSCharacterSet.h>
|
2010-08-26 22:45:23 +02:00
|
|
|
#import <Foundation/NSData.h>
|
2007-02-08 17:30:29 +01:00
|
|
|
#import <Foundation/NSEnumerator.h>
|
2009-06-10 22:54:02 +02:00
|
|
|
#import <Foundation/NSValue.h>
|
2007-02-08 17:30:29 +01:00
|
|
|
|
2008-08-21 21:17:38 +02:00
|
|
|
#import <EOControl/EOQualifier.h>
|
|
|
|
|
2010-01-05 23:34:35 +01:00
|
|
|
#import <NGExtensions/NSDictionary+misc.h>
|
2010-11-02 14:16:05 +01:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2012-04-16 17:24:16 +02:00
|
|
|
#import <NGExtensions/NGBase64Coding.h>
|
2007-09-04 17:38:25 +02:00
|
|
|
|
2010-08-26 22:45:23 +02:00
|
|
|
#import <NGMime/NGMimeHeaderFieldGenerator.h>
|
2010-11-02 14:16:05 +01:00
|
|
|
#import <SBJson/SBJsonParser.h>
|
|
|
|
|
2007-05-30 22:02:27 +02:00
|
|
|
#import "NSArray+Utilities.h"
|
2006-07-29 00:43:28 +02:00
|
|
|
#import "NSDictionary+URL.h"
|
|
|
|
|
2007-05-30 22:02:27 +02:00
|
|
|
#import "NSString+Utilities.h"
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
static NSMutableCharacterSet *urlNonEndingChars = nil;
|
|
|
|
static NSMutableCharacterSet *urlAfterEndingChars = nil;
|
2007-12-13 01:00:55 +01:00
|
|
|
static NSMutableCharacterSet *urlStartChars = nil;
|
2007-05-15 22:52:01 +02:00
|
|
|
|
2010-01-05 23:34:35 +01:00
|
|
|
static NSString **cssEscapingStrings = NULL;
|
|
|
|
static unichar *cssEscapingCharacters = NULL;
|
2010-01-08 16:03:02 +01:00
|
|
|
static int cssEscapingCount;
|
2010-01-05 23:34:35 +01:00
|
|
|
|
2014-02-04 21:03:02 +01:00
|
|
|
static unichar thisCharCode[29];
|
|
|
|
static NSString *controlCharString = nil;
|
|
|
|
static NSCharacterSet *controlCharSet = nil;
|
|
|
|
|
2006-07-29 00:43:28 +02:00
|
|
|
@implementation NSString (SOGoURLExtension)
|
|
|
|
|
|
|
|
- (NSString *) composeURLWithAction: (NSString *) action
|
|
|
|
parameters: (NSDictionary *) urlParameters
|
|
|
|
andHash: (BOOL) useHash
|
|
|
|
{
|
|
|
|
NSMutableString *completeURL;
|
|
|
|
|
|
|
|
completeURL = [NSMutableString new];
|
|
|
|
[completeURL autorelease];
|
|
|
|
|
2006-10-03 18:09:43 +02:00
|
|
|
[completeURL appendString: [self urlWithoutParameters]];
|
2006-07-29 00:43:28 +02:00
|
|
|
if (![completeURL hasSuffix: @"/"])
|
|
|
|
[completeURL appendString: @"/"];
|
|
|
|
[completeURL appendString: action];
|
2010-06-10 20:44:38 +02:00
|
|
|
if (urlParameters)
|
|
|
|
[completeURL appendString: [urlParameters asURLParameters]];
|
2006-07-29 00:43:28 +02:00
|
|
|
if (useHash)
|
|
|
|
[completeURL appendString: @"#"];
|
|
|
|
|
|
|
|
return completeURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) hostlessURL
|
|
|
|
{
|
|
|
|
NSString *newURL;
|
|
|
|
NSRange hostR, locationR;
|
|
|
|
|
|
|
|
if ([self hasPrefix: @"/"])
|
2006-12-14 21:17:46 +01:00
|
|
|
{
|
|
|
|
newURL = [self copy];
|
|
|
|
[newURL autorelease];
|
|
|
|
}
|
2006-07-29 00:43:28 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hostR = [self rangeOfString: @"://"];
|
|
|
|
locationR = [[self substringFromIndex: (hostR.location + hostR.length)]
|
|
|
|
rangeOfString: @"/"];
|
2007-07-09 22:46:36 +02:00
|
|
|
newURL = [self substringFromIndex: (hostR.location + hostR.length
|
|
|
|
+ locationR.location)];
|
2006-07-29 00:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newURL;
|
|
|
|
}
|
|
|
|
|
2006-10-03 18:09:43 +02:00
|
|
|
- (NSString *) urlWithoutParameters;
|
|
|
|
{
|
|
|
|
NSRange r;
|
|
|
|
NSString *newUrl;
|
|
|
|
|
|
|
|
r = [self rangeOfString:@"?" options: NSBackwardsSearch];
|
|
|
|
if (r.length > 0)
|
2009-06-10 22:54:02 +02:00
|
|
|
newUrl = [self substringToIndex: NSMaxRange (r) - 1];
|
2006-10-03 18:09:43 +02:00
|
|
|
else
|
|
|
|
newUrl = self;
|
|
|
|
|
|
|
|
return newUrl;
|
|
|
|
}
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
- (NSRange) _rangeOfURLInRange: (NSRange) refRange
|
|
|
|
{
|
|
|
|
int start, length;
|
2007-05-16 18:30:04 +02:00
|
|
|
NSRange workRange;
|
2007-05-15 22:52:01 +02:00
|
|
|
|
2007-09-07 17:38:34 +02:00
|
|
|
// [urlNonEndingChars addCharactersInString: @">&=,.:;\t \r\n"];
|
|
|
|
// [urlAfterEndingChars addCharactersInString: @"()[]{}&;<\t \r\n"];
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
if (!urlNonEndingChars)
|
|
|
|
{
|
|
|
|
urlNonEndingChars = [NSMutableCharacterSet new];
|
2008-08-25 22:52:48 +02:00
|
|
|
[urlNonEndingChars addCharactersInString: @"=,.:;&()\t \r\n"];
|
2007-05-15 22:52:01 +02:00
|
|
|
}
|
|
|
|
if (!urlAfterEndingChars)
|
|
|
|
{
|
|
|
|
urlAfterEndingChars = [NSMutableCharacterSet new];
|
2008-08-25 22:52:48 +02:00
|
|
|
[urlAfterEndingChars addCharactersInString: @"()[]\t \r\n"];
|
2007-05-15 22:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start = refRange.location;
|
|
|
|
while (start > -1
|
2007-05-16 18:30:04 +02:00
|
|
|
&& ![urlAfterEndingChars characterIsMember:
|
|
|
|
[self characterAtIndex: start]])
|
2007-05-15 22:52:01 +02:00
|
|
|
start--;
|
|
|
|
start++;
|
2011-10-13 12:53:16 +02:00
|
|
|
|
2012-10-30 21:59:22 +01:00
|
|
|
length = [self length];
|
2011-10-13 12:53:16 +02:00
|
|
|
// In [UIxMailPartTextViewer flatContentAsString], we first escape HTML entities and then
|
|
|
|
// add URLs. Therefore, the brackets (inequality signs <>) have been encoded at this point.
|
2012-10-30 21:59:22 +01:00
|
|
|
if (length > (start + 4)
|
|
|
|
&& [[self substringWithRange: NSMakeRange (start, 4)] compare: @"<"] == NSOrderedSame)
|
2011-10-13 12:53:16 +02:00
|
|
|
start += 4;
|
|
|
|
|
2012-10-30 21:59:22 +01:00
|
|
|
length -= start;
|
2007-05-16 18:30:04 +02:00
|
|
|
workRange = [self rangeOfCharacterFromSet: urlAfterEndingChars
|
2012-10-30 21:59:22 +01:00
|
|
|
options: NSLiteralSearch range: NSMakeRange (start, length)];
|
2007-05-16 18:30:04 +02:00
|
|
|
if (workRange.location != NSNotFound)
|
|
|
|
length = workRange.location - start;
|
2007-05-15 22:52:01 +02:00
|
|
|
while
|
2007-05-16 17:56:53 +02:00
|
|
|
(length > 0
|
|
|
|
&& [urlNonEndingChars characterIsMember:
|
|
|
|
[self characterAtIndex: (start + length - 1)]])
|
2007-05-15 22:52:01 +02:00
|
|
|
length--;
|
|
|
|
|
2011-10-13 12:53:16 +02:00
|
|
|
// Remove trailing ">"
|
|
|
|
if (([self length] >= start + length + 1)
|
|
|
|
&& [[self substringWithRange: NSMakeRange (start, length + 1)] hasSuffix: @">"])
|
|
|
|
length -= 3;
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
return NSMakeRange (start, length);
|
|
|
|
}
|
|
|
|
|
2007-05-16 23:42:47 +02:00
|
|
|
- (void) _handleURLs: (NSMutableString *) selfCopy
|
|
|
|
textToMatch: (NSString *) match
|
|
|
|
prefix: (NSString *) prefix
|
2007-05-30 22:02:27 +02:00
|
|
|
inRanges: (NSMutableArray *) ranges
|
2007-05-15 22:52:01 +02:00
|
|
|
{
|
2008-08-22 01:35:34 +02:00
|
|
|
NSEnumerator *enumRanges;
|
|
|
|
NSMutableArray *newRanges;
|
|
|
|
NSRange matchRange, currentUrlRange, rest;
|
2009-06-10 22:54:02 +02:00
|
|
|
NSRange *rangePtr;
|
|
|
|
NSString *urlText, *newUrlText;
|
2008-08-22 02:49:23 +02:00
|
|
|
unsigned int length, matchLength, offset;
|
2007-12-13 01:00:55 +01:00
|
|
|
int startLocation;
|
2007-05-15 22:52:01 +02:00
|
|
|
|
2007-12-13 01:00:55 +01:00
|
|
|
if (!urlStartChars)
|
|
|
|
{
|
|
|
|
urlStartChars = [NSMutableCharacterSet new];
|
|
|
|
[urlStartChars addCharactersInString: @"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
@"0123456789:@"];
|
|
|
|
}
|
2009-06-10 22:54:02 +02:00
|
|
|
|
|
|
|
newRanges = [NSMutableArray array];
|
2007-05-16 23:42:47 +02:00
|
|
|
matchLength = [match length];
|
2008-08-22 01:35:34 +02:00
|
|
|
rest.location = -1;
|
2009-06-10 22:54:02 +02:00
|
|
|
|
2008-08-22 01:35:34 +02:00
|
|
|
matchRange = [selfCopy rangeOfString: match];
|
|
|
|
while (matchRange.location != NSNotFound)
|
2007-12-13 01:00:55 +01:00
|
|
|
{
|
2008-08-22 01:35:34 +02:00
|
|
|
startLocation = matchRange.location;
|
|
|
|
while (startLocation > rest.location
|
2007-12-13 01:00:55 +01:00
|
|
|
&& [urlStartChars characterIsMember:
|
|
|
|
[selfCopy characterAtIndex: startLocation]])
|
|
|
|
startLocation--;
|
2008-08-22 01:35:34 +02:00
|
|
|
matchRange.location = startLocation + 1;
|
|
|
|
|
2013-02-05 22:11:37 +01:00
|
|
|
// We avoid going out of bounds if the mail content actually finishes
|
|
|
|
// with the @ (or something else) character
|
|
|
|
if (matchRange.location < [selfCopy length])
|
|
|
|
{
|
|
|
|
currentUrlRange = [selfCopy _rangeOfURLInRange: matchRange];
|
|
|
|
if (![ranges hasRangeIntersection: currentUrlRange])
|
|
|
|
if (currentUrlRange.length > matchLength)
|
|
|
|
[newRanges addNonNSObject: ¤tUrlRange
|
|
|
|
withSize: sizeof (NSRange)
|
|
|
|
copy: YES];
|
|
|
|
|
|
|
|
rest.location = NSMaxRange (currentUrlRange);
|
|
|
|
length = [selfCopy length];
|
|
|
|
rest.length = length - rest.location;
|
|
|
|
matchRange = [selfCopy rangeOfString: match
|
|
|
|
options: 0 range: rest];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matchRange.location = NSNotFound;
|
|
|
|
}
|
2008-08-22 01:35:34 +02:00
|
|
|
}
|
|
|
|
|
2008-08-22 02:49:23 +02:00
|
|
|
// Make the substitutions, keep track of the new offset
|
|
|
|
offset = 0;
|
|
|
|
enumRanges = [newRanges objectEnumerator];
|
2009-06-10 22:54:02 +02:00
|
|
|
while ((rangePtr = [[enumRanges nextObject] pointerValue]))
|
2008-08-22 01:35:34 +02:00
|
|
|
{
|
2009-06-10 22:54:02 +02:00
|
|
|
rangePtr->location += offset;
|
|
|
|
urlText = [selfCopy substringFromRange: *rangePtr];
|
2008-08-22 01:35:34 +02:00
|
|
|
newUrlText = [NSString stringWithFormat: @"<a href=\"%@%@\">%@</a>",
|
2012-06-01 16:29:30 +02:00
|
|
|
([urlText hasPrefix: prefix]? @"" : prefix),
|
|
|
|
urlText, urlText];
|
2009-06-10 22:54:02 +02:00
|
|
|
[selfCopy replaceCharactersInRange: *rangePtr
|
2012-06-01 16:29:30 +02:00
|
|
|
withString: newUrlText];
|
2008-08-22 02:49:23 +02:00
|
|
|
offset += ([newUrlText length] - [urlText length]);
|
2012-06-01 16:29:30 +02:00
|
|
|
|
2008-08-22 01:35:34 +02:00
|
|
|
// Add range for further substitutions
|
2009-06-30 21:09:57 +02:00
|
|
|
currentUrlRange = NSMakeRange (rangePtr->location, [newUrlText length]);
|
2009-06-10 22:54:02 +02:00
|
|
|
[ranges addNonNSObject: ¤tUrlRange
|
|
|
|
withSize: sizeof (NSRange)
|
|
|
|
copy: YES];
|
2007-05-15 22:52:01 +02:00
|
|
|
}
|
2009-06-10 22:54:02 +02:00
|
|
|
[newRanges freeNonNSObjects];
|
2007-05-16 23:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) stringByDetectingURLs
|
|
|
|
{
|
|
|
|
NSMutableString *selfCopy;
|
2007-05-30 22:02:27 +02:00
|
|
|
NSMutableArray *ranges;
|
2007-05-16 23:42:47 +02:00
|
|
|
|
2009-06-10 22:54:02 +02:00
|
|
|
ranges = [NSMutableArray array];
|
2007-05-16 23:42:47 +02:00
|
|
|
selfCopy = [NSMutableString stringWithString: self];
|
2007-05-30 22:02:27 +02:00
|
|
|
[self _handleURLs: selfCopy
|
|
|
|
textToMatch: @"://"
|
2012-06-01 16:29:30 +02:00
|
|
|
prefix: @""
|
|
|
|
inRanges: ranges];
|
2007-05-30 22:02:27 +02:00
|
|
|
[self _handleURLs: selfCopy
|
|
|
|
textToMatch: @"@"
|
2012-06-01 16:29:30 +02:00
|
|
|
prefix: @"mailto:"
|
|
|
|
inRanges: ranges];
|
2009-06-10 22:54:02 +02:00
|
|
|
[ranges freeNonNSObjects];
|
2007-05-15 22:52:01 +02:00
|
|
|
|
|
|
|
return selfCopy;
|
|
|
|
}
|
|
|
|
|
2009-01-07 17:45:15 +01:00
|
|
|
- (NSString *) doubleQuotedString
|
2007-05-25 19:30:24 +02:00
|
|
|
{
|
|
|
|
NSMutableString *representation;
|
|
|
|
|
|
|
|
representation = [NSMutableString stringWithString: self];
|
|
|
|
[representation replaceString: @"\\" withString: @"\\\\"];
|
|
|
|
[representation replaceString: @"\"" withString: @"\\\""];
|
|
|
|
[representation replaceString: @"/" withString: @"\\/"];
|
|
|
|
[representation replaceString: @"\f" withString: @"\\f"];
|
|
|
|
[representation replaceString: @"\n" withString: @"\\n"];
|
|
|
|
[representation replaceString: @"\r" withString: @"\\r"];
|
|
|
|
[representation replaceString: @"\t" withString: @"\\t"];
|
|
|
|
|
|
|
|
return [NSString stringWithFormat: @"\"%@\"", representation];
|
|
|
|
}
|
|
|
|
|
2014-02-04 21:03:02 +01:00
|
|
|
- (NSCharacterSet *) safeCharacterSet
|
2009-01-07 17:45:15 +01:00
|
|
|
{
|
2013-01-30 21:47:20 +01:00
|
|
|
if (!controlCharSet)
|
|
|
|
{
|
2014-02-04 21:03:02 +01:00
|
|
|
int i, j;
|
|
|
|
|
2013-01-30 21:47:20 +01:00
|
|
|
// Create an array of chars for all control characters between 0x00 and 0x1F,
|
|
|
|
// apart from \t, \n, \f and \r (0x08, 0x09, 0x0A, 0x0C and 0x0D)
|
2013-06-05 21:53:53 +02:00
|
|
|
for (i = 0, j = 0x00; j < 0x08; i++, j++) {
|
|
|
|
thisCharCode[i] = j;
|
2013-01-30 21:47:20 +01:00
|
|
|
}
|
2013-06-05 21:53:53 +02:00
|
|
|
thisCharCode[i++] = 0x0B;
|
|
|
|
for (j = 0x0E; j <= 0x1F; i++, j++) {
|
|
|
|
thisCharCode[i] = j;
|
2013-01-30 21:47:20 +01:00
|
|
|
}
|
2014-02-04 21:03:02 +01:00
|
|
|
|
2013-06-05 21:53:53 +02:00
|
|
|
// Also add some unicode separators
|
|
|
|
thisCharCode[i++] = 0x2028; // line separator
|
|
|
|
thisCharCode[i++] = 0x2029; // paragraph separator
|
|
|
|
controlCharString = [NSString stringWithCharacters:thisCharCode length:i];
|
2013-01-30 21:47:20 +01:00
|
|
|
controlCharSet = [NSCharacterSet characterSetWithCharactersInString: controlCharString];
|
|
|
|
[controlCharSet retain];
|
|
|
|
}
|
|
|
|
|
2014-02-04 21:03:02 +01:00
|
|
|
return controlCharSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) jsonRepresentation
|
|
|
|
{
|
|
|
|
NSString *cleanedString;
|
|
|
|
|
2013-01-30 21:47:20 +01:00
|
|
|
// Escape double quotes and remove control characters
|
2014-02-04 21:03:02 +01:00
|
|
|
cleanedString = [[[self doubleQuotedString] componentsSeparatedByCharactersInSet: [self safeCharacterSet]]
|
2013-01-30 21:47:20 +01:00
|
|
|
componentsJoinedByString: @""];
|
|
|
|
return cleanedString;
|
2009-01-07 17:45:15 +01:00
|
|
|
}
|
|
|
|
|
2010-01-05 23:34:35 +01:00
|
|
|
- (void) _setupCSSEscaping
|
|
|
|
{
|
|
|
|
NSArray *strings, *characters;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
strings = [NSArray arrayWithObjects: @"_U_", @"_D_", @"_H_", @"_A_", @"_S_",
|
2013-11-13 21:44:57 +01:00
|
|
|
@"_C_", @"_CO_", @"_SP_", @"_SQ_", @"_AM_", @"_P_", @"_DS_", nil];
|
2010-01-12 20:43:47 +01:00
|
|
|
[strings retain];
|
2010-01-05 23:34:35 +01:00
|
|
|
cssEscapingStrings = [strings asPointersOfObjects];
|
|
|
|
|
|
|
|
characters = [NSArray arrayWithObjects: @"_", @".", @"#", @"@", @"*", @":",
|
2013-11-13 21:44:57 +01:00
|
|
|
@",", @" ", @"'", @"&", @"+", @"$", nil];
|
2010-01-05 23:34:35 +01:00
|
|
|
cssEscapingCount = [strings count];
|
2010-01-08 16:03:02 +01:00
|
|
|
cssEscapingCharacters = NSZoneMalloc (NULL,
|
|
|
|
(cssEscapingCount + 1)
|
|
|
|
* sizeof (unichar));
|
2010-01-05 23:34:35 +01:00
|
|
|
for (count = 0; count < cssEscapingCount; count++)
|
|
|
|
*(cssEscapingCharacters + count)
|
|
|
|
= [[characters objectAtIndex: count] characterAtIndex: 0];
|
|
|
|
*(cssEscapingCharacters + cssEscapingCount) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) _cssCharacterIndex: (unichar) character
|
|
|
|
{
|
|
|
|
int idx, count;
|
|
|
|
|
|
|
|
idx = -1;
|
|
|
|
for (count = 0; idx == -1 && count < cssEscapingCount; count++)
|
|
|
|
if (*(cssEscapingCharacters + count) == character)
|
|
|
|
idx = count;
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2009-01-16 16:33:07 +01:00
|
|
|
- (NSString *) asCSSIdentifier
|
|
|
|
{
|
|
|
|
NSMutableString *cssIdentifier;
|
2010-01-05 23:34:35 +01:00
|
|
|
unichar currentChar;
|
|
|
|
int count, max, idx;
|
|
|
|
|
|
|
|
if (!cssEscapingStrings)
|
|
|
|
[self _setupCSSEscaping];
|
2009-01-16 16:33:07 +01:00
|
|
|
|
2010-01-05 23:34:35 +01:00
|
|
|
cssIdentifier = [NSMutableString string];
|
|
|
|
max = [self length];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
currentChar = [self characterAtIndex: count];
|
|
|
|
idx = [self _cssCharacterIndex: currentChar];
|
|
|
|
if (idx > -1)
|
|
|
|
[cssIdentifier appendString: cssEscapingStrings[idx]];
|
|
|
|
else
|
2010-01-07 20:24:46 +01:00
|
|
|
[cssIdentifier appendFormat: @"%C", currentChar];
|
2010-01-05 23:34:35 +01:00
|
|
|
}
|
2009-01-16 16:33:07 +01:00
|
|
|
|
|
|
|
return cssIdentifier;
|
|
|
|
}
|
|
|
|
|
2010-01-05 23:34:35 +01:00
|
|
|
- (int) _cssStringIndex: (NSString *) string
|
|
|
|
{
|
|
|
|
int idx, count;
|
|
|
|
|
|
|
|
idx = -1;
|
|
|
|
for (count = 0; idx == -1 && count < cssEscapingCount; count++)
|
|
|
|
if ([string hasPrefix: *(cssEscapingStrings + count)])
|
|
|
|
idx = count;
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) fromCSSIdentifier
|
|
|
|
{
|
|
|
|
NSMutableString *newString;
|
|
|
|
NSString *currentString;
|
|
|
|
int count, length, max, idx;
|
2010-01-07 20:53:03 +01:00
|
|
|
unichar currentChar;
|
2010-01-05 23:34:35 +01:00
|
|
|
|
|
|
|
if (!cssEscapingStrings)
|
|
|
|
[self _setupCSSEscaping];
|
|
|
|
|
|
|
|
newString = [NSMutableString string];
|
|
|
|
max = [self length];
|
|
|
|
for (count = 0; count < max - 2; count++)
|
|
|
|
{
|
2010-01-07 20:53:03 +01:00
|
|
|
currentChar = [self characterAtIndex: count];
|
|
|
|
if (currentChar == '_')
|
2010-01-05 23:34:35 +01:00
|
|
|
{
|
2010-01-07 20:53:03 +01:00
|
|
|
/* The difficulty here is that most escaping strings are 3 chars
|
|
|
|
long except one. Therefore we must juggle a little bit with the
|
|
|
|
lengths in order to avoid an overflow exception. */
|
|
|
|
length = 4;
|
|
|
|
if (count + length > max)
|
|
|
|
length = max - count;
|
|
|
|
currentString = [self substringFromRange: NSMakeRange (count, length)];
|
|
|
|
idx = [self _cssStringIndex: currentString];
|
|
|
|
if (idx > -1)
|
|
|
|
{
|
|
|
|
[newString appendFormat: @"%C", cssEscapingCharacters[idx]];
|
|
|
|
count += [cssEscapingStrings[idx] length] - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[newString appendFormat: @"%C", currentChar];
|
2010-01-05 23:34:35 +01:00
|
|
|
}
|
|
|
|
else
|
2010-01-07 20:53:03 +01:00
|
|
|
[newString appendFormat: @"%C", currentChar];
|
2010-01-05 23:34:35 +01:00
|
|
|
}
|
|
|
|
currentString = [self substringFromRange: NSMakeRange (count, max - count)];
|
|
|
|
[newString appendString: currentString];
|
|
|
|
|
|
|
|
return newString;
|
|
|
|
}
|
|
|
|
|
2007-08-09 22:59:30 +02:00
|
|
|
- (NSString *) pureEMailAddress
|
|
|
|
{
|
|
|
|
NSString *pureAddress;
|
|
|
|
NSRange delimiter;
|
|
|
|
|
|
|
|
delimiter = [self rangeOfString: @"<"];
|
|
|
|
if (delimiter.location == NSNotFound)
|
|
|
|
pureAddress = self;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pureAddress = [self substringFromIndex: NSMaxRange (delimiter)];
|
|
|
|
delimiter = [pureAddress rangeOfString: @">"];
|
|
|
|
if (delimiter.location != NSNotFound)
|
|
|
|
pureAddress = [pureAddress substringToIndex: delimiter.location];
|
|
|
|
}
|
|
|
|
|
|
|
|
return pureAddress;
|
|
|
|
}
|
|
|
|
|
2007-08-24 00:10:50 +02:00
|
|
|
- (NSString *) asQPSubjectString: (NSString *) encoding
|
|
|
|
{
|
|
|
|
NSString *qpString, *subjectString;
|
|
|
|
NSData *subjectData, *destSubjectData;
|
2010-08-26 22:45:23 +02:00
|
|
|
NSUInteger length, destLength;
|
|
|
|
unsigned char *destString;
|
2007-08-24 00:10:50 +02:00
|
|
|
|
2010-08-26 22:45:23 +02:00
|
|
|
#warning "encoding" parameter is not useful
|
2007-08-24 00:10:50 +02:00
|
|
|
subjectData = [self dataUsingEncoding: NSUTF8StringEncoding];
|
2010-08-26 22:45:23 +02:00
|
|
|
length = [subjectData length];
|
|
|
|
destLength = length * 3;
|
|
|
|
destString = calloc (destLength, sizeof (char));
|
2007-08-24 00:10:50 +02:00
|
|
|
|
2010-08-26 22:45:23 +02:00
|
|
|
NGEncodeQuotedPrintableMime ([subjectData bytes], length,
|
|
|
|
destString, destLength);
|
|
|
|
|
|
|
|
destSubjectData = [NSData dataWithBytesNoCopy: destString
|
|
|
|
length: strlen ((char *) destString)
|
|
|
|
freeWhenDone: YES];
|
2007-08-24 00:10:50 +02:00
|
|
|
qpString = [[NSString alloc] initWithData: destSubjectData
|
|
|
|
encoding: NSASCIIStringEncoding];
|
|
|
|
[qpString autorelease];
|
|
|
|
if ([qpString length] > [self length])
|
2008-09-22 22:31:57 +02:00
|
|
|
{
|
|
|
|
qpString = [qpString stringByReplacingString: @" " withString: @"_"];
|
2010-04-15 20:27:08 +02:00
|
|
|
subjectString = [NSString stringWithFormat: @"=?%@?q?%@?=",
|
2008-09-22 22:31:57 +02:00
|
|
|
encoding, qpString];
|
|
|
|
}
|
2007-08-24 00:10:50 +02:00
|
|
|
else
|
|
|
|
subjectString = self;
|
|
|
|
|
|
|
|
return subjectString;
|
|
|
|
}
|
|
|
|
|
2008-08-21 21:17:38 +02:00
|
|
|
- (BOOL) caseInsensitiveMatches: (NSString *) match
|
|
|
|
{
|
|
|
|
EOQualifier *sq;
|
|
|
|
NSString *format;
|
|
|
|
|
|
|
|
format = [NSString stringWithFormat:
|
|
|
|
@"(description isCaseInsensitiveLike: '%@')",
|
|
|
|
match];
|
|
|
|
sq = [EOQualifier qualifierWithQualifierFormat: format];
|
|
|
|
|
2009-04-01 15:49:24 +02:00
|
|
|
return [(id<EOQualifierEvaluation>)sq evaluateWithObject: self];
|
2008-08-21 21:17:38 +02:00
|
|
|
}
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
#if LIB_FOUNDATION_LIBRARY
|
2007-04-11 21:33:25 +02:00
|
|
|
- (BOOL) boolValue
|
|
|
|
{
|
|
|
|
return !([self isEqualToString: @"0"]
|
|
|
|
|| [self isEqualToString: @"NO"]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
- (int) timeValue
|
|
|
|
{
|
2012-05-29 20:56:15 +02:00
|
|
|
int time;
|
|
|
|
NSInteger i;
|
2009-11-29 05:19:32 +01:00
|
|
|
|
|
|
|
if ([self length] > 0)
|
|
|
|
{
|
|
|
|
i = [self rangeOfString: @":"].location;
|
|
|
|
if (i == NSNotFound)
|
|
|
|
time = [self intValue];
|
|
|
|
else
|
|
|
|
time = [[self substringToIndex: i] intValue];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
time = -1;
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2009-11-19 00:16:15 +01:00
|
|
|
- (BOOL) isJSONString
|
|
|
|
{
|
|
|
|
NSDictionary *jsonData;
|
|
|
|
|
|
|
|
#warning this method is a quick and dirty way of detecting the file-format
|
2010-10-26 20:20:46 +02:00
|
|
|
jsonData = [self objectFromJSONString];
|
2009-11-19 00:16:15 +01:00
|
|
|
|
|
|
|
return (jsonData != nil);
|
|
|
|
}
|
|
|
|
|
2010-10-26 20:20:46 +02:00
|
|
|
- (id) objectFromJSONString
|
|
|
|
{
|
2010-11-02 14:16:05 +01:00
|
|
|
SBJsonParser *parser;
|
2010-10-26 20:20:46 +02:00
|
|
|
NSObject *object;
|
2010-11-02 14:16:05 +01:00
|
|
|
NSError *error;
|
2012-04-05 20:50:05 +02:00
|
|
|
NSString *unescaped;
|
2010-10-26 20:20:46 +02:00
|
|
|
|
|
|
|
object = nil;
|
|
|
|
|
|
|
|
if ([self length] > 0)
|
|
|
|
{
|
2010-11-02 14:16:05 +01:00
|
|
|
parser = [SBJsonParser new];
|
|
|
|
[parser autorelease];
|
|
|
|
error = nil;
|
|
|
|
object = [parser objectWithString: self
|
|
|
|
error: &error];
|
|
|
|
if (error)
|
|
|
|
{
|
2012-04-05 20:50:05 +02:00
|
|
|
[self errorWithFormat: @"json parser: %@,"
|
|
|
|
@" attempting once more after unescaping...", error];
|
|
|
|
unescaped = [self stringByReplacingString: @"\\\\"
|
|
|
|
withString: @"\\"];
|
|
|
|
object = [parser objectWithString: unescaped
|
|
|
|
error: &error];
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"total failure. Original string is: %@", self];
|
|
|
|
object = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self logWithFormat: @"initial object deserialized successfully!"];
|
2010-11-02 14:16:05 +01:00
|
|
|
}
|
2010-10-26 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2011-06-16 16:38:14 +02:00
|
|
|
- (NSString *) asSafeSQLString
|
|
|
|
{
|
|
|
|
return [[self stringByReplacingString: @"\\" withString: @"\\\\"]
|
|
|
|
stringByReplacingString: @"'" withString: @"\\'"];
|
|
|
|
}
|
2010-12-29 14:01:16 +01:00
|
|
|
|
2011-07-19 22:01:13 +02:00
|
|
|
- (NSUInteger) countOccurrencesOfString: (NSString *) substring
|
|
|
|
{
|
|
|
|
NSRange matchRange, substrRange;
|
|
|
|
BOOL done = NO;
|
|
|
|
NSUInteger selfLen, substrLen, count = 0;
|
|
|
|
|
|
|
|
selfLen = [self length];
|
|
|
|
substrLen = [substring length];
|
|
|
|
|
|
|
|
matchRange = NSMakeRange (0, selfLen);
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
substrRange = [self rangeOfString: substring options: 0 range: matchRange];
|
|
|
|
if (substrRange.location == NSNotFound)
|
|
|
|
done = YES;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
matchRange.location = substrRange.location + 1;
|
|
|
|
if (matchRange.location + substrLen > selfLen)
|
|
|
|
done = YES;
|
|
|
|
else
|
|
|
|
matchRange.length = selfLen - matchRange.location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-08-13 17:29:44 +02:00
|
|
|
- (NSString *) stringByReplacingPrefix: (NSString *) oldPrefix
|
|
|
|
withPrefix: (NSString *) newPrefix
|
|
|
|
{
|
|
|
|
NSUInteger oldPrefixLength;
|
|
|
|
NSString *newString;
|
|
|
|
|
|
|
|
if (![self hasPrefix: oldPrefix])
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"string does not have the specified prefix"];
|
|
|
|
|
|
|
|
oldPrefixLength = [oldPrefix length];
|
|
|
|
newString = [NSString stringWithFormat: @"%@%@",
|
|
|
|
newPrefix,
|
|
|
|
[self substringFromIndex: oldPrefixLength]];
|
|
|
|
|
|
|
|
return newString;
|
|
|
|
}
|
|
|
|
|
2012-04-16 17:24:16 +02:00
|
|
|
- (NSString *) encryptWithKey: (NSString *) theKey
|
|
|
|
{
|
|
|
|
NSMutableData *encryptedPassword;
|
|
|
|
NSMutableString *key;
|
|
|
|
NSString *result;
|
|
|
|
NSUInteger i, passLength, theKeyLength, keyLength;
|
|
|
|
unichar p, k, e;
|
|
|
|
|
|
|
|
if ([theKey length] > 0)
|
|
|
|
{
|
|
|
|
// The length of the key must be greater (or equal) than
|
|
|
|
// the length of the password
|
|
|
|
key = [NSMutableString string];
|
|
|
|
keyLength = 0;
|
|
|
|
|
|
|
|
passLength = [self length];
|
|
|
|
theKeyLength = [theKey length];
|
|
|
|
while (keyLength < passLength)
|
|
|
|
{
|
|
|
|
[key appendString: theKey];
|
|
|
|
keyLength += theKeyLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedPassword = [NSMutableData data];
|
|
|
|
for (i = 0; i < passLength; i++)
|
|
|
|
{
|
|
|
|
p = [self characterAtIndex: i];
|
|
|
|
k = [key characterAtIndex: i];
|
|
|
|
e = p ^ k;
|
|
|
|
[encryptedPassword appendBytes: (void *)&e length: 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
result = [encryptedPassword stringByEncodingBase64];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = nil;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) decryptWithKey: (NSString *) theKey
|
|
|
|
{
|
|
|
|
NSMutableString *result;
|
|
|
|
NSMutableString *key;
|
|
|
|
NSData *decoded;
|
|
|
|
unichar *decryptedPassword;
|
|
|
|
NSUInteger i, theKeyLength, keyLength, decodedLength;
|
|
|
|
unichar p, k;
|
|
|
|
|
|
|
|
if ([theKey length] > 0)
|
|
|
|
{
|
|
|
|
decoded = [self dataByDecodingBase64];
|
|
|
|
decryptedPassword = (unichar *)[decoded bytes];
|
|
|
|
|
|
|
|
// The length of the key must be greater (or equal) than
|
|
|
|
// the length of the password
|
|
|
|
key = [NSMutableString string];
|
|
|
|
keyLength = 0;
|
|
|
|
decodedLength = ([decoded length] / 2); /* 1 unichar = 2 bytes/char */
|
|
|
|
theKeyLength = [theKey length];
|
|
|
|
|
|
|
|
while (keyLength < decodedLength)
|
|
|
|
{
|
|
|
|
[key appendString: theKey];
|
|
|
|
keyLength += theKeyLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = [NSMutableString string];
|
|
|
|
for (i = 0; i < decodedLength; i++)
|
|
|
|
{
|
|
|
|
k = [key characterAtIndex: i];
|
|
|
|
p = decryptedPassword[i] ^ k;
|
|
|
|
[result appendFormat: @"%C", p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = nil;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-07-29 00:43:28 +02:00
|
|
|
@end
|