1+ extern bool first_file ;
2+ int binary_scan (char * input )
3+ {
4+ /* Get file MD5 */
5+ char * hexmd5 = strndup (input , MD5_LEN * 2 );
6+ scanlog ("Bin File md5 to be scanned: %s\n" , hexmd5 );
7+ uint8_t bin_md5 [MD5_LEN ];
8+ ldb_hex_to_bin (hexmd5 , MD5_LEN * 2 , bin_md5 );
9+ free (hexmd5 );
10+
11+ uint8_t zero_md5 [MD5_LEN ] = {0xd4 ,0x1d ,0x8c ,0xd9 ,0x8f ,0x00 ,0xb2 ,0x04 ,0xe9 ,0x80 ,0x09 ,0x98 ,0xec ,0xf8 ,0x42 ,0x7e }; //empty string md5
12+
13+ if (!memcmp (zero_md5 ,bin_md5 , MD5_LEN )) //the md5 key of an empty string must be skipped.
14+ return -1 ;
15+
16+ if (ldb_key_exists (oss_file , bin_md5 ))
17+ {
18+ scanlog ("bin file md5 match\n" );
19+ char * file_name = field_n (3 ,input );
20+ int target_len = strchr (file_name ,',' ) - file_name ;
21+ char * target = strndup (file_name , target_len );
22+ scan_data_t * scan = scan_data_init (target , 1 , 1 );
23+ free (target );
24+ memcpy (scan -> md5 , bin_md5 , MD5_LEN );
25+ scan -> match_type = MATCH_FILE ;
26+ compile_matches (scan );
27+
28+ if (scan -> best_match )
29+ {
30+ scanlog ("Match output starts\n" );
31+ if (!quiet )
32+ output_matches_json (scan );
33+
34+ scan_data_free (scan );
35+ return 0 ;
36+ }
37+ else
38+ {
39+ scanlog ("No best match, scanning binary\n" );
40+ }
41+
42+ scan_data_free (scan );
43+ }
44+
45+ binary_match_t result = {NULL , NULL , NULL };
46+ int sensibility = 1 ;
47+ while (sensibility < 100 )
48+ {
49+ char * bfp = strdup (input );
50+ result = binary_scan_run (bfp , sensibility );
51+ free (bfp );
52+ if (!result .components )
53+ return -1 ;
54+ if (result .components -> items > 1 && result .components -> headp .lh_first -> component -> hits > 0 )
55+ break ;
56+ component_list_destroy (result .components );
57+ free (result .file );
58+ free (result .md5 );
59+ sensibility ++ ;
60+ };
61+
62+ component_list_t * comp_list_sorted = calloc (1 , sizeof (component_list_t ));
63+ component_list_init (comp_list_sorted , 10 );
64+ struct comp_entry * item = NULL ;
65+ LIST_FOREACH (item , & result .components -> headp , entries )
66+ {
67+ component_list_add (comp_list_sorted ,item -> component , sort_by_hits , false);
68+ }
69+
70+ if (!quiet )
71+ if (!first_file )
72+ printf ("," );
73+ first_file = false;
74+ item = NULL ;
75+ printf ("\"%s\":{\"hash\":\"%s\",\"id\":\"bin_snippet\",\"matched\":[" , result .file , result .md5 );
76+ LIST_FOREACH (item , & comp_list_sorted -> headp , entries )
77+ {
78+ printf ("{\"purl\":\"%s\", \"hits\": %d}" ,item -> component -> purls [0 ], item -> component -> hits );
79+ if (item -> entries .le_next )
80+ printf ("," );
81+ }
82+ printf ("]}" );
83+ component_list_destroy (result .components );
84+ free (result .file );
85+ free (result .md5 );
86+ free (comp_list_sorted );
87+
88+ return 0 ;
89+
90+ }
0 commit comments