-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgvfsmain.cpp
252 lines (236 loc) · 6.69 KB
/
gvfsmain.cpp
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
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dirent.h>
struct metadata
{
int keyword;
char* text;
};
struct rootEntry
{
char name[255];
long childOffset;
long metaOffset;
int numMetaKeys;
time_t lastUpdate;
metadata metaList[10];
};
int GVFSOpen(char* fileName);
long readInteger(unsigned char* ch, int pointer, int size);
long long readLongInteger(unsigned char* ch, int pointer, int size);
int readRoot(long offset);
int readChild(long offset, int level);
void writeOutput(rootEntry entry, int level);
metadata readMeta(int offset);
unsigned char* fileBuffer;
FILE* outFile;
unsigned long long baseTime;
using namespace std;
int main(int args, char** argv)
{
if (args != 2)
{
printf("GVFS Metadata parser\nUsage: gvfs PathName\nPath name should not include \'\\\' at end\n");
return 0;
}
DIR *dir;
struct dirent *ent;
dir = opendir(argv[1]);
if (dir != NULL)
{
while ((ent = readdir(dir)) != NULL)
{
char path[512];
if (!strcmp (ent->d_name, "."))
continue;
if (!strcmp (ent->d_name, ".."))
continue;
sprintf(path,"%s\\%s\0",argv[1],ent->d_name);
GVFSOpen(path);
}
}
//int result = GVFSOpen("c:\\uuid-a1b42ca8-2718-4ca2-a44d-65e428f1de26");
return 0;
}
long readInteger(unsigned char *ch, int pointer, int size) {
int i;
long p = 0;
for(i = 0; i < size; i++)
p = ((p << 8) | (unsigned char)ch[pointer + i]);
return p;
}
long long readLongInteger(unsigned char *ch, int pointer, int size) {
int i;
long long p = 0;
for(i = 0; i < size; i++)
p = ((p << 8) | (unsigned char)ch[pointer + i]);
return p;
}
int GVFSOpen(char* fileName)
{
FILE* inFile;
inFile = fopen(fileName,"rb");
if (inFile == NULL)
{
return 1;
}
char outputPath[512];
sprintf(outputPath,"%s\.txt\0",fileName);
outFile = fopen(outputPath,"wb");
//get file size
fseek(inFile,0,SEEK_END);
long sizeFile = ftell(inFile);
fseek(inFile,0,SEEK_SET);
fileBuffer = (unsigned char*) malloc(sizeof(char) * sizeFile);
if (fileBuffer == NULL)
{
//memory error
return 2;
}
int check = fread(fileBuffer,1,sizeFile,inFile);
if (fileBuffer[0] == 0xDA && fileBuffer[1] == 0x1A)
{
//possible winner
char header[5];
strncpy(header,(char*)&fileBuffer[2],4);
header[4] = '\0';
if (strncmp(header,"meta",4) == 0)
{
//get base time
baseTime = readLongInteger(fileBuffer,24,8);
//find root offset
long ptr = readInteger((unsigned char*)fileBuffer,16,4);
readRoot(ptr);
}
}
fclose(inFile);
free(fileBuffer);
return 0;
}
int readRoot(long offset)
{
rootEntry rEntry;
long nameOffset = readInteger(fileBuffer,offset,4);
rEntry.name[0] = fileBuffer[nameOffset];
rEntry.name[1] = '\0';
rEntry.childOffset = readInteger(fileBuffer,offset + 4,4);
rEntry.metaOffset = readInteger(fileBuffer,offset + 8,4);
rEntry.lastUpdate = 0;
int numberChildren = readInteger(fileBuffer,rEntry.childOffset,4);
long tempOffset = rEntry.childOffset + 4;
rEntry.numMetaKeys = readInteger(fileBuffer, rEntry.metaOffset,4);
for (int n = 0;n<rEntry.numMetaKeys;n++)
{
rEntry.metaList[n] = readMeta(rEntry.metaOffset + 4 + (n * 8));
}
writeOutput(rEntry, 0);
for (int i = 0;i < numberChildren;i++)
{
readChild(tempOffset,1);
tempOffset +=16;
}
return 0;
}
int readChild(long offset, int level)
{
rootEntry cEntry;
long nameOffset = readInteger(fileBuffer,offset,4);
int nameLength = 0;
for (int i = 0;i< 250;i++)
{
if (fileBuffer[nameOffset + i] == 0x00)
{
nameLength = i;
i = 255;
}
}
strncpy(cEntry.name,(char*)&fileBuffer[nameOffset],nameLength);
cEntry.name[nameLength] = '\0';
cEntry.childOffset = readInteger(fileBuffer,offset + 4,4);
cEntry.metaOffset = readInteger(fileBuffer,offset + 8,4);
cEntry.lastUpdate = readInteger(fileBuffer,offset+12,4)+ baseTime;
cEntry.numMetaKeys = readInteger(fileBuffer, cEntry.metaOffset,4);
for (int n = 0;n<cEntry.numMetaKeys;n++)
{
cEntry.metaList[n] = readMeta(cEntry.metaOffset + 4 + (n * 8));
}
writeOutput(cEntry,level);
//free up strings from metakeys
for (int n = 0;n<cEntry.numMetaKeys;n++)
{
delete[] cEntry.metaList[n].text;
}
int numberChildren = readInteger(fileBuffer,cEntry.childOffset,4);
long tempOffset = cEntry.childOffset + 4;
if (numberChildren > 0)
{
for (int i = 0;i < numberChildren;i++)
{
readChild(tempOffset,level + 1);
tempOffset +=16;
}
}
return 0;
}
char* createMetaString(int numKeys, metadata list[10])
{
char* retString = new char[1024];
retString[0] = '\0';
char* tempStr = new char[128];
tempStr[0] = '\0';
for (int i=0;i< numKeys;i++)
{
sprintf(tempStr,"||%i,%s\0",list[i].keyword, list[i].text);
strcat(retString,tempStr);
tempStr[0] = '\0';
}
return retString;
}
void writeOutput(rootEntry entry, int level)
{
char output[1024];
if (entry.lastUpdate != 0)
{
char timeDisplay[30];
time_t tempTime = entry.lastUpdate;
sprintf(timeDisplay,"%s\0",asctime(gmtime(&tempTime)));
char* metaString = createMetaString(entry.numMetaKeys, entry.metaList);
sprintf(output,"%s, %lu, %lu, %s %i %s\x0D\x0A\0",entry.name,entry.childOffset, entry.metaOffset, timeDisplay, entry.numMetaKeys, metaString);
}
else
{
//root entry
char* metaString = createMetaString(entry.numMetaKeys, entry.metaList);
sprintf(output,"%s, %lu, %lu, %lu %i %s\x0D\x0A\0",entry.name,entry.childOffset, entry.metaOffset, entry.lastUpdate, entry.numMetaKeys,metaString);
}
char tab = 0x09;
for (int i=0;i< level;i++)
{
fwrite((char*)&tab,1,1,outFile);
}
fwrite(output,strlen(output),1,outFile);
fflush(outFile);
}
metadata readMeta(int offset)
{
metadata retMet;
retMet.keyword = readInteger(fileBuffer,offset, 4);
int stringOffset = readInteger(fileBuffer, offset+4, 4);
int strLen = 0;
for (int i = 0; i< 100; i++)
{
if (fileBuffer[stringOffset + i] == '\0')
{
strLen = i;
i = 101;
}
}
retMet.text = new char[strLen+1];
strncpy(retMet.text,(char*)&fileBuffer[stringOffset],strLen);
retMet.text[strLen] = '\0';
return retMet;
}