Exactly match the percent escapes used by OSX

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@31501 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2010-10-12 10:59:05 +00:00
parent bdaf339b92
commit 69322fcaca
2 changed files with 74 additions and 3 deletions

View file

@ -40,6 +40,7 @@ function may be incorrect
#define EXPOSE_NSURL_IVARS 1
#import "Foundation/NSArray.h"
#import "Foundation/NSCoder.h"
#import "Foundation/NSData.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSException.h"
#import "Foundation/NSFileManager.h"
@ -54,6 +55,73 @@ function may be incorrect
NSString * const NSURLErrorDomain = @"NSURLErrorDomain";
NSString * const NSErrorFailingURLStringKey = @"NSErrorFailingURLStringKey";
@interface NSString (NSURLPrivate)
- (NSString*) _stringByAddingPercentEscapes;
@end
@implementation NSString (NSURLPrivate)
/* Like the normal percent escape method, but with additional characters
* escaped.
*/
- (NSString*) _stringByAddingPercentEscapes
{
NSData *data = [self dataUsingEncoding: NSUTF8StringEncoding];
NSString *s = nil;
if (data != nil)
{
unsigned char *src = (unsigned char*)[data bytes];
unsigned int slen = [data length];
unsigned char *dst;
unsigned int spos = 0;
unsigned int dpos = 0;
dst = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), slen * 3);
while (spos < slen)
{
unsigned char c = src[spos++];
unsigned int hi;
unsigned int lo;
if (c <= 32
|| c > 126
|| c == 34
|| c == 35
|| c == 37
|| c == 59
|| c == 60
|| c == 62
|| c == 63
|| c == 91
|| c == 92
|| c == 93
|| c == 94
|| c == 96
|| c == 123
|| c == 124
|| c == 125)
{
dst[dpos++] = '%';
hi = (c & 0xf0) >> 4;
dst[dpos++] = (hi > 9) ? 'A' + hi - 10 : '0' + hi;
lo = (c & 0x0f);
dst[dpos++] = (lo > 9) ? 'A' + lo - 10 : '0' + lo;
}
else
{
dst[dpos++] = c;
}
}
s = [[NSString alloc] initWithBytes: dst
length: dpos
encoding: NSASCIIStringEncoding];
NSZoneFree(NSDefaultMallocZone(), dst);
IF_NO_GC([s autorelease];)
}
return s;
}
@end
/*
* Structure describing a URL.
* All the char* fields may be NULL pointers, except path, which
@ -542,8 +610,7 @@ static unsigned urlAlign;
{
NSString *aUrlString = [NSString alloc];
aPath
= [aPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
aPath = [aPath _stringByAddingPercentEscapes];
if ([aHost length] > 0)
{
NSRange r = [aHost rangeOfString: @"@"];