Browse code

code cleanup

git-svn: trunk@3269

Tomasz Kojm authored on 2007/10/05 22:29:59
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Fri Oct  5 14:30:38 CEST 2007 (tk)
2
+----------------------------------
3
+  * libclamav/readdb.c: code cleanup
4
+
1 5
 Fri Oct  5 11:02:07 BST 2007 (njh)
2 6
 ----------------------------------
3 7
   * clamav-milter:	Corrected comment on the meaning of NTRIES
... ...
@@ -599,11 +599,14 @@ static int scomp(const void *a, const void *b)
599 599
     return *(const uint32_t *)a - *(const uint32_t *)b;
600 600
 }
601 601
 
602
-static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo, unsigned short mode, unsigned int options)
602
+#define MD5_HDB	    0
603
+#define MD5_MDB	    1
604
+#define MD5_FP	    2
605
+static int cli_loadmd5(FILE *fd, struct cl_engine **engine, unsigned int *signo, uint8_t mode, unsigned int options)
603 606
 {
604 607
 	char buffer[FILEBUFF], *pt;
605 608
 	int ret = CL_SUCCESS;
606
-	uint8_t md5f = 0, sizef = 1, found;
609
+	uint8_t size_field = 1, md5_field = 0, found;
607 610
 	uint32_t line = 0, i;
608 611
 	struct cli_md5_node *new;
609 612
 	struct cli_bm_patt *bm_new;
... ...
@@ -615,9 +618,9 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
615 615
 	return ret;
616 616
     }
617 617
 
618
-    if(mode == 2) {
619
-	md5f = 1;
620
-	sizef = 0;
618
+    if(mode == MD5_MDB) {
619
+	size_field = 0;
620
+	md5_field = 1;
621 621
     }
622 622
 
623 623
     while(fgets(buffer, FILEBUFF, fd)) {
... ...
@@ -630,17 +633,17 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
630 630
 	    break;
631 631
 	}
632 632
 
633
-	if(mode == 1) /* fp */
633
+	if(mode == MD5_FP) /* fp */
634 634
 	    new->fp = 1;
635 635
 
636
-	if(!(pt = cli_strtok(buffer, md5f, ":"))) {
636
+	if(!(pt = cli_strtok(buffer, md5_field, ":"))) {
637 637
 	    free(new);
638 638
 	    ret = CL_EMALFDB;
639 639
 	    break;
640 640
 	}
641 641
 
642 642
 	if(!(new->md5 = (unsigned char *) cli_hex2str(pt))) {
643
-	    cli_errmsg("cli_loadhdb: Malformed MD5 string at line %u\n", line);
643
+	    cli_errmsg("cli_loadmd5: Malformed MD5 string at line %u\n", line);
644 644
 	    free(pt);
645 645
 	    free(new);
646 646
 	    ret = CL_EMALFDB;
... ...
@@ -648,7 +651,7 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
648 648
 	}
649 649
 	free(pt);
650 650
 
651
-	if(!(pt = cli_strtok(buffer, sizef, ":"))) {
651
+	if(!(pt = cli_strtok(buffer, size_field, ":"))) {
652 652
 	    free(new->md5);
653 653
 	    free(new);
654 654
 	    ret = CL_EMALFDB;
... ...
@@ -664,7 +667,7 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
664 664
 	    break;
665 665
 	}
666 666
 
667
-	if(mode == 2) { /* section MD5 */
667
+	if(mode == MD5_MDB) { /* section MD5 */
668 668
 	    if(!(*engine)->md5_sect) {
669 669
 		(*engine)->md5_sect = (struct cli_matcher *) cli_calloc(sizeof(struct cli_matcher), 1);
670 670
 		if(!(*engine)->md5_sect) {
... ...
@@ -675,7 +678,7 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
675 675
 		    break;
676 676
 		}
677 677
 		if((ret = cli_bm_init((*engine)->md5_sect))) {
678
-		    cli_errmsg("cli_loadhdb: Can't initialise BM pattern matcher\n");
678
+		    cli_errmsg("cli_loadmd5: Can't initialise BM pattern matcher\n");
679 679
 		    free(new->virname);
680 680
 		    free(new->md5);
681 681
 		    free(new);
... ...
@@ -686,7 +689,7 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
686 686
 
687 687
 	    bm_new = (struct cli_bm_patt *) cli_calloc(1, sizeof(struct cli_bm_patt));
688 688
 	    if(!bm_new) {
689
-		cli_errmsg("cli_loadhdb: Can't allocate memory for bm_new\n");
689
+		cli_errmsg("cli_loadmd5: Can't allocate memory for bm_new\n");
690 690
 		free(new->virname);
691 691
 		free(new->md5);
692 692
 		free(new);
... ...
@@ -710,7 +713,7 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
710 710
 		md5_sect->soff_len++;
711 711
 		md5_sect->soff = (uint32_t *) cli_realloc2(md5_sect->soff, md5_sect->soff_len * sizeof(uint32_t));
712 712
 		if(!md5_sect->soff) {
713
-		    cli_errmsg("cli_loadhdb: Can't realloc md5_sect->soff\n");
713
+		    cli_errmsg("cli_loadmd5: Can't realloc md5_sect->soff\n");
714 714
 		    free(bm_new->pattern);
715 715
 		    free(bm_new->virname);
716 716
 		    free(bm_new);
... ...
@@ -724,7 +727,7 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
724 724
 	    free(new);
725 725
 
726 726
 	    if((ret = cli_bm_addpatt(md5_sect, bm_new))) {
727
-		cli_errmsg("cli_loadhdb: Error adding BM pattern\n");
727
+		cli_errmsg("cli_loadmd5: Error adding BM pattern\n");
728 728
 		free(bm_new->pattern);
729 729
 		free(bm_new->virname);
730 730
 		free(bm_new);
... ...
@@ -733,7 +736,7 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
733 733
 
734 734
 	} else {
735 735
 	    if(!(*engine)->md5_hlist) {
736
-		cli_dbgmsg("cli_loadhdb: Initializing MD5 list structure\n");
736
+		cli_dbgmsg("cli_loadmd5: Initializing MD5 list structure\n");
737 737
 		(*engine)->md5_hlist = cli_calloc(256, sizeof(struct cli_md5_node *));
738 738
 		if(!(*engine)->md5_hlist) {
739 739
 		    free(new->virname);
... ...
@@ -750,13 +753,13 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
750 750
     }
751 751
 
752 752
     if(!line) {
753
-	cli_errmsg("cli_loadhdb: Empty database file\n");
753
+	cli_errmsg("cli_loadmd5: Empty database file\n");
754 754
 	cl_free(*engine);
755 755
 	return CL_EMALFDB;
756 756
     }
757 757
 
758 758
     if(ret) {
759
-	cli_errmsg("cli_loadhdb: Problem parsing database at line %u\n", line);
759
+	cli_errmsg("cli_loadmd5: Problem parsing database at line %u\n", line);
760 760
 	cl_free(*engine);
761 761
 	return ret;
762 762
     }
... ...
@@ -951,9 +954,6 @@ static int cli_load(const char *filename, struct cl_engine **engine, unsigned in
951 951
 	uint8_t skipped = 0;
952 952
 
953 953
 
954
-    if(cli_strbcasestr(filename, ".inc"))
955
-	return cli_loaddbdir(filename, engine, signo, options);
956
-
957 954
     if((fd = fopen(filename, "rb")) == NULL) {
958 955
 	cli_errmsg("cli_load(): Can't open file %s\n", filename);
959 956
 	return CL_EOPEN;
... ...
@@ -971,23 +971,23 @@ static int cli_load(const char *filename, struct cl_engine **engine, unsigned in
971 971
 	ret = cli_cvdload(fd, engine, signo, warn, options);
972 972
 
973 973
     } else if(cli_strbcasestr(filename, ".hdb")) {
974
-	ret = cli_loadhdb(fd, engine, signo, 0, options);
974
+	ret = cli_loadmd5(fd, engine, signo, MD5_HDB, options);
975 975
 
976 976
     } else if(cli_strbcasestr(filename, ".hdu")) {
977 977
 	if(options & CL_DB_PUA)
978
-	    ret = cli_loadhdb(fd, engine, signo, 0, options);
978
+	    ret = cli_loadmd5(fd, engine, signo, MD5_HDB, options);
979 979
 	else
980 980
 	    skipped = 1;
981 981
 
982 982
     } else if(cli_strbcasestr(filename, ".fp")) {
983
-	ret = cli_loadhdb(fd, engine, signo, 1, options);
983
+	ret = cli_loadmd5(fd, engine, signo, MD5_FP, options);
984 984
 
985 985
     } else if(cli_strbcasestr(filename, ".mdb")) {
986
-	ret = cli_loadhdb(fd, engine, signo, 2, options);
986
+	ret = cli_loadmd5(fd, engine, signo, MD5_MDB, options);
987 987
 
988 988
     } else if(cli_strbcasestr(filename, ".mdu")) {
989 989
 	if(options & CL_DB_PUA)
990
-	    ret = cli_loadhdb(fd, engine, signo, 2, options);
990
+	    ret = cli_loadmd5(fd, engine, signo, MD5_MDB, options);
991 991
 	else
992 992
 	    skipped = 1;
993 993
 
... ...
@@ -1044,6 +1044,27 @@ int cl_loaddb(const char *filename, struct cl_engine **engine, unsigned int *sig
1044 1044
     return cli_load(filename, engine, signo, CL_DB_STDOPT);
1045 1045
 }
1046 1046
 
1047
+#define CLI_DBEXT(ext)				\
1048
+    (						\
1049
+	cli_strbcasestr(ext, ".db")    ||	\
1050
+	cli_strbcasestr(ext, ".db2")   ||	\
1051
+	cli_strbcasestr(ext, ".db3")   ||	\
1052
+	cli_strbcasestr(ext, ".hdb")   ||	\
1053
+	cli_strbcasestr(ext, ".hdu")   ||	\
1054
+	cli_strbcasestr(ext, ".fp")    ||	\
1055
+	cli_strbcasestr(ext, ".mdb")   ||	\
1056
+	cli_strbcasestr(ext, ".mdu")   ||	\
1057
+	cli_strbcasestr(ext, ".ndb")   ||	\
1058
+	cli_strbcasestr(ext, ".ndu")   ||	\
1059
+	cli_strbcasestr(ext, ".sdb")   ||	\
1060
+	cli_strbcasestr(ext, ".zmd")   ||	\
1061
+	cli_strbcasestr(ext, ".rmd")   ||	\
1062
+	cli_strbcasestr(ext, ".pdb")   ||	\
1063
+	cli_strbcasestr(ext, ".wdb")   ||	\
1064
+	cli_strbcasestr(ext, ".inc")   ||	\
1065
+	cli_strbcasestr(ext, ".cvd")		\
1066
+    )
1067
+
1047 1068
 static int cli_loaddbdir_l(const char *dirname, struct cl_engine **engine, unsigned int *signo, unsigned int options)
1048 1069
 {
1049 1070
 	DIR *dd;
... ...
@@ -1090,24 +1111,7 @@ static int cli_loaddbdir_l(const char *dirname, struct cl_engine **engine, unsig
1090 1090
 	if(dent->d_ino)
1091 1091
 #endif
1092 1092
 	{
1093
-	    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") &&
1094
-	    (cli_strbcasestr(dent->d_name, ".db")   ||
1095
-	     cli_strbcasestr(dent->d_name, ".db2")  ||
1096
-	     cli_strbcasestr(dent->d_name, ".db3")  ||
1097
-	     cli_strbcasestr(dent->d_name, ".hdb")  ||
1098
-	     cli_strbcasestr(dent->d_name, ".hdu")  ||
1099
-	     cli_strbcasestr(dent->d_name, ".fp")   ||
1100
-	     cli_strbcasestr(dent->d_name, ".mdb")  ||
1101
-	     cli_strbcasestr(dent->d_name, ".mdu")  ||
1102
-	     cli_strbcasestr(dent->d_name, ".ndb")  ||
1103
-	     cli_strbcasestr(dent->d_name, ".ndu")  ||
1104
-	     cli_strbcasestr(dent->d_name, ".sdb")  ||
1105
-	     cli_strbcasestr(dent->d_name, ".zmd")  ||
1106
-	     cli_strbcasestr(dent->d_name, ".rmd")  ||
1107
-	     cli_strbcasestr(dent->d_name, ".pdb")  ||
1108
-	     cli_strbcasestr(dent->d_name, ".wdb")  ||
1109
-	     cli_strbcasestr(dent->d_name, ".inc")  ||
1110
-	     cli_strbcasestr(dent->d_name, ".cvd"))) {
1093
+	    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") && CLI_DBEXT(dent->d_name)) {
1111 1094
 
1112 1095
 		dbfile = (char *) cli_malloc(strlen(dent->d_name) + strlen(dirname) + 2);
1113 1096
 
... ...
@@ -1117,7 +1121,13 @@ static int cli_loaddbdir_l(const char *dirname, struct cl_engine **engine, unsig
1117 1117
 		    return CL_EMEM;
1118 1118
 		}
1119 1119
 		sprintf(dbfile, "%s/%s", dirname, dent->d_name);
1120
-		if((ret = cli_load(dbfile, engine, signo, options))) {
1120
+
1121
+		if(cli_strbcasestr(dbfile, ".inc"))
1122
+		    ret = cli_loaddbdir(dbfile, engine, signo, options);
1123
+		else
1124
+		    ret = cli_load(dbfile, engine, signo, options);
1125
+
1126
+		if(ret) {
1121 1127
 		    cli_dbgmsg("cli_loaddbdir(): error loading database %s\n", dbfile);
1122 1128
 		    free(dbfile);
1123 1129
 		    closedir(dd);
... ...
@@ -1246,26 +1256,7 @@ int cl_statinidir(const char *dirname, struct cl_stat *dbstat)
1246 1246
 	if(dent->d_ino)
1247 1247
 #endif
1248 1248
 	{
1249
-	    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") &&
1250
-	    (cli_strbcasestr(dent->d_name, ".db")  ||
1251
-	    cli_strbcasestr(dent->d_name, ".db2")  || 
1252
-	    cli_strbcasestr(dent->d_name, ".db3")  || 
1253
-	    cli_strbcasestr(dent->d_name, ".hdb")  || 
1254
-	    cli_strbcasestr(dent->d_name, ".hdu")  || 
1255
-	    cli_strbcasestr(dent->d_name, ".fp")   || 
1256
-	    cli_strbcasestr(dent->d_name, ".mdb")  ||
1257
-	    cli_strbcasestr(dent->d_name, ".mdu")  ||
1258
-	    cli_strbcasestr(dent->d_name, ".ndb")  || 
1259
-	    cli_strbcasestr(dent->d_name, ".ndu")  || 
1260
-	    cli_strbcasestr(dent->d_name, ".sdb")  || 
1261
-	    cli_strbcasestr(dent->d_name, ".zmd")  || 
1262
-	    cli_strbcasestr(dent->d_name, ".rmd")  || 
1263
-	    cli_strbcasestr(dent->d_name, ".cfg")  ||
1264
-	    cli_strbcasestr(dent->d_name, ".pdb")  ||
1265
-	    cli_strbcasestr(dent->d_name, ".wdb")  ||
1266
-	    cli_strbcasestr(dent->d_name, ".inc")   ||
1267
-	    cli_strbcasestr(dent->d_name, ".cvd"))) {
1268
-
1249
+	    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") && CLI_DBEXT(dent->d_name)) {
1269 1250
 		dbstat->entries++;
1270 1251
 		dbstat->stattab = (struct stat *) cli_realloc2(dbstat->stattab, dbstat->entries * sizeof(struct stat));
1271 1252
 		if(!dbstat->stattab) {
... ...
@@ -1291,10 +1282,7 @@ int cl_statinidir(const char *dirname, struct cl_stat *dbstat)
1291 1291
 		}
1292 1292
 
1293 1293
 		if(cli_strbcasestr(dent->d_name, ".inc")) {
1294
-		    if(strstr(dent->d_name, "main"))
1295
-			sprintf(fname, "%s/main.inc/main.info", dirname);
1296
-		    else
1297
-			sprintf(fname, "%s/daily.inc/daily.info", dirname);
1294
+		    sprintf(fname, "%s/%s/%s.info", dirname, dent->d_name, strstr(dent->d_name, "daily") ? "daily" : "main");
1298 1295
 		} else {
1299 1296
 		    sprintf(fname, "%s/%s", dirname, dent->d_name);
1300 1297
 		}
... ...
@@ -1356,26 +1344,7 @@ int cl_statchkdir(const struct cl_stat *dbstat)
1356 1356
 	if(dent->d_ino)
1357 1357
 #endif
1358 1358
 	{
1359
-	    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") &&
1360
-	    (cli_strbcasestr(dent->d_name, ".db")  ||
1361
-	    cli_strbcasestr(dent->d_name, ".db2")  || 
1362
-	    cli_strbcasestr(dent->d_name, ".db3")  || 
1363
-	    cli_strbcasestr(dent->d_name, ".hdb")  || 
1364
-	    cli_strbcasestr(dent->d_name, ".hdu")  || 
1365
-	    cli_strbcasestr(dent->d_name, ".fp")   || 
1366
-	    cli_strbcasestr(dent->d_name, ".mdb")  ||
1367
-	    cli_strbcasestr(dent->d_name, ".mdu")  ||
1368
-	    cli_strbcasestr(dent->d_name, ".ndb")  || 
1369
-	    cli_strbcasestr(dent->d_name, ".ndu")  || 
1370
-	    cli_strbcasestr(dent->d_name, ".sdb")  || 
1371
-	    cli_strbcasestr(dent->d_name, ".zmd")  || 
1372
-	    cli_strbcasestr(dent->d_name, ".rmd")  || 
1373
-	    cli_strbcasestr(dent->d_name, ".cfg")  ||
1374
-	    cli_strbcasestr(dent->d_name, ".pdb")  ||
1375
-	    cli_strbcasestr(dent->d_name, ".wdb")  ||
1376
-	    cli_strbcasestr(dent->d_name, ".inc")   ||
1377
-	    cli_strbcasestr(dent->d_name, ".cvd"))) {
1378
-
1359
+	    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") && CLI_DBEXT(dent->d_name)) {
1379 1360
                 fname = cli_malloc(strlen(dbstat->dir) + strlen(dent->d_name) + 32);
1380 1361
 		if(!fname) {
1381 1362
 		    closedir(dd);
... ...
@@ -1383,10 +1352,7 @@ int cl_statchkdir(const struct cl_stat *dbstat)
1383 1383
 		}
1384 1384
 
1385 1385
 		if(cli_strbcasestr(dent->d_name, ".inc")) {
1386
-		    if(strstr(dent->d_name, "main"))
1387
-			sprintf(fname, "%s/main.inc/main.info", dbstat->dir);
1388
-		    else
1389
-			sprintf(fname, "%s/daily.inc/daily.info", dbstat->dir);
1386
+		    sprintf(fname, "%s/%s/%s.info", dbstat->dir, dent->d_name, strstr(dent->d_name, "daily") ? "daily" : "main");
1390 1387
 		} else {
1391 1388
 		    sprintf(fname, "%s/%s", dbstat->dir, dent->d_name);
1392 1389
 		}