git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@1440 77e5149b-7576-45b1-b177-96237e5ba77b
Nigel Horne authored on 2005/03/26 16:49:26... | ... |
@@ -14,13 +14,17 @@ |
14 | 14 |
* You should have received a copy of the GNU General Public License |
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 |
+ * |
|
18 |
+ * The algorithm is based on kdepim/ktnef/lib/ktnefparser.cpp from |
|
19 |
+ * KDE, rewritten in C by NJH. That algorithm is released under the GPL and is |
|
20 |
+ * Copyright (C) 2002 Michael Goffioul <kdeprint@swing.be> |
|
17 | 21 |
*/ |
18 | 22 |
|
19 | 23 |
#if HAVE_CONFIG_H |
20 | 24 |
#include "clamav-config.h" |
21 | 25 |
#endif |
22 | 26 |
|
23 |
-static char const rcsid[] = "$Id: tnef.c,v 1.8 2005/03/25 23:00:48 nigelhorne Exp $"; |
|
27 |
+static char const rcsid[] = "$Id: tnef.c,v 1.9 2005/03/26 07:49:26 nigelhorne Exp $"; |
|
24 | 28 |
|
25 | 29 |
#include <stdio.h> |
26 | 30 |
|
... | ... |
@@ -28,18 +32,14 @@ static char const rcsid[] = "$Id: tnef.c,v 1.8 2005/03/25 23:00:48 nigelhorne Ex |
28 | 28 |
#include "clamav.h" |
29 | 29 |
#include "others.h" |
30 | 30 |
#include "tnef.h" |
31 |
+#if CL_DEBUG |
|
32 |
+#include "mbox.h" |
|
33 |
+#endif |
|
31 | 34 |
#include "blob.h" |
32 | 35 |
|
33 |
-static int tnef_message(int desc); |
|
34 |
-static int tnef_attachment(int desc, const char *dir, fileblob **fbref); |
|
36 |
+static int tnef_message(FILE *fp); |
|
37 |
+static int tnef_attachment(FILE *fp, const char *dir, fileblob **fbref); |
|
35 | 38 |
|
36 |
-/* |
|
37 |
- * The algorithm will be based on kdepim/ktnef/lib/ktnefparser.cpp from |
|
38 |
- * KDE, rewritten in C by NJH. The algorithm is released under the GPL and is |
|
39 |
- * Copyright (C) 2002 Michael Goffioul <kdeprint@swing.be> |
|
40 |
- * |
|
41 |
- * TODO: Use mmap on systems that support it |
|
42 |
- */ |
|
43 | 39 |
#define TNEF_SIGNATURE 0x223E9f78 |
44 | 40 |
#define LVL_MESSAGE 0x01 |
45 | 41 |
#define LVL_ATTACHMENT 0x02 |
... | ... |
@@ -66,7 +66,6 @@ static int tnef_attachment(int desc, const char *dir, fileblob **fbref); |
66 | 66 |
#endif |
67 | 67 |
#endif |
68 | 68 |
|
69 |
-/* FIXME: use stdio */ |
|
70 | 69 |
/* FIXME: only works on little endian machines */ |
71 | 70 |
int |
72 | 71 |
cli_tnef(const char *dir, int desc) |
... | ... |
@@ -75,34 +74,46 @@ cli_tnef(const char *dir, int desc) |
75 | 75 |
uint16_t i16; |
76 | 76 |
uint8_t i8; |
77 | 77 |
fileblob *fb; |
78 |
- int ret, alldone; |
|
78 |
+ int i, ret, alldone; |
|
79 |
+ FILE *fp; |
|
79 | 80 |
|
80 | 81 |
lseek(desc, 0L, SEEK_SET); |
81 | 82 |
|
82 |
- if(cli_readn(desc, &i32, sizeof(uint32_t)) != sizeof(uint32_t)) |
|
83 |
- return CL_EIO; |
|
83 |
+ i = dup(desc); |
|
84 |
+ if((fp = fdopen(i, "rb")) == NULL) { |
|
85 |
+ cli_errmsg("Can't open descriptor %d\n", desc); |
|
86 |
+ close(i); |
|
87 |
+ return CL_EOPEN; |
|
88 |
+ } |
|
84 | 89 |
|
85 |
- if(host32(i32) != host32(TNEF_SIGNATURE)) |
|
90 |
+ if(fread(&i32, sizeof(uint32_t), 1, fp) != 1) { |
|
91 |
+ fclose(fp); |
|
92 |
+ return CL_EIO; |
|
93 |
+ } |
|
94 |
+ if(i32 != TNEF_SIGNATURE) { |
|
95 |
+ fclose(fp); |
|
86 | 96 |
return CL_EFORMAT; |
97 |
+ } |
|
87 | 98 |
|
88 |
- if(cli_readn(desc, &i16, sizeof(uint16_t)) != sizeof(uint16_t)) |
|
99 |
+ if(fread(&i16, sizeof(uint16_t), 1, fp) != 1) { |
|
100 |
+ fclose(fp); |
|
89 | 101 |
return CL_EIO; |
102 |
+ } |
|
90 | 103 |
|
91 | 104 |
fb = NULL; |
92 | 105 |
ret = CL_CLEAN; |
93 | 106 |
alldone = 0; |
94 | 107 |
|
95 | 108 |
do { |
96 |
- switch(cli_readn(desc, &i8, sizeof(uint8_t))) { |
|
97 |
- case -1: |
|
98 |
- perror("read"); |
|
99 |
- ret = CL_EIO; |
|
100 |
- alldone = 1; |
|
101 |
- break; |
|
109 |
+ switch(fread(&i8, sizeof(uint8_t), 1, fp)) { |
|
102 | 110 |
case 0: |
111 |
+ if(ferror(fp)) { |
|
112 |
+ perror("read"); |
|
113 |
+ ret = CL_EIO; |
|
114 |
+ } |
|
103 | 115 |
alldone = 1; |
104 | 116 |
break; |
105 |
- case sizeof(uint8_t): |
|
117 |
+ case 1: |
|
106 | 118 |
break; |
107 | 119 |
default: |
108 | 120 |
ret = CL_EIO; |
... | ... |
@@ -114,7 +125,7 @@ cli_tnef(const char *dir, int desc) |
114 | 114 |
switch(i8) { |
115 | 115 |
case LVL_MESSAGE: |
116 | 116 |
/*cli_dbgmsg("TNEF - found message\n");*/ |
117 |
- if(tnef_message(desc) != 0) { |
|
117 |
+ if(tnef_message(fp) != 0) { |
|
118 | 118 |
cli_errmsg("Error reading TNEF message\n"); |
119 | 119 |
ret = CL_EFORMAT; |
120 | 120 |
alldone = 1; |
... | ... |
@@ -122,7 +133,7 @@ cli_tnef(const char *dir, int desc) |
122 | 122 |
break; |
123 | 123 |
case LVL_ATTACHMENT: |
124 | 124 |
/*cli_dbgmsg("TNEF - found attachment\n");*/ |
125 |
- if(tnef_attachment(desc, dir, &fb) != 0) { |
|
125 |
+ if(tnef_attachment(fp, dir, &fb) != 0) { |
|
126 | 126 |
cli_errmsg("Error reading TNEF message\n"); |
127 | 127 |
ret = CL_EFORMAT; |
128 | 128 |
alldone = 1; |
... | ... |
@@ -142,11 +153,12 @@ cli_tnef(const char *dir, int desc) |
142 | 142 |
fileblobDestroy(fb); |
143 | 143 |
fb = NULL; |
144 | 144 |
} |
145 |
+ fclose(fp); |
|
145 | 146 |
return CL_CLEAN; |
146 | 147 |
} |
147 | 148 |
|
148 | 149 |
static int |
149 |
-tnef_message(int desc) |
|
150 |
+tnef_message(FILE *fp) |
|
150 | 151 |
{ |
151 | 152 |
uint32_t i32, length; |
152 | 153 |
uint16_t i16, tag, type; |
... | ... |
@@ -155,20 +167,20 @@ tnef_message(int desc) |
155 | 155 |
char *string; |
156 | 156 |
#endif |
157 | 157 |
|
158 |
- if(cli_readn(desc, &i32, sizeof(uint32_t)) != sizeof(uint32_t)) |
|
158 |
+ if(fread(&i32, sizeof(uint32_t), 1, fp) != 1) |
|
159 | 159 |
return -1; |
160 | 160 |
|
161 | 161 |
tag = i32 & 0xFFFF; |
162 | 162 |
type = (i32 & 0xFFFF0000) >> 16; |
163 | 163 |
|
164 |
- if(cli_readn(desc, &i32, sizeof(uint32_t)) != sizeof(uint32_t)) |
|
164 |
+ if(fread(&i32, sizeof(uint32_t), 1, fp) != 1) |
|
165 | 165 |
return -1; |
166 | 166 |
|
167 | 167 |
length = i32; |
168 | 168 |
|
169 |
- /*cli_dbgmsg("message tag 0x%x, type 0x%x, length %u\n", tag, type, length);*/ |
|
169 |
+ cli_dbgmsg("message tag 0x%x, type 0x%x, length %u\n", tag, type, length); |
|
170 | 170 |
|
171 |
- offset = lseek(desc, 0L, SEEK_CUR); |
|
171 |
+ offset = ftell(fp); |
|
172 | 172 |
|
173 | 173 |
/* |
174 | 174 |
* a lot of this stuff should be only discovered in debug mode... |
... | ... |
@@ -180,13 +192,14 @@ tnef_message(int desc) |
180 | 180 |
#if CL_DEBUG |
181 | 181 |
case attTNEFVERSION: |
182 | 182 |
/*assert(length == sizeof(uint32_t))*/ |
183 |
- if(cli_readn(desc, &i32, sizeof(uint32_t)) != sizeof(uint32_t)) |
|
183 |
+ if(fread(&i32, sizeof(uint32_t), 1, fp) != 1) |
|
184 | 184 |
return -1; |
185 | 185 |
cli_dbgmsg("TNEF version %d\n", i32); |
186 | 186 |
break; |
187 | 187 |
case attOEMCODEPAGE: |
188 |
+ /* 8 bytes, but just print the first 4 */ |
|
188 | 189 |
/*assert(length == sizeof(uint32_t))*/ |
189 |
- if(cli_readn(desc, &i32, sizeof(uint32_t)) != sizeof(uint32_t)) |
|
190 |
+ if(fread(&i32, sizeof(uint32_t), 1, fp) != 1) |
|
190 | 191 |
return -1; |
191 | 192 |
cli_dbgmsg("TNEF codepage %d\n", i32); |
192 | 193 |
break; |
... | ... |
@@ -195,7 +208,7 @@ tnef_message(int desc) |
195 | 195 |
break; |
196 | 196 |
case attMSGCLASS: |
197 | 197 |
string = cli_malloc(length + 1); |
198 |
- if((unsigned int)cli_readn(desc, string, length) != length) |
|
198 |
+ if(fread(string, 1, length, fp) != length) |
|
199 | 199 |
return -1; |
200 | 200 |
string[length] = '\0'; |
201 | 201 |
cli_dbgmsg("TNEF class %s\n", string); |
... | ... |
@@ -207,39 +220,39 @@ tnef_message(int desc) |
207 | 207 |
#endif |
208 | 208 |
} |
209 | 209 |
|
210 |
- /*cli_dbgmsg("%lu %lu\n", offset + length, lseek(desc, 0L, SEEK_CUR));*/ |
|
210 |
+ cli_dbgmsg("%lu %lu\n", (long)(offset + length), ftell(fp)); |
|
211 | 211 |
|
212 |
- lseek(desc, offset + length, SEEK_SET); /* shouldn't be needed */ |
|
212 |
+ fseek(fp, offset + length, SEEK_SET); |
|
213 | 213 |
|
214 | 214 |
/* Checksum - TODO, verify */ |
215 |
- if(cli_readn(desc, &i16, sizeof(uint16_t)) != sizeof(uint16_t)) |
|
215 |
+ if(fread(&i16, sizeof(uint16_t), 1, fp) != 1) |
|
216 | 216 |
return -1; |
217 | 217 |
|
218 | 218 |
return 0; |
219 | 219 |
} |
220 | 220 |
|
221 | 221 |
static int |
222 |
-tnef_attachment(int desc, const char *dir, fileblob **fbref) |
|
222 |
+tnef_attachment(FILE *fp, const char *dir, fileblob **fbref) |
|
223 | 223 |
{ |
224 | 224 |
uint32_t i32, length, todo; |
225 | 225 |
uint16_t i16, tag, type; |
226 | 226 |
off_t offset; |
227 | 227 |
char *string; |
228 | 228 |
|
229 |
- if(cli_readn(desc, &i32, sizeof(uint32_t)) != sizeof(uint32_t)) |
|
229 |
+ if(fread(&i32, sizeof(uint32_t), 1, fp) != 1) |
|
230 | 230 |
return -1; |
231 | 231 |
|
232 | 232 |
tag = i32 & 0xFFFF; |
233 | 233 |
type = (i32 & 0xFFFF0000) >> 16; |
234 | 234 |
|
235 |
- if(cli_readn(desc, &i32, sizeof(uint32_t)) != sizeof(uint32_t)) |
|
235 |
+ if(fread(&i32, sizeof(uint32_t), 1, fp) != 1) |
|
236 | 236 |
return -1; |
237 | 237 |
|
238 | 238 |
length = i32; |
239 | 239 |
|
240 |
- /*cli_dbgmsg("message tag 0x%x, type 0x%x, length %u\n", tag, type, length);*/ |
|
240 |
+ cli_dbgmsg("attachment tag 0x%x, type 0x%x, length %u\n", tag, type, length); |
|
241 | 241 |
|
242 |
- offset = lseek(desc, 0L, SEEK_CUR); |
|
242 |
+ offset = ftell(fp); |
|
243 | 243 |
|
244 | 244 |
switch(tag) { |
245 | 245 |
case attATTACHTITLE: |
... | ... |
@@ -251,7 +264,7 @@ tnef_attachment(int desc, const char *dir, fileblob **fbref) |
251 | 251 |
return -1; |
252 | 252 |
string = cli_malloc(length + 1); |
253 | 253 |
|
254 |
- if((unsigned int)cli_readn(desc, string, length) != length) |
|
254 |
+ if(fread(string, 1, length, fp) != length) |
|
255 | 255 |
return -1; |
256 | 256 |
string[length] = '\0'; |
257 | 257 |
cli_dbgmsg("TNEF filename %s\n", string); |
... | ... |
@@ -265,12 +278,10 @@ tnef_attachment(int desc, const char *dir, fileblob **fbref) |
265 | 265 |
if(*fbref == NULL) |
266 | 266 |
return -1; |
267 | 267 |
} |
268 |
- /* FIXME: use stdio */ |
|
269 |
- todo = length; |
|
270 |
- while(todo) { |
|
271 |
- unsigned char *c; |
|
268 |
+ for(todo = length; todo; todo--) { |
|
269 |
+ int c; |
|
272 | 270 |
|
273 |
- if(cli_readn(desc, &c, 1) != 1) |
|
271 |
+ if((c = fgetc(fp)) == EOF) |
|
274 | 272 |
break; |
275 | 273 |
fileblobAddData(*fbref, (const unsigned char *)&c, 1); |
276 | 274 |
} |
... | ... |
@@ -282,10 +293,10 @@ tnef_attachment(int desc, const char *dir, fileblob **fbref) |
282 | 282 |
|
283 | 283 |
/*cli_dbgmsg("%lu %lu\n", offset + length, lseek(desc, 0L, SEEK_CUR));*/ |
284 | 284 |
|
285 |
- lseek(desc, offset + length, SEEK_SET); /* shouldn't be needed */ |
|
285 |
+ fseek(fp, (long)(offset + length), SEEK_SET); /* shouldn't be needed */ |
|
286 | 286 |
|
287 | 287 |
/* Checksum - TODO, verify */ |
288 |
- if(cli_readn(desc, &i16, sizeof(uint16_t)) != sizeof(uint16_t)) |
|
288 |
+ if(fread(&i16, sizeof(uint16_t), 1, fp) != 1) |
|
289 | 289 |
return -1; |
290 | 290 |
|
291 | 291 |
return 0; |