Browse code

fmapify cli_scanbzip

Török Edvin authored on 2011/06/11 03:22:46
Showing 3 changed files
... ...
@@ -16549,9 +16549,9 @@ $as_echo "$ac_cv_libbz2_libs" >&6; }
16549 16549
     test -z "$LIBBZ2_PREFIX" || LDFLAGS="$LDFLAGS -L$LIBBZ2_PREFIX/$acl_libdirstem";
16550 16550
 
16551 16551
     have_bzprefix="no"
16552
-    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for BZ2_bzReadOpen in -lbz2" >&5
16553
-$as_echo_n "checking for BZ2_bzReadOpen in -lbz2... " >&6; }
16554
-if ${ac_cv_lib_bz2_BZ2_bzReadOpen+:} false; then :
16552
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for BZ2_bzDecompressInit in -lbz2" >&5
16553
+$as_echo_n "checking for BZ2_bzDecompressInit in -lbz2... " >&6; }
16554
+if ${ac_cv_lib_bz2_BZ2_bzDecompressInit+:} false; then :
16555 16555
   $as_echo_n "(cached) " >&6
16556 16556
 else
16557 16557
   ac_check_lib_save_LIBS=$LIBS
... ...
@@ -16565,27 +16565,27 @@ cat confdefs.h - <<_ACEOF >conftest.$ac_ext
16565 16565
 #ifdef __cplusplus
16566 16566
 extern "C"
16567 16567
 #endif
16568
-char BZ2_bzReadOpen ();
16568
+char BZ2_bzDecompressInit ();
16569 16569
 int
16570 16570
 main ()
16571 16571
 {
16572
-return BZ2_bzReadOpen ();
16572
+return BZ2_bzDecompressInit ();
16573 16573
   ;
16574 16574
   return 0;
16575 16575
 }
16576 16576
 _ACEOF
16577 16577
 if ac_fn_c_try_link "$LINENO"; then :
16578
-  ac_cv_lib_bz2_BZ2_bzReadOpen=yes
16578
+  ac_cv_lib_bz2_BZ2_bzDecompressInit=yes
16579 16579
 else
16580
-  ac_cv_lib_bz2_BZ2_bzReadOpen=no
16580
+  ac_cv_lib_bz2_BZ2_bzDecompressInit=no
16581 16581
 fi
16582 16582
 rm -f core conftest.err conftest.$ac_objext \
16583 16583
     conftest$ac_exeext conftest.$ac_ext
16584 16584
 LIBS=$ac_check_lib_save_LIBS
16585 16585
 fi
16586
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_bz2_BZ2_bzReadOpen" >&5
16587
-$as_echo "$ac_cv_lib_bz2_BZ2_bzReadOpen" >&6; }
16588
-if test "x$ac_cv_lib_bz2_BZ2_bzReadOpen" = xyes; then :
16586
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_bz2_BZ2_bzDecompressInit" >&5
16587
+$as_echo "$ac_cv_lib_bz2_BZ2_bzDecompressInit" >&6; }
16588
+if test "x$ac_cv_lib_bz2_BZ2_bzDecompressInit" = xyes; then :
16589 16589
   have_bzprefix="yes"
16590 16590
 fi
16591 16591
 
... ...
@@ -582,7 +582,7 @@ then
582 582
     test -z "$LIBBZ2_PREFIX" || LDFLAGS="$LDFLAGS -L$LIBBZ2_PREFIX/$acl_libdirstem";
583 583
 
584 584
     have_bzprefix="no"
585
-    AC_CHECK_LIB([bz2], [BZ2_bzReadOpen], [have_bzprefix="yes"])
585
+    AC_CHECK_LIB([bz2], [BZ2_bzDecompressInit], [have_bzprefix="yes"])
586 586
     if test "x$have_bzprefix" = "xno"; then
587 587
         AC_DEFINE([NOBZ2PREFIX],1,[bzip funtions do not have bz2 prefix])
588 588
     fi
... ...
@@ -558,7 +558,7 @@ static int cli_scangzip(cli_ctx *ctx)
558 558
 
559 559
 
560 560
 #ifndef HAVE_BZLIB_H
561
-static int cli_scanbzip(int desc, cli_ctx *ctx) {
561
+static int cli_scanbzip(cli_ctx *ctx) {
562 562
     cli_warnmsg("cli_scanbzip: bzip2 support not compiled in\n");
563 563
     return CL_CLEAN;
564 564
 }
... ...
@@ -566,102 +566,94 @@ static int cli_scanbzip(int desc, cli_ctx *ctx) {
566 566
 #else
567 567
 
568 568
 #ifdef NOBZ2PREFIX
569
-#define BZ2_bzReadOpen bzReadOpen
570
-#define BZ2_bzReadClose bzReadClose
571
-#define BZ2_bzRead bzRead
569
+#define BZ2_bzDecompressInit bzDecompressInit
570
+#define BZ2_bzDecompress bzDecompress
571
+#define BZ2_bzDecompressEnd bzDecompressEnd
572 572
 #endif
573 573
 
574
-static int cli_scanbzip(int desc, cli_ctx *ctx)
574
+static int cli_scanbzip(cli_ctx *ctx)
575 575
 {
576
-	int fd, bytes, ret = CL_CLEAN, bzerror = 0;
577
-	unsigned long int size = 0;
578
-	char *buff;
579
-	FILE *fs;
580
-	char *tmpname;
581
-	BZFILE *bfd;
582
-
583
-
584
-    if((fs = fdopen(dup(desc), "rb")) == NULL) {
585
-	cli_dbgmsg("Bzip: Can't open descriptor %d.\n", desc);
586
-	return CL_EOPEN;
587
-    }
588
-
589
-    if((bfd = BZ2_bzReadOpen(&bzerror, fs, 0, 0, NULL, 0)) == NULL) {
590
-	cli_dbgmsg("Bzip: Can't initialize bzip2 library (descriptor: %d).\n", desc);
591
-	fclose(fs);
576
+    int ret = CL_CLEAN, fd, rc;
577
+    unsigned long int size = 0;
578
+    char *tmpname;
579
+    bz_stream strm;
580
+    size_t off = 0;
581
+    size_t avail;
582
+    char buf[FILEBUFF];
583
+
584
+    memset(&strm, 0, sizeof(strm));
585
+    strm.next_out = buf;
586
+    strm.avail_out = sizeof(buf);
587
+    rc = BZ2_bzDecompressInit(&strm, 0, 0);
588
+    if (BZ_OK != rc) {
589
+	cli_dbgmsg("Bzip: DecompressInit failed: %d\n", rc);
592 590
 	return CL_EOPEN;
593 591
     }
594 592
 
595 593
     if((ret = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &fd))) {
596 594
 	cli_dbgmsg("Bzip: Can't generate temporary file.\n");
597
-	BZ2_bzReadClose(&bzerror, bfd);
598
-	fclose(fs);
595
+	BZ2_bzDecompressEnd(&strm);
599 596
 	return ret;
600 597
     }
601 598
 
602
-    if(!(buff = (char *) cli_malloc(FILEBUFF))) {
603
-	cli_dbgmsg("Bzip: Unable to malloc %u bytes.\n", FILEBUFF);
604
-	close(fd);
605
-	if(!ctx->engine->keeptmp) {
606
-	    if (cli_unlink(tmpname)) {
607
-	    	free(tmpname);
608
-		fclose(fs);
609
-		BZ2_bzReadClose(&bzerror, bfd);
610
-		return CL_EUNLINK;
599
+    do {
600
+	if (!strm.avail_in) {
601
+	    strm.next_in = (void*)fmap_need_off_once_len(*ctx->fmap, off, FILEBUFF, &avail);
602
+	    strm.avail_in = avail;
603
+	    off += avail;
604
+	    if (!strm.avail_in) {
605
+		cli_dbgmsg("Bzip: premature end of compressed stream\n");
606
+		break;
611 607
 	    }
612 608
 	}
613
-	free(tmpname);	
614
-	fclose(fs);
615
-	BZ2_bzReadClose(&bzerror, bfd);
616
-	return CL_EMEM;
617
-    }
618 609
 
619
-    while((bytes = BZ2_bzRead(&bzerror, bfd, buff, FILEBUFF)) > 0) {
620
-	size += bytes;
621
-
622
-	if(cli_checklimits("Bzip", ctx, size + FILEBUFF, 0, 0)!=CL_CLEAN)
610
+	rc = BZ2_bzDecompress(&strm);
611
+	if (BZ_OK != rc && BZ_STREAM_END != rc) {
612
+	    cli_dbgmsg("Bzip: decompress error: %d\n", rc);
623 613
 	    break;
614
+	}
624 615
 
625
-	if(cli_writen(fd, buff, bytes) != bytes) {
626
-	    cli_dbgmsg("Bzip: Can't write to file.\n");
627
-	    BZ2_bzReadClose(&bzerror, bfd);
628
-	    close(fd);
629
-	    if(!ctx->engine->keeptmp) {
630
-		if (cli_unlink(tmpname)) {
631
-		    free(tmpname);
632
-		    free(buff);
633
-		    fclose(fs);
634
-		    return CL_EUNLINK;
616
+	if (!strm.avail_out || BZ_STREAM_END == rc) {
617
+	    size += sizeof(buf) - strm.avail_out;
618
+
619
+	    if(cli_checklimits("Bzip", ctx, size + FILEBUFF, 0, 0)!=CL_CLEAN)
620
+		break;
621
+
622
+	    if(cli_writen(fd, buf, sizeof(buf) - strm.avail_out) != sizeof(buf) - strm.avail_out) {
623
+		cli_dbgmsg("Bzip: Can't write to file.\n");
624
+		BZ2_bzDecompressEnd(&strm);
625
+		close(fd);
626
+		if(!ctx->engine->keeptmp) {
627
+		    if (cli_unlink(tmpname)) {
628
+			free(tmpname);
629
+			return CL_EUNLINK;
630
+		    }
635 631
 		}
632
+		free(tmpname);
633
+		return CL_EWRITE;
636 634
 	    }
637
-	    free(tmpname);	
638
-	    free(buff);
639
-	    fclose(fs);
640
-	    return CL_EWRITE;
635
+	    strm.next_out = buf;
636
+	    strm.avail_out = sizeof(buf);
641 637
 	}
642
-    }
638
+    } while (BZ_STREAM_END != rc);
643 639
 
644
-    free(buff);
645
-    BZ2_bzReadClose(&bzerror, bfd);
640
+    BZ2_bzDecompressEnd(&strm);
646 641
 
647 642
     if(ret == CL_VIRUS) {
648 643
 	close(fd);
649 644
 	if(!ctx->engine->keeptmp)
650 645
 	    if (cli_unlink(tmpname)) ret = CL_EUNLINK;
651
-	free(tmpname);	
652
-	fclose(fs);
646
+	free(tmpname);
653 647
 	return ret;
654 648
     }
655 649
 
656
-    lseek(fd, 0, SEEK_SET);
657 650
     if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
658 651
 	cli_dbgmsg("Bzip: Infected with %s\n", *ctx->virname);
659 652
     }
660 653
     close(fd);
661 654
     if(!ctx->engine->keeptmp)
662 655
 	if (cli_unlink(tmpname)) ret = CL_EUNLINK;
663
-    free(tmpname);	
664
-    fclose(fs);
656
+    free(tmpname);
665 657
 
666 658
     return ret;
667 659
 }
... ...
@@ -2199,7 +2191,7 @@ static int magic_scandesc(int desc, cli_ctx *ctx, cli_file_t type)
2199 2199
 	else if(ret == CL_CLEAN) {
2200 2200
 	    if(ctx->recursion != ctx->engine->maxreclevel)
2201 2201
 		cache_add(hash, hashed_size, ctx); /* Only cache if limits are not reached */
2202
-	    else 
2202
+	    else
2203 2203
 		emax_reached(ctx);
2204 2204
 	}
2205 2205
 
... ...
@@ -2274,7 +2266,7 @@ static int magic_scandesc(int desc, cli_ctx *ctx, cli_file_t type)
2274 2274
 
2275 2275
 	case CL_TYPE_BZ:
2276 2276
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_BZ))
2277
-		ret = cli_scanbzip(desc, ctx);
2277
+		ret = cli_scanbzip(ctx);
2278 2278
 	    break;
2279 2279
 
2280 2280
 	case CL_TYPE_ARJ: