Browse code

Remove duplicate code. Fix memory leak.

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

Trog authored on 2004/03/26 18:37:09
Showing 5 changed files
... ...
@@ -1,3 +1,6 @@
1
+Fri Mar 26 09:47:50 GMT 2004 (trog)
2
+  * libclamav: Remove duplicate code. Fix memory leak.
3
+
1 4
 Thu Mar 25 22:51:53 GMT 2004 (njh)
2 5
 ----------------------------------
3 6
   * libclamav:	Removed even more calls to realloc and some duplicate code
... ...
@@ -131,57 +131,6 @@ typedef struct property_tag
131 131
 
132 132
 unsigned char magic_id[] = { 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1};
133 133
 
134
-/* Function: readn
135
-	Try hard to read the requested number of bytes
136
-*/
137
-static int readn(int fd, void *buff, unsigned int count)
138
-{
139
-	int retval;
140
-	unsigned int todo;
141
-	unsigned char *current;
142
-
143
-	todo = count;
144
-	current = (unsigned char *) buff;
145
-
146
-	do {
147
-		retval = read(fd, current, todo);
148
-		if (retval == 0) {
149
-			return (count - todo);
150
-		}
151
-		if (retval < 0) {
152
-			return -1;
153
-		}
154
-		todo -= retval;
155
-		current += retval;
156
-	} while (todo > 0);
157
-
158
-	return count;
159
-}
160
-
161
-/* Function: writen
162
-	Try hard to write the specified number of bytes
163
-*/
164
-static int writen(int fd, void *buff, unsigned int count)
165
-{
166
-	int retval;
167
-	unsigned int todo;
168
-	unsigned char *current;
169
-
170
-	todo = count;
171
-	current = (unsigned char *) buff;
172
-
173
-	do {
174
-		retval = write(fd, current, todo);
175
-		if (retval < 0) {
176
-			return -1;
177
-		}
178
-		todo -= retval;
179
-		current += retval;
180
-	} while (todo > 0);
181
-
182
-	return count;
183
-}
184
-
185 134
 static char *get_property_name(char *name, int size)
186 135
 {
187 136
 	int i, j;
... ...
@@ -310,7 +259,7 @@ static int ole2_read_block(int fd, ole2_header_t *hdr, void *buff, int32_t block
310 310
 	if (lseek(fd, offset, SEEK_SET) != offset) {
311 311
 		return FALSE;
312 312
 	}
313
-	if (readn(fd, buff, (1 << hdr->log2_big_block_size)) != (1 << hdr->log2_big_block_size)) {
313
+	if (cli_readn(fd, buff, (1 << hdr->log2_big_block_size)) != (1 << hdr->log2_big_block_size)) {
314 314
 		return FALSE;
315 315
 	}
316 316
 	return TRUE;
... ...
@@ -539,7 +488,7 @@ static int handler_writefile(int fd, ole2_header_t *hdr, property_t *prop, const
539 539
 			}
540 540
 			/* buff now contains the block with 8 small blocks in it */
541 541
 			offset = 64 * (current_block % 8);
542
-			if (writen(ofd, &buff[offset], MIN(len,64)) != MIN(len,64)) {
542
+			if (cli_writen(ofd, &buff[offset], MIN(len,64)) != MIN(len,64)) {
543 543
 				close(ofd);
544 544
 				return FALSE;
545 545
 			}
... ...
@@ -552,7 +501,7 @@ static int handler_writefile(int fd, ole2_header_t *hdr, property_t *prop, const
552 552
 				close(ofd);
553 553
 				return FALSE;
554 554
 			}
555
-			if (writen(ofd, &buff, MIN(len,(1 << hdr->log2_big_block_size))) !=
555
+			if (cli_writen(ofd, &buff, MIN(len,(1 << hdr->log2_big_block_size))) !=
556 556
 							MIN(len,(1 << hdr->log2_big_block_size))) {
557 557
 				close(ofd);
558 558
 				return FALSE;
... ...
@@ -570,56 +519,56 @@ static int ole2_read_header(int fd, ole2_header_t *hdr)
570 570
 {
571 571
 	int i;
572 572
 	
573
-	if (readn(fd, &hdr->magic, 8) != 8) {
573
+	if (cli_readn(fd, &hdr->magic, 8) != 8) {
574 574
 		return FALSE;
575 575
 	}
576
-	if (readn(fd, &hdr->clsid, 16) != 16) {
576
+	if (cli_readn(fd, &hdr->clsid, 16) != 16) {
577 577
 		return FALSE;
578 578
 	}
579
-	if (readn(fd, &hdr->minor_version, 2) != 2) {
579
+	if (cli_readn(fd, &hdr->minor_version, 2) != 2) {
580 580
 		return FALSE;
581 581
 	}
582
-	if (readn(fd, &hdr->dll_version, 2) != 2) {
582
+	if (cli_readn(fd, &hdr->dll_version, 2) != 2) {
583 583
 		return FALSE;
584 584
 	}
585
-	if (readn(fd, &hdr->byte_order, 2) != 2) {
585
+	if (cli_readn(fd, &hdr->byte_order, 2) != 2) {
586 586
 		return FALSE;
587 587
 	}
588
-	if (readn(fd, &hdr->log2_big_block_size, 2) != 2) {
588
+	if (cli_readn(fd, &hdr->log2_big_block_size, 2) != 2) {
589 589
 		return FALSE;
590 590
 	}
591
-	if (readn(fd, &hdr->log2_small_block_size, 4) != 4) {
591
+	if (cli_readn(fd, &hdr->log2_small_block_size, 4) != 4) {
592 592
 		return FALSE;
593 593
 	}
594
-	if (readn(fd, &hdr->reserved, 8) != 8) {
594
+	if (cli_readn(fd, &hdr->reserved, 8) != 8) {
595 595
 		return FALSE;
596 596
 	}
597
-	if (readn(fd, &hdr->bat_count, 4) != 4) {
597
+	if (cli_readn(fd, &hdr->bat_count, 4) != 4) {
598 598
 		return FALSE;
599 599
 	}
600
-	if (readn(fd, &hdr->prop_start, 4) != 4) {
600
+	if (cli_readn(fd, &hdr->prop_start, 4) != 4) {
601 601
 		return FALSE;
602 602
 	}
603
-	if (readn(fd, &hdr->signature, 4) != 4) {
603
+	if (cli_readn(fd, &hdr->signature, 4) != 4) {
604 604
 		return FALSE;
605 605
 	}
606
-	if (readn(fd, &hdr->sbat_cutoff, 4) != 4) {
606
+	if (cli_readn(fd, &hdr->sbat_cutoff, 4) != 4) {
607 607
 		return FALSE;
608 608
 	}
609
-	if (readn(fd, &hdr->sbat_start, 4) != 4) {
609
+	if (cli_readn(fd, &hdr->sbat_start, 4) != 4) {
610 610
 		return FALSE;
611 611
 	}
612
-	if (readn(fd, &hdr->sbat_block_count, 4) != 4) {
612
+	if (cli_readn(fd, &hdr->sbat_block_count, 4) != 4) {
613 613
 		return FALSE;
614 614
 	}
615
-	if (readn(fd, &hdr->xbat_start, 4) != 4) {
615
+	if (cli_readn(fd, &hdr->xbat_start, 4) != 4) {
616 616
 		return FALSE;
617 617
 	}
618
-	if (readn(fd, &hdr->xbat_count, 4) != 4) {
618
+	if (cli_readn(fd, &hdr->xbat_count, 4) != 4) {
619 619
 		return FALSE;
620 620
 	}
621 621
 	for (i=0 ; i < 109 ; i++) {
622
-		if (readn(fd, &hdr->bat_array[i], 4) != 4) {
622
+		if (cli_readn(fd, &hdr->bat_array[i], 4) != 4) {
623 623
 			return FALSE;
624 624
 		}
625 625
 	}
... ...
@@ -637,7 +586,7 @@ int cli_ole2_extract(int fd, const char *dirname)
637 637
 	hdr_size = sizeof(struct ole2_header_tag) - sizeof(int32_t);
638 638
 
639 639
 #if defined(HAVE_ATTRIB_PACKED) || defined(HAVE_PRAGMA_PACK)
640
-	if (readn(fd, &hdr, hdr_size) != hdr_size) {
640
+	if (cli_readn(fd, &hdr, hdr_size) != hdr_size) {
641 641
 		return 0;
642 642
 	}
643 643
 #else
... ...
@@ -360,3 +360,55 @@ int cli_rmdirs(const char *dirname)
360 360
     closedir(dd);
361 361
     return 0;
362 362
 }
363
+
364
+/* Function: readn
365
+        Try hard to read the requested number of bytes
366
+*/
367
+int cli_readn(int fd, void *buff, unsigned int count)
368
+{
369
+        int retval;
370
+        unsigned int todo;
371
+        unsigned char *current;
372
+                                                                                                                                    
373
+        todo = count;
374
+        current = (unsigned char *) buff;
375
+                                                                                                                                    
376
+        do {
377
+                retval = read(fd, current, todo);
378
+                if (retval == 0) {
379
+                        return (count - todo);
380
+                }
381
+                if (retval < 0) {
382
+                        return -1;
383
+                }
384
+                todo -= retval;
385
+                current += retval;
386
+        } while (todo > 0);
387
+                                                                                                                                    
388
+        return count;
389
+}
390
+                                                                                                                                    
391
+/* Function: writen
392
+        Try hard to write the specified number of bytes
393
+*/
394
+int cli_writen(int fd, void *buff, unsigned int count)
395
+{
396
+        int retval;
397
+        unsigned int todo;
398
+        unsigned char *current;
399
+                                                                                                                                    
400
+        todo = count;
401
+        current = (unsigned char *) buff;
402
+                                                                                                                                    
403
+        do {
404
+                retval = write(fd, current, todo);
405
+                if (retval < 0) {
406
+                        return -1;
407
+                }
408
+                todo -= retval;
409
+                current += retval;
410
+        } while (todo > 0);
411
+                                                                                                                                    
412
+        return count;
413
+}
414
+
... ...
@@ -30,5 +30,7 @@ void *cli_calloc(size_t nmemb, size_t size);
30 30
 void *cli_realloc(void *ptr, size_t size);
31 31
 int cli_rmdirs(const char *dirname);
32 32
 char *cli_md5stream(FILE *fd);
33
+int cli_readn(int fd, void *buff, unsigned int count);
34
+int cli_writen(int fd, void *buff, unsigned int count);
33 35
 
34 36
 #endif
... ...
@@ -111,33 +111,6 @@ vba_version_t vba_version[] = {
111 111
                                   2 +  /* type1 record count */ \
112 112
                                   2)   /* unknown */
113 113
 
114
-/* Function: vba_readn
115
-        Try hard to read the requested number of bytes
116
-*/
117
-static int vba_readn(int fd, void *buff, unsigned int count)
118
-{
119
-        int retval;
120
-        unsigned int todo;
121
-        unsigned char *current;
122
- 
123
-        todo = count;
124
-        current = (unsigned char *) buff;
125
- 
126
-        do {
127
-                retval = read(fd, current, todo);
128
-                if (retval == 0) {
129
-                        return (count - todo);
130
-                }
131
-                if (retval < 0) {
132
-                        return -1;
133
-                }
134
-                todo -= retval;
135
-                current += retval;
136
-        } while (todo > 0);
137
- 
138
-        return count;
139
-}
140
-
141 114
 static char *get_unicode_name(char *name, int size, int is_mac)
142 115
 {
143 116
         int i, j;
... ...
@@ -175,7 +148,7 @@ static void vba56_test_middle(int fd)
175 175
 		0x85, 0x2e, 0x02, 0x60, 0x8c, 0x4d, 0x0b, 0xb4, 0x00, 0x00
176 176
 	};
177 177
 
178
-        if (vba_readn(fd, &test_middle, 20) != 20) {
178
+        if (cli_readn(fd, &test_middle, 20) != 20) {
179 179
                 return;
180 180
         }
181 181
 
... ...
@@ -195,7 +168,7 @@ static int vba_read_project_strings(int fd, int is_mac)
195 195
 	uint32_t offset;
196 196
 
197 197
 	for (;;) {
198
-		if (vba_readn(fd, &length, 2) != 2) {
198
+		if (cli_readn(fd, &length, 2) != 2) {
199 199
 			return FALSE;
200 200
 		}
201 201
 		length = vba_endian_convert_16(length, is_mac);
... ...
@@ -210,9 +183,10 @@ static int vba_read_project_strings(int fd, int is_mac)
210 210
 			return FALSE;
211 211
 		}
212 212
 		offset = lseek(fd, 0, SEEK_CUR);
213
-		if (vba_readn(fd, buff, length) != length) {
213
+		if (cli_readn(fd, buff, length) != length) {
214 214
 			cli_dbgmsg("read name failed - rewinding\n");
215 215
 			lseek(fd, offset, SEEK_SET);
216
+			free(buff);
216 217
 			break;
217 218
 		}
218 219
 		name = get_unicode_name(buff, length, is_mac);
... ...
@@ -225,16 +199,17 @@ static int vba_read_project_strings(int fd, int is_mac)
225 225
 		/* TODO: Need to check if types H(same as G) and D(same as C) exist */
226 226
 		if (!strncmp ("*\\G", name, 3) || !strncmp ("*\\H", name, 3)
227 227
 			 	|| !strncmp("*\\C", name, 3) || !strncmp("*\\D", name, 3)) {
228
-			if (vba_readn(fd, &length, 2) != 2) {
228
+			if (cli_readn(fd, &length, 2) != 2) {
229 229
 				return FALSE;
230 230
 			}
231 231
 			length = vba_endian_convert_16(length, is_mac);
232 232
 			if (length != 0) {
233 233
 				lseek(fd, -2, SEEK_CUR);
234
+				free(name);
234 235
 				continue;
235 236
 			}
236 237
 			buff = (unsigned char *) cli_malloc(10);
237
-			if (vba_readn(fd, buff, 10) != 10) {
238
+			if (cli_readn(fd, buff, 10) != 10) {
238 239
 				cli_errmsg("failed to read blob\n");
239 240
 				free(buff);
240 241
 				free(name);
... ...
@@ -292,7 +267,7 @@ vba_project_t *vba56_dir_read(const char *dir)
292 292
         }
293 293
 	free(fullname);
294 294
 
295
-	if (vba_readn(fd, &magic, 2) != 2) {
295
+	if (cli_readn(fd, &magic, 2) != 2) {
296 296
 		close(fd);
297 297
 		return NULL;
298 298
 	}
... ...
@@ -301,7 +276,7 @@ vba_project_t *vba56_dir_read(const char *dir)
301 301
 		return NULL;
302 302
 	}
303 303
 
304
-	if (vba_readn(fd, &version, 4) != 4) {
304
+	if (cli_readn(fd, &version, 4) != 4) {
305 305
 		close(fd);
306 306
 		return NULL;
307 307
 	}
... ...
@@ -326,48 +301,48 @@ vba_project_t *vba56_dir_read(const char *dir)
326 326
 	/*****************************************/
327 327
 
328 328
 	/* two bytes, should be equal to 0x00ff */
329
-	if (vba_readn(fd, &ooff, 2) != 2) {
329
+	if (cli_readn(fd, &ooff, 2) != 2) {
330 330
 		close(fd);
331 331
 		return NULL;
332 332
 	}
333 333
 
334
-	if (vba_readn(fd, &LidA, 4) != 4) {
334
+	if (cli_readn(fd, &LidA, 4) != 4) {
335 335
 		close(fd);
336 336
 		return NULL;
337 337
 	}
338 338
 
339
-	if (vba_readn(fd, &LidB, 4) != 4) {
339
+	if (cli_readn(fd, &LidB, 4) != 4) {
340 340
 		close(fd);
341 341
 		return NULL;
342 342
 	}
343 343
 
344
-	if (vba_readn(fd, &CharSet, 2) != 2) {
344
+	if (cli_readn(fd, &CharSet, 2) != 2) {
345 345
 		close(fd);
346 346
 		return NULL;
347 347
 	}
348
-	if (vba_readn(fd, &LenA, 2) != 2) {
348
+	if (cli_readn(fd, &LenA, 2) != 2) {
349 349
 		close(fd);
350 350
 		return NULL;
351 351
 	}
352 352
 
353
-	if (vba_readn(fd, &UnknownB, 4) != 4) {
353
+	if (cli_readn(fd, &UnknownB, 4) != 4) {
354 354
 		close(fd);
355 355
 		return NULL;
356 356
 	}
357
-	if (vba_readn(fd, &UnknownC, 4) != 4) {
357
+	if (cli_readn(fd, &UnknownC, 4) != 4) {
358 358
 		close(fd);
359 359
 		return NULL;
360 360
 	}
361 361
 
362
-	if (vba_readn(fd, &LenB, 2) != 2) {
362
+	if (cli_readn(fd, &LenB, 2) != 2) {
363 363
 		close(fd);
364 364
 		return NULL;
365 365
 	}
366
-	if (vba_readn(fd, &LenC, 2) != 2) {
366
+	if (cli_readn(fd, &LenC, 2) != 2) {
367 367
 		close(fd);
368 368
 		return NULL;
369 369
 	}
370
-	if (vba_readn(fd, &LenD, 2) != 2) {
370
+	if (cli_readn(fd, &LenD, 2) != 2) {
371 371
 		close(fd);
372 372
 		return NULL;
373 373
 	}
... ...
@@ -393,7 +368,7 @@ vba_project_t *vba56_dir_read(const char *dir)
393 393
 	
394 394
 	/* junk some more stuff */
395 395
 	do {
396
-		if (vba_readn(fd, &ooff, 2) != 2) {
396
+		if (cli_readn(fd, &ooff, 2) != 2) {
397 397
 			close(fd);
398 398
 			return NULL;
399 399
 		}
... ...
@@ -401,7 +376,7 @@ vba_project_t *vba56_dir_read(const char *dir)
401 401
 
402 402
 	/* check for alignment error */
403 403
 	lseek(fd, -3, SEEK_CUR);
404
-	if (vba_readn(fd, &ooff, 2) != 2) {
404
+	if (cli_readn(fd, &ooff, 2) != 2) {
405 405
  		close(fd);
406 406
 		return NULL;
407 407
 	}
... ...
@@ -409,7 +384,7 @@ vba_project_t *vba56_dir_read(const char *dir)
409 409
 		lseek(fd, 1, SEEK_CUR);
410 410
 	}
411 411
 	
412
-	if (vba_readn(fd, &ooff, 2) != 2) {
412
+	if (cli_readn(fd, &ooff, 2) != 2) {
413 413
 		close(fd);
414 414
 		return NULL;
415 415
 	}
... ...
@@ -419,7 +394,7 @@ vba_project_t *vba56_dir_read(const char *dir)
419 419
 		ooff = vba_endian_convert_16(ooff, is_mac);
420 420
 		lseek(fd, ooff, SEEK_CUR);
421 421
 	}
422
-	if (vba_readn(fd, &ooff, 2) != 2) {
422
+	if (cli_readn(fd, &ooff, 2) != 2) {
423 423
 		close(fd);
424 424
 		return NULL;
425 425
 	}
... ...
@@ -429,7 +404,7 @@ vba_project_t *vba56_dir_read(const char *dir)
429 429
 	}
430 430
 	lseek(fd, 100, SEEK_CUR);
431 431
 
432
-	if (vba_readn(fd, &record_count, 2) != 2) {
432
+	if (cli_readn(fd, &record_count, 2) != 2) {
433 433
 		close(fd);
434 434
 		return NULL;
435 435
 	}
... ...
@@ -443,7 +418,7 @@ vba_project_t *vba56_dir_read(const char *dir)
443 443
 					record_count);
444 444
 	vba_project->count = record_count;
445 445
 	for (i=0 ; i < record_count ; i++) {
446
-		if (vba_readn(fd, &length, 2) != 2) {
446
+		if (cli_readn(fd, &length, 2) != 2) {
447 447
 			goto out_error;
448 448
 		}
449 449
 		length = vba_endian_convert_16(length, is_mac);
... ...
@@ -452,7 +427,7 @@ vba_project_t *vba56_dir_read(const char *dir)
452 452
 			cli_dbgmsg("cli_malloc failed\n");
453 453
 			goto out_error;
454 454
 		}
455
-		if (vba_readn(fd, buff, length) != length) {
455
+		if (cli_readn(fd, buff, length) != length) {
456 456
 			cli_dbgmsg("read name failed\n");
457 457
 			free(buff);
458 458
 			goto out_error;
... ...
@@ -462,7 +437,7 @@ vba_project_t *vba56_dir_read(const char *dir)
462 462
 		free(buff);
463 463
 
464 464
 		/* some kind of string identifier ?? */
465
-		if (vba_readn(fd, &length, 2) != 2) {
465
+		if (cli_readn(fd, &length, 2) != 2) {
466 466
 			free(vba_project->name[i]);
467 467
 			goto out_error;
468 468
 		}
... ...
@@ -470,14 +445,14 @@ vba_project_t *vba56_dir_read(const char *dir)
470 470
 		lseek(fd, length, SEEK_CUR);
471 471
 
472 472
 		/* unknown stuff */
473
-		if (vba_readn(fd, &ooff, 2) != 2) {
473
+		if (cli_readn(fd, &ooff, 2) != 2) {
474 474
 			free(vba_project->name[i]);
475 475
 			goto out_error;
476 476
 		}
477 477
 		ooff = vba_endian_convert_16(ooff, is_mac);
478 478
 		if (ooff == 0xFFFF) {
479 479
 			lseek(fd, 2, SEEK_CUR);
480
-			if (vba_readn(fd, &ooff, 2) != 2) {
480
+			if (cli_readn(fd, &ooff, 2) != 2) {
481 481
 				free(vba_project->name[i]);
482 482
 				goto out_error;
483 483
 			}
... ...
@@ -488,7 +463,7 @@ vba_project_t *vba56_dir_read(const char *dir)
488 488
 		}
489 489
 
490 490
 		lseek(fd, 8, SEEK_CUR);
491
-		if (vba_readn(fd, &byte_count, 1) != 1) {
491
+		if (cli_readn(fd, &byte_count, 1) != 1) {
492 492
 			free(vba_project->name[i]);
493 493
 			goto out_error;
494 494
 		}
... ...
@@ -496,7 +471,7 @@ vba_project_t *vba56_dir_read(const char *dir)
496 496
 			lseek(fd, 8, SEEK_CUR);
497 497
 		}
498 498
 		lseek(fd, 6, SEEK_CUR);
499
-		if (vba_readn(fd, &offset, 4) != 4) {
499
+		if (cli_readn(fd, &offset, 4) != 4) {
500 500
 			free(vba_project->name[i]);
501 501
 			goto out_error;
502 502
 		}
... ...
@@ -559,10 +534,10 @@ unsigned char *vba_decompress(int fd, uint32_t offset, int *size)
559 559
 	
560 560
 	lseek(fd, offset+3, SEEK_SET); /* 1byte ?? , 2byte length ?? */ 
561 561
 	
562
-	while (vba_readn(fd, &flag, 1) == 1) {
562
+	while (cli_readn(fd, &flag, 1) == 1) {
563 563
 		for (mask = 1; mask < 0x100; mask<<=1) {
564 564
 			if (flag & mask) {
565
-				if (vba_readn(fd, &token, 2) != 2) {
565
+				if (cli_readn(fd, &token, 2) != 2) {
566 566
 					if (result.data) {
567 567
 						free(result.data);
568 568
 					}
... ...
@@ -604,7 +579,7 @@ unsigned char *vba_decompress(int fd, uint32_t offset, int *size)
604 604
 				if ((pos != 0) &&
605 605
 					((pos % VBA_COMPRESSION_WINDOW) == 0) && clean) {
606 606
 					
607
-					if (vba_readn(fd, &token, 2) != 2) {
607
+					if (cli_readn(fd, &token, 2) != 2) {
608 608
 						if (result.data) {
609 609
 							free(result.data);
610 610
 						}
... ...
@@ -617,7 +592,7 @@ unsigned char *vba_decompress(int fd, uint32_t offset, int *size)
617 617
 					byte_array_append(&result, buffer, VBA_COMPRESSION_WINDOW);
618 618
 					break;
619 619
 				}
620
-				if (vba_readn(fd, buffer+(pos%VBA_COMPRESSION_WINDOW), 1) == 1){
620
+				if (cli_readn(fd, buffer+(pos%VBA_COMPRESSION_WINDOW), 1) == 1){
621 621
 					pos++;
622 622
 				}
623 623
 				clean = TRUE;