git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@778 77e5149b-7576-45b1-b177-96237e5ba77b
Nigel Horne authored on 2004/08/22 19:37:32... | ... |
@@ -1,3 +1,9 @@ |
1 |
+Sun Aug 22 11:35:30 BST 2004 (njh) |
|
2 |
+---------------------------------- |
|
3 |
+ * libclamav: Save email attachments to disc earlier (as the attachment is |
|
4 |
+ decoded rather than after decoding has finished), this |
|
5 |
+ saves memory at a small speed penalty |
|
6 |
+ |
|
1 | 7 |
Sun Aug 22 02:07:13 CEST 2004 (tk) |
2 | 8 |
---------------------------------- |
3 | 9 |
* freshclam: Support version verification through DNS (DNSDatabaseInfo). |
... | ... |
@@ -16,6 +16,9 @@ |
16 | 16 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | 17 |
* |
18 | 18 |
* $Log: blob.c,v $ |
19 |
+ * Revision 1.15 2004/08/22 10:34:24 nigelhorne |
|
20 |
+ * Use fileblob |
|
21 |
+ * |
|
19 | 22 |
* Revision 1.14 2004/08/01 08:20:58 nigelhorne |
20 | 23 |
* Scan pathnames in Cygwin |
21 | 24 |
* |
... | ... |
@@ -44,17 +47,23 @@ |
44 | 44 |
* Change LOG to Log |
45 | 45 |
* |
46 | 46 |
*/ |
47 |
-static char const rcsid[] = "$Id: blob.c,v 1.14 2004/08/01 08:20:58 nigelhorne Exp $"; |
|
47 |
+static char const rcsid[] = "$Id: blob.c,v 1.15 2004/08/22 10:34:24 nigelhorne Exp $"; |
|
48 | 48 |
|
49 | 49 |
#if HAVE_CONFIG_H |
50 | 50 |
#include "clamav-config.h" |
51 | 51 |
#endif |
52 | 52 |
|
53 |
+#include <stdio.h> |
|
53 | 54 |
#include <stdlib.h> |
54 | 55 |
#include <string.h> |
56 |
+#include <errno.h> |
|
57 |
+ |
|
58 |
+#include <sys/param.h> /* for NAME_MAX */ |
|
59 |
+ |
|
55 | 60 |
#if C_DARWIN |
56 | 61 |
#include <sys/types.h> |
57 | 62 |
#endif |
63 |
+ |
|
58 | 64 |
#include "mbox.h" |
59 | 65 |
#include "blob.h" |
60 | 66 |
#include "others.h" |
... | ... |
@@ -148,7 +157,7 @@ blobGetFilename(const blob *b) |
148 | 148 |
assert(b != NULL); |
149 | 149 |
assert(b->magic == BLOB); |
150 | 150 |
|
151 |
- return(b->name); |
|
151 |
+ return b->name; |
|
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
void |
... | ... |
@@ -286,3 +295,141 @@ blobGrow(blob *b, size_t len) |
286 | 286 |
} |
287 | 287 |
} |
288 | 288 |
} |
289 |
+ |
|
290 |
+fileblob * |
|
291 |
+fileblobCreate(void) |
|
292 |
+{ |
|
293 |
+#ifdef CL_DEBUG |
|
294 |
+ fileblob *fb = (fileblob *)cli_calloc(1, sizeof(fileblob)); |
|
295 |
+ if(fb) |
|
296 |
+ fb->b.magic = BLOB; |
|
297 |
+ cli_dbgmsg("blobCreate\n"); |
|
298 |
+ return fb; |
|
299 |
+#else |
|
300 |
+ return (fileblob *)cli_calloc(1, sizeof(fileblob)); |
|
301 |
+#endif |
|
302 |
+} |
|
303 |
+ |
|
304 |
+void |
|
305 |
+fileblobDestroy(fileblob *fb) |
|
306 |
+{ |
|
307 |
+ assert(fb != NULL); |
|
308 |
+ |
|
309 |
+ if(fb->b.name) { |
|
310 |
+ assert(fb->fp != NULL); |
|
311 |
+ fclose(fb->fp); |
|
312 |
+ free(fb->b.name); |
|
313 |
+ |
|
314 |
+ assert(fb->b.data == NULL); |
|
315 |
+ } else if(fb->b.data) { |
|
316 |
+ cli_errmsg("fileblobDestroy: file not saved: report to bugs@clamav.net\n"); |
|
317 |
+ free(fb->b.data); |
|
318 |
+ } |
|
319 |
+ free(fb); |
|
320 |
+} |
|
321 |
+ |
|
322 |
+void |
|
323 |
+fileblobSetFilename(fileblob *fb, const char *dir, const char *filename) |
|
324 |
+{ |
|
325 |
+ int fd; |
|
326 |
+ const char *suffix; |
|
327 |
+ size_t suffixLen = 0; |
|
328 |
+ char fullname[NAME_MAX + 1]; |
|
329 |
+ |
|
330 |
+ if(fb->b.name) |
|
331 |
+ return; |
|
332 |
+ |
|
333 |
+ /* |
|
334 |
+ * Some programs are broken and use an idea of a ".suffix" |
|
335 |
+ * to determine the file type rather than looking up the |
|
336 |
+ * magic number. CPM has a lot to answer for... |
|
337 |
+ * FIXME: the suffix now appears twice in the filename... |
|
338 |
+ */ |
|
339 |
+ suffix = strrchr(filename, '.'); |
|
340 |
+ if(suffix == NULL) |
|
341 |
+ suffix = ""; |
|
342 |
+ else { |
|
343 |
+ suffixLen = strlen(suffix); |
|
344 |
+ if(suffixLen > 4) { |
|
345 |
+ /* Found a full stop which isn't a suffix */ |
|
346 |
+ suffix = ""; |
|
347 |
+ suffixLen = 0; |
|
348 |
+ } |
|
349 |
+ } |
|
350 |
+ blobSetFilename(&fb->b, filename); |
|
351 |
+ |
|
352 |
+ /* |
|
353 |
+ * Reload the filename, it may be different from the one we've |
|
354 |
+ * asked for, e.g. '/'s taken out |
|
355 |
+ */ |
|
356 |
+ filename = blobGetFilename(&fb->b); |
|
357 |
+ |
|
358 |
+ snprintf(fullname, sizeof(fullname) - 1 - suffixLen, "%s/%.*sXXXXXX", dir, |
|
359 |
+ (int)(sizeof(fullname) - 9 - suffixLen - strlen(dir)), filename); |
|
360 |
+#if defined(C_LINUX) || defined(C_BSD) || defined(HAVE_MKSTEMP) || defined(C_SOLARIS) || defined(C_CYGWIN) |
|
361 |
+ fd = mkstemp(fullname); |
|
362 |
+#else |
|
363 |
+ (void)mktemp(fullname); |
|
364 |
+ fd = open(fullname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_BINARY, 0600); |
|
365 |
+#endif |
|
366 |
+ |
|
367 |
+ if(fd < 0) { |
|
368 |
+ cli_errmsg("Can't create temporary file %s: %s\n", fullname, strerror(errno)); |
|
369 |
+ cli_dbgmsg("%lu %d %d\n", suffixLen, sizeof(fullname), strlen(fullname)); |
|
370 |
+ return; |
|
371 |
+ } |
|
372 |
+ |
|
373 |
+ cli_dbgmsg("Saving attachment as %s\n", fullname); |
|
374 |
+ |
|
375 |
+ fb->fp = fdopen(fd, "wb"); |
|
376 |
+ |
|
377 |
+ if(fb->fp == NULL) { |
|
378 |
+ cli_errmsg("Can't create file %s: %s\n", fullname, strerror(errno)); |
|
379 |
+ cli_dbgmsg("%lu %d %d\n", suffixLen, sizeof(fullname), strlen(fullname)); |
|
380 |
+ close(fd); |
|
381 |
+ |
|
382 |
+ return; |
|
383 |
+ } |
|
384 |
+ if(fb->b.data) { |
|
385 |
+ if(fwrite(fb->b.data, fb->b.len, 1, fb->fp) != 1) |
|
386 |
+ cli_errmsg("fileblobSetFilename: Can't write to temporary file %s: %s\n", fb->b.name, strerror(errno)); |
|
387 |
+ free(fb->b.data); |
|
388 |
+ fb->b.data = NULL; |
|
389 |
+ fb->b.len = fb->b.size = 0; |
|
390 |
+ } |
|
391 |
+ |
|
392 |
+ /* |
|
393 |
+ * Add the suffix back to the end of the filename. Tut-tut, filenames |
|
394 |
+ * should be independant of their usage on UNIX type systems. |
|
395 |
+ */ |
|
396 |
+ if(suffixLen > 1) { |
|
397 |
+ char stub[NAME_MAX + 1]; |
|
398 |
+ |
|
399 |
+ snprintf(stub, sizeof(stub), "%s%s", fullname, suffix); |
|
400 |
+#ifdef C_LINUX |
|
401 |
+ rename(stub, fullname); |
|
402 |
+#else |
|
403 |
+ link(stub, fullname); |
|
404 |
+ unlink(stub); |
|
405 |
+#endif |
|
406 |
+ } |
|
407 |
+} |
|
408 |
+ |
|
409 |
+void |
|
410 |
+fileblobAddData(fileblob *fb, const unsigned char *data, size_t len) |
|
411 |
+{ |
|
412 |
+ if(len == 0) |
|
413 |
+ return; |
|
414 |
+ |
|
415 |
+ if(fb->fp) { |
|
416 |
+ if(fwrite(data, len, 1, fb->fp) != 1) |
|
417 |
+ cli_errmsg("fileblobAddData: Can't write %u bytes to temporary file %s: %s\n", len, fb->b.name, strerror(errno)); |
|
418 |
+ } else |
|
419 |
+ blobAddData(&(fb->b), data, len); |
|
420 |
+} |
|
421 |
+ |
|
422 |
+const char * |
|
423 |
+fileblobGetFilename(const fileblob *fb) |
|
424 |
+{ |
|
425 |
+ return blobGetFilename(&(fb->b)); |
|
426 |
+} |
... | ... |
@@ -18,6 +18,12 @@ |
18 | 18 |
* $LOG$ |
19 | 19 |
*/ |
20 | 20 |
|
21 |
+#ifndef __BLOB_H |
|
22 |
+#define __BLOB_H |
|
23 |
+ |
|
24 |
+/* |
|
25 |
+ * Resizable chunk of memory |
|
26 |
+ */ |
|
21 | 27 |
typedef struct blob { |
22 | 28 |
char *name; /* filename */ |
23 | 29 |
unsigned char *data; /* the stuff itself */ |
... | ... |
@@ -40,3 +46,19 @@ unsigned long blobGetDataSize(const blob *b); |
40 | 40 |
void blobClose(blob *b); |
41 | 41 |
int blobcmp(const blob *b1, const blob *b2); |
42 | 42 |
void blobGrow(blob *b, size_t len); |
43 |
+ |
|
44 |
+/* |
|
45 |
+ * Like a blob, but associated with a file |
|
46 |
+ */ |
|
47 |
+typedef struct fileblob { |
|
48 |
+ FILE *fp; |
|
49 |
+ blob b; |
|
50 |
+} fileblob; |
|
51 |
+ |
|
52 |
+fileblob *fileblobCreate(void); |
|
53 |
+void fileblobDestroy(fileblob *fb); |
|
54 |
+void fileblobSetFilename(fileblob *fb, const char *dir, const char *filename); |
|
55 |
+const char *fileblobGetFilename(const fileblob *fb); |
|
56 |
+void fileblobAddData(fileblob *fb, const unsigned char *data, size_t len); |
|
57 |
+ |
|
58 |
+#endif /*_BLOB_H*/ |
... | ... |
@@ -17,6 +17,9 @@ |
17 | 17 |
* |
18 | 18 |
* Change History: |
19 | 19 |
* $Log: mbox.c,v $ |
20 |
+ * Revision 1.109 2004/08/22 10:34:24 nigelhorne |
|
21 |
+ * Use fileblob |
|
22 |
+ * |
|
20 | 23 |
* Revision 1.108 2004/08/21 11:57:57 nigelhorne |
21 | 24 |
* Use line.[ch] |
22 | 25 |
* |
... | ... |
@@ -312,7 +315,7 @@ |
312 | 312 |
* Compilable under SCO; removed duplicate code with message.c |
313 | 313 |
* |
314 | 314 |
*/ |
315 |
-static char const rcsid[] = "$Id: mbox.c,v 1.108 2004/08/21 11:57:57 nigelhorne Exp $"; |
|
315 |
+static char const rcsid[] = "$Id: mbox.c,v 1.109 2004/08/22 10:34:24 nigelhorne Exp $"; |
|
316 | 316 |
|
317 | 317 |
#if HAVE_CONFIG_H |
318 | 318 |
#include "clamav-config.h" |
... | ... |
@@ -1314,17 +1317,14 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con |
1314 | 1314 |
if(mainMessage) { |
1315 | 1315 |
const text *u_line = uuencodeBegin(mainMessage); |
1316 | 1316 |
if(u_line) { |
1317 |
- blob *aBlob; |
|
1317 |
+ fileblob *fb; |
|
1318 | 1318 |
|
1319 | 1319 |
cli_dbgmsg("Found uuencoded message in multipart/mixed mainMessage\n"); |
1320 | 1320 |
messageSetEncoding(mainMessage, "x-uuencode"); |
1321 |
- aBlob = messageToBlob(mainMessage); |
|
1321 |
+ fb = messageToFileblob(mainMessage, dir); |
|
1322 | 1322 |
|
1323 |
- if(aBlob) { |
|
1324 |
- assert(blobGetFilename(aBlob) != NULL); |
|
1325 |
- blobClose(aBlob); |
|
1326 |
- blobList[numberOfAttachments++] = aBlob; |
|
1327 |
- } |
|
1323 |
+ if(fb) |
|
1324 |
+ fileblobDestroy(fb); |
|
1328 | 1325 |
} |
1329 | 1326 |
if(mainMessage != messageIn) |
1330 | 1327 |
messageDestroy(mainMessage); |
... | ... |
@@ -1491,13 +1491,10 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con |
1491 | 1491 |
*/ |
1492 | 1492 |
aText = textAdd(aText, messageGetBody(aMessage)); |
1493 | 1493 |
} else { |
1494 |
- blob *aBlob = messageToBlob(aMessage); |
|
1494 |
+ fileblob *fb = messageToFileblob(aMessage, dir); |
|
1495 | 1495 |
|
1496 |
- if(aBlob) { |
|
1497 |
- assert(blobGetFilename(aBlob) != NULL); |
|
1498 |
- blobClose(aBlob); |
|
1499 |
- blobList[numberOfAttachments++] = aBlob; |
|
1500 |
- } |
|
1496 |
+ if(fb) |
|
1497 |
+ fileblobDestroy(fb); |
|
1501 | 1498 |
} |
1502 | 1499 |
assert(aMessage == messages[i]); |
1503 | 1500 |
messageDestroy(messages[i]); |
... | ... |
@@ -1520,6 +1517,12 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con |
1520 | 1520 |
#ifdef CL_DEBUG |
1521 | 1521 |
assert(blobs[i]->magic == BLOB); |
1522 | 1522 |
#endif |
1523 |
+ /* |
|
1524 |
+ * TODO: Now that fileblob is used |
|
1525 |
+ * this checking doesn't happen, |
|
1526 |
+ * need another means to save scanning |
|
1527 |
+ * two attachments that are the same |
|
1528 |
+ */ |
|
1523 | 1529 |
for(j = 0; j < numberOfAttachments; j++) |
1524 | 1530 |
if(blobcmp(blobs[i], blobList[j]) == 0) |
1525 | 1531 |
break; |
... | ... |
@@ -1673,26 +1676,11 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con |
1673 | 1673 |
/*if((strcasecmp(cptr, "octet-stream") == 0) || |
1674 | 1674 |
(strcasecmp(cptr, "x-msdownload") == 0)) {*/ |
1675 | 1675 |
{ |
1676 |
- blob *aBlob = messageToBlob(mainMessage); |
|
1676 |
+ fileblob *fb = messageToFileblob(mainMessage, dir); |
|
1677 | 1677 |
|
1678 |
- if(aBlob) { |
|
1679 |
- cli_dbgmsg("Saving main message as attachment %d\n", nBlobs); |
|
1680 |
- assert(blobGetFilename(aBlob) != NULL); |
|
1681 |
- /* |
|
1682 |
- * It's likely that we won't have built |
|
1683 |
- * a set of attachments |
|
1684 |
- */ |
|
1685 |
- if(blobs == NULL) |
|
1686 |
- blobs = blobList; |
|
1687 |
- for(i = 0; i < nBlobs; i++) |
|
1688 |
- if(blobs[i] == NULL) |
|
1689 |
- break; |
|
1690 |
- blobClose(aBlob); |
|
1691 |
- blobs[i] = aBlob; |
|
1692 |
- if(i == nBlobs) { |
|
1693 |
- nBlobs++; |
|
1694 |
- assert(nBlobs < MAX_ATTACHMENTS); |
|
1695 |
- } |
|
1678 |
+ if(fb) { |
|
1679 |
+ cli_dbgmsg("Saving main message as attachment\n"); |
|
1680 |
+ fileblobDestroy(fb); |
|
1696 | 1681 |
} |
1697 | 1682 |
} /*else |
1698 | 1683 |
cli_warnmsg("Discarded application not sent as attachment\n");*/ |
... | ... |
@@ -1717,7 +1705,7 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con |
1717 | 1717 |
cli_dbgmsg("%d attachments found\n", nBlobs); |
1718 | 1718 |
|
1719 | 1719 |
if(nBlobs == 0) { |
1720 |
- blob *b; |
|
1720 |
+ fileblob *fb; |
|
1721 | 1721 |
|
1722 | 1722 |
/* |
1723 | 1723 |
* No attachments - scan the text portions, often files |
... | ... |
@@ -1725,16 +1713,14 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con |
1725 | 1725 |
*/ |
1726 | 1726 |
cli_dbgmsg("%d multiparts found\n", multiparts); |
1727 | 1727 |
for(i = 0; i < multiparts; i++) { |
1728 |
- b = messageToBlob(messages[i]); |
|
1729 |
- |
|
1730 |
- assert(b != NULL); |
|
1728 |
+ fb = messageToFileblob(messages[i], dir); |
|
1731 | 1729 |
|
1732 |
- cli_dbgmsg("Saving multipart %d, encoded with scheme %d\n", |
|
1733 |
- i, messageGetEncoding(messages[i])); |
|
1730 |
+ if(fb) { |
|
1731 |
+ cli_dbgmsg("Saving multipart %d, encoded with scheme %d\n", |
|
1732 |
+ i, messageGetEncoding(messages[i])); |
|
1734 | 1733 |
|
1735 |
- (void)saveFile(b, dir); |
|
1736 |
- |
|
1737 |
- blobDestroy(b); |
|
1734 |
+ fileblobDestroy(fb); |
|
1735 |
+ } |
|
1738 | 1736 |
} |
1739 | 1737 |
|
1740 | 1738 |
if(mainMessage) { |
... | ... |
@@ -1751,13 +1737,10 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con |
1751 | 1751 |
*/ |
1752 | 1752 |
messageSetEncoding(mainMessage, "x-uuencode"); |
1753 | 1753 |
|
1754 |
- if((b = messageToBlob(mainMessage)) != NULL) { |
|
1755 |
- if((cptr = blobGetFilename(b)) != NULL) { |
|
1754 |
+ if((fb = messageToFileblob(mainMessage, dir)) != NULL) { |
|
1755 |
+ if((cptr = fileblobGetFilename(fb)) != NULL) |
|
1756 | 1756 |
cli_dbgmsg("Found uuencoded message %s\n", cptr); |
1757 |
- |
|
1758 |
- (void)saveFile(b, dir); |
|
1759 |
- } |
|
1760 |
- blobDestroy(b); |
|
1757 |
+ fileblobDestroy(fb); |
|
1761 | 1758 |
} |
1762 | 1759 |
} else if((encodingLine(mainMessage) != NULL) && |
1763 | 1760 |
((t_line = bounceBegin(mainMessage)) != NULL)) { |
... | ... |
@@ -1790,12 +1773,11 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con |
1790 | 1790 |
(strstr(txt, "8bit") == NULL)) |
1791 | 1791 |
break; |
1792 | 1792 |
} |
1793 |
- if(t && ((b = textToBlob(t_line, NULL)) != NULL)) { |
|
1793 |
+ if(t && ((fb = fileblobCreate()) != NULL)) { |
|
1794 | 1794 |
cli_dbgmsg("Found a bounce message\n"); |
1795 |
- |
|
1796 |
- saveFile(b, dir); |
|
1797 |
- |
|
1798 |
- blobDestroy(b); |
|
1795 |
+ fileblobSetFilename(fb, dir, "bounce"); |
|
1796 |
+ fb = textToFileblob(t_line, fb); |
|
1797 |
+ fileblobDestroy(fb); |
|
1799 | 1798 |
} |
1800 | 1799 |
} else { |
1801 | 1800 |
bool saveIt; |
... | ... |
@@ -1817,16 +1799,17 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con |
1817 | 1817 |
* Unfortunately this generates a |
1818 | 1818 |
* lot of false positives that a bounce |
1819 | 1819 |
* has been found when it hasn't. |
1820 |
+ * |
|
1821 |
+ * TODO: use fileblobCreate here |
|
1820 | 1822 |
*/ |
1821 |
- if((b = blobCreate()) != NULL) { |
|
1823 |
+ if((fb = fileblobCreate()) != NULL) { |
|
1822 | 1824 |
cli_dbgmsg("Found a bounce message with no header\n"); |
1823 |
- blobAddData(b, "Received: by clamd\n", 19); |
|
1824 |
- |
|
1825 |
- b = textToBlob(t_line, b); |
|
1825 |
+ fileblobSetFilename(fb, dir, "bounce"); |
|
1826 |
+ fileblobAddData(fb, "Received: by clamd\n", 19); |
|
1826 | 1827 |
|
1827 |
- saveFile(b, dir); |
|
1828 |
+ fb = textToFileblob(t_line, fb); |
|
1828 | 1829 |
|
1829 |
- blobDestroy(b); |
|
1830 |
+ fileblobDestroy(fb); |
|
1830 | 1831 |
} |
1831 | 1832 |
saveIt = FALSE; |
1832 | 1833 |
} else |
... | ... |
@@ -2183,19 +2166,17 @@ parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const c |
2183 | 2183 |
static void |
2184 | 2184 |
saveTextPart(message *m, const char *dir) |
2185 | 2185 |
{ |
2186 |
- blob *b; |
|
2186 |
+ fileblob *fb; |
|
2187 | 2187 |
|
2188 | 2188 |
messageAddArgument(m, "filename=textportion"); |
2189 |
- if((b = messageToBlob(m)) != NULL) { |
|
2189 |
+ if((fb = messageToFileblob(m, dir)) != NULL) { |
|
2190 | 2190 |
/* |
2191 | 2191 |
* Save main part to scan that |
2192 | 2192 |
*/ |
2193 | 2193 |
cli_dbgmsg("Saving main message, encoded with scheme %d\n", |
2194 | 2194 |
messageGetEncoding(m)); |
2195 | 2195 |
|
2196 |
- (void)saveFile(b, dir); |
|
2197 |
- |
|
2198 |
- blobDestroy(b); |
|
2196 |
+ fileblobDestroy(fb); |
|
2199 | 2197 |
} |
2200 | 2198 |
} |
2201 | 2199 |
|
... | ... |
@@ -2206,6 +2187,8 @@ saveTextPart(message *m, const char *dir) |
2206 | 2206 |
* OLE2 files if that is disabled or pattern match --exclude, but |
2207 | 2207 |
* we need access to the command line options/clamav.conf here to |
2208 | 2208 |
* be able to do that |
2209 |
+ * |
|
2210 |
+ * FIXME: duplicated code with fileblobSetFilename() |
|
2209 | 2211 |
*/ |
2210 | 2212 |
static bool |
2211 | 2213 |
saveFile(const blob *b, const char *dir) |
... | ... |
@@ -17,6 +17,9 @@ |
17 | 17 |
* |
18 | 18 |
* Change History: |
19 | 19 |
* $Log: message.c,v $ |
20 |
+ * Revision 1.73 2004/08/22 10:34:24 nigelhorne |
|
21 |
+ * Use fileblob |
|
22 |
+ * |
|
20 | 23 |
* Revision 1.72 2004/08/21 11:57:57 nigelhorne |
21 | 24 |
* Use line.[ch] |
22 | 25 |
* |
... | ... |
@@ -213,7 +216,7 @@ |
213 | 213 |
* uuencodebegin() no longer static |
214 | 214 |
* |
215 | 215 |
*/ |
216 |
-static char const rcsid[] = "$Id: message.c,v 1.72 2004/08/21 11:57:57 nigelhorne Exp $"; |
|
216 |
+static char const rcsid[] = "$Id: message.c,v 1.73 2004/08/22 10:34:24 nigelhorne Exp $"; |
|
217 | 217 |
|
218 | 218 |
#if HAVE_CONFIG_H |
219 | 219 |
#include "clamav-config.h" |
... | ... |
@@ -973,18 +976,18 @@ messageClean(message *m) |
973 | 973 |
* Decode and transfer the contents of the message into a blob |
974 | 974 |
* The caller must free the returned blob |
975 | 975 |
*/ |
976 |
-blob * |
|
977 |
-messageToBlob(message *m) |
|
976 |
+fileblob * |
|
977 |
+messageToFileblob(message *m, const char *dir) |
|
978 | 978 |
{ |
979 |
- blob *b; |
|
979 |
+ fileblob *fb; |
|
980 | 980 |
const text *t_line = NULL; |
981 | 981 |
char *filename; |
982 | 982 |
|
983 | 983 |
assert(m != NULL); |
984 | 984 |
|
985 |
- b = blobCreate(); |
|
985 |
+ fb = fileblobCreate(); |
|
986 | 986 |
|
987 |
- if(b == NULL) |
|
987 |
+ if(fb == NULL) |
|
988 | 988 |
return NULL; |
989 | 989 |
|
990 | 990 |
/* |
... | ... |
@@ -995,7 +998,7 @@ messageToBlob(message *m) |
995 | 995 |
|
996 | 996 |
if(t_line == NULL) { |
997 | 997 |
/*cli_warnmsg("UUENCODED attachment is missing begin statement\n");*/ |
998 |
- blobDestroy(b); |
|
998 |
+ fileblobDestroy(fb); |
|
999 | 999 |
return NULL; |
1000 | 1000 |
} |
1001 | 1001 |
|
... | ... |
@@ -1003,14 +1006,14 @@ messageToBlob(message *m) |
1003 | 1003 |
|
1004 | 1004 |
if(filename == NULL) { |
1005 | 1005 |
cli_dbgmsg("UUencoded attachment sent with no filename\n"); |
1006 |
- blobDestroy(b); |
|
1006 |
+ fileblobDestroy(fb); |
|
1007 | 1007 |
return NULL; |
1008 | 1008 |
} |
1009 | 1009 |
cli_chomp(filename); |
1010 | 1010 |
|
1011 | 1011 |
cli_dbgmsg("Set uuencode filename to \"%s\"\n", filename); |
1012 | 1012 |
|
1013 |
- blobSetFilename(b, filename); |
|
1013 |
+ fileblobSetFilename(fb, dir, filename); |
|
1014 | 1014 |
t_line = t_line->t_next; |
1015 | 1015 |
} else if((t_line = binhexBegin(m)) != NULL) { |
1016 | 1016 |
unsigned char byte; |
... | ... |
@@ -1039,7 +1042,7 @@ messageToBlob(message *m) |
1039 | 1039 |
tmp = blobCreate(); |
1040 | 1040 |
|
1041 | 1041 |
if(tmp == NULL) { |
1042 |
- blobDestroy(b); |
|
1042 |
+ fileblobDestroy(fb); |
|
1043 | 1043 |
return NULL; |
1044 | 1044 |
} |
1045 | 1045 |
|
... | ... |
@@ -1061,7 +1064,7 @@ messageToBlob(message *m) |
1061 | 1061 |
if(data == NULL) { |
1062 | 1062 |
cli_warnmsg("Couldn't locate the binhex message that was claimed to be there\n"); |
1063 | 1063 |
blobDestroy(tmp); |
1064 |
- blobDestroy(b); |
|
1064 |
+ fileblobDestroy(fb); |
|
1065 | 1065 |
return NULL; |
1066 | 1066 |
} |
1067 | 1067 |
if(data[0] != ':') { |
... | ... |
@@ -1072,7 +1075,7 @@ messageToBlob(message *m) |
1072 | 1072 |
*/ |
1073 | 1073 |
cli_warnmsg("8 bit binhex code is not yet supported\n"); |
1074 | 1074 |
blobDestroy(tmp); |
1075 |
- blobDestroy(b); |
|
1075 |
+ fileblobDestroy(fb); |
|
1076 | 1076 |
return NULL; |
1077 | 1077 |
} |
1078 | 1078 |
|
... | ... |
@@ -1089,7 +1092,7 @@ messageToBlob(message *m) |
1089 | 1089 |
uptr = cli_malloc(len); |
1090 | 1090 |
if(uptr == NULL) { |
1091 | 1091 |
blobDestroy(tmp); |
1092 |
- blobDestroy(b); |
|
1092 |
+ fileblobDestroy(fb); |
|
1093 | 1093 |
return NULL; |
1094 | 1094 |
} |
1095 | 1095 |
memcpy(uptr, data, len); |
... | ... |
@@ -1158,7 +1161,7 @@ messageToBlob(message *m) |
1158 | 1158 |
blob *u = blobCreate(); /* uncompressed data */ |
1159 | 1159 |
|
1160 | 1160 |
if(u == NULL) { |
1161 |
- blobDestroy(b); |
|
1161 |
+ fileblobDestroy(fb); |
|
1162 | 1162 |
blobDestroy(tmp); |
1163 | 1163 |
return NULL; |
1164 | 1164 |
} |
... | ... |
@@ -1205,7 +1208,7 @@ messageToBlob(message *m) |
1205 | 1205 |
} |
1206 | 1206 |
if(len == 0) { |
1207 | 1207 |
cli_warnmsg("Discarding empty binHex attachment\n"); |
1208 |
- blobDestroy(b); |
|
1208 |
+ fileblobDestroy(fb); |
|
1209 | 1209 |
blobDestroy(tmp); |
1210 | 1210 |
return NULL; |
1211 | 1211 |
} |
... | ... |
@@ -1222,19 +1225,19 @@ messageToBlob(message *m) |
1222 | 1222 |
*/ |
1223 | 1223 |
byte = data[0]; |
1224 | 1224 |
if(byte >= len) { |
1225 |
- blobDestroy(b); |
|
1225 |
+ fileblobDestroy(fb); |
|
1226 | 1226 |
blobDestroy(tmp); |
1227 | 1227 |
return NULL; |
1228 | 1228 |
} |
1229 | 1229 |
filename = cli_malloc(byte + 1); |
1230 | 1230 |
if(filename == NULL) { |
1231 |
- blobDestroy(b); |
|
1231 |
+ fileblobDestroy(fb); |
|
1232 | 1232 |
blobDestroy(tmp); |
1233 | 1233 |
return NULL; |
1234 | 1234 |
} |
1235 | 1235 |
memcpy(filename, &data[1], byte); |
1236 | 1236 |
filename[byte] = '\0'; |
1237 |
- blobSetFilename(b, filename); |
|
1237 |
+ fileblobSetFilename(fb, dir, filename); |
|
1238 | 1238 |
/*ptr = cli_malloc(strlen(filename) + 6);*/ |
1239 | 1239 |
ptr = cli_malloc(byte + 6); |
1240 | 1240 |
if(ptr) { |
... | ... |
@@ -1273,11 +1276,11 @@ messageToBlob(message *m) |
1273 | 1273 |
len, l); |
1274 | 1274 |
len = l; |
1275 | 1275 |
} |
1276 |
- blobAddData(b, &data[byte], len); |
|
1276 |
+ fileblobAddData(fb, &data[byte], len); |
|
1277 | 1277 |
|
1278 | 1278 |
blobDestroy(tmp); |
1279 | 1279 |
|
1280 |
- return b; |
|
1280 |
+ return fb; |
|
1281 | 1281 |
} else { |
1282 | 1282 |
filename = (char *)messageFindArgument(m, "filename"); |
1283 | 1283 |
if(filename == NULL) { |
... | ... |
@@ -1295,7 +1298,7 @@ messageToBlob(message *m) |
1295 | 1295 |
messageSetEncoding(m, "base64"); |
1296 | 1296 |
} |
1297 | 1297 |
|
1298 |
- blobSetFilename(b, filename); |
|
1298 |
+ fileblobSetFilename(fb, dir, filename); |
|
1299 | 1299 |
|
1300 | 1300 |
t_line = messageGetBody(m); |
1301 | 1301 |
} |
... | ... |
@@ -1306,7 +1309,7 @@ messageToBlob(message *m) |
1306 | 1306 |
*/ |
1307 | 1307 |
if(t_line == NULL) { |
1308 | 1308 |
cli_warnmsg("Empty attachment not saved\n"); |
1309 |
- blobDestroy(b); |
|
1309 |
+ fileblobDestroy(fb); |
|
1310 | 1310 |
return NULL; |
1311 | 1311 |
} |
1312 | 1312 |
|
... | ... |
@@ -1314,7 +1317,7 @@ messageToBlob(message *m) |
1314 | 1314 |
/* |
1315 | 1315 |
* Fast copy |
1316 | 1316 |
*/ |
1317 |
- return textToBlob(t_line, b); |
|
1317 |
+ return textToFileblob(t_line, fb); |
|
1318 | 1318 |
|
1319 | 1319 |
do { |
1320 | 1320 |
unsigned char data[1024]; |
... | ... |
@@ -1338,7 +1341,9 @@ messageToBlob(message *m) |
1338 | 1338 |
|
1339 | 1339 |
assert(uptr <= &data[sizeof(data)]); |
1340 | 1340 |
|
1341 |
- blobAddData(b, data, (size_t)(uptr - data)); |
|
1341 |
+ if(uptr != data) |
|
1342 |
+ fileblobAddData(fb, data, (size_t)(uptr - data)); |
|
1343 |
+ |
|
1342 | 1344 |
/* |
1343 | 1345 |
* According to RFC1521, '=' is used to pad out |
1344 | 1346 |
* the last byte and should be used as evidence |
... | ... |
@@ -1359,11 +1364,11 @@ messageToBlob(message *m) |
1359 | 1359 |
|
1360 | 1360 |
ptr = decode(m, NULL, data, base64, FALSE); |
1361 | 1361 |
if(ptr) |
1362 |
- blobAddData(b, data, (size_t)(ptr - data)); |
|
1362 |
+ fileblobAddData(fb, data, (size_t)(ptr - data)); |
|
1363 | 1363 |
m->base64chars = 0; |
1364 | 1364 |
} |
1365 | 1365 |
|
1366 |
- return b; |
|
1366 |
+ return fb; |
|
1367 | 1367 |
} |
1368 | 1368 |
|
1369 | 1369 |
/* |
... | ... |
@@ -16,6 +16,9 @@ |
16 | 16 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | 17 |
* |
18 | 18 |
* $Log: message.h,v $ |
19 |
+ * Revision 1.14 2004/08/22 10:34:24 nigelhorne |
|
20 |
+ * Use fileblob |
|
21 |
+ * |
|
19 | 22 |
* Revision 1.13 2004/08/21 11:57:57 nigelhorne |
20 | 23 |
* Use line.[ch] |
21 | 24 |
* |
... | ... |
@@ -91,7 +94,7 @@ int messageAddStr(message *m, const char *data); |
91 | 91 |
int messageAddStrAtTop(message *m, const char *data); |
92 | 92 |
const text *messageGetBody(const message *m); |
93 | 93 |
void messageClean(message *m); |
94 |
-blob *messageToBlob(message *m); |
|
94 |
+fileblob *messageToFileblob(message *m, const char *dir); |
|
95 | 95 |
text *messageToText(message *m); |
96 | 96 |
const text *uuencodeBegin(const message *m); |
97 | 97 |
const text *binhexBegin(const message *m); |
... | ... |
@@ -16,6 +16,9 @@ |
16 | 16 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | 17 |
* |
18 | 18 |
* $Log: text.c,v $ |
19 |
+ * Revision 1.10 2004/08/22 10:34:24 nigelhorne |
|
20 |
+ * Use fileblob |
|
21 |
+ * |
|
19 | 22 |
* Revision 1.9 2004/08/21 11:57:57 nigelhorne |
20 | 23 |
* Use line.[ch] |
21 | 24 |
* |
... | ... |
@@ -36,7 +39,7 @@ |
36 | 36 |
* |
37 | 37 |
*/ |
38 | 38 |
|
39 |
-static char const rcsid[] = "$Id: text.c,v 1.9 2004/08/21 11:57:57 nigelhorne Exp $"; |
|
39 |
+static char const rcsid[] = "$Id: text.c,v 1.10 2004/08/22 10:34:24 nigelhorne Exp $"; |
|
40 | 40 |
|
41 | 41 |
#if HAVE_CONFIG_H |
42 | 42 |
#include "clamav-config.h" |
... | ... |
@@ -56,6 +59,7 @@ static char const rcsid[] = "$Id: text.c,v 1.9 2004/08/21 11:57:57 nigelhorne Ex |
56 | 56 |
#include <string.h> |
57 | 57 |
#include <ctype.h> |
58 | 58 |
#include <assert.h> |
59 |
+#include <stdio.h> |
|
59 | 60 |
|
60 | 61 |
#include "line.h" |
61 | 62 |
#include "mbox.h" |
... | ... |
@@ -272,3 +276,28 @@ textToBlob(const text *t, blob *b) |
272 | 272 |
|
273 | 273 |
return b; |
274 | 274 |
} |
275 |
+ |
|
276 |
+fileblob * |
|
277 |
+textToFileblob(const text *t, fileblob *fb) |
|
278 |
+{ |
|
279 |
+ assert(fb != NULL); |
|
280 |
+ assert(t != NULL); |
|
281 |
+ |
|
282 |
+ if(fb == NULL) { |
|
283 |
+ fb = fileblobCreate(); |
|
284 |
+ |
|
285 |
+ if(fb == NULL) |
|
286 |
+ return NULL; |
|
287 |
+ } |
|
288 |
+ |
|
289 |
+ do { |
|
290 |
+ if(t->t_line) { |
|
291 |
+ const char *l = lineGetData(t->t_line); |
|
292 |
+ |
|
293 |
+ fileblobAddData(fb, (unsigned char *)l, strlen(l)); |
|
294 |
+ } |
|
295 |
+ fileblobAddData(fb, (unsigned char *)"\n", 1); |
|
296 |
+ } while((t = t->t_next) != NULL); |
|
297 |
+ |
|
298 |
+ return fb; |
|
299 |
+} |
... | ... |
@@ -16,6 +16,9 @@ |
16 | 16 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | 17 |
* |
18 | 18 |
* $Log: text.h,v $ |
19 |
+ * Revision 1.6 2004/08/22 10:34:24 nigelhorne |
|
20 |
+ * Use fileblob |
|
21 |
+ * |
|
19 | 22 |
* Revision 1.5 2004/08/21 11:57:57 nigelhorne |
20 | 23 |
* Use line.[ch] |
21 | 24 |
* |
... | ... |
@@ -40,3 +43,4 @@ text *textCopy(const text *t_head); |
40 | 40 |
text *textAdd(text *t_head, const text *t); |
41 | 41 |
text *textAddMessage(text *aText, message *aMessage); |
42 | 42 |
blob *textToBlob(const text *t, blob *b); |
43 |
+fileblob *textToFileblob(const text *t, fileblob *fb); |