@@ -15,38 +15,50 @@ public ClamEngine ()
15
15
throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
16
16
17
17
engine = ClamBindings . cl_engine_new ( ) ;
18
-
19
- string dbDir = Marshal . PtrToStringAnsi ( ClamBindings . cl_retdbdir ( ) ) ;
20
- uint signo = 0 ;
21
-
22
- ret = ClamBindings . cl_load ( dbDir , engine , ref signo , ( uint ) ClamScanOptions . CL_SCAN_STDOPT ) ;
23
-
24
- if ( ret != ClamReturnCode . CL_SUCCESS )
25
- throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
26
-
27
- ret = ( ClamReturnCode ) ClamBindings . cl_engine_compile ( engine ) ;
28
-
29
- if ( ret != ClamReturnCode . CL_SUCCESS )
30
- throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
18
+
19
+ try
20
+ {
21
+ string dbDir = Marshal . PtrToStringAnsi ( ClamBindings . cl_retdbdir ( ) ) ;
22
+ uint signo = 0 ;
23
+
24
+ ret = ClamBindings . cl_load ( dbDir , engine , ref signo , ( uint ) ClamScanOptions . CL_SCAN_STDOPT ) ;
25
+
26
+ if ( ret != ClamReturnCode . CL_SUCCESS )
27
+ throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
28
+
29
+ ret = ( ClamReturnCode ) ClamBindings . cl_engine_compile ( engine ) ;
30
+
31
+ if ( ret != ClamReturnCode . CL_SUCCESS )
32
+ throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
33
+ }
34
+ catch
35
+ {
36
+ ret = ClamBindings . cl_engine_free ( engine ) ;
37
+
38
+ if ( ret != ClamReturnCode . CL_SUCCESS )
39
+ Console . Error . WriteLine ( "Freeing allocated engine failed" ) ;
40
+ }
31
41
}
32
42
33
43
public ClamResult ScanFile ( string filepath , uint options = ( uint ) ClamScanOptions . CL_SCAN_STDOPT )
34
44
{
35
45
ulong scanned = 0 ;
36
46
IntPtr vname = ( IntPtr ) null ;
37
47
ClamReturnCode ret = ClamBindings . cl_scanfile ( filepath , ref vname , ref scanned , engine , options ) ;
38
-
39
- if ( ret == ClamReturnCode . CL_VIRUS ) {
40
- string virus = Marshal . PtrToStringAnsi ( vname ) ;
41
-
42
- ClamResult result = new ClamResult ( ) ;
48
+
49
+ if ( ret == ClamReturnCode . CL_VIRUS )
50
+ {
51
+ string virus = Marshal . PtrToStringAnsi ( vname ) ;
52
+
53
+ ClamResult result = new ClamResult ( ) ;
43
54
result . ReturnCode = ret ;
44
55
result . VirusName = virus ;
45
56
result . FullPath = filepath ;
46
-
57
+
47
58
return result ;
48
- } else if ( ret == ClamReturnCode . CL_CLEAN )
49
- return null ;
59
+ }
60
+ else if ( ret == ClamReturnCode . CL_CLEAN )
61
+ return new ClamResult ( ) { ReturnCode = ret } ;
50
62
else
51
63
throw new Exception ( "Expected either CL_CLEAN or CL_VIRUS, got: " + ret ) ;
52
64
}
@@ -56,7 +68,7 @@ public void Dispose()
56
68
ClamReturnCode ret = ClamBindings . cl_engine_free ( engine ) ;
57
69
58
70
if ( ret != ClamReturnCode . CL_SUCCESS )
59
- throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
71
+ Console . Error . WriteLine ( "Freeing allocated engine failed" ) ;
60
72
}
61
73
}
62
74
}
0 commit comments