-
Notifications
You must be signed in to change notification settings - Fork 2
/
decloak.cpp
152 lines (132 loc) · 3.16 KB
/
decloak.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
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include "array.h"
#include "rs.h"
void inform(char *fmt, ...)
{
va_list arg;
va_start(arg, fmt);
vfprintf(stderr, fmt, arg);
va_end(arg);
}
void panic(char *fmt, ...)
{
va_list arg;
va_start(arg, fmt);
vfprintf(stderr, fmt, arg);
va_end(arg);
exit(0);
}
int main()
{
Array<unsigned char> sectors(2880*512);
Array<unsigned char> newsectors(2880*512);
int i,j,k;
FILE *fp;
// Initialize Reed-Solomon code
init_rs();
HANDLE hDrive;
LPVOID pDriveSectors;
DWORD bytesWritten,dw;
hDrive = CreateFile(
"\\\\.\\A:",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING,
NULL);
if (!hDrive)
panic("Couldn't open disk drive A:");
pDriveSectors = VirtualAlloc(
NULL,
512*2880,
MEM_COMMIT,
PAGE_READWRITE);
if (!pDriveSectors)
panic("Couldn't allocate enough buffers for a 1.44MB diskette");
#define INVALID_SET_FILE_POINTER (DWORD) -1
for(dw=0; dw<2880; dw+=8) {
if (INVALID_SET_FILE_POINTER == SetFilePointer(
hDrive,
dw*512,
NULL,
FILE_BEGIN))
panic("Couldn't seek!");
if(0 == ReadFile(
hDrive,
((UCHAR *)pDriveSectors)+512*dw,
512*8,
&bytesWritten,
NULL))
inform("Defective sector: %d\n", dw);
printf("\b\b\b\b\b\b\b\b\b%03d/%03d", 100*dw/2879, 100);
}
CopyMemory(sectors, pDriveSectors, 512*2880);
VirtualFree(
pDriveSectors,
0,
MEM_RELEASE);
CloseHandle(hDrive);
j=0; k=0;
for(j=0;j<2880*512;j++) {
newsectors[j] = sectors[k];
k+=4096;
if(k>=2880*512) {
k-=2880*512;
k++;
}
}
int iFileLen = 0;
fp = fopen("datanew", "wb");
i = 0;
switch(eras_dec_rs(((unsigned char *)newsectors)+i*(NN+1), NULL, 0)) {
case -1:
panic("File impaired beyond correction\n");
break;
case 0:
printf(".");
iFileLen = *(unsigned int *) (unsigned char *)newsectors;
fwrite(((unsigned char *)newsectors)+i*(NN+1)+20,
1, (iFileLen>KK-20)?KK-20:iFileLen, fp);
break;
default:
printf("E");
iFileLen = *(unsigned int *) (unsigned char *)newsectors;
fwrite(((unsigned char *)newsectors)+i*(NN+1)+20,
1, (iFileLen>KK-20)?KK-20:iFileLen, fp);
break;
}
i++;
iFileLen -= (iFileLen>KK-20)?KK-20:iFileLen;
while(iFileLen) {
switch(eras_dec_rs(((unsigned char *)newsectors)+i*(NN+1), NULL, 0)) {
case -1:
panic("File impaired beyond correction\n");
break;
case 0:
if((i%80) == 0)
printf(".");
fwrite(((unsigned char *)newsectors)+i*(NN+1),
1, (iFileLen>KK)?KK:iFileLen, fp);
break;
default:
printf("E");
fwrite(((unsigned char *)newsectors)+i*(NN+1),
1, (iFileLen>KK)?KK:iFileLen, fp);
break;
}
i++;
iFileLen -= (iFileLen>KK)?KK:iFileLen;
}
fclose(fp);
rename("datanew", (const char*) (unsigned char *)newsectors+4);
printf("\nCreated file '%s' (%d bytes)",
(unsigned char *)newsectors+4,
*((unsigned int *)(unsigned char *)newsectors));
return 0;
}