-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFHTAnalysis.java
284 lines (204 loc) · 9.97 KB
/
FHTAnalysis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import java.io.*;
import java.util.Scanner;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
public class FHTAnalysis {
public static final int maxByteValue = 256;
/*public static final String[] fileTypes = { "text\\plain", "text\\pdf", "text\\rdf+xml", "text\\rsf+xml",
"text\\xhtml+xml", "text\\html", "image\\png", "image\\jpeg", "audio\\mpeg", "video\\mp4",
"video\\quicktime", "applicaiton\\x-sh", "application\\gzip", "application\\msword",
"application\\octet-stream" };
*/
public static final String[] fileTypes = { "application\\x-elc"
};
public double[][] get_byte_frequency_matrix(int numOfBytes) {
double[][] matrix = new double[numOfBytes][maxByteValue];
return matrix;
}
public void writeFingerPrint(File fingerPrintFile, double[][] fingerPrint, int numOfHeaderTrailerBytes,
int numOfFilesProcessed) throws Exception {
FileWriter fw = new FileWriter(fingerPrintFile);
fw.write(Integer.toString(numOfFilesProcessed + 1));
for (int headerTrailerByteIndex = 0; headerTrailerByteIndex < numOfHeaderTrailerBytes; headerTrailerByteIndex++) {
fw.write("\n");
for (int byteIndex = 0; byteIndex < maxByteValue; byteIndex++) {
String temp = Double.toString(fingerPrint[headerTrailerByteIndex][byteIndex]) + "\t";
fw.write(temp);
}
}
fw.close();
}
// calculating byte frequency
public double[][] updateByteFrequency(double[][] byteFrequency, int numOfFilesProcessed, double[][] byteCount,
int numOfHeaderTrailerBytes) {
for (int headerTrailerByteIndex = 0; headerTrailerByteIndex < numOfHeaderTrailerBytes; headerTrailerByteIndex++) {
for (int byteIndex = 0; byteIndex < maxByteValue; byteIndex++) {
byteFrequency[headerTrailerByteIndex][byteIndex] = (byteFrequency[headerTrailerByteIndex][byteIndex] * numOfFilesProcessed + byteCount[headerTrailerByteIndex][byteIndex])
/ (numOfFilesProcessed + 1);
}
}
return byteFrequency;
}
public JSONArray getJsonObject(int numOfHeaderTrailerBytes,double[][] byteFrequency){
JSONArray jarray = new JSONArray();
//JSONObject json = new JSONObject();
for (int rowIdx = 0 ; rowIdx < numOfHeaderTrailerBytes; rowIdx++)
{
for(int columnIdx = 0; columnIdx < maxByteValue; columnIdx++){
JSONObject json = new JSONObject();
json.put("i",rowIdx);
json.put("j",columnIdx);
json.put("value", byteFrequency[rowIdx][columnIdx]);
System.out.println(json);
jarray.add(json);
}
}
return jarray;
}
public void writeJsonFile(String fileType, String fileName, JSONArray json)throws IOException {
FileWriter jsonFile = new FileWriter("C:\\PolarDump\\" + fileType + "\\" +fileName +".json");
jsonFile.write(json.toJSONString());
jsonFile.close();
}
public static void main(String[] args) throws Exception {
FHTAnalysis fht = new FHTAnalysis();
// num of header or trailer bytes considered.
int numOfHeaderTrailerBytes = 4;
for (int fileTypesIndex = 0; fileTypesIndex < fileTypes.length; fileTypesIndex++) {
// Selecting the file type and setting the path to the folder which
// has the data and fingerprint file corresponding to that file type
File fileType = new File("C:\\PolarDump\\" + fileTypes[fileTypesIndex] + "\\data\\");
File[] files = fileType.listFiles();
String headerFingerPrintFile = "Header_" + fileTypes[fileTypesIndex].replace("\\", "-");
String trailerFingerPrintFile = "Trailer_" + fileTypes[fileTypesIndex].replace("\\", "-");
//create header finger print file
File headerFingerPrint = new File("C:\\PolarDump\\" + fileTypes[fileTypesIndex] + "\\" + headerFingerPrintFile +Integer.toString(numOfHeaderTrailerBytes) + ".txt");
System.out.println("C:\\PolarDump\\" + fileTypes[fileTypesIndex] + "\\" + headerFingerPrintFile + Integer.toString(numOfHeaderTrailerBytes) + ".txt");
//create trailer finger print file
File trailerFingerPrint = new File(
"C:\\PolarDump\\" + fileTypes[fileTypesIndex] + "\\" + trailerFingerPrintFile +Integer.toString(numOfHeaderTrailerBytes)+ ".txt");
int num = 0;
//if trailer finger print file does not exists , then create one .
//initialize first count to files processed to 0
//initialize maxbyte number of rows to 0
if (!trailerFingerPrint.exists()) {
trailerFingerPrint.createNewFile();
FileWriter fw = new FileWriter(trailerFingerPrint);
fw.write("0");
for (int trailerByteIndex = 0; trailerByteIndex < numOfHeaderTrailerBytes; trailerByteIndex++) {
fw.write("\n");
for (int byteIndex = 0; byteIndex < maxByteValue; byteIndex++) {
String temp = Double.toString(0.0) + "\t";
fw.write(temp);
}
}
fw.close();
}
//if header finger print file does not exists , then create one .
//initialize first count to files processed to 0
//initialize maxbyte number of rows to 0
if (!headerFingerPrint.exists()) {
headerFingerPrint.createNewFile();
FileWriter fw = new FileWriter(headerFingerPrint);
fw.write("0");// no of files processed = 0
for (int headerByteIndex = 0; headerByteIndex < numOfHeaderTrailerBytes; headerByteIndex++) {
fw.write("\n");
for (int byteIndex = 0; byteIndex < maxByteValue; byteIndex++) {
String temp = Double.toString(0.0) + "\t";
fw.write(temp);
}
}
fw.close();
}
// get the headerByteFrequency
double[][] headerByteFrequency = fht.get_byte_frequency_matrix(numOfHeaderTrailerBytes);
System.out.println("headerByteFrequency allocated ");
// get the trailerByteFrequency
double[][] trailerByteFrequency = fht.get_byte_frequency_matrix(numOfHeaderTrailerBytes);
for (File file : files) {
// input file
FileInputStream fIS = new FileInputStream(file);
Scanner scHFP = new Scanner(new FileReader(headerFingerPrint));
Scanner scTFP = new Scanner(new FileReader(trailerFingerPrint));
int numOfFilesProcessed = scHFP.nextInt();
System.out.println("Number of files processed : " + numOfFilesProcessed);
scHFP.nextLine();
// load the header figerprint
for (int headerTrailerByteIndex = 0; headerTrailerByteIndex < numOfHeaderTrailerBytes; headerTrailerByteIndex++) {
String[] terms = scHFP.nextLine().split("\\t");
for (int byteIndex = 0; byteIndex < maxByteValue; byteIndex++) {
headerByteFrequency[headerTrailerByteIndex][byteIndex] = Double.parseDouble(terms[byteIndex]);
}
}
scHFP.close();
// load the header figerprint
numOfFilesProcessed = scTFP.nextInt();
scTFP.nextLine();
for (int headerTrailerByteIndex = 0; headerTrailerByteIndex < numOfHeaderTrailerBytes; headerTrailerByteIndex++) {
String[] terms = scTFP.nextLine().split("\\t");
for (int byteIndex = 0; byteIndex < maxByteValue; byteIndex++) {
trailerByteFrequency[headerTrailerByteIndex][byteIndex] = Double.parseDouble(terms[byteIndex]);
}
}
scTFP.close();
// read the data from the file in consideration to a byte file
byte[] byteFile = new byte[(int) file.length()];
fIS.read(byteFile);
fIS.close();
// header and trailer byte frequency matrix for file in
// consideration
double[][] headerByteCount = fht.get_byte_frequency_matrix(numOfHeaderTrailerBytes);
double[][] trailerByteCount = fht.get_byte_frequency_matrix(numOfHeaderTrailerBytes);
// header frequency & trailer frequency
// Calculate Header and Trailer byte frequency
if (byteFile.length > numOfHeaderTrailerBytes) {
for (int headerTrailerByteIndex = 0; headerTrailerByteIndex < numOfHeaderTrailerBytes; headerTrailerByteIndex++) {
headerByteCount[headerTrailerByteIndex][0xFF & byteFile[headerTrailerByteIndex]]++;
trailerByteCount[headerTrailerByteIndex][0xFF & byteFile[byteFile.length - numOfHeaderTrailerBytes + headerTrailerByteIndex]]++; //
}
} else {
System.out.println(byteFile.length);
int byteIndexJ = 0;
int byteIndexI = 0;
//headerFingerPrint
for (byteIndexJ = 0; byteIndexJ < byteFile.length; byteIndexJ++) {
headerByteCount[byteIndexJ][0xFF & byteFile[byteIndexJ]]++;
}
for (byteIndexI = byteIndexJ; byteIndexI < numOfHeaderTrailerBytes; byteIndexI++) {
for (int byteIndexK = 0; byteIndexK < maxByteValue; byteIndexK++) {
headerByteCount[byteIndexI][byteIndexK]--;
}
}
/* commenting trailer analysis section since it won't be considered for analysis when file lengthn is less than numOfHeaderTrailerBytes considered.
//trailerFingerPrint
for (byteIndexJ = 0, byteIndexI = byteFile.length; byteIndexI > 0; byteIndexI--, byteIndexJ++) {
// headerByteCount[i][0xFF & byteFile[i]]++;
trailerByteCount[byteIndexJ][0xFF & byteFile[byteFile.length - byteIndexI]]++;
}
for (byteIndexI = byteIndexJ; byteIndexI < numOfHeaderTrailerBytes; byteIndexI++) {
for (byteIndexJ = 0; byteIndexJ < maxByteValue; byteIndexJ++) {
trailerByteCount[byteIndexI][byteIndexJ]--;
}
}
*/
}
// update header byte frequency
headerByteFrequency = fht.updateByteFrequency(headerByteFrequency, numOfFilesProcessed, headerByteCount,
numOfHeaderTrailerBytes);
// update trailer byte frequency
trailerByteFrequency = fht.updateByteFrequency(trailerByteFrequency, numOfFilesProcessed,
trailerByteCount, numOfHeaderTrailerBytes);
// write header fingerprint
fht.writeFingerPrint(headerFingerPrint, headerByteFrequency, numOfHeaderTrailerBytes,
numOfFilesProcessed);
// write trailer fingerprint
fht.writeFingerPrint(trailerFingerPrint, trailerByteFrequency, numOfHeaderTrailerBytes,
numOfFilesProcessed);
}
JSONArray headerJson = fht.getJsonObject(numOfHeaderTrailerBytes,headerByteFrequency);
JSONArray trailerJson = fht.getJsonObject(numOfHeaderTrailerBytes,trailerByteFrequency);
fht.writeJsonFile(fileTypes[fileTypesIndex], headerFingerPrintFile, headerJson);
fht.writeJsonFile(fileTypes[fileTypesIndex], trailerFingerPrintFile, trailerJson);
}
}
}