Browse code

removed double close()

git-svn: trunk@3307

Nigel Horne authored on 2007/10/23 05:33:20
Showing 2 changed files
... ...
@@ -1,3 +1,8 @@
1
+Mon Oct 22 20:39:10 BST 2007 (njh)
2
+----------------------------------
3
+  * libclamav/vba_extract.c:	Fixed warnings, removed double close() and
4
+  			some code tidies
5
+
1 6
 Thu Oct 18 20:42:11 EDT 2007 (tk)
2 7
 ---------------------------------
3 8
   * libclamav/dsig.c: fix integer wrap introduced in r3305 (bb#688)
... ...
@@ -4,7 +4,7 @@
4 4
  *  Copyright (C) 2004-2005 trog@uncon.org
5 5
  *
6 6
  *  This code is based on the OpenOffice and libgsf sources.
7
- *                  
7
+ *
8 8
  *  This program is free software; you can redistribute it and/or modify
9 9
  *  it under the terms of the GNU General Public License as published by
10 10
  *  the Free Software Foundation; either version 2 of the License, or
... ...
@@ -20,6 +20,9 @@
20 20
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 21
  *  MA 02110-1301, USA.
22 22
  */
23
+#if HAVE_CONFIG_H
24
+#include "clamav-config.h"
25
+#endif
23 26
 
24 27
 #include <stdio.h>
25 28
 #include <string.h>
... ...
@@ -35,12 +38,9 @@
35 35
 
36 36
 #include "clamav.h"
37 37
 
38
-#if HAVE_CONFIG_H
39
-#include "clamav-config.h"
40
-#endif
41
-
42 38
 #include "vba_extract.h"
43 39
 #include "others.h"
40
+#include "blob.h"
44 41
 
45 42
 #ifndef	O_BINARY
46 43
 #define	O_BINARY	0
... ...
@@ -61,7 +61,7 @@ static uint16_t vba_endian_convert_16(uint16_t value, int is_mac)
61 61
 	else
62 62
 		return le16_to_host(value);
63 63
 }
64
- 
64
+
65 65
 static uint32_t vba_endian_convert_32(uint32_t value, int is_mac)
66 66
 {
67 67
 	if (is_mac)
... ...
@@ -70,13 +70,8 @@ static uint32_t vba_endian_convert_32(uint32_t value, int is_mac)
70 70
 		return le32_to_host(value);
71 71
 }
72 72
 
73
-typedef struct byte_array_tag {
74
-	unsigned int length;
75
-	unsigned char *data;
76
-} byte_array_t;
77
-
78 73
 #define NUM_VBA_VERSIONS 14
79
-vba_version_t vba_version[] = {
74
+static const vba_version_t vba_version[] = {
80 75
 	{ { 0x5e, 0x00, 0x00, 0x01 }, "Office 97",              5, FALSE},
81 76
 	{ { 0x5f, 0x00, 0x00, 0x01 }, "Office 97 SR1",          5, FALSE },
82 77
 	{ { 0x65, 0x00, 0x00, 0x01 }, "Office 2000 alpha?",     6, FALSE },
... ...
@@ -101,7 +96,8 @@ vba_version_t vba_version[] = {
101 101
                                   2 +  /* type1 record count */ \
102 102
                                   2)   /* unknown */
103 103
 
104
-static char *get_unicode_name(char *name, int size, int is_mac)
104
+static char *
105
+get_unicode_name(const char *name, int size, int is_mac)
105 106
 {
106 107
         int i, j;
107 108
         char *newname;
... ...
@@ -126,11 +122,11 @@ static char *get_unicode_name(char *name, int size, int is_mac)
126 126
 			else {
127 127
 				const uint16_t x = (((uint16_t)name[i]) << 8) | name[i+1];
128 128
 				newname[j++] = '_';
129
-				newname[j++] = 'a'+((x&0xF));
130
-				newname[j++] = 'a'+((x>>4)&0xF);
131
-				newname[j++] = 'a'+((x>>8)&0xF);
132
-				newname[j++] = 'a'+((x>>16)&0xF);
133
-				newname[j++] = 'a'+((x>>24)&0xF);
129
+				newname[j++] = (char)('a'+((x&0xF)));
130
+				newname[j++] = (char)('a'+((x>>4)&0xF));
131
+				newname[j++] = (char)('a'+((x>>8)&0xF));
132
+				newname[j++] = (char)('a'+((x>>16)&0xF));	/* FIXME: x>>16 MUST == 0 */
133
+				newname[j++] = (char)('a'+((x>>24)&0xF));	/* FIXME: x>>24 MUST == 0 */
134 134
 			}
135 135
                         newname[j++] = '_';
136 136
                 }
... ...
@@ -150,7 +146,7 @@ static void vba56_test_middle(int fd)
150 150
 	};
151 151
 	/* MS Office middle */
152 152
 	static const uint8_t middle2_str[20] = {
153
-		0x00, 0x00, 0xe1, 0x2e, 0x45, 0x0d, 0x8f, 0xe0, 0x1a, 0x10, 
153
+		0x00, 0x00, 0xe1, 0x2e, 0x45, 0x0d, 0x8f, 0xe0, 0x1a, 0x10,
154 154
 		0x85, 0x2e, 0x02, 0x60, 0x8c, 0x4d, 0x0b, 0xb4, 0x00, 0x00
155 155
 	};
156 156
 
... ...
@@ -171,7 +167,8 @@ static void vba56_test_middle(int fd)
171 171
 static int vba_read_project_strings(int fd, int is_mac)
172 172
 {
173 173
 	uint16_t length;
174
-	unsigned char *buff, *name;
174
+	unsigned char *buff;
175
+	char *name;
175 176
 	uint32_t offset;
176 177
 
177 178
 	for (;;) {
... ...
@@ -196,7 +193,7 @@ static int vba_read_project_strings(int fd, int is_mac)
196 196
 			free(buff);
197 197
 			break;
198 198
 		}
199
-		name = get_unicode_name(buff, length, is_mac);
199
+		name = get_unicode_name((const char *)buff, length, is_mac);
200 200
 		if (name) {
201 201
 			cli_dbgmsg("name: %s\n", name);
202 202
 		} else {
... ...
@@ -209,7 +206,7 @@ static int vba_read_project_strings(int fd, int is_mac)
209 209
 		   having a 12 byte trailer */
210 210
 		/* TODO: Need to check if types H(same as G) and D(same as C) exist */
211 211
 		if (name && (!strncmp ("*\\G", name, 3) || !strncmp ("*\\H", name, 3)
212
-			 	|| !strncmp("*\\C", name, 3) || !strncmp("*\\D", name, 3))) {
212
+				|| !strncmp("*\\C", name, 3) || !strncmp("*\\D", name, 3))) {
213 213
 			if (cli_readn(fd, &length, 2) != 2) {
214 214
 				return FALSE;
215 215
 			}
... ...
@@ -254,7 +251,7 @@ vba_project_t *vba56_dir_read(const char *dir)
254 254
 	unsigned char magic[2];
255 255
 	unsigned char version[4];
256 256
 	unsigned char *buff;
257
-        unsigned char vba56_signature[] = { 0xcc, 0x61 };
257
+        const unsigned char vba56_signature[] = { 0xcc, 0x61 };
258 258
 	uint16_t record_count, length;
259 259
 	uint16_t ooff;
260 260
 	uint16_t byte_count;
... ...
@@ -321,7 +318,7 @@ vba_project_t *vba56_dir_read(const char *dir)
321 321
 			cli_warnmsg("Unable to guess VBA type\n");
322 322
 			close(fd);
323 323
 			return NULL;
324
-		}	
324
+		}
325 325
 	} else {
326 326
 		cli_dbgmsg("VBA Project: %s, VBA Version=%d\n", vba_version[i].name,
327 327
                                 vba_version[i].vba_version);
... ...
@@ -395,7 +392,7 @@ vba_project_t *vba56_dir_read(const char *dir)
395 395
 		close(fd);
396 396
 		return NULL;
397 397
 	}
398
-	
398
+
399 399
 	/* junk some more stuff */
400 400
 	do {
401 401
 		if (cli_readn(fd, &ooff, 2) != 2) {
... ...
@@ -407,13 +404,13 @@ vba_project_t *vba56_dir_read(const char *dir)
407 407
 	/* check for alignment error */
408 408
 	lseek(fd, -3, SEEK_CUR);
409 409
 	if (cli_readn(fd, &ooff, 2) != 2) {
410
- 		close(fd);
410
+		close(fd);
411 411
 		return NULL;
412 412
 	}
413 413
 	if (ooff != 0xFFFF) {
414 414
 		lseek(fd, 1, SEEK_CUR);
415 415
 	}
416
-	
416
+
417 417
 	if (cli_readn(fd, &ooff, 2) != 2) {
418 418
 		close(fd);
419 419
 		return NULL;
... ...
@@ -450,7 +447,7 @@ vba_project_t *vba56_dir_read(const char *dir)
450 450
 		close(fd);
451 451
 		return NULL;
452 452
 	}
453
-	
453
+
454 454
 	vba_project = (vba_project_t *) cli_malloc(sizeof(struct vba_project_tag));
455 455
 	if (!vba_project) {
456 456
 		close(fd);
... ...
@@ -492,7 +489,7 @@ vba_project_t *vba56_dir_read(const char *dir)
492 492
 			free(buff);
493 493
 			goto out_error;
494 494
 		}
495
-		vba_project->name[i] = get_unicode_name(buff, length, is_mac);
495
+		vba_project->name[i] = get_unicode_name((const char *)buff, length, is_mac);
496 496
 		if (!vba_project->name[i]) {
497 497
 			offset = lseek(fd, 0, SEEK_CUR);
498 498
 			vba_project->name[i] = (char *) cli_malloc(18);
... ...
@@ -546,10 +543,10 @@ vba_project_t *vba56_dir_read(const char *dir)
546 546
 		cli_dbgmsg("offset:%u\n", offset);
547 547
 		lseek(fd, 2, SEEK_CUR);
548 548
 	}
549
-	
550
-	
549
+
550
+
551 551
 	{ /* There appears to be some code in here */
552
-	
552
+
553 553
 	off_t foffset;
554 554
 
555 555
 		foffset = lseek(fd, 0, SEEK_CUR);
... ...
@@ -574,45 +571,28 @@ out_error:
574 574
 
575 575
 #define VBA_COMPRESSION_WINDOW 4096
576 576
 
577
-static void byte_array_append(byte_array_t *array, unsigned char *src, unsigned int len)
578
-{
579
-	if (array->length == 0) {
580
-		array->data = (unsigned char *) cli_malloc(len);
581
-		if (!array->data) {
582
-			return;
583
-		}
584
-		array->length = len;
585
-		memcpy(array->data, src, len);
586
-	} else {
587
-		array->data = realloc(array->data, array->length+len);
588
-		if (!array->data) {
589
-			return;
590
-		}	
591
-		memcpy(array->data+array->length, src, len);
592
-		array->length += len;
593
-	}
594
-}
595
-
596 577
 unsigned char *vba_decompress(int fd, uint32_t offset, int *size)
597 578
 {
598 579
 	unsigned int i, pos=0, shift, win_pos, clean=TRUE, mask, distance;
599 580
 	uint8_t flag;
600 581
 	uint16_t token, len;
582
+	size_t s;
583
+	blob *b;
584
+	unsigned char *ret;
601 585
 	unsigned char buffer[VBA_COMPRESSION_WINDOW];
602
-	byte_array_t result;
603
-	
604
-	result.length=0;
605
-	result.data=NULL;
606
-	
607
-	lseek(fd, offset+3, SEEK_SET); /* 1byte ?? , 2byte length ?? */ 
608
-	
586
+
587
+	b = blobCreate();
588
+
589
+	if(b == NULL)
590
+		return NULL;
591
+
592
+	lseek(fd, offset+3, SEEK_SET); /* 1byte ?? , 2byte length ?? */
593
+
609 594
 	while (cli_readn(fd, &flag, 1) == 1) {
610 595
 		for (mask = 1; mask < 0x100; mask<<=1) {
611 596
 			if (flag & mask) {
612 597
 				if (cli_readn(fd, &token, 2) != 2) {
613
-					if (result.data) {
614
-						free(result.data);
615
-					}
598
+					blobDestroy(b);
616 599
 					if (size) {
617 600
 						*size = 0;
618 601
 					}
... ...
@@ -635,14 +615,14 @@ unsigned char *vba_decompress(int fd, uint32_t offset, int *size)
635 635
 						shift = 4;
636 636
 					}
637 637
 				}
638
-				len = (token & ((1 << shift) -1)) + 3;
638
+				len = (uint16_t)((token & ((1 << shift) -1)) + 3);
639 639
 				distance = token >> shift;
640 640
 				clean = TRUE;
641
-				
641
+
642 642
 				for (i=0 ; i < len; i++) {
643 643
 					unsigned int srcpos;
644 644
 					unsigned char c;
645
-					
645
+
646 646
 					srcpos = (pos - distance - 1) % VBA_COMPRESSION_WINDOW;
647 647
 					c = buffer[srcpos];
648 648
 					buffer[pos++ % VBA_COMPRESSION_WINDOW]= c;
... ...
@@ -650,18 +630,15 @@ unsigned char *vba_decompress(int fd, uint32_t offset, int *size)
650 650
 			} else {
651 651
 				if ((pos != 0) &&
652 652
 					((pos % VBA_COMPRESSION_WINDOW) == 0) && clean) {
653
-					
653
+
654 654
 					if (cli_readn(fd, &token, 2) != 2) {
655
-						if (result.data) {
656
-							free(result.data);
657
-						}
658
-						if (size) {
659
-                                         	       *size = 0;
660
-                                        	}
655
+						blobDestroy(b);
656
+						if(size)
657
+						       *size = 0;
661 658
 						return NULL;
662 659
 					}
663 660
 					clean = FALSE;
664
-					byte_array_append(&result, buffer, VBA_COMPRESSION_WINDOW);
661
+					(void)blobAddData(b, buffer, VBA_COMPRESSION_WINDOW);
665 662
 					break;
666 663
 				}
667 664
 				if (cli_readn(fd, buffer+(pos%VBA_COMPRESSION_WINDOW), 1) == 1){
... ...
@@ -671,34 +648,45 @@ unsigned char *vba_decompress(int fd, uint32_t offset, int *size)
671 671
 			}
672 672
 		}
673 673
 	}
674
-			
675
-	if (pos % VBA_COMPRESSION_WINDOW) {
676
-		byte_array_append(&result, buffer, pos % VBA_COMPRESSION_WINDOW);
677
-	}
678
-	if (size) {
679
-		*size = result.length;
680
-	}
681
-	return result.data;
682 674
 
675
+	if (pos % VBA_COMPRESSION_WINDOW)
676
+		if(blobAddData(b, buffer, pos%VBA_COMPRESSION_WINDOW) < 0) {
677
+			if(size)
678
+			       *size = 0;
679
+			blobDestroy(b);
680
+			return NULL;
681
+		}
682
+	s = blobGetDataSize(b);
683
+	ret = cli_malloc(s);
684
+	if(ret == NULL) {
685
+		blobDestroy(b);
686
+		if(size)
687
+		       *size = 0;
688
+		return NULL;
689
+	}
690
+	if(size)
691
+		*size = (int)s;
692
+	memcpy(ret, blobGetData(b), s);
693
+	blobDestroy(b);
694
+	return ret;
683 695
 }
684 696
 
685 697
 static uint32_t ole_copy_file_data(int ifd, int ofd, uint32_t len)
686 698
 {
687
-        unsigned char data[8192];
688 699
         unsigned int count, rem;
689
-        unsigned int todo;
700
+        unsigned char data[FILEBUFF];
690 701
 
691 702
         rem = len;
692 703
 
693 704
         while (rem > 0) {
694
-                todo = MIN(8192, rem);
705
+                unsigned int todo = MIN(sizeof(data), rem);
706
+
695 707
                 count = cli_readn(ifd, data, todo);
696 708
                 if (count != todo) {
697 709
                         return len-rem;
698 710
                 }
699
-                if (cli_writen(ofd, data, count) != count) {
711
+                if((unsigned int)cli_writen(ofd, data, count) != count)
700 712
                         return len-rem-count;
701
-                }
702 713
                 rem -= count;
703 714
         }
704 715
         return len;
... ...
@@ -714,7 +702,7 @@ int cli_decode_ole_object(int fd, const char *dir)
714 714
 	if (fstat(fd, &statbuf) == -1) {
715 715
 		return -1;
716 716
 	}
717
-	
717
+
718 718
 	if (cli_readn(fd, &object_size, 4) != 4) {
719 719
 		return -1;
720 720
 	}
... ...
@@ -725,39 +713,41 @@ int cli_decode_ole_object(int fd, const char *dir)
725 725
 		if (lseek(fd, 2, SEEK_CUR) == -1) {
726 726
 			return -1;
727 727
 		}
728
-		
728
+
729 729
 		/* Skip attachment name */
730 730
 		do {
731 731
 			if (cli_readn(fd, &ch, 1) != 1) {
732 732
 				return -1;
733 733
 			}
734 734
 		} while (ch);
735
-		
735
+
736 736
 		/* Skip attachment full path */
737 737
 		do {
738 738
 			if (cli_readn(fd, &ch, 1) != 1) {
739 739
 				return -1;
740 740
 			}
741 741
 		} while (ch);
742
-		
742
+
743 743
 		/* Skip unknown data */
744 744
 		if (lseek(fd, 8, SEEK_CUR) == -1) {
745 745
 			return -1;
746 746
 		}
747
-		
747
+
748 748
 		/* Skip attachment full path */
749 749
 		do {
750 750
 			if (cli_readn(fd, &ch, 1) != 1) {
751 751
 				return -1;
752 752
 			}
753 753
 		} while (ch);
754
-		
754
+
755 755
 		if (cli_readn(fd, &object_size, 4) != 4) {
756 756
 			return -1;
757 757
 		}
758 758
 		object_size = vba_endian_convert_32(object_size, FALSE);
759 759
 	}
760 760
 	fullname = cli_malloc(strlen(dir) + 18);
761
+	if(fullname == NULL)
762
+		return -1;
761 763
 	sprintf(fullname, "%s/_clam_ole_object", dir);
762 764
 	ofd = open(fullname, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0600);
763 765
 	free(fullname);
... ...
@@ -790,7 +780,7 @@ static int ppt_read_atom_header(int fd, atom_header_t *atom_header)
790 790
 		return FALSE;
791 791
 	}
792 792
 	atom_header->ver_inst = vba_endian_convert_16(atom_header->ver_inst, FALSE);
793
-	atom_header->version = atom_header->ver_inst & 0x000f;
793
+	atom_header->version = (uint8_t)(atom_header->ver_inst & 0x000f);
794 794
 	atom_header->instance = atom_header->ver_inst >> 4;
795 795
 	if (cli_readn(fd, &atom_header->type, 2) != 2) {
796 796
 		cli_dbgmsg("read ppt_current_user failed\n");
... ...
@@ -822,41 +812,41 @@ static int ppt_unlzw(const char *dir, int fd, uint32_t length)
822 822
 	char *fullname;
823 823
 	uint32_t bufflen;
824 824
 	z_stream stream;
825
-	
825
+
826 826
 	fullname = cli_malloc(strlen(dir) + 17);
827 827
 	if (!fullname) {
828 828
 		return FALSE;
829 829
 	}
830
-	sprintf(fullname, "%s/ppt%.8lx.doc", dir, lseek(fd, 0, SEEK_CUR));
831
-	
830
+	sprintf(fullname, "%s/ppt%.8lx.doc", dir, (long)lseek(fd, 0L, SEEK_CUR));
831
+
832 832
 	ofd = open(fullname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600);
833 833
 	free(fullname);
834 834
         if (ofd == -1) {
835 835
                 cli_dbgmsg("ppt_unlzw Open outfile failed\n");
836 836
                 return FALSE;
837 837
         }
838
-	
838
+
839 839
 	stream.zalloc = Z_NULL;
840 840
 	stream.zfree = Z_NULL;
841 841
 	stream.opaque = (void *)0;
842
-	
842
+
843 843
 	stream.next_in = inbuff;
844 844
 	bufflen = stream.avail_in = MIN(length, PPT_LZW_BUFFSIZE);
845
-	
845
+
846 846
 	if (cli_readn(fd, inbuff, stream.avail_in) != (int64_t)stream.avail_in) {
847 847
 		close(ofd);
848 848
 		return FALSE;
849 849
 	}
850 850
 	length -= stream.avail_in;
851
-	
851
+
852 852
 	retval = inflateInit(&stream);
853 853
 	if (retval != Z_OK) {
854 854
 		cli_dbgmsg(" ppt_unlzw !Z_OK: %d\n", retval);
855 855
 	}
856
-	
856
+
857 857
 	stream.next_out = outbuff;
858 858
 	stream.avail_out = PPT_LZW_BUFFSIZE;
859
-	
859
+
860 860
 	do {
861 861
 		if (stream.avail_out == 0) {
862 862
 			if (cli_writen(ofd, outbuff, PPT_LZW_BUFFSIZE)
... ...
@@ -880,7 +870,7 @@ static int ppt_unlzw(const char *dir, int fd, uint32_t length)
880 880
 		}
881 881
 		retval = inflate(&stream, Z_NO_FLUSH);
882 882
 	} while (retval == Z_OK);
883
-	
883
+
884 884
 	if (cli_writen(ofd, outbuff, bufflen) != (int64_t)bufflen) {
885 885
 		close(ofd);
886 886
 		inflateEnd(&stream);
... ...
@@ -893,24 +883,20 @@ static int ppt_unlzw(const char *dir, int fd, uint32_t length)
893 893
 
894 894
 static char *ppt_stream_iter(int fd)
895 895
 {
896
-	atom_header_t atom_header;
897 896
 	uint32_t ole_id;
898 897
 	char *out_dir;
899 898
 	off_t offset;
900
-	
899
+	atom_header_t atom_header;
900
+
901 901
 	/* Create a directory to store the extracted OLE2 objects */
902 902
 	out_dir = cli_gentemp(NULL);
903 903
 	if(mkdir(out_dir, 0700)) {
904
-	    printf("ScanOLE2 -> Can't create temporary directory %s\n", out_dir);
905
-	    free(out_dir);
906
-	    close(fd);
907
-	    return NULL;
904
+		cli_errmsg("ScanOLE2 -> Can't create temporary directory %s\n", out_dir);
905
+		free(out_dir);
906
+		return NULL;
908 907
 	}
909 908
 
910
-	while (1) {
911
-		if (!ppt_read_atom_header(fd, &atom_header)) {
912
-			break;
913
-		}
909
+	while(ppt_read_atom_header(fd, &atom_header)) {
914 910
 		ppt_print_atom_header(&atom_header);
915 911
 
916 912
 		if (atom_header.length == 0) {
... ...
@@ -928,7 +914,7 @@ static char *ppt_stream_iter(int fd)
928 928
 			}
929 929
 			ole_id = vba_endian_convert_32(ole_id, FALSE);
930 930
 			cli_dbgmsg("OleID: %d, length: %d\n",
931
-					ole_id, atom_header.length-4);
931
+					(int)ole_id, (int)atom_header.length-4);
932 932
 			if (!ppt_unlzw(out_dir, fd, atom_header.length-4)) {
933 933
 				cli_dbgmsg("ppt_unlzw failed\n");
934 934
 				cli_rmdirs(out_dir);
... ...
@@ -967,7 +953,7 @@ char *ppt_vba_read(const char *dir)
967 967
 		cli_dbgmsg("Open  PowerPoint Document failed\n");
968 968
 		return NULL;
969 969
 	}
970
-	
970
+
971 971
 	out_dir = ppt_stream_iter(fd);
972 972
 	close(fd);
973 973
 	return out_dir;
... ...
@@ -1054,6 +1040,10 @@ typedef struct mac_token2_tag {
1054 1054
 
1055 1055
 } mac_token2_t;
1056 1056
 
1057
+static	void	wm_free_intnames(macro_intnames_t *macro_intnames);
1058
+static	void	wm_free_extnames(macro_extnames_t *macro_extnames);
1059
+static	void	wm_free_macro_info(macro_info_t *macro_info);
1060
+
1057 1061
 static void wm_print_fib(mso_fib_t *fib)
1058 1062
 {
1059 1063
 	cli_dbgmsg("magic: 0x%.4x\n", fib->magic);
... ...
@@ -1063,7 +1053,7 @@ static void wm_print_fib(mso_fib_t *fib)
1063 1063
 	cli_dbgmsg("macro offset: 0x%.4x\n", fib->macro_offset);
1064 1064
 	cli_dbgmsg("macro len: 0x%.4x\n\n", fib->macro_len);
1065 1065
 }
1066
-	
1066
+
1067 1067
 static int wm_read_fib(int fd, mso_fib_t *fib)
1068 1068
 {
1069 1069
 	if (cli_readn(fd, &fib->magic, 2) != 2) {
... ...
@@ -1081,7 +1071,7 @@ static int wm_read_fib(int fd, mso_fib_t *fib)
1081 1081
 	if (cli_readn(fd, &fib->lid, 2) != 2) {
1082 1082
 		cli_dbgmsg("read wm_fib failed\n");
1083 1083
 		return FALSE;
1084
-	}	
1084
+	}
1085 1085
 	if (cli_readn(fd, &fib->next, 2) != 2) {
1086 1086
 		cli_dbgmsg("read wm_fib failed\n");
1087 1087
 		return FALSE;
... ...
@@ -1090,13 +1080,13 @@ static int wm_read_fib(int fd, mso_fib_t *fib)
1090 1090
 		cli_dbgmsg("read wm_fib failed\n");
1091 1091
 		return FALSE;
1092 1092
 	}
1093
-	
1093
+
1094 1094
 	/* don't need the information is this block, so seek forward */
1095 1095
 	if (lseek(fd, 0x118, SEEK_SET) != 0x118) {
1096 1096
 		cli_dbgmsg("lseek wm_fib failed\n");
1097 1097
 		return FALSE;
1098 1098
 	}
1099
-	
1099
+
1100 1100
 	if (cli_readn(fd, &fib->macro_offset, 4) != 4) {
1101 1101
 		cli_dbgmsg("read wm_fib failed\n");
1102 1102
 		return FALSE;
... ...
@@ -1113,7 +1103,7 @@ static int wm_read_fib(int fd, mso_fib_t *fib)
1113 1113
 	fib->status = vba_endian_convert_16(fib->status, FALSE);
1114 1114
 	fib->macro_offset = vba_endian_convert_32(fib->macro_offset, FALSE);
1115 1115
 	fib->macro_len = vba_endian_convert_32(fib->macro_len, FALSE);
1116
-	
1116
+
1117 1117
 	return TRUE;
1118 1118
 }
1119 1119
 
... ...
@@ -1130,7 +1120,7 @@ static int wm_read_macro_entry(int fd, macro_entry_t *macro_entry)
1130 1130
 	if (cli_readn(fd, &macro_entry->intname_i, 2) != 2) {
1131 1131
 		cli_dbgmsg("read macro_entry failed\n");
1132 1132
 		return FALSE;
1133
-	}	
1133
+	}
1134 1134
 	if (cli_readn(fd, &macro_entry->extname_i, 2) != 2) {
1135 1135
 		cli_dbgmsg("read macro_entry failed\n");
1136 1136
 		return FALSE;
... ...
@@ -1155,7 +1145,7 @@ static int wm_read_macro_entry(int fd, macro_entry_t *macro_entry)
1155 1155
 		cli_dbgmsg("read macro_entry failed\n");
1156 1156
 		return FALSE;
1157 1157
 	}
1158
-	
1158
+
1159 1159
 	macro_entry->intname_i = vba_endian_convert_16(macro_entry->intname_i, FALSE);
1160 1160
 	macro_entry->extname_i = vba_endian_convert_16(macro_entry->extname_i, FALSE);
1161 1161
 	macro_entry->xname_i = vba_endian_convert_16(macro_entry->xname_i, FALSE);
... ...
@@ -1190,21 +1180,20 @@ static macro_info_t *wm_read_macro_info(int fd)
1190 1190
 	for (i=0 ; i < macro_info->count ; i++) {
1191 1191
 		if (!wm_read_macro_entry(fd,
1192 1192
 				&macro_info->macro_entry[i])) {
1193
-			free(macro_info->macro_entry);
1194
-			free(macro_info);
1193
+			wm_free_macro_info(macro_info);
1195 1194
 			return NULL;
1196 1195
 		}
1197 1196
 	}
1198 1197
 	return macro_info;
1199 1198
 }
1200 1199
 
1201
-static void wm_free_macro_info(macro_info_t *macro_info)
1200
+static	void
1201
+wm_free_macro_info(macro_info_t *macro_info)
1202 1202
 {
1203 1203
 	if (macro_info) {
1204 1204
 		free(macro_info->macro_entry);
1205 1205
 		free(macro_info);
1206 1206
 	}
1207
-	return;
1208 1207
 }
1209 1208
 
1210 1209
 static int wm_read_oxo3(int fd)
... ...
@@ -1220,7 +1209,7 @@ static int wm_read_oxo3(int fd)
1220 1220
 		return FALSE;
1221 1221
 	}
1222 1222
 	cli_dbgmsg("oxo3 records1: %d\n", count);
1223
-	
1223
+
1224 1224
 	if (cli_readn(fd, &count, 1) != 1) {
1225 1225
 		cli_dbgmsg("read oxo3 record2 failed\n");
1226 1226
 		return FALSE;
... ...
@@ -1244,7 +1233,7 @@ static int wm_read_oxo3(int fd)
1244 1244
 			cli_dbgmsg("lseek oxo3 failed\n");
1245 1245
 			return FALSE;
1246 1246
 		}
1247
-	}				
1247
+	}
1248 1248
 	cli_dbgmsg("oxo3 records2: %d\n", count);
1249 1249
 	return TRUE;
1250 1250
 }
... ...
@@ -1254,12 +1243,12 @@ static menu_info_t *wm_read_menu_info(int fd)
1254 1254
 	int i;
1255 1255
 	menu_info_t *menu_info;
1256 1256
 	menu_entry_t *menu_entry;
1257
-	
1257
+
1258 1258
 	menu_info = (menu_info_t *) cli_malloc(sizeof(menu_info_t));
1259 1259
 	if (!menu_info) {
1260 1260
 		return NULL;
1261 1261
 	}
1262
-	
1262
+
1263 1263
 	if (cli_readn(fd, &menu_info->count, 2) != 2) {
1264 1264
 		cli_dbgmsg("read menu_info failed\n");
1265 1265
 		free(menu_info);
... ...
@@ -1267,14 +1256,14 @@ static menu_info_t *wm_read_menu_info(int fd)
1267 1267
 	}
1268 1268
 	menu_info->count = vba_endian_convert_16(menu_info->count, FALSE);
1269 1269
 	cli_dbgmsg("menu_info count: %d\n", menu_info->count);
1270
-	
1270
+
1271 1271
 	menu_info->menu_entry =
1272 1272
 		(menu_entry_t *) cli_malloc(sizeof(menu_entry_t) * menu_info->count);
1273 1273
 	if (!menu_info->menu_entry) {
1274 1274
 		free(menu_info);
1275 1275
 		return NULL;
1276 1276
 	}
1277
-	
1277
+
1278 1278
 	for (i=0 ; i < menu_info->count ; i++) {
1279 1279
 		menu_entry = &menu_info->menu_entry[i];
1280 1280
 		if (cli_readn(fd, &menu_entry->context, 2) != 2) {
... ...
@@ -1303,7 +1292,7 @@ static menu_info_t *wm_read_menu_info(int fd)
1303 1303
 		cli_dbgmsg("menu entry: %d.%d\n", menu_entry->menu, menu_entry->pos);
1304 1304
 	}
1305 1305
 	return menu_info;
1306
-	
1306
+
1307 1307
 abort:
1308 1308
 	cli_dbgmsg("read menu_entry failed\n");
1309 1309
 	free(menu_info->menu_entry);
... ...
@@ -1317,25 +1306,24 @@ static void wm_free_menu_info(menu_info_t *menu_info)
1317 1317
 		free(menu_info->menu_entry);
1318 1318
 		free(menu_info);
1319 1319
 	}
1320
-	return;
1321 1320
 }
1322 1321
 
1323 1322
 static macro_extnames_t *wm_read_macro_extnames(int fd)
1324 1323
 {
1325
-	int i, is_unicode=0;
1324
+	int is_unicode=0;
1326 1325
 	int16_t size;
1327
-	off_t offset_end;	
1326
+	off_t offset_end;
1328 1327
 	macro_extnames_t *macro_extnames;
1329 1328
 	macro_extname_t *macro_extname;
1330 1329
 	unsigned char *name_tmp;
1331
-	
1332
-	macro_extnames = (macro_extnames_t *) cli_malloc(sizeof(macro_extnames_t));
1330
+
1331
+	macro_extnames = (macro_extnames_t *)cli_malloc(sizeof(macro_extnames_t));
1333 1332
 	if (!macro_extnames) {
1334 1333
 		return NULL;
1335 1334
 	}
1336 1335
 	macro_extnames->count = 0;
1337 1336
 	macro_extnames->macro_extname = NULL;
1338
-	
1337
+
1339 1338
 	offset_end = lseek(fd, 0, SEEK_CUR);
1340 1339
 	if (cli_readn(fd, &size, 2) != 2) {
1341 1340
 		cli_dbgmsg("read macro_extnames failed\n");
... ...
@@ -1362,7 +1350,7 @@ static macro_extnames_t *wm_read_macro_extnames(int fd)
1362 1362
 				sizeof(macro_extname_t) * macro_extnames->count);
1363 1363
 		if (macro_extnames->macro_extname == NULL) {
1364 1364
 			cli_dbgmsg("read macro_extnames failed\n");
1365
-			goto abort;;
1365
+			goto abort;
1366 1366
 		}
1367 1367
 
1368 1368
 		macro_extname = &macro_extnames->macro_extname[macro_extnames->count-1];
... ...
@@ -1373,21 +1361,22 @@ static macro_extnames_t *wm_read_macro_extnames(int fd)
1373 1373
 			}
1374 1374
 			lseek(fd, 1, SEEK_CUR);
1375 1375
 			if (macro_extname->length > 0) {
1376
-			    name_tmp = (char *) cli_malloc(macro_extname->length*2);
1377
-			    if (name_tmp == NULL) {
1376
+			    name_tmp = (unsigned char *)cli_malloc(macro_extname->length*2);
1377
+			    if(name_tmp == NULL)
1378 1378
 				goto abort;
1379
-			    }
1380
-			    if (cli_readn(fd, name_tmp, macro_extname->length*2) != 
1379
+			    if (cli_readn(fd, name_tmp, macro_extname->length*2) !=
1381 1380
 						macro_extname->length*2) {
1382 1381
 				cli_dbgmsg("read macro_extnames failed\n");
1383 1382
 				free(name_tmp);
1384 1383
 				goto abort;
1385 1384
 			    }
1386 1385
 			    macro_extname->extname =
1387
-				get_unicode_name(name_tmp, macro_extname->length*2, FALSE);
1386
+				(unsigned char *)get_unicode_name((const char *)name_tmp, macro_extname->length*2, FALSE);
1388 1387
 			    free(name_tmp);
1389 1388
 			} else {
1390
-			    macro_extname->extname = cli_strdup("[no name]");
1389
+			    macro_extname->extname = (unsigned char *)cli_strdup("[no name]");
1390
+			    if(macro_extname->extname == NULL)
1391
+				goto abort;
1391 1392
 			    macro_extname->length = 10;
1392 1393
 			}
1393 1394
 		} else {
... ...
@@ -1396,11 +1385,10 @@ static macro_extnames_t *wm_read_macro_extnames(int fd)
1396 1396
 				goto abort;
1397 1397
 			}
1398 1398
 			if (macro_extname->length > 0) {
1399
-			    macro_extname->extname = (char *) cli_malloc(macro_extname->length+1);
1400
-			    if (!macro_extname->extname) {
1399
+			    macro_extname->extname = (unsigned char *)cli_malloc(macro_extname->length+1);
1400
+			    if(macro_extname->extname == NULL)
1401 1401
 				goto abort;
1402
-			    }
1403
-			    if (cli_readn(fd, macro_extname->extname, macro_extname->length) != 
1402
+			    if (cli_readn(fd, macro_extname->extname, macro_extname->length) !=
1404 1403
 						macro_extname->length) {
1405 1404
 				cli_dbgmsg("read macro_extnames failed\n");
1406 1405
 				free(macro_extname->extname);
... ...
@@ -1408,63 +1396,58 @@ static macro_extnames_t *wm_read_macro_extnames(int fd)
1408 1408
 			    }
1409 1409
 			    macro_extname->extname[macro_extname->length] = '\0';
1410 1410
 			} else {
1411
-			    macro_extname->extname = cli_strdup("[no name]");
1411
+			    macro_extname->extname = (unsigned char *)cli_strdup("[no name]");
1412
+			    if(macro_extname->extname == NULL)
1413
+				goto abort;
1412 1414
 			    macro_extname->length = 10;
1413 1415
 			}
1414 1416
 		}
1415 1417
 		if (cli_readn(fd, &macro_extname->numref, 2) != 2) {
1416 1418
 			cli_dbgmsg("read macro_extnames failed\n");
1417
-			return NULL;
1418
-		}	
1419
+			goto abort;
1420
+		}
1419 1421
 		macro_extname->numref = vba_endian_convert_16(macro_extname->numref, FALSE);
1420 1422
 		cli_dbgmsg("ext name: %s\n", macro_extname->extname);
1421 1423
 	}
1422 1424
 	return macro_extnames;
1423
-	
1425
+
1424 1426
 abort:
1425
-	if (macro_extnames->macro_extname != NULL) {
1426
-		for (i=0 ; i < macro_extnames->count-1 ; i++) {
1427
-			free(macro_extnames->macro_extname[i].extname);
1428
-		}
1429
-		free(macro_extnames->macro_extname);
1430
-	}
1431
-	free(macro_extnames);
1427
+	macro_extnames->count--;
1428
+	wm_free_extnames(macro_extnames);
1432 1429
 	return NULL;
1433 1430
 }
1434 1431
 
1435
-static void wm_free_extnames(macro_extnames_t *macro_extnames)
1432
+static void
1433
+wm_free_extnames(macro_extnames_t *macro_extnames)
1436 1434
 {
1437
-	int i;
1438
-	
1439 1435
 	if (macro_extnames) {
1440
-		for (i=0 ; i < macro_extnames->count ; i++) {
1436
+		int i;
1437
+
1438
+		for(i = 0 ;i < macro_extnames->count; i++)
1441 1439
 			free(macro_extnames->macro_extname[i].extname);
1442
-		}
1443 1440
 		free(macro_extnames->macro_extname);
1444 1441
 		free(macro_extnames);
1445 1442
 	}
1446
-	return;
1447 1443
 }
1448 1444
 
1449 1445
 static macro_intnames_t *wm_read_macro_intnames(int fd)
1450 1446
 {
1451
-	int i;
1447
+	uint16_t i, junk;
1452 1448
 	macro_intnames_t *macro_intnames;
1453 1449
 	macro_intname_t *macro_intname;
1454
-	uint16_t junk;
1455
-	
1450
+
1456 1451
 	macro_intnames = (macro_intnames_t *) cli_malloc(sizeof(macro_intnames_t));
1457 1452
 	if (!macro_intnames) {
1458 1453
 		return NULL;
1459 1454
 	}
1460
-	
1455
+
1461 1456
 	if (cli_readn(fd, &macro_intnames->count, 2) != 2) {
1462 1457
 		cli_dbgmsg("read macro_intnames failed\n");
1463 1458
 		return NULL;
1464 1459
 	}
1465 1460
 	macro_intnames->count = vba_endian_convert_16(macro_intnames->count, FALSE);
1466 1461
 	cli_dbgmsg("int names count: %d\n", macro_intnames->count);
1467
-	
1462
+
1468 1463
 	macro_intnames->macro_intname =
1469 1464
 		(macro_intname_t *) cli_malloc(sizeof(macro_intname_t) * macro_intnames->count);
1470 1465
 	if (!macro_intnames->macro_intname) {
... ...
@@ -1482,9 +1465,9 @@ static macro_intnames_t *wm_read_macro_intnames(int fd)
1482 1482
 		if (cli_readn(fd, &macro_intname->length, 1) != 1) {
1483 1483
 			cli_dbgmsg("read macro_intnames failed\n");
1484 1484
 			macro_intnames->count = i;
1485
-			goto abort;;
1486
-		}	
1487
-		macro_intname->intname = (char *) cli_malloc(macro_intname->length+1);
1485
+			goto abort;
1486
+		}
1487
+		macro_intname->intname = (unsigned char *)cli_malloc(macro_intname->length+1);
1488 1488
 		if (!macro_intname->intname) {
1489 1489
 			macro_intnames->count = i;
1490 1490
 			goto abort;
... ...
@@ -1504,26 +1487,21 @@ static macro_intnames_t *wm_read_macro_intnames(int fd)
1504 1504
 	}
1505 1505
 	return macro_intnames;
1506 1506
 abort:
1507
-	for (i=0 ; i < macro_intnames->count ; i++) {
1508
-		free(macro_intnames->macro_intname[i].intname);
1509
-	}
1510
-	free(macro_intnames->macro_intname);
1511
-	free(macro_intnames);
1507
+	wm_free_intnames(macro_intnames);
1512 1508
 	return NULL;
1513 1509
 }
1514 1510
 
1515
-static void wm_free_intnames(macro_intnames_t *macro_intnames)
1511
+static void
1512
+wm_free_intnames(macro_intnames_t *macro_intnames)
1516 1513
 {
1517
-	int i;
1518
-	
1519 1514
 	if (macro_intnames) {
1520
-		for (i=0 ; i < macro_intnames->count ; i++) {
1515
+		int i;
1516
+
1517
+		for(i = 0; i < macro_intnames->count; i++)
1521 1518
 			free(macro_intnames->macro_intname[i].intname);
1522
-		}
1523 1519
 		free(macro_intnames->macro_intname);
1524 1520
 		free(macro_intnames);
1525 1521
 	}
1526
-	return;
1527 1522
 }
1528 1523
 
1529 1524
 vba_project_t *wm_dir_read(const char *dir)
... ...
@@ -1538,7 +1516,7 @@ vba_project_t *wm_dir_read(const char *dir)
1538 1538
 	macro_intnames_t *macro_intnames=NULL;
1539 1539
 	vba_project_t *vba_project=NULL;
1540 1540
 	char *fullname;
1541
-	
1541
+
1542 1542
 	fullname = (char *) cli_malloc(strlen(dir) + 14);
1543 1543
 	if (!fullname) {
1544 1544
 		return NULL;
... ...
@@ -1550,28 +1528,28 @@ vba_project_t *wm_dir_read(const char *dir)
1550 1550
 		cli_dbgmsg("Open WordDocument failed\n");
1551 1551
 		return NULL;
1552 1552
 	}
1553
-	
1553
+
1554 1554
 	if (!wm_read_fib(fd, &fib)) {
1555 1555
 		close(fd);
1556 1556
 		return NULL;
1557 1557
 	}
1558 1558
 	wm_print_fib(&fib);
1559
-	
1559
+
1560 1560
 	if (lseek(fd, fib.macro_offset, SEEK_SET) != (int64_t)fib.macro_offset) {
1561 1561
 		cli_dbgmsg("lseek macro_offset failed\n");
1562 1562
 		close(fd);
1563 1563
 		return NULL;
1564 1564
 	}
1565
-	
1565
+
1566 1566
 	end_offset = fib.macro_offset + fib.macro_len;
1567
-	
1567
+
1568 1568
 	if (cli_readn(fd, &start_id, 1) != 1) {
1569 1569
 		cli_dbgmsg("read start_id failed\n");
1570 1570
 		close(fd);
1571 1571
 		return NULL;
1572 1572
 	}
1573 1573
 	cli_dbgmsg("start_id: %d\n", start_id);
1574
-	
1574
+
1575 1575
 	while ((lseek(fd, 0, SEEK_CUR) < end_offset) && !done) {
1576 1576
 		if (cli_readn(fd, &info_id, 1) != 1) {
1577 1577
 			cli_dbgmsg("read macro_info failed\n");
... ...
@@ -1606,7 +1584,7 @@ vba_project_t *wm_dir_read(const char *dir)
1606 1606
 				macro_intnames = wm_read_macro_intnames(fd);
1607 1607
 				if (macro_intnames == NULL) {
1608 1608
 					done = TRUE;
1609
-				}				
1609
+				}
1610 1610
 				break;
1611 1611
 			case 0x12:
1612 1612
 				/* No sure about these, always seems to
... ...
@@ -1623,7 +1601,7 @@ vba_project_t *wm_dir_read(const char *dir)
1623 1623
 				done = 1;
1624 1624
 		}
1625 1625
 	}
1626
-	
1626
+
1627 1627
 	if (macro_info) {
1628 1628
 		vba_project = (vba_project_t *) cli_malloc(sizeof(struct vba_project_tag));
1629 1629
 		if (!vba_project) {
... ...
@@ -1640,7 +1618,8 @@ vba_project_t *wm_dir_read(const char *dir)
1640 1640
 					macro_info->count);
1641 1641
 		if (!vba_project->offset) {
1642 1642
 			free(vba_project->name);
1643
-			free(vba_project->dir);
1643
+			if(vba_project->dir)
1644
+				free(vba_project->dir);
1644 1645
 			free(vba_project);
1645 1646
 			vba_project = NULL;
1646 1647
 			goto abort;
... ...
@@ -1697,7 +1676,7 @@ unsigned char *wm_decrypt_macro(int fd, uint32_t offset, uint32_t len,
1697 1697
 {
1698 1698
 	unsigned char *buff;
1699 1699
 	uint32_t i;
1700
-	
1700
+
1701 1701
 	if (lseek(fd, offset, SEEK_SET) != (int64_t)offset) {
1702 1702
 		return NULL;
1703 1703
 	}
... ...
@@ -1710,10 +1689,8 @@ unsigned char *wm_decrypt_macro(int fd, uint32_t offset, uint32_t len,
1710 1710
 		free(buff);
1711 1711
 		return NULL;
1712 1712
 	}
1713
-	if (key != 0) {
1714
-		for (i=0 ; i < len; i++) {
1715
-			buff[i] = buff[i] ^ key;
1716
-		}
1717
-	}
1713
+	if (key != 0)
1714
+		for (i=0 ; i < len; i++)
1715
+			buff[i] ^= key;
1718 1716
 	return buff;
1719 1717
 }