-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
encrypt.c
201 lines (160 loc) · 4.1 KB
/
encrypt.c
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
/*
* Generate an encrypted version of the specified input file.
*
* Optionally update the CRC check of the file - this is necessary
* for the .TAP version we generate for the ZX Spectrum.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
typedef unsigned char byte;
// calculate the checksum for the given region
byte crc(byte *start, int length)
{
byte tcrc = 0;
for (int i = 0; i < length; i++)
tcrc = tcrc ^ start[i];
return tcrc;
}
int main(int argc, char *argv[])
{
// check argument count
if (argc != 3 && argc != 4)
{
printf("Usage: encrypt [-crc] input output\n");
return 1;
}
// flags / arguments
int do_crc = 0;
char *input = NULL;
char *output = NULL;
// look for flags
if (argc == 4)
{
// Should we run a CRC update?
for (int i = 1; i < argc ; i++)
{
if (strcmp(argv[i], "-crc") == 0)
{
do_crc = 1;
}
}
}
// look for input/output names.
for (int i = 1; i < argc ; i++)
{
// Ignore flags
if (strcmp(argv[i], "-crc") == 0)
{
continue;
}
// input file goes first
if (input == NULL)
{
input = argv[i];
continue;
}
// output file goes second.
if (output == NULL)
{
output = argv[i];
continue;
}
}
printf("Input file: %s\n", input);
printf("Output file: %s\n", output);
if (do_crc)
{
printf("CRC will be updated\n");
}
else
{
printf("No CRC update\n");
}
// Open the file for reading
FILE *f = fopen(input, "r");
if (f == NULL)
{
printf("Failed to open input file: %s\n", input);
return 1;
}
// get file size
fseek(f, 0L, SEEK_END);
size_t sz = ftell(f);
printf("File size is %ld bytes\n", sz);
// get back to start
fseek(f, 0L, SEEK_SET);
// allocate memory
unsigned char *buf = malloc(sz);
if (buf == NULL)
{
printf("failed to allocate memory\n");
return 1;
}
// read the file
int n = fread(buf, sizeof(unsigned char), sz, f);
if (ferror(f) != 0)
{
printf("error reading\n");
return 1;
}
if (n != sz)
{
printf("short read\n");
return 1;
}
fclose(f);
// Look for our flag
for (int i = 0; i < sz; i++)
{
if ((buf[i] == 'S') &&
(buf[i + 1] == 'K') &&
(buf[i + 2] == 'X'))
{
printf("Found start of section to encrypt.\n");
// starting key
char k = '%';
// XOR with our key, and increase that key
for (int pos = i ; pos < sz; pos++)
{
buf[pos] ^= k;
k++;
}
if (do_crc)
{
// Now we have to fixup the CRC
unsigned char *pos = buf;
for (int i = 1; i < sz; i++)
{
size_t blocksize = pos[0] | (pos[1] << 8);
if ((pos + blocksize + 1) < (buf + sz))
{
if (blocksize > 1)
{
char ccrc = crc(&pos[2], blocksize - 1);
if (pos[blocksize + 1] != ccrc)
{
printf("Updated CRC\n");
pos[blocksize + 1] = ccrc;
}
}
}
pos += blocksize + 2;
}
}
// Write
FILE *nw = fopen(output, "w");
if (nw == NULL)
{
printf("Failed to open file for writing: %s\n", output);
}
fwrite(buf, sz, 1, nw);
fclose(nw);
printf("All done\n");
return 0;
}
}
printf("Failed to find encryption start position\n");
return 1;
}