tweak parsing end of headers

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@33734 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2011-08-14 18:38:13 +00:00
parent 209d75627d
commit 4e5360267c
2 changed files with 124 additions and 72 deletions

View file

@ -1,3 +1,9 @@
2011-08-06 Richard Frith-Macdonald <rfm@gnu.org>
* Source/Additions/GSMime.m:
Attempt fix for recording of excess data in the case where we are
parsing mime headers only and the headers are incomplete.
2011-08-13 15:28 David Chisnall <theraven@gna.org> 2011-08-13 15:28 David Chisnall <theraven@gna.org>
* libs/base/trunk/Source/NSBundle.m, * libs/base/trunk/Source/NSBundle.m,

View file

@ -1522,6 +1522,7 @@ wordData(NSString *word)
[self parseHeaders: [NSData dataWithBytes: "\r\n\r\n" length: 4] [self parseHeaders: [NSData dataWithBytes: "\r\n\r\n" length: 4]
remaining: 0]; remaining: 0];
flags.wantEndOfLine = 0; flags.wantEndOfLine = 0;
flags.excessData = 0;
flags.inBody = 0; flags.inBody = 0;
flags.complete = 1; /* Finished parsing */ flags.complete = 1; /* Finished parsing */
return NO; /* Want no more data */ return NO; /* Want no more data */
@ -1535,11 +1536,13 @@ wordData(NSString *word)
[data appendData: d]; [data appendData: d];
bytes = (unsigned char*)[data bytes]; bytes = (unsigned char*)[data bytes];
dataEnd = [data length]; dataEnd = [data length];
d = nil;
} }
else else
{ {
NSUInteger i = NSMaxRange(r); NSUInteger i = NSMaxRange(r);
i -= [data length]; // Bytes to append to headers
[data appendBytes: [d bytes] length: i]; [data appendBytes: [d bytes] length: i];
bytes = (unsigned char*)[data bytes]; bytes = (unsigned char*)[data bytes];
dataEnd = [data length]; dataEnd = [data length];
@ -3035,98 +3038,141 @@ unfold(const unsigned char *src, const unsigned char *end, BOOL *folded)
* range at index NSNotFound.<br /> * range at index NSNotFound.<br />
* Permits a bare LF as a line terminator for maximum compatibility.<br /> * Permits a bare LF as a line terminator for maximum compatibility.<br />
* Also checks for an empty line overlapping the existing data and the * Also checks for an empty line overlapping the existing data and the
* new data. * new data.<br />
* Also, handles the special case of an empty line and no further headers.
*/ */
- (NSRange) _endOfHeaders: (NSData*)newData - (NSRange) _endOfHeaders: (NSData*)newData
{
unsigned nl = [newData length];
if (nl > 0)
{ {
unsigned int ol = [data length]; unsigned int ol = [data length];
unsigned int nl = [newData length];
unsigned int len = ol + nl;
unsigned int pos = ol;
const unsigned char *op = (const unsigned char*)[data bytes];
const unsigned char *np = (const unsigned char*)[newData bytes]; const unsigned char *np = (const unsigned char*)[newData bytes];
char c;
#define C(X) ((X) < ol ? op[(X)] : np[(X)-ol])
if (ol > 0) if (ol > 0)
{ {
const unsigned char *op = (const unsigned char*)[data bytes]; /* Find the start of any trailing CRLF or LF sequence we have already
* checked.
if (np[0] == '\r' && nl > 1 && np[1] == '\n')
{
/* We have a CRLF in the new data, so we check for a
* newline at the end of the old data
*/ */
if (op[ol-1] == '\n') while (pos > 0)
{ {
return NSMakeRange(0, 2); c = C(pos - 1);
if (c != '\r' && c != '\n')
{
break;
}
pos--;
} }
} }
else if (np[0] == '\n')
{ /* Check for a document with no headera
if (op[ol-1] == '\n')
{
/* LF in old and LF in new data ... empty line.
*/ */
return NSMakeRange(0, 1); if (0 == pos)
}
else if (op[ol-1] == '\r')
{ {
/* We have a newline crossing the boundary of old and if (len < 1)
* new data (CR in the old data and LF in new data). {
return NSMakeRange(NSNotFound, 0);
}
c = C(0);
if ('\n' == c)
{
return NSMakeRange(0, 1); // no headers ... just an LF.
}
if (len < 2)
{
return NSMakeRange(NSNotFound, 0);
}
if ('\r' == c && '\n' == C(1))
{
return NSMakeRange(0, 2); // no headers ... just a CRLF.
}
}
/* Now check for pairs of line ends overlapping the old and new data
*/ */
if (ol > 1 && op[ol-2] == '\n') if (pos < ol)
{ {
return NSMakeRange(0, 1); if (pos >= len - 2)
{
return NSMakeRange(NSNotFound, 0);
} }
else if (nl > 1) c = C(pos);
if ('\n' == c)
{ {
if (np[1] == '\n') char c1 = C(pos + 1);
if ('\n' == c1)
{ {
return NSMakeRange(0, 2); return NSMakeRange(pos, 2); // LFLF
} }
else if (nl > 2 && np[1] == '\r' && np[2] == '\n') if ('\r' == c1 && pos < len - 3 && '\n' == C(pos + 2))
{ {
return NSMakeRange(0, 3); return NSMakeRange(pos, 3); // LFCRLF
} }
} }
else if ('\r' == c)
{
char c1 = C(pos + 1);
if ('\n' == c1 && pos < len - 3)
{
char c2 = C(pos + 2);
if ('\n' == c2)
{
return NSMakeRange(pos, 3); // CRLFLF
}
if ('\r' == c2 && pos < len - 4 && '\n' == C(pos + 3))
{
return NSMakeRange(pos, 4); // CRLFCRLF
}
} }
} }
} }
if (nl >= 2) /* Now check for end of headers in new data.
*/
pos = 0;
while (pos < nl - 2)
{ {
unsigned int i; c = np[pos];
if ('\n' == c)
{
char c1 = np[pos + 1];
for (i = 0; i < nl; i++) if ('\n' == c1)
{ {
if (np[i] == '\r') return NSMakeRange(pos + ol, 2); // LFLF
}
if ('\r' == c1 && pos < nl - 3 && '\n' == np[pos + 2])
{ {
unsigned l = (nl - i); return NSMakeRange(pos + ol, 3); // LFCRLF
}
}
else if ('\r' == c)
{
char c1 = np[pos + 1];
if (l >= 4 && np[i+1] == '\n' && np[i+2] == '\r' if ('\n' == c1 && pos < nl - 3)
&& np[i+3] == '\n')
{ {
return NSMakeRange(i, 4); char c2 = np[pos + 2];
}
if (l >= 3 && np[i+1] == '\n' && np[i+2] == '\n')
{
return NSMakeRange(i, 3);
}
}
else if (np[i] == '\n')
{
unsigned l = (nl - i);
if (l >= 3 && np[i+1] == '\r' && np[i+2] == '\n') if ('\n' == c2)
{ {
return NSMakeRange(i, 3); return NSMakeRange(pos + ol, 3); // CRLFLF
} }
if (l >= 2 && np[i+1] == '\n') if ('\r' == c2 && pos < nl - 4 && '\n' == np[pos + 3])
{ {
return NSMakeRange(i, 2); return NSMakeRange(pos + ol, 4); // CRLFCRLF
} }
} pos++;
} }
} }
pos++;
} }
return NSMakeRange(NSNotFound, 0); return NSMakeRange(NSNotFound, 0);