Branch data Line data Source code
1 : : // $Id: FileAnalyzer.cc,v 1.1.4.2 2006/06/01 17:18:10 sommer Exp $
2 : :
3 : : #include "FileAnalyzer.h"
4 : :
5 : : #ifdef HAVE_LIBMAGIC
6 : : magic_t File_Analyzer::magic = 0;
7 : : magic_t File_Analyzer::magic_mime = 0;
8 : : #endif
9 : :
10 : 0 : File_Analyzer::File_Analyzer(Connection* conn)
11 : 0 : : TCP_ApplicationAnalyzer(AnalyzerTag::File, conn)
12 : : {
13 : 0 : buffer_len = 0;
14 : :
15 : : #ifdef HAVE_LIBMAGIC
16 [ # # # # ]: 0 : if ( ! magic )
17 : : {
18 : 0 : InitMagic(&magic, MAGIC_NONE);
19 : 0 : InitMagic(&magic_mime, MAGIC_MIME);
20 : : }
21 : : #endif
22 : 0 : }
23 : :
24 : 0 : void File_Analyzer::DeliverStream(int len, const u_char* data, bool orig)
25 : : {
26 : 0 : TCP_ApplicationAnalyzer::DeliverStream(len, data, orig);
27 : :
28 : 0 : int n = min(len, BUFFER_SIZE - buffer_len);
29 : :
30 [ # # ]: 0 : if ( n )
31 : : {
32 : 0 : strncpy(buffer + buffer_len, (const char*) data, n);
33 : 0 : buffer_len += n;
34 : :
35 [ # # ]: 0 : if ( buffer_len == BUFFER_SIZE )
36 : 0 : Identify();
37 : : }
38 : : return;
39 : : }
40 : :
41 : 0 : void File_Analyzer::Done()
42 : : {
43 : 0 : TCP_ApplicationAnalyzer::Done();
44 : :
45 [ # # # # ]: 0 : if ( buffer_len && buffer_len != BUFFER_SIZE )
46 : 0 : Identify();
47 : 0 : }
48 : :
49 : 0 : void File_Analyzer::Identify()
50 : : {
51 : 0 : const char* descr = 0;
52 : 0 : const char* mime = 0;
53 : :
54 : : #ifdef HAVE_LIBMAGIC
55 [ # # ]: 0 : if ( magic )
56 : 0 : descr = magic_buffer(magic, buffer, buffer_len);
57 : :
58 [ # # ]: 0 : if ( magic_mime )
59 : 0 : mime = magic_buffer(magic_mime, buffer, buffer_len);
60 : : #endif
61 : :
62 : 0 : val_list* vl = new val_list;
63 : 0 : vl->append(BuildConnVal());
64 : 0 : vl->append(new StringVal(buffer_len, buffer));
65 [ # # ]: 0 : vl->append(new StringVal(descr ? descr : "<unknown>"));
66 [ # # ]: 0 : vl->append(new StringVal(mime ? mime : "<unknown>"));
67 : 0 : ConnectionEvent(file_transferred, vl);
68 : 0 : }
69 : :
70 : : #ifdef HAVE_LIBMAGIC
71 : 0 : void File_Analyzer::InitMagic(magic_t* magic, int flags)
72 : : {
73 : 0 : *magic = magic_open(flags);
74 : :
75 [ # # ]: 0 : if ( ! *magic )
76 : 0 : error(fmt("can't init libmagic: %s", magic_error(*magic)));
77 : :
78 [ # # ]: 0 : else if ( magic_load(*magic, 0) < 0 )
79 : : {
80 : 0 : error(fmt("can't load magic file: %s", magic_error(*magic)));
81 : 0 : magic_close(*magic);
82 : 0 : *magic = 0;
83 : : }
84 [ + - ][ + - ]: 6 : }
85 : 3 : #endif
|