Browse code

Handle para 4 of RFC2231

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@1068 77e5149b-7576-45b1-b177-96237e5ba77b

Nigel Horne authored on 2004/11/08 01:42:55
Showing 3 changed files
... ...
@@ -1,3 +1,7 @@
1
+Sun Nov  7 16:42:10 GMT 2004 (njh)
2
+----------------------------------
3
+  * libclamav/message.c:	Handle paragraph 4 of RFC2231
4
+
1 5
 Sat Nov  6 21:45:21 GMT 2004 (njh)
2 6
 ----------------------------------
3 7
   * libclamav/mbox.c:	Fix possible crash when handling illegal RFC2047 header
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: mbox.c,v $
20
+ * Revision 1.166  2004/11/07 16:39:00  nigelhorne
21
+ * Handle para 4 of RFC2231
22
+ *
20 23
  * Revision 1.165  2004/11/06 21:43:23  nigelhorne
21 24
  * Fix possible segfault in handling broken RFC2047 headers
22 25
  *
... ...
@@ -483,7 +486,7 @@
483 483
  * Compilable under SCO; removed duplicate code with message.c
484 484
  *
485 485
  */
486
-static	char	const	rcsid[] = "$Id: mbox.c,v 1.165 2004/11/06 21:43:23 nigelhorne Exp $";
486
+static	char	const	rcsid[] = "$Id: mbox.c,v 1.166 2004/11/07 16:39:00 nigelhorne Exp $";
487 487
 
488 488
 #if HAVE_CONFIG_H
489 489
 #include "clamav-config.h"
... ...
@@ -2421,8 +2424,6 @@ parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const c
2421 2421
 	if(copy == NULL)
2422 2422
 		return -1;
2423 2423
 
2424
-	ptr = copy;
2425
-
2426 2424
 	switch(commandNumber) {
2427 2425
 		case CONTENT_TYPE:
2428 2426
 			/*
... ...
@@ -2751,10 +2752,12 @@ rfc2047(const char *in)
2751 2751
 			pout += len;
2752 2752
 
2753 2753
 	}
2754
-	if(out)
2755
-		*pout = '\0';
2754
+	if(out == NULL)
2755
+		return NULL;
2756
+
2757
+	*pout = '\0';
2756 2758
 
2757
-	cli_dbgmsg("rfc2047 returns '%s'\n", (out) ? out : "null");
2759
+	cli_dbgmsg("rfc2047 returns '%s'\n", out);
2758 2760
 	return out;
2759 2761
 }
2760 2762
 
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: message.c,v $
20
+ * Revision 1.109  2004/11/07 16:39:00  nigelhorne
21
+ * Handle para 4 of RFC2231
22
+ *
20 23
  * Revision 1.108  2004/10/31 09:28:27  nigelhorne
21 24
  * Improve the handling of blank filenames
22 25
  *
... ...
@@ -321,7 +324,7 @@
321 321
  * uuencodebegin() no longer static
322 322
  *
323 323
  */
324
-static	char	const	rcsid[] = "$Id: message.c,v 1.108 2004/10/31 09:28:27 nigelhorne Exp $";
324
+static	char	const	rcsid[] = "$Id: message.c,v 1.109 2004/11/07 16:39:00 nigelhorne Exp $";
325 325
 
326 326
 #if HAVE_CONFIG_H
327 327
 #include "clamav-config.h"
... ...
@@ -383,6 +386,7 @@ static	const	char	*messageGetArgument(const message *m, int arg);
383 383
 static	void	*messageExport(message *m, const char *dir, void *(*create)(void), void (*destroy)(void *), void (*setFilename)(void *, const char *, const char *), void (*addData)(void *, const unsigned char *, size_t), void *(*exportText)(const text *, void *));
384 384
 static	int	usefulArg(const char *arg);
385 385
 static	void	messageDedup(message *m);
386
+static	char	*rfc2231(const char *in);
386 387
 static	int	simil(const char *str1, const char *str2);
387 388
 
388 389
 /*
... ...
@@ -695,7 +699,7 @@ messageAddArgument(message *m, const char *arg)
695 695
 		m->mimeArguments = ptr;
696 696
 	}
697 697
 
698
-	m->mimeArguments[offset] = strdup(arg);
698
+	m->mimeArguments[offset] = rfc2231(arg);
699 699
 
700 700
 	/*
701 701
 	 * This is terribly broken from an RFC point of view but is useful
... ...
@@ -2531,12 +2535,93 @@ messageDedup(message *m)
2531 2531
 }
2532 2532
 
2533 2533
 /*
2534
+ * Handle RFC2231 encoding. Returns a malloc'd buffer that the caller must
2535
+ * free, or NULL on error.
2536
+ *
2537
+ * TODO: Currently only handles paragraph 4 of RFC2231 e.g.
2538
+ *	 protocol*=ansi-x3.4-1968''application%2Fpgp-signature;
2539
+ */
2540
+static char *
2541
+rfc2231(const char *in)
2542
+{
2543
+	char *out;
2544
+	char *ptr;
2545
+	char *ret;
2546
+	enum { LANGUAGE, CHARSET, CONTENTS } field = LANGUAGE;
2547
+
2548
+	ptr = strstr(in, "*=");
2549
+
2550
+	if(ptr == NULL)	/* quick return */
2551
+		return strdup(in);
2552
+
2553
+	cli_dbgmsg("rfc2231 '%s'\n", in);
2554
+
2555
+	ret = cli_malloc(strlen(in) + 1);
2556
+
2557
+	if(ret == NULL)
2558
+		return NULL;
2559
+
2560
+	for(out = ret; in != ptr; in++)
2561
+		*out++ = *in;
2562
+
2563
+	*out++ = '=';
2564
+
2565
+	/*
2566
+	 * We don't do anything with the language and character set, just skip
2567
+	 * over them!
2568
+	 */
2569
+	while(*in) {
2570
+		switch(field) {
2571
+			case LANGUAGE:
2572
+				if(*in == '\'')
2573
+					field = CHARSET;
2574
+				break;
2575
+			case CHARSET:
2576
+				if(*in == '\'')
2577
+					field = CONTENTS;
2578
+				break;
2579
+			case CONTENTS:
2580
+				if(*in == '%') {
2581
+					unsigned char byte;
2582
+
2583
+					if((*++in == '\0') || (*in == '\n'))
2584
+						break;
2585
+
2586
+					byte = hex(*in);
2587
+
2588
+					if((*++in == '\0') || (*in == '\n')) {
2589
+						*out++ = byte;
2590
+						break;
2591
+					}
2592
+
2593
+					byte <<= 4;
2594
+					byte += hex(*in);
2595
+					*out++ = byte;
2596
+				} else
2597
+					*out++ = *in;
2598
+		}
2599
+		in++;
2600
+	}
2601
+
2602
+	if(field != CONTENTS) {
2603
+		cli_warnmsg("Invalid RFC2231 header: '%s'\n", in);
2604
+		free(ret);
2605
+		return strdup("");
2606
+	}
2607
+				
2608
+	*out = '\0';
2609
+
2610
+	cli_dbgmsg("rfc2231 returns '%s'\n", ret);
2611
+
2612
+	return ret;
2613
+}
2614
+
2615
+/*
2534 2616
  * common/simil:
2535 2617
  *	From Computing Magazine 20/8/92
2536 2618
  * Returns %ge number from 0 to 100 - how similar are 2 strings?
2537 2619
  * 100 for exact match, < for error
2538 2620
  */
2539
-
2540 2621
 struct	pstr_list {	/* internal stack */
2541 2622
 	char	*d1;
2542 2623
 	struct	pstr_list	*next;