From 6bed6c0e20bb1eec8968e3823d16c2f5002333b5 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Wed, 29 Jun 2022 16:58:28 -0400 Subject: [PATCH] fix(core): don't remove double quotes when parsing versit strings Fixes #3530 Fixes #3930 --- SOPE/NGCards/NSDictionary+NGCards.m | 2 +- SOPE/NGCards/NSString+NGCards.h | 2 +- SOPE/NGCards/NSString+NGCards.m | 66 ++++++++++++++++------------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/SOPE/NGCards/NSDictionary+NGCards.m b/SOPE/NGCards/NSDictionary+NGCards.m index 22ce351da..8b197df8e 100644 --- a/SOPE/NGCards/NSDictionary+NGCards.m +++ b/SOPE/NGCards/NSDictionary+NGCards.m @@ -59,7 +59,7 @@ && ![subValue hasSuffix: @"\""]) subValue = [NSString stringWithFormat: @"\"%@\"", subValue]; - escaped = [subValue escapedForCards]; + escaped = [subValue escapedForCardsAsAttributes: asAttributes]; if ([escaped length] > 0) { [aString appendString: escaped]; diff --git a/SOPE/NGCards/NSString+NGCards.h b/SOPE/NGCards/NSString+NGCards.h index b4cca93ff..4d31cd014 100644 --- a/SOPE/NGCards/NSString+NGCards.h +++ b/SOPE/NGCards/NSString+NGCards.h @@ -34,7 +34,7 @@ - (NSString *) foldedForVersitCards; - (NSArray *) asCardAttributeValues; -- (NSString *) escapedForCards; +- (NSString *) escapedForCardsAsAttributes: (BOOL) asAttributes; - (NSString *) rfc822Email; - (NSTimeInterval) durationAsTimeInterval; diff --git a/SOPE/NGCards/NSString+NGCards.m b/SOPE/NGCards/NSString+NGCards.m index 834df439a..b72e0ea51 100644 --- a/SOPE/NGCards/NSString+NGCards.m +++ b/SOPE/NGCards/NSString+NGCards.m @@ -69,7 +69,7 @@ NSString *newString; unichar *characters, *currentChar, *lastChar, *newPart, *newCurrentChar; NSUInteger max; - BOOL isEscaped; + BOOL isEscaped, isQuoted; values = [NSMutableArray array]; @@ -83,10 +83,23 @@ newCurrentChar = newPart; isEscaped = NO; + isQuoted = (max > 1 && [self hasPrefix: @"\""] && [self hasSuffix: @"\""]); + if (isQuoted) + { + // Remove leading and trailing quotes + currentChar++; + lastChar--; + } while (currentChar < lastChar) { - if (isEscaped) + if (isQuoted) + { + // Don't escape characters + *newCurrentChar = *currentChar; + newCurrentChar++; + } + else if (isEscaped) { if (*currentChar == 'n' || *currentChar == 'N') *newCurrentChar = '\n'; @@ -107,10 +120,10 @@ isEscaped = YES; else if (*currentChar == ',') { - newString = [[NSString alloc] - initWithCharactersNoCopy: newPart - length: (newCurrentChar - newPart) - freeWhenDone: YES]; + // The comma is a value delimiter + newString = [[NSString alloc] initWithCharactersNoCopy: newPart + length: (newCurrentChar - newPart) + freeWhenDone: YES]; [values addObject: newString]; [newString release]; newPart = NSZoneMalloc (NULL, max * sizeof (unichar)); @@ -125,10 +138,9 @@ currentChar++; } - newString = [[NSString alloc] - initWithCharactersNoCopy: newPart - length: (newCurrentChar - newPart) - freeWhenDone: YES]; + newString = [[NSString alloc] initWithCharactersNoCopy: newPart + length: (newCurrentChar - newPart) + freeWhenDone: YES]; [values addObject: newString]; [newString release]; @@ -137,7 +149,7 @@ return values; } -- (NSString *) escapedForCards +- (NSString *) escapedForCardsAsAttributes: (BOOL) asAttributes { NSMutableString *string; unsigned int len, i; @@ -152,33 +164,29 @@ { c = [self characterAtIndex: i]; - if (isQuoted) - { - if (c == '"') - isQuoted = NO; - - [string appendFormat: @"%C", c]; - continue; - } + if (!isQuoted || !asAttributes) + switch (c) + { + case ',': + [string appendString: @"\\,"]; + continue; + //case ':': + // [string appendString: @"\\:"]; + //break; + case ';': + [string appendString: @"\\;"]; + continue; + } switch (c) { case '"': - isQuoted = YES; + isQuoted = !isQuoted; [string appendFormat: @"%C", c]; break; case '\\': [string appendString: @"\\\\"]; break; - case ',': - [string appendString: @"\\,"]; - break; - //case ':': - // [string appendString: @"\\:"]; - //break; - case ';': - [string appendString: @"\\;"]; - break; case'\n': [string appendString: @"\\n"]; break;