Browse code

fixed RAR support

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

Tomasz Kojm authored on 2004/03/17 04:39:49
Showing 3 changed files
... ...
@@ -1,3 +1,9 @@
1
+Tue Mar 16 20:43:20 CET 2004 (tk)
2
+---------------------------------
3
+  * libclamav: unrarlib: don't use UnstoreFile() because it causes memory
4
+	       corruption. Restored support for standard archives (yesterday's
5
+	       fix disabled RAR support completely).
6
+
1 7
 Mon Mar 15 20:53:10 CET 2004 (tk)
2 8
 ---------------------------------
3 9
   * clamav-milter: version number increased to 0.70
... ...
@@ -136,7 +136,6 @@ cl_node *root)
136 136
 
137 137
     pt= buff;
138 138
     length = SCANBUFF;
139
-
140 139
     while((bytes = read(desc, buff, SCANBUFF)) > 0) {
141 140
 
142 141
 	if(scanned != NULL)
... ...
@@ -286,6 +285,7 @@ static int cli_scanrar(int desc, const char **virname, long int *scanned, const
286 286
 	} else {
287 287
 	    cli_dbgmsg("RAR -> Can't decompress file %s\n", rarlist->item.Name);
288 288
 	    fclose(tmp);
289
+	    tmp = NULL;
289 290
 	    ret = CL_ERAR; /* WinRAR 3.0 ? */
290 291
 	    break;
291 292
 	}
... ...
@@ -302,6 +302,9 @@ static int cli_scanrar(int desc, const char **virname, long int *scanned, const
302 302
     cli_scanrar_inuse = 0;
303 303
     pthread_cleanup_pop(0);
304 304
 #endif
305
+    
306
+    cli_dbgmsg("RAR -> Exit code: %d\n", ret);
307
+
305 308
     return ret;
306 309
 }
307 310
 
... ...
@@ -883,7 +886,6 @@ int cl_scanfile(const char *filename, const char **virname, unsigned long int *s
883 883
     if((fd = open(filename, O_RDONLY)) == -1)
884 884
 	return CL_EOPEN;
885 885
 
886
-    cli_dbgmsg("Scanning %s\n", filename);
887 886
     ret = cl_scandesc(fd, virname, scanned, root, limits, options);
888 887
     close(fd);
889 888
 
... ...
@@ -303,11 +303,11 @@ static struct NewMainArchiveHeader NewMhd;
303 303
 static struct NewFileHeader NewLhd;
304 304
 static struct BlockHeader BlockHead;
305 305
 
306
-static UBYTE *TempMemory;                          /* temporary unpack-buffer      */
307
-static char *CommMemory;
306
+static UBYTE *TempMemory = NULL;                          /* temporary unpack-buffer      */
307
+static char *CommMemory = NULL;
308 308
 
309 309
 
310
-static UBYTE *UnpMemory;
310
+static UBYTE *UnpMemory = NULL;
311 311
 static char ArgName[NM];                           /* current file in rar archive  */
312 312
 static char ArcFileName[NM];                       /* file to decompress           */
313 313
 
... ...
@@ -407,7 +407,8 @@ int urarlib_get(void *output,
407 407
   temp_output_buffer = NULL;
408 408
   temp_output_buffer_offset=size;           /* set size of the temp buffer  */
409 409
 
410
-  retcode = ExtrFile(desc);                     /* unpack file now!             */
410
+  retcode = ExtrFile(desc);                     /* unpack file now! */
411
+
411 412
 
412 413
   memset(Password,0,sizeof(Password));      /* clear password               */
413 414
 
... ...
@@ -423,9 +424,15 @@ int urarlib_get(void *output,
423 423
   }
424 424
 #endif
425 425
 
426
-  free(UnpMemory);                          /* free memory                  */
427
-  free(TempMemory);
428
-  free(CommMemory);
426
+  if(UnpMemory)
427
+    free(UnpMemory);
428
+
429
+  if(TempMemory)
430
+    free(TempMemory);
431
+
432
+  if(CommMemory)
433
+    free(CommMemory);
434
+
429 435
   UnpMemory=NULL;
430 436
   TempMemory=NULL;
431 437
   CommMemory=NULL;
... ...
@@ -433,7 +440,8 @@ int urarlib_get(void *output,
433 433
 
434 434
   if(retcode == FALSE)
435 435
   {
436
-    free(temp_output_buffer);               /* free memory and return NULL  */
436
+    if(temp_output_buffer)
437
+	free(temp_output_buffer);               /* free memory and return NULL  */
437 438
     temp_output_buffer=NULL;
438 439
     *(DWORD*)output=0;                      /* pointer on errors            */
439 440
     *size=0;
... ...
@@ -533,7 +541,6 @@ int urarlib_list(int desc, ArchiveList_struct *list)
533 533
     if ((ReadBlockResult = ReadBlock(FILE_HEAD | READSUBBLOCK)) <= 0) /* read name of the next  */
534 534
     {                                       /* file within the RAR archive  */
535 535
       cli_dbgmsg("Couldn't read next filename from archive (I/O error): %d\n", ReadBlockResult);
536
-      NoOfFilesInArchive = 0;
537 536
       break;                                /* error, file not found in     */
538 537
     }                                       /* archive or I/O error         */
539 538
     if (BlockHead.HeadType==SUB_HEAD)
... ...
@@ -590,9 +597,15 @@ int urarlib_list(int desc, ArchiveList_struct *list)
590 590
   }
591 591
 #endif
592 592
 
593
-  free(UnpMemory);                          /* free memory                  */
594
-  free(TempMemory);
595
-  free(CommMemory);
593
+  if(UnpMemory)
594
+    free(UnpMemory);
595
+
596
+  if(TempMemory)
597
+    free(TempMemory);
598
+
599
+  if(CommMemory)
600
+    free(CommMemory);
601
+
596 602
   UnpMemory=NULL;
597 603
   TempMemory=NULL;
598 604
   CommMemory=NULL;
... ...
@@ -957,9 +970,9 @@ BOOL ExtrFile(int desc)
957 957
 
958 958
   if ((UnpMemory=malloc(UNP_MEMORY))==NULL)
959 959
   {
960
-    debug_log("Can't allocate memory for decompression!");
960
+    cli_dbgmsg("unrarlib: Can't allocate memory for decompression!");
961 961
     return FALSE;
962
-  }
962
+  } else cli_dbgmsg("unrarlib: Allocated %d bytes.\n", UNP_MEMORY);
963 963
 
964 964
 #ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
965 965
   MemRARFile->offset+=NewMhd.HeadSize-MainHeadSize;
... ...
@@ -999,19 +1012,15 @@ BOOL ExtrFile(int desc)
999 999
     /* *** file found! ***                                                  */
1000 1000
     {
1001 1001
       {
1002
-        temp_output_buffer=malloc(NewLhd.UnpSize);/* allocate memory for the*/
1002
+	cli_dbgmsg("unrarlib: Allocating %d bytes\n", NewLhd.UnpSize);
1003
+        if((temp_output_buffer=malloc(NewLhd.UnpSize)) == NULL) { ;/* allocate memory for the*/
1004
+	    cli_errmsg("unrarlib: Can't malloc %d bytes\n", NewLhd.UnpSize);
1005
+	    ReturnCode = FALSE;
1006
+	    break;
1007
+	}
1003 1008
       }
1004 1009
       *temp_output_buffer_offset=0;         /* file. The default offset     */
1005 1010
                                             /* within the buffer is 0       */
1006
-
1007
-      if(temp_output_buffer == NULL)
1008
-      {
1009
-        debug_log("can't allocate memory for the file decompression");
1010
-        ReturnCode=FALSE;
1011
-        break;                              /* error, can't extract file!   */
1012
-      }
1013
-
1014
-
1015 1011
     }
1016 1012
 
1017 1013
     /* in case of a solid archive, we need to decompress any single file till
... ...
@@ -1040,14 +1049,17 @@ BOOL ExtrFile(int desc)
1040 1040
 
1041 1041
       if (NewLhd.Method==0x30)
1042 1042
       {
1043
-        UnstoreFile();
1043
+	cli_dbgmsg("unrarlib: Unstore method temporarily not supported\n");
1044
+        /* UnstoreFile(); */
1045
+        ReturnCode=FALSE;
1046
+        break;                              /* error, can't extract file! */
1044 1047
       } else
1045 1048
       {
1049
+	cli_dbgmsg("unrarlib: Unpack()\n");
1046 1050
         Unpack(UnpMemory, FileFound);
1047 1051
       }
1048 1052
 
1049 1053
 
1050
-
1051 1054
 #ifdef _DO_CRC32_CHECK                      /* calculate CRC32              */
1052 1055
       if((UBYTE*)temp_output_buffer != NULL)
1053 1056
       {
... ...
@@ -1072,7 +1084,9 @@ BOOL ExtrFile(int desc)
1072 1072
   } while(stricomp(ArgName, ArcFileName) != 0);/* exit if file is extracted */
1073 1073
 
1074 1074
   /* free memory, clear password and close archive                          */
1075
-  free(UnpMemory);
1075
+  if(UnpMemory)
1076
+    free(UnpMemory);
1077
+
1076 1078
   UnpMemory=NULL;
1077 1079
 #ifndef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
1078 1080
   if (ArcPtr!=NULL){
... ...
@@ -1082,7 +1096,7 @@ BOOL ExtrFile(int desc)
1082 1082
   }
1083 1083
 #endif
1084 1084
 
1085
-  return ReturnCode;                        /* file extracted successful!   */
1085
+  return ReturnCode;
1086 1086
 }
1087 1087
 
1088 1088
 /* **************************************************************************
... ...
@@ -1241,7 +1255,7 @@ BOOL UnstoreFile(void)
1241 1241
   if ((long)(*temp_output_buffer_offset=UnpRead(temp_output_buffer,
1242 1242
                                                 NewLhd.UnpSize))==-1)
1243 1243
   {
1244
-    debug_log("Read error of stored file!");
1244
+    cli_dbgmsg("unrarlib: Read error of stored file!");
1245 1245
     return FALSE;
1246 1246
   }
1247 1247
   return TRUE;
... ...
@@ -1612,6 +1626,7 @@ unsigned int UnpRead(unsigned char *Addr,unsigned int Count)
1612 1612
     ReadSize=(unsigned int)((Count>(unsigned long)UnpPackedSize) ?
1613 1613
                                                   UnpPackedSize : Count);
1614 1614
 #ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
1615
+    cli_dbgmsg("unrarlib: UnpREAD: Using memory->memory decompression\n");
1615 1616
     if(MemRARFile->data == NULL)
1616 1617
       return(0);
1617 1618
     RetCode=tread(MemRARFile, ReadAddr, ReadSize);
... ...
@@ -1627,6 +1642,9 @@ unsigned int UnpRead(unsigned char *Addr,unsigned int Count)
1627 1627
     UnpPackedSize-=RetCode;
1628 1628
       break;
1629 1629
   }
1630
+
1631
+  cli_dbgmsg("CurUnpRead == %d, TotalRead == %d, Count == %d, UnpPackedSize == %d\n", CurUnpRead, TotalRead, Count, UnpPackedSize);
1632
+
1630 1633
   if (RetCode!= -1)
1631 1634
   {
1632 1635
     RetCode=TotalRead;
... ...
@@ -1634,7 +1652,7 @@ unsigned int UnpRead(unsigned char *Addr,unsigned int Count)
1634 1634
     {
1635 1635
       if (Encryption<20)
1636 1636
           {
1637
-            debug_log("Old Crypt() not supported!");
1637
+            cli_dbgmsg("unrarlib: Old Crypt() not supported!");
1638 1638
           }
1639 1639
       else
1640 1640
       {