Browse code

sigtool/sigtool.c: improve handling of bytecode.info (bb#2292)

Tomasz Kojm authored on 2011/02/16 03:20:05
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Feb 15 19:19:31 CET 2011 (tk)
2
+---------------------------------
3
+ * sigtool/sigtool.c: improve handling of bytecode.info (bb#2292)
4
+
1 5
 Thu Apr 14 21:29:36 EEST 2011 (edwin)
2 6
 -------------------------------------
3 7
  * libclamav/others.c: make sure TLS key is initializer (bb #2588).
... ...
@@ -110,12 +110,6 @@ static const struct dblist_s {
110 110
     { NULL,	    0 }
111 111
 };
112 112
 
113
-struct dblist_scan
114
-{
115
-    char *name;
116
-    struct dblist_scan *next;
117
-};
118
-
119 113
 static const char *getdbname(const char *str)
120 114
 {
121 115
     if(strstr(str, "main"))
... ...
@@ -413,7 +407,7 @@ static char *sha256file(const char *file, unsigned int *size)
413 413
     return sha;
414 414
 }
415 415
 
416
-static int writeinfo(const char *dbname, const char *builder, const char *header, const struct optstruct *opts, const struct dblist_scan *dbl)
416
+static int writeinfo(const char *dbname, const char *builder, const char *header, const struct optstruct *opts, char * const *dblist2, unsigned int dblist2cnt)
417 417
 {
418 418
 	FILE *fh;
419 419
 	unsigned int i, bytes;
... ...
@@ -440,21 +434,20 @@ static int writeinfo(const char *dbname, const char *builder, const char *header
440 440
 	return -1;
441 441
     }
442 442
 
443
-    if(dbl) {
444
-	while(dbl) {
445
-	    if(!(pt = sha256file(dbl->name, &bytes))) {
443
+    if(dblist2cnt) {
444
+	for(i = 0; i < dblist2cnt; i++) {
445
+	    if(!(pt = sha256file(dblist2[i], &bytes))) {
446 446
 		mprintf("!writeinfo: Can't generate SHA256 for %s\n", file);
447 447
 		fclose(fh);
448 448
 		return -1;
449 449
 	    }
450
-	    if(fprintf(fh, "%s:%u:%s\n", dbl->name, bytes, pt) < 0) {
450
+	    if(fprintf(fh, "%s:%u:%s\n", dblist2[i], bytes, pt) < 0) {
451 451
 		mprintf("!writeinfo: Can't write to info file\n");
452 452
 		fclose(fh);
453 453
 		free(pt);
454 454
 		return -1;
455 455
 	    }
456 456
 	    free(pt);
457
-	    dbl = dbl->next;
458 457
 	}
459 458
     } else {
460 459
 	for(i = 0; dblist[i].name; i++) {
... ...
@@ -612,6 +605,11 @@ static int script2cdiff(const char *script, const char *builder, const struct op
612 612
     return 0;
613 613
 }
614 614
 
615
+static int qcompare(const void *a, const void *b)
616
+{
617
+    return strcmp(*(char * const *) a, *(char * const *) b);
618
+}
619
+
615 620
 static int build(const struct optstruct *opts)
616 621
 {
617 622
 	int ret, bc = 0;
... ...
@@ -627,17 +625,15 @@ static int build(const struct optstruct *opts)
627 627
 	time_t timet;
628 628
 	struct tm *brokent;
629 629
 	struct cl_cvd *oldcvd;
630
-	struct dblist_scan *dblist2 = NULL, *lspt;
630
+	char **dblist2 = NULL;
631
+	unsigned int dblist2cnt = 0;
631 632
 	DIR *dd;
632 633
 	struct dirent *dent;
633 634
 
634
-#define FREE_LS(x)	    \
635
-    while(x) {		    \
636
-	lspt = x;	    \
637
-	x = x->next;	    \
638
-	free(lspt->name);   \
639
-	free(lspt);	    \
640
-    }
635
+#define FREE_LS(x)		    \
636
+    for(i = 0; i < dblist2cnt; i++) \
637
+	free(x[i]);		    \
638
+    free(x);
641 639
 
642 640
     if(!optget(opts, "server")->enabled) {
643 641
 	mprintf("!build: --server is required for --build\n");
... ...
@@ -679,46 +675,42 @@ static int build(const struct optstruct *opts)
679 679
 	    while((dent = readdir(dd))) {
680 680
 		if(dent->d_ino) {
681 681
 		    if(cli_strbcasestr(dent->d_name, ".cbc")) {
682
-			lspt = (struct dblist_scan *) malloc(sizeof(struct dblist_scan));
683
-			if(!lspt) {
684
-			    FREE_LS(dblist2);
682
+			dblist2 = (char **) realloc(dblist2, (dblist2cnt + 1) * sizeof(char *));
683
+			if(!dblist2) { /* dblist2 leaked but we don't really care */
685 684
 			    mprintf("!build: Memory allocation error\n");
686 685
 			    return -1;
687 686
 			}
688
-			lspt->name = strdup(dent->d_name);
689
-			if(!lspt->name) {
687
+			dblist2[dblist2cnt] = strdup(dent->d_name);
688
+			if(!dblist2[dblist2cnt]) {
690 689
 			    FREE_LS(dblist2);
691 690
 			    mprintf("!build: Memory allocation error\n");
692 691
 			    return -1;
693 692
 			}
694
-			lspt->next = dblist2;
695
-			dblist2 = lspt;
696
-			entries++;
693
+			dblist2cnt++;
697 694
 		    }
698 695
 		}
699 696
 	    }
700 697
 	    closedir(dd);
698
+	    entries += dblist2cnt;
699
+	    qsort(dblist2, dblist2cnt, sizeof(char *), qcompare);
700
+
701 701
 	    if(!access("last.hdb", R_OK)) {
702
-		if(!dblist2) {
702
+		if(!dblist2cnt) {
703 703
 		    mprintf("!build: dblist2 == NULL (no .cbc files?)\n");
704 704
 		    return -1;
705 705
 		}
706
-		lspt = dblist2;
707
-		while(lspt->next)
708
-		    lspt = lspt->next;
709
-		lspt->next = (struct dblist_scan *) malloc(sizeof(struct dblist_scan));
710
-		if(!lspt->next) {
711
-		    FREE_LS(dblist2);
706
+		dblist2 = (char **) realloc(dblist2, (dblist2cnt + 1) * sizeof(char *));
707
+		if(!dblist2) {
712 708
 		    mprintf("!build: Memory allocation error\n");
713 709
 		    return -1;
714 710
 		}
715
-		lspt->next->name = strdup("last.hdb");
716
-		lspt->next->next = NULL;
717
-		if(!lspt->next->name) {
711
+		dblist2[dblist2cnt] = strdup("last.hdb");
712
+		if(!dblist2[dblist2cnt]) {
718 713
 		    FREE_LS(dblist2);
719 714
 		    mprintf("!build: Memory allocation error\n");
720 715
 		    return -1;
721 716
 		}
717
+		dblist2cnt++;
722 718
 		entries += countlines("last.hdb");
723 719
 	    }
724 720
 	} else {
... ...
@@ -831,7 +823,7 @@ static int build(const struct optstruct *opts)
831 831
     /* add current time */
832 832
     sprintf(header + strlen(header), ":%u", (unsigned int) timet);
833 833
 
834
-    if(writeinfo(dbname, builder, header, opts, dblist2) == -1) {
834
+    if(writeinfo(dbname, builder, header, opts, dblist2, dblist2cnt) == -1) {
835 835
 	mprintf("!build: Can't generate info file\n");
836 836
 	FREE_LS(dblist2);
837 837
 	return -1;
... ...
@@ -869,16 +861,14 @@ static int build(const struct optstruct *opts)
869 869
 	    FREE_LS(dblist2);
870 870
 	    return -1;
871 871
 	}
872
-	lspt = dblist2;
873
-	while(lspt) {
874
-	    if(tar_addfile(-1, tar, lspt->name) == -1) {
872
+	for(i = 0; i < dblist2cnt; i++) {
873
+	    if(tar_addfile(-1, tar, dblist2[i]) == -1) {
875 874
 		gzclose(tar);
876 875
 		unlink(tarfile);
877 876
 		free(tarfile);
878 877
 		FREE_LS(dblist2);
879 878
 		return -1;
880 879
 	    }
881
-	    lspt = lspt->next;
882 880
 	}
883 881
     } else {
884 882
 	for(i = 0; dblist[i].name; i++) {