@@ -1239,35 +1239,25 @@ static unsafe bool IsBoundary (byte* text, int length, Boundary boundary, out bo
1239
1239
{
1240
1240
final = false ;
1241
1241
1242
- if ( boundary . Length > length )
1243
- return false;
1242
+ if ( boundary . IsMboxMarker ) {
1243
+ // for mbox markers, we only care about the first 5 characters
1244
+ if ( length < boundary . Length )
1245
+ return false;
1246
+
1247
+ length = boundary . Length ;
1248
+ } else {
1249
+ // if the length isn't exactly equal to either the normal or final boundary lengths,
1250
+ // then this clearly isn't a match
1251
+ if ( boundary . Length != length && boundary . FinalLength != length )
1252
+ return false;
1253
+ }
1244
1254
1245
1255
fixed ( byte * boundaryptr = boundary . Marker ) {
1246
1256
// make sure that the text matches the boundary
1247
- if ( ! CStringsEqual ( text , boundaryptr , boundary . Length ) )
1257
+ if ( ! CStringsEqual ( text , boundaryptr , length ) )
1248
1258
return false;
1249
1259
1250
- // if this is an mbox marker, we're done
1251
- if ( boundary . IsMboxMarker ) {
1252
- final = true;
1253
- return true;
1254
- }
1255
-
1256
- byte* inptr = text + boundary . Length ;
1257
- byte * inend = text + length ;
1258
-
1259
- if ( length >= boundary . FinalLength && inptr [ 0 ] == ( byte ) '-' && inptr [ 1 ] == ( byte ) '-' ) {
1260
- final = true ;
1261
- inptr += 2 ;
1262
- }
1263
-
1264
- // the boundary may optionally be followed by lwsp
1265
- while ( inptr < inend ) {
1266
- if ( ! ( * inptr ) . IsWhitespace ( ) )
1267
- return false;
1268
-
1269
- inptr++ ;
1270
- }
1260
+ final = length == boundary . FinalLength ;
1271
1261
}
1272
1262
1273
1263
return true ;
@@ -1279,20 +1269,30 @@ unsafe BoundaryType CheckBoundary (int startIndex, byte* start, int length)
1279
1269
return BoundaryType. None ;
1280
1270
1281
1271
if ( boundaries != null ) {
1272
+ byte * end = start + length ;
1282
1273
bool final ;
1283
1274
1275
+ // ignore trailing whitespace characters
1276
+ if ( end [ - 1 ] == ( byte ) '\r ' )
1277
+ end-- ;
1278
+
1279
+ while ( end > start && end [ - 1 ] . IsWhitespace ( ) )
1280
+ end-- ;
1281
+
1282
+ int matchLength = ( int ) ( end - start ) ;
1283
+
1284
1284
currentBoundary = boundaries ;
1285
1285
1286
1286
if ( ! currentBoundary . IsMboxMarker ) {
1287
1287
// check immediate boundary
1288
- if ( IsBoundary ( start , length , currentBoundary , out final ) )
1288
+ if ( IsBoundary ( start , matchLength , currentBoundary , out final ) )
1289
1289
return final ? BoundaryType . ImmediateEndBoundary : BoundaryType . ImmediateBoundary ;
1290
1290
1291
1291
currentBoundary = currentBoundary . Next ;
1292
1292
1293
1293
// check parent boundaries
1294
1294
while ( currentBoundary != null && ! currentBoundary . IsMboxMarker ) {
1295
- if ( IsBoundary ( start , length , currentBoundary , out final ) )
1295
+ if ( IsBoundary ( start , matchLength , currentBoundary , out final ) )
1296
1296
return final ? BoundaryType . ParentEndBoundary : BoundaryType . ParentBoundary ;
1297
1297
1298
1298
currentBoundary = currentBoundary . Next ;
@@ -1303,7 +1303,7 @@ unsafe BoundaryType CheckBoundary (int startIndex, byte* start, int length)
1303
1303
// now it is time to check the mbox From-marker
1304
1304
long curOffset = contentEnd > 0 ? GetOffset ( startIndex ) : contentEnd ;
1305
1305
1306
- if ( curOffset >= contentEnd && IsBoundary ( start , length , currentBoundary , out final ) )
1306
+ if ( curOffset >= contentEnd && IsBoundary ( start , matchLength , currentBoundary , out final ) )
1307
1307
return BoundaryType. ParentEndBoundary ;
1308
1308
}
1309
1309
}
0 commit comments