From 868c7b19cd4cb5281e5b65d85f19f723978762b5 Mon Sep 17 00:00:00 2001 From: Wolfgang Sourdeau Date: Thu, 6 Sep 2012 20:54:29 +0000 Subject: [PATCH] Monotone-Parent: f643d3c57f70e9da6cd4bbc6b07967927a9a0d53 Monotone-Revision: 053b5d33cbcf453e7c7216c0b2a168a0f0ffa0d5 Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2012-09-06T20:54:29 Monotone-Branch: ca.inverse.sogo --- ChangeLog | 5 +++ OpenChange/NSString+MAPIStore.h | 2 + OpenChange/NSString+MAPIStore.m | 73 ++++++++++++++++++++++++++++++--- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index a8ec85b14..7a2b719ce 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2012-09-06 Wolfgang Sourdeau + * OpenChange/NSString+MAPIStore.m + (-stringByReplacingPercentEscapesUsingEncoding:): reimplemented + method properly, to work-around the weird behaviour of the + original in GNUstep. + * OpenChange/MAPIStoreMailContext.m (+listContextsForUser:withTDBIndexing:inMemCtx:): we escape the url string of non-ascii folder names. diff --git a/OpenChange/NSString+MAPIStore.h b/OpenChange/NSString+MAPIStore.h index 6db35543c..e5006c2ae 100644 --- a/OpenChange/NSString+MAPIStore.h +++ b/OpenChange/NSString+MAPIStore.h @@ -34,6 +34,8 @@ - (NSData *) convertHexStringToBytes; +- (NSString *) stringByReplacingPercentEscapesUsingEncoding: (NSStringEncoding) encoding; + @end #endif /* NSSTRING+MAPISTORE_H */ diff --git a/OpenChange/NSString+MAPIStore.m b/OpenChange/NSString+MAPIStore.m index 186c30e46..c8e31866e 100644 --- a/OpenChange/NSString+MAPIStore.m +++ b/OpenChange/NSString+MAPIStore.m @@ -67,19 +67,38 @@ return unicode; } +- (char) _decodeHexByte: (char) byteChar +{ + char newByte; + + if (byteChar >= 48 && byteChar <= 57) + newByte = (uint8_t) byteChar - 48; + else if (byteChar >= 65 && byteChar <= 70) + newByte = (uint8_t) byteChar - 55; + else if (byteChar >= 97 && byteChar <= 102) + newByte = (uint8_t) byteChar - 87; + else + newByte = -1; + + return newByte; +} + - (BOOL) _decodeHexByte: (uint8_t *) byte atPos: (NSUInteger) pos { BOOL error = NO; + char newByte; unichar byteChar; byteChar = [self characterAtIndex: pos]; - if (byteChar >= 48 && byteChar <= 57) - *byte = (uint8_t) byteChar - 48; - else if (byteChar >= 65 && byteChar <= 70) - *byte = (uint8_t) byteChar - 55; - else if (byteChar >= 97 && byteChar <= 102) - *byte = (uint8_t) byteChar - 87; + if (byteChar < 256) + { + newByte = [self _decodeHexByte: (char) byteChar]; + if (newByte == -1) + error = YES; + else + *byte = newByte; + } else error = YES; @@ -132,4 +151,46 @@ return decoded; } +- (NSString *) stringByReplacingPercentEscapesUsingEncoding: (NSStringEncoding) encoding +{ + NSString *newString; + NSData *data; + NSUInteger count, length, newCount; + const char *bytes; + char *newBytes; + char newByte0, newByte1; + + data = [self dataUsingEncoding: NSASCIIStringEncoding]; + length = [data length]; + bytes = [data bytes]; + newBytes = NSZoneMalloc (NULL, sizeof (char) * length); + newCount = 0; + for (count = 0; count < length; count++) + { + if (bytes[count] == '%') + { + newByte0 = [self _decodeHexByte: bytes[count+1]]; + newByte1 = [self _decodeHexByte: bytes[count+2]]; + if ((newByte0 != -1) && (newByte1 != -1)) + { + newBytes[newCount] = (((newByte0 << 4) & 0xf0) + | (newByte1 & 0x0f)); + count += 2; + } + else + newBytes[newCount] = bytes[count]; + } + else + newBytes[newCount] = bytes[count]; + newCount++; + } + + data = [NSData dataWithBytesNoCopy: newBytes length: newCount freeWhenDone: YES]; + newString = [[NSString alloc] + initWithData: data encoding: encoding]; + [newString autorelease]; + + return newString; +} + @end