fixup for encoded words

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@37740 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2014-03-11 09:46:54 +00:00
parent 768a04455c
commit 60d00a2402
5 changed files with 85 additions and 28 deletions

View file

@ -1,16 +1,24 @@
2014-03-11 Richard Frith-Macdonald <rfm@gnu.org>
* Source/Additions/GSMime.m:
When generating document with a header containing adjacent non-ascii
words which need to be encoded, omit the space between those words
for consistency with the change on Fe 3rd.
2014-02-18 Fred Kiefer <FredKiefer@gmx.de>
* Source/NSDateFormatter.m
(-initWithDateFormat:allowNaturalLanguage:):
Use method -setDateFormat: to get correct behaviour.
* Source/NSDateFormatter.m (-setDateFormat:): User ASSIGNCOPY for _dateFormat.
* Source/NSDateFormatter.m (-setDateFormat:):
Use ASSIGNCOPY for _dateFormat.
2014-02-14 Quentin Mathe <quentin.mathe@gmail.com>
* Headers/Foundation/NSNotification.h
* Source/NSNotificationCenter.m
(-addObserverForName:selector:queue:usingBlock:): Added new Mac OS X 10.6
method.
(-addObserverForName:selector:queue:usingBlock:):
Added new Mac OS X 10.6 method.
2014-02-14 Quentin Mathe <quentin.mathe@gmail.com>

View file

@ -509,7 +509,7 @@ selectCharacterSet(NSString *str, NSData **d)
* For an ascii word, we just return the data.
*/
static NSData*
wordData(NSString *word)
wordData(NSString *word, BOOL *encoded)
{
NSData *d = nil;
NSString *charset;
@ -517,6 +517,7 @@ wordData(NSString *word)
charset = selectCharacterSet(word, &d);
if ([charset isEqualToString: @"us-ascii"] == YES)
{
*encoded = NO;
return d;
}
else
@ -525,6 +526,7 @@ wordData(NSString *word)
char buf[len + 1];
NSMutableData *md;
*encoded = YES;
[charset getCString: buf
maxLength: len + 1
encoding: NSISOLatin1StringEncoding];
@ -3734,13 +3736,18 @@ static NSUInteger
appendString(NSMutableData *m, NSUInteger offset, NSUInteger fold,
NSString *str, BOOL *ok)
{
NSUInteger pos = 0;
NSUInteger size = [str length];
NSUInteger pos = 0;
NSUInteger size = [str length];
BOOL hadEncodedWord = NO;
BOOL needSpace = NO;
*ok = YES;
while (pos < size)
{
NSRange r = NSMakeRange(pos, size - pos);
NSString *s = nil;
NSData *d = nil;
BOOL e = NO;
r = [str rangeOfCharacterFromSet: whitespace
options: NSLiteralSearch
@ -3749,7 +3756,7 @@ appendString(NSMutableData *m, NSUInteger offset, NSUInteger fold,
{
/* Found space at the start of the string, so we reduce
* it to a single space in the output, or omit it entirely
* if the string is nothing but space.
* if the string is contains nothing more but space.
*/
pos++;
while (pos < size
@ -3759,37 +3766,60 @@ appendString(NSMutableData *m, NSUInteger offset, NSUInteger fold,
}
if (pos < size)
{
offset = appendBytes(m, offset, fold, " ", 1);
needSpace = YES; // We need a space before the next word.
}
}
else if (r.length == 0)
{
NSString *sub;
NSData *d;
/* No space found ... we must output the entire string without
* folding it.
/* No more space found ... we must output the remaining string
* without folding it.
*/
sub = [str substringWithRange: NSMakeRange(pos, size - pos)];
s = [str substringWithRange: NSMakeRange(pos, size - pos)];
pos = size;
d = wordData(sub);
offset = appendBytes(m, offset, fold, [d bytes], [d length]);
d = wordData(s, &e);
}
else
{
NSString *sub;
NSData *d;
/* Output the substring up to the first space.
/* Output the substring up to the next space.
*/
sub = [str substringWithRange: NSMakeRange(pos, r.location - pos)];
s = [str substringWithRange: NSMakeRange(pos, r.location - pos)];
pos = r.location;
d = wordData(sub);
offset = appendBytes(m, offset, fold, [d bytes], [d length]);
d = wordData(s, &e);
}
if (fold > 0 && offset > fold)
if (nil != d)
{
*ok = NO;
/* We have a 'word' to output ... do that after dealing with any
* space needede between the last word and the new one.
*/
if (YES == needSpace)
{
if (YES == e && YES == hadEncodedWord)
{
/* We can't have space between two encoded words, so
* we incorporate the space at the start of the next
* encoded word.
*/
s = [@" " stringByAppendingString: s];
d = wordData(s, &e);
}
else
{
/* Add the needed space before the next word.
*/
offset = appendBytes(m, offset, fold, " ", 1);
if (fold > 0 && offset > fold)
{
*ok = NO;
}
}
needSpace = NO;
}
hadEncodedWord = e;
offset = appendBytes(m, offset, fold, [d bytes], [d length]);
if (fold > 0 && offset > fold)
{
*ok = NO;
}
}
}
return offset;

View file

@ -94,7 +94,7 @@ int main()
data = [[data mutableCopy] autorelease];
[(NSMutableData*)data appendBytes: "\r\n\r\n" length: 4];
[parser parse: data];
doc = [parser document];
doc = [parser mimeDocument];
PASS([[parser excess] length] == 5, "Can detect excess data in multipart");
[parser release];
@ -182,13 +182,26 @@ int main()
doc = [GSMimeParser documentFromData: data];
PASS_EQUAL(idoc, doc, "rawMimeData reproduces document");
/* Test a document containing encoded words in header
/* Test parse of a document containing encoded words in header.
* Use JavaMail encoded words (different format from those GSMime
* produces).
*/
data = [NSData dataWithContentsOfFile: @"mime11.dat"];
idoc = exact(0, data);
doc = [GSMimeParser documentFromData: data];
PASS_EQUAL(idoc, doc, "mime11.dat documents are the same");
/* Test a document with adjacent encoded words in headers, as
* produced by GSMime
*/
data = [NSData dataWithContentsOfFile: @"mime12.dat"];
idoc = exact(0, data);
doc = [GSMimeParser documentFromData: data];
PASS_EQUAL(idoc, doc, "mime12.dat documents are the same");
data = [idoc rawMimeData];
doc = [GSMimeParser documentFromData: data];
PASS_EQUAL(idoc, doc, "rawMimeData reproduces document");
[arp release]; arp = nil;
return 0;

View file

@ -0,0 +1,6 @@
MIME-Version: 1.0
Content-Type: text/plain
Subject: Avant de partir, n'oubliez pas de =?iso-8859-1?b?cHLpcGFyZXI=?= votre
=?iso-8859-1?b?c+lqb3Vy?==?iso-8859-1?b?IOA=?= Paris
hello

View file

@ -106,7 +106,7 @@ int main()
PASS ([parser isComplete], "parse is complete");
PASS ([parser isComplete], "parse is complete");
PASS_EQUAL([[parser document] convertToText],
PASS_EQUAL([[parser mimeDocument] convertToText],
@"This is the data in the first chunk\r\nand this is the second one\r\n"
@"consequence", "content correct");