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
|
|
|
*
|
2010-12-29 14:01:16 +01:00
|
|
|
* Copyright (C) 2006-2011 Inverse inc.
|
2006-07-29 00:43:28 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
2010-12-29 14:01:16 +01:00
|
|
|
* Ludovic Marcotte <lmarcotte@inverse.ca>
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-06-23 15:34:50 +02:00
|
|
|
#ifndef __OpenBSD__
|
2011-04-05 15:34:55 +02:00
|
|
|
#include <crypt.h>
|
2011-06-23 15:34:50 +02:00
|
|
|
#endif
|
2011-04-05 15:34:55 +02:00
|
|
|
|
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>
|
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"
|
|
|
|
|
2010-12-29 14:01:16 +01:00
|
|
|
#define _XOPEN_SOURCE 1
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/md5.h>
|
|
|
|
#include <openssl/sha.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
|
|
|
|
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
|
|
|
|
|
|
|
// In [UIxMailPartTextViewer flatContentAsString], we first escape HTML entities and then
|
|
|
|
// add URLs. Therefore, the brackets (inequality signs <>) have been encoded at this point.
|
|
|
|
if ([[self substringWithRange: NSMakeRange (start, 4)] compare: @"<"] == NSOrderedSame)
|
|
|
|
start += 4;
|
|
|
|
|
2007-05-15 22:52:01 +02:00
|
|
|
length = [self length] - start;
|
2009-06-10 22:54:02 +02:00
|
|
|
workRange = NSMakeRange (start, length);
|
2007-05-16 18:30:04 +02:00
|
|
|
workRange = [self rangeOfCharacterFromSet: urlAfterEndingChars
|
|
|
|
options: NSLiteralSearch range: workRange];
|
|
|
|
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;
|
|
|
|
|
|
|
|
currentUrlRange = [selfCopy _rangeOfURLInRange: matchRange];
|
2009-06-30 21:09:57 +02:00
|
|
|
if (![ranges hasRangeIntersection: currentUrlRange])
|
|
|
|
if (currentUrlRange.length > matchLength)
|
|
|
|
[newRanges addNonNSObject: ¤tUrlRange
|
|
|
|
withSize: sizeof (NSRange)
|
|
|
|
copy: YES];
|
|
|
|
|
|
|
|
rest.location = NSMaxRange (currentUrlRange);
|
2007-05-15 22:52:01 +02:00
|
|
|
length = [selfCopy length];
|
|
|
|
rest.length = length - rest.location;
|
2008-08-22 01:35:34 +02:00
|
|
|
matchRange = [selfCopy rangeOfString: match
|
|
|
|
options: 0 range: rest];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if ([urlText hasPrefix: prefix]) prefix = @"";
|
|
|
|
newUrlText = [NSString stringWithFormat: @"<a href=\"%@%@\">%@</a>",
|
|
|
|
prefix, urlText, urlText];
|
2009-06-10 22:54:02 +02:00
|
|
|
[selfCopy replaceCharactersInRange: *rangePtr
|
2008-08-22 01:35:34 +02:00
|
|
|
withString: newUrlText];
|
2008-08-22 02:49:23 +02:00
|
|
|
offset += ([newUrlText length] - [urlText length]);
|
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: @"://"
|
|
|
|
prefix: @""
|
|
|
|
inRanges: ranges];
|
|
|
|
[self _handleURLs: selfCopy
|
|
|
|
textToMatch: @"@"
|
|
|
|
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: @"\b" withString: @"\\b"];
|
|
|
|
[representation replaceString: @"\f" withString: @"\\f"];
|
|
|
|
[representation replaceString: @"\n" withString: @"\\n"];
|
|
|
|
[representation replaceString: @"\r" withString: @"\\r"];
|
|
|
|
[representation replaceString: @"\t" withString: @"\\t"];
|
|
|
|
|
|
|
|
return [NSString stringWithFormat: @"\"%@\"", representation];
|
|
|
|
}
|
|
|
|
|
2009-01-07 17:45:15 +01:00
|
|
|
- (NSString *) jsonRepresentation
|
|
|
|
{
|
|
|
|
return [self doubleQuotedString];
|
|
|
|
}
|
|
|
|
|
2010-01-05 23:34:35 +01:00
|
|
|
- (void) _setupCSSEscaping
|
|
|
|
{
|
|
|
|
NSArray *strings, *characters;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
strings = [NSArray arrayWithObjects: @"_U_", @"_D_", @"_H_", @"_A_", @"_S_",
|
2012-02-09 21:34:52 +01:00
|
|
|
@"_C_", @"_CO_", @"_SP_", @"_SQ_", @"_AM_", @"_P_", 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: @"_", @".", @"#", @"@", @"*", @":",
|
2012-02-09 21:34:52 +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
|
|
|
|
{
|
|
|
|
int i, time;
|
|
|
|
|
|
|
|
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;
|
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)
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"json parser: %@", error];
|
2010-11-02 14:19:02 +01:00
|
|
|
[self errorWithFormat: @"original string is: %@", self];
|
2010-11-02 14:16:05 +01:00
|
|
|
object = nil;
|
|
|
|
}
|
2010-10-26 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2011-02-02 02:58:56 +01:00
|
|
|
- (NSString *) asCryptStringUsingSalt: (NSString *) theSalt
|
2010-12-29 14:01:16 +01:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
// The salt is weak here, but who cares anyway, crypt should not
|
|
|
|
// be used anymore
|
2011-04-05 15:34:55 +02:00
|
|
|
buf = crypt([self UTF8String], [theSalt UTF8String]);
|
2010-12-29 14:01:16 +01:00
|
|
|
return [NSString stringWithUTF8String: buf];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) asMD5String
|
|
|
|
{
|
|
|
|
unsigned char md[MD5_DIGEST_LENGTH];
|
|
|
|
char buf[80];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(md, 0, MD5_DIGEST_LENGTH);
|
|
|
|
memset(buf, 0, 80);
|
|
|
|
|
|
|
|
EVP_Digest((const void *) [self UTF8String], strlen([self UTF8String]), md, NULL, EVP_md5(), NULL);
|
|
|
|
for (i = 0; i < MD5_DIGEST_LENGTH; i++)
|
|
|
|
sprintf(&(buf[i*2]), "%02x", md[i]);
|
|
|
|
|
|
|
|
return [NSString stringWithUTF8String: buf];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) asSHA1String
|
|
|
|
{
|
|
|
|
unsigned char sha[SHA_DIGEST_LENGTH];
|
|
|
|
char buf[80];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(sha, 0, SHA_DIGEST_LENGTH);
|
|
|
|
memset(buf, 0, 80);
|
|
|
|
|
|
|
|
SHA1((const void *)[self UTF8String], strlen([self UTF8String]), sha);
|
|
|
|
for (i = 0; i < SHA_DIGEST_LENGTH; i++)
|
|
|
|
sprintf(&(buf[i*2]), "%02x", sha[i]);
|
|
|
|
|
|
|
|
return [NSString stringWithUTF8String: buf];
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-07-29 00:43:28 +02:00
|
|
|
@end
|