Browse code

NEW_WORLD: Improved handling of files containing CRs

git-svn: trunk@1642

Nigel Horne authored on 2005/07/03 06:05:36
Showing 2 changed files
... ...
@@ -1,3 +1,8 @@
1
+Sat Jul  2 22:05:03 BST 2005 (njh)
2
+----------------------------------
3
+  * libclamav/mbox.c:	NEW_WORLD: Improved the handling of files which contain
4
+				carriage returns
5
+
1 6
 Thu Jun 30 15:51:54 BST 2005 (njh)
2 7
 ----------------------------------
3 8
   * clamav-milter:	Honour LogClean
... ...
@@ -15,7 +15,7 @@
15 15
  *  along with this program; if not, write to the Free Software
16 16
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 17
  */
18
-static	char	const	rcsid[] = "$Id: mbox.c,v 1.250 2005/06/19 16:04:43 nigelhorne Exp $";
18
+static	char	const	rcsid[] = "$Id: mbox.c,v 1.251 2005/07/02 21:04:04 nigelhorne Exp $";
19 19
 
20 20
 #if HAVE_CONFIG_H
21 21
 #include "clamav-config.h"
... ...
@@ -492,7 +492,7 @@ cli_mbox(const char *dir, int desc, unsigned int options)
492 492
 			long b64size = scanelem->size;
493 493
 
494 494
 			cli_dbgmsg("b64size = %lu\n", b64size);
495
-			while(*b64start != '\n') {
495
+			while((*b64start != '\n') && (*b64start != '\r')) {
496 496
 				b64start++;
497 497
 				b64size--;
498 498
 			}
... ...
@@ -503,14 +503,15 @@ cli_mbox(const char *dir, int desc, unsigned int options)
503 503
 				if(*b64start == ';') {
504 504
 					b64start++;
505 505
 					b64size--;
506
-				} else if(*b64start == '\n') {
507
-					b64start++;
508
-					b64size--;
509
-					if((*b64start == '\n') || (*b64start == '\r')) {
510
-						b64start++;
511
-						b64size--;
512
-						break;
513
-					}
506
+				} else if((memcmp(b64start, "\n\n", 2) == 0) ||
507
+					  (memcmp(b64start, "\r\r", 2) == 0)) {
508
+					b64start += 2;
509
+					b64size -= 2;
510
+					break;
511
+				} else if(memcmp(b64start, "\r\n\r\n", 4) == 0) {
512
+					b64start += 4;
513
+					b64size -= 4;
514
+					break;
514 515
 				}
515 516
 				b64start++;
516 517
 				b64size--;
... ...
@@ -524,6 +525,7 @@ cli_mbox(const char *dir, int desc, unsigned int options)
524 524
 				}
525 525
 
526 526
 			if(b64size > 0L) {
527
+				int lastline;
527 528
 				cli_dbgmsg("cli_mbox: decoding %ld base64 bytes\n", b64size);
528 529
 
529 530
 				line = NULL;
... ...
@@ -533,9 +535,11 @@ cli_mbox(const char *dir, int desc, unsigned int options)
533 533
 					return CL_EMEM;
534 534
 				messageSetEncoding(m, "base64");
535 535
 
536
+				lastline = 0;
537
+
536 538
 				do {
537 539
 					int length = 0;
538
-					char *newline;
540
+					char *newline, *equal;
539 541
 
540 542
 					/*printf("%ld: ", b64size); fflush(stdout);*/
541 543
 
... ...
@@ -554,18 +558,25 @@ cli_mbox(const char *dir, int desc, unsigned int options)
554 554
 					memcpy(line, b64start, length);
555 555
 					line[length] = '\0';
556 556
 
557
+					equal = strchr(line, '=');
558
+					if(equal) {
559
+						lastline++;
560
+						*equal = '\0';
561
+					}
557 562
 					/*puts(line);*/
558 563
 
559 564
 					if(messageAddStr(m, line) < 0)
560 565
 						break;
561 566
 
562 567
 					if((b64size > 0) && (*ptr == '\r')) {
563
-						ptr++;
568
+						b64start = ++ptr;
569
+						--b64size;
570
+					}
571
+					if((b64size > 0) && (*ptr == '\n')) {
572
+						b64start = ++ptr;
564 573
 						--b64size;
565 574
 					}
566
-					b64start = ++ptr;
567
-					--b64size;
568
-					if(strchr(line, '='))
575
+					if(lastline)
569 576
 						break;
570 577
 				} while(b64size > 0L);
571 578
 
... ...
@@ -594,7 +605,7 @@ cli_mbox(const char *dir, int desc, unsigned int options)
594 594
 				if(*quotedstart == ';') {
595 595
 					quotedstart++;
596 596
 					quotedsize--;
597
-				} else if(*quotedstart == '\n') {
597
+				} else if((*quotedstart == '\n') || (*quotedstart == '\r')) {
598 598
 					quotedstart++;
599 599
 					quotedsize--;
600 600
 					if((*quotedstart == '\n') || (*quotedstart == '\r')) {
... ...
@@ -649,11 +660,13 @@ cli_mbox(const char *dir, int desc, unsigned int options)
649 649
 						break;
650 650
 
651 651
 					if((quotedsize > 0) && (*ptr == '\r')) {
652
-						ptr++;
652
+						quotedstart = ++ptr;
653
+						--quotedsize;
654
+					}
655
+					if((quotedsize > 0) && (*ptr == '\n')) {
656
+						quotedstart = ++ptr;
653 657
 						--quotedsize;
654 658
 					}
655
-					quotedstart = ++ptr;
656
-					--quotedsize;
657 659
 				} while(quotedsize > 0L);
658 660
 
659 661
 				free(line);