-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileRecovery.cpp
More file actions
241 lines (208 loc) · 5.91 KB
/
FileRecovery.cpp
File metadata and controls
241 lines (208 loc) · 5.91 KB
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
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include <math.h>
using namespace std;
void outPutMkfs(char inPutAdd[20])
{
char temp[100]="sudo dd if=/dev/";
strcat(temp,inPutAdd);
strcat(temp, " skip=2048 count=1 of=mkfs.vfat ");
system(temp);
//system("xxd mkfs.vfat");//tiao shi ming ling
}
void creatVfat(char inPutAdd[20])
{
char temp[100]="sudo mkfs.vfat /dev/";
strcat(temp,inPutAdd);
strcat(temp, "1");
system(temp);
}
bool isFat32(int &num1,int& num2)
{
if(num1==0&&num2==0)
{
return true;
}
else
{
return false;
}
}
int analyzeVbrAndGetNumber(int &startingLABOfThePartition)
{
unsigned char vbr[512];//char -127~128 unsigned 0~256
memset(vbr,0,512);
int rootDirectory;
int v[512];
memset(v,0,512);
// string a[512];
int i=0;
FILE *fp;
fp=fopen("mkfs.vfat","r");
if(fp==NULL){cout<<"error";}
while(i<512)
{
vbr[i]=getc(fp);
v[i]=vbr[i]-0;
i++;
}
/**************************************/
int sectorsPerCluster=v[13];
int sizeInSectorsOfTheReservedArea=v[15]*256+v[14];
int numberOfFATs=v[16];
int sizeOfEachFATInSectors=v[36]+v[37]*256+v[38]*65536+v[39]*16777216 ;
int clusterWhereRootDirectoryCanBeFound=(v[44]+v[45]*256+v[46]*65536+v[47]*16777216-2)*sectorsPerCluster;
//*****************************************************************//
if(isFat32(v[22],v[23]))//FAT32
{
rootDirectory=startingLABOfThePartition + sizeInSectorsOfTheReservedArea +
numberOfFATs*sizeOfEachFATInSectors + clusterWhereRootDirectoryCanBeFound;
} else//FAT16
{
rootDirectory=startingLABOfThePartition + sizeInSectorsOfTheReservedArea +
numberOfFATs*sizeOfEachFATInSectors;
}
return rootDirectory;
}
void outPutRootDir(char inPutAdd[20],int &rootDirNum)
{
char temp[100]="sudo dd if=/dev/";
char num[20];
sprintf(num,"%d",rootDirNum);
strcat(temp,inPutAdd);
strcat(temp," skip=");
strcat(temp,num);
strcat(temp, " count=1 of=rootDir.x ");
system(temp);
// system("xxd rootDir.x");//tiao shi
getchar();
}
void writeToDisk(unsigned char vbr[512],char inPutAdd[20],int &rootDirNum)
{
FILE *fp ;
fp=fopen("rootDir.x","w");
for (int i = 0; i < 512;i++) {
fprintf(fp,"%c",vbr[i]);
}
fclose(fp);
char temp[100]="sudo dd if=rootDir.x ";
char num[20];
sprintf(num,"%d",rootDirNum);
strcat(temp," count=1 of=/dev/");
strcat(temp,inPutAdd);
strcat(temp," seek=");
strcat(temp,num);
system(temp);
printf("File is here!");
}
void analyzeRootDir(int &rootDirNumber,char inPutAdd[20])
{
unsigned char vbr[512];//char -127~128 unsigned char 0~256
memset(vbr, 0, 512);
int rootDirectory;
int v[512];
memset(v, 0, 512);
// string a[512];
int i = 0;
FILE *fp = NULL;
fp = fopen("rootDir.x", "r");
if (fp == NULL) { cout << "error"; }
while (i < 512) {
vbr[i] = getc(fp);
v[i] = vbr[i] - 0;
i++;
}
//************************************
int num_inLNF=11;
for (int j = 0; j < 10; ++j)
{
if (v[num_inLNF]==15)//isLNF
{
int jishu=2;
if(v[num_inLNF-11]==229)//is DELETE
{
vbr[num_inLNF-11]=(unsigned char)'101';
vbr[num_inLNF+21]=(unsigned char)'101';
}
num_inLNF+=32*jishu;
} else
{
if(v[num_inLNF-11]==229)//is DELETE
{
vbr[num_inLNF-11]=(unsigned char)'101';
}
num_inLNF+=32;
}
}
writeToDisk(vbr,inPutAdd,rootDirNumber);
/////////////////////////////////
// int num_inLNF=11;//chu shi ji shu isLAN
// int num_bof=0;//begin of per file
// for (int k = 0; k <10; k++) // du qu shi ge wen jian
// {
// string fileName;
// char t;
// if(v[num_inLNF]==15)// isLNF
// {
// for (int j = 1; j <11 ; j=j+2) //Get The File Name
// { t=(char)v[num_bof+j];fileName+=t; }
// for (int j = 14; j <26 ; j=j+2) //Get The File Name
// { t=(char)v[num_bof+j];fileName+=t; }
// for (int j = 28; j <32 ; j=j+2) //Get The File Name
// { t=(char)v[num_bof+j];fileName+=t; }
// num_inLNF=num_inLNF+howManyBytes(v[num_bof]);
// num_bof=num_inLNF-11;
// }
// cout<<"FileName:"<<fileName<<endl<<endl;
// /////////////Created Time
}
void mountIt(char inPutAdd[20])
{
char temp[100]="sudo mkdir /mnt/";
strcat(temp,inPutAdd);
strcat(temp,"1");
system(temp);
char temp1[100]="sudo mount /dev/";
strcat(temp1,inPutAdd);
strcat(temp1,"1 /mnt/");
strcat(temp1,inPutAdd);
strcat(temp1,"1");
system(temp1);
//system("lsblk");
}
void unMountIt(char inPutAdd[20])
{
char t[100] = "umount /dev/";
strcat(t, inPutAdd);
system(t);
char t1[100] = "umount /dev/";
strcat(t1, inPutAdd);
strcat(t1, "1");
system(t1);
}
int main() {
char inPutAdd[20];
int startingLABOfThePartition;
cout << "\nPlease input the address you want analyze.\n"<<"eg." << "sdb" << endl;
fflush(stdin);//
cin >> inPutAdd;
//******************************************************
//unMountIt(inPutAdd);
creatVfat(inPutAdd);
outPutMkfs(inPutAdd);
cout << "\nPlease input starting LAB of the partition. \n"<<"eg" << " 2048" << endl;
fflush(stdin);
cin >> startingLABOfThePartition;
int rootNumber=analyzeVbrAndGetNumber(startingLABOfThePartition);
//mountIt(inPutAdd);
outPutRootDir(inPutAdd,rootNumber);
analyzeRootDir(rootNumber,inPutAdd);
cout<<"\n\nThe program will automatically mount the hard disk.\n";
unMountIt(inPutAdd);
mountIt(inPutAdd);
cout<<"\n\nPress the Enter key and end the program.\n";
getchar();
return 0;
}