1 
2 /* MD5DEEP
3  *
4  * By Jesse Kornblum
5  *
6  * This is a work of the US Government. In accordance with 17 USC 105,
7  * copyright protection is not available for any work of the US Government.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  */
14 
15 // $Id$
16 
17 #include "ssdeep.h"
18 
19 
20 typedef struct dir_table {
21   TCHAR *name;
22   struct dir_table *next;
23 } dir_table;
24 
25 dir_table *my_table = NULL;
26 
27 
28 /* This function was used in the dark ages for debugging
29 static void dump_table(void)
30 {
31   struct dir_table *t = my_table;
32   while (t != NULL)
33   {
34     print_status (_TEXT("* %s"), t->name);
35     t = t->next;
36   }
37   print_status ("-- end of table --");
38 }
39 */
40 
41 
42 
done_processing_dir(TCHAR * fn)43 int done_processing_dir(TCHAR *fn)
44 {
45   dir_table *last, *temp;
46   TCHAR *d_name = (TCHAR *)malloc(sizeof(TCHAR) * SSDEEP_PATH_MAX);
47 
48 #ifdef _WIN32
49   _wfullpath(d_name,fn,SSDEEP_PATH_MAX);
50 #else
51   realpath(fn,d_name);
52 #endif
53 
54   if (my_table == NULL)
55   {
56     internal_error("Table is NULL in done_processing_dir");
57 
58     // This code never gets executed...
59     free(d_name);
60     return FALSE;
61   }
62 
63   temp = my_table;
64 
65   if (!_tcsncmp(d_name,temp->name,SSDEEP_PATH_MAX))
66   {
67     my_table = my_table->next;
68     free(temp->name);
69     free(temp);
70     free(d_name);
71     return TRUE;
72   }
73 
74   while (temp->next != NULL)
75   {
76     last = temp;
77     temp = temp->next;
78     if (!_tcsncmp(d_name,temp->name,SSDEEP_PATH_MAX))
79     {
80       last->next = temp->next;
81       free(temp->name);
82       free(temp);
83       free(d_name);
84       return TRUE;
85     }
86   }
87 
88   internal_error("%s: Directory %s not found in done_processing_dir",
89 		 __progname, d_name);
90 
91   // This code never gets executed...
92   //  free (d_name);
93   return FALSE;
94 }
95 
96 
97 
98 
processing_dir(TCHAR * fn)99 int processing_dir(TCHAR *fn)
100 {
101   dir_table *new_dir, *temp;
102   TCHAR *d_name = (TCHAR *)malloc(sizeof(TCHAR) * SSDEEP_PATH_MAX);
103 
104 #ifdef _WIN32
105   _wfullpath(d_name,fn,SSDEEP_PATH_MAX);
106 #else
107   realpath(fn,d_name);
108 #endif
109 
110   if (my_table == NULL)
111   {
112     my_table = (dir_table*)malloc(sizeof(dir_table));
113     my_table->name = _tcsdup(d_name);
114     my_table->next = NULL;
115 
116     free(d_name);
117     return TRUE;
118   }
119 
120   temp = my_table;
121 
122   while (temp->next != NULL)
123   {
124     /* We should never be adding a directory that is already here */
125     if (!_tcsncmp(temp->name,d_name,SSDEEP_PATH_MAX))
126     {
127       internal_error("%s: Attempt to add existing %s in processing_dir",
128 		     __progname, d_name);
129       // Does not execute
130       free(d_name);
131       return FALSE;
132     }
133     temp = temp->next;
134   }
135 
136   new_dir = (dir_table*)malloc(sizeof(dir_table));
137   new_dir->name = _tcsdup(d_name);
138   new_dir->next = NULL;
139   temp->next = new_dir;
140 
141   free(d_name);
142   return TRUE;
143 }
144 
145 
have_processed_dir(TCHAR * fn)146 int have_processed_dir(TCHAR *fn)
147 {
148   dir_table *temp;
149   TCHAR *d_name;
150 
151   if (my_table == NULL)
152     return FALSE;
153 
154   d_name = (TCHAR *)malloc(sizeof(TCHAR) * SSDEEP_PATH_MAX);
155 #ifdef _WIN32
156   _wfullpath(d_name,fn,SSDEEP_PATH_MAX);
157 #else
158   realpath(fn,d_name);
159 #endif
160 
161   temp = my_table;
162   while (temp != NULL)
163   {
164     if (!_tcsncmp(temp->name,d_name,SSDEEP_PATH_MAX))
165     {
166       free(d_name);
167       return TRUE;
168     }
169 
170     temp = temp->next;
171   }
172 
173   free(d_name);
174   return FALSE;
175 }
176 
177 
178 
179 
180