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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
// M1M2M3 M4M5M6 M7M8M9 MAMBMC 000000 P1P2P3
// M1M2M3 M4M5M6 M7M8M9 MAMBMC P1P2P3 000000
// M1M2M3 M4M5M6 M7M8M9 P1P2P3 MAMBMC 000000
// M1M2M3 M4M5M6 P1P2P3 M7M8M9 MAMBMC 000000
// M1M2M3 P1P2P3 M4M5M6 M7M8M9 MAMBMC 000000
// P1P2P3 M1M2M3 M4M5M6 M7M8M9 MAMBMC 000000
// M1M2M3 M4M5M6 M7M8M9 MAMBMC 000000 P1P2P3
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define DISK_COUNT 6
#define MESSAGE_SIZE 12
#define BLOCK_SIZE 3
#define PADDING_SIZE 3
#define PARITY_SIZE 3
#define DISK_SIZE 0x40
#define POPCOUNT __builtin_popcount
#define CTZ __builtin_ctz
enum command {
COMMAND_WRITE,
COMMAND_READ,
};
FILE *disks[DISK_COUNT];
#ifndef WIN32
#define LINE_SIZE (2*BLOCK_SIZE + 1)
#else
#define LINE_SIZE (2*BLOCK_SIZE + 2)
static ssize_t
getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) {
register char c;
register char *cs = NULL;
register int length = 0;
while ((c = getc (stream)) != EOF) {
cs = (char *) realloc (cs, ++length + 1);
if ((*(cs + length - 1) = c) == '\n') {
*(cs + length) = '\0';
break;
}
}
// return the allocated memory if lineptr is null
if ((*lineptr) == NULL) {
*lineptr = cs;
} else {
// check if enough memory is allocated
if ((length + 1) < *n)
*lineptr = (char *) realloc (*lineptr, length + 1);
memcpy (*lineptr, cs, length);
free (cs);
}
return (ssize_t) (*n = strlen (*lineptr));
}
#endif
static void
open_disks (uint8_t address)
{
uint16_t bad_disks = 0;
for (int i = 0; i < DISK_COUNT; ++i) {
char filename[] = "disk0";
filename[strlen (filename) - 1] += i;
disks[i] = fopen (filename, "r+");
if (!disks[i]) { // disk file was deleted
disks[i] = fopen (filename, "w+");
if (!disks[i]) {
fprintf (stderr, "ERROR: unable to open file: %s\n", filename);
exit (EXIT_FAILURE);
}
fprintf (stderr, "WARNING: found bad disk (missing): %s\n", filename);
bad_disks |= 1 << i;
} else { // disk file wasn't deleted
fseek (disks[i], 0, SEEK_END);
if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format
fprintf (stderr, "WARNING: found bad disk (corrupted): %s\n", filename);
bad_disks |= 1 << i;
} rewind (disks[i]);
}
}
// there were bad disks
int bad_disk_count = POPCOUNT (bad_disks);
if (bad_disk_count == 1) { // recompute lost disk
fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n");
int bad_disk = CTZ (bad_disks);
for (int j = 0; j < DISK_SIZE; ++j) {
uint8_t block_bytes[MESSAGE_SIZE + PADDING_SIZE + PARITY_SIZE] = {0};
// read blocks from good disks
bool empty = false;
for (int k = 0; k < DISK_COUNT; ++k) {
if (k == bad_disk) continue;
int c = fgetc (disks[k]);
if (c == ' ') {
fgetc (disks[k]);
fgetc (disks[k]);
fgetc (disks[k]);
fgetc (disks[k]);
fgetc (disks[k]);
empty = true;
} else {
ungetc(c, disks[k]);
fscanf(disks[k], "%02hhX%02hhX%02hhX",
&block_bytes[k * BLOCK_SIZE + 0],
&block_bytes[k * BLOCK_SIZE + 1],
&block_bytes[k * BLOCK_SIZE + 2]);
} fgetc (disks[k]);
}
if (empty) { // address empty
for (int l = 0; l < BLOCK_SIZE; ++l) {
fprintf (disks[bad_disk], " ");
} fprintf (disks[bad_disk], "\n");
continue;
}
// compute lost block
block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00;
block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00;
block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00;
for (int k = 0; k < DISK_COUNT; ++k) {
if (k == bad_disk) continue;
block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0];
block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1];
block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2];
}
// write lost block
fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX\n",
block_bytes[bad_disk*BLOCK_SIZE + 0],
block_bytes[bad_disk*BLOCK_SIZE + 1],
block_bytes[bad_disk*BLOCK_SIZE + 2]);
}
} else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost
fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n");
for (int i = 0; i < DISK_COUNT; ++i) {
for (int j = 0; j < DISK_SIZE; ++j)
fputs (" \n", disks[i]);
}
}
// position head for writing/reading message
for (int i = 0; i < DISK_COUNT; ++i)
fseek (disks[i], address*LINE_SIZE, SEEK_SET);
}
static void
close_disks (void)
{
for (int i = 0; i < DISK_COUNT; ++i)
fclose (disks[i]);
}
int
main (void)
{
fprintf (stderr, "INFO: initializing RAID 5 XOR on 5 disks with 12 byte messages...\n");
// check disk integrity on startup
open_disks (0);
close_disks ( );
for (;;) {
char *line = NULL;
size_t line_size = 0;
const char *head;
int assigned;
int scanned;
// read whole line
printf ("> ");
scanned = getline (&line, &line_size, stdin);
if (scanned == -1)
exit (EXIT_SUCCESS);
else if (scanned == 1)
continue;
head = line;
uint8_t address; { // scan address
assigned = sscanf (head, "%hhx%n", &address, &scanned);
if (assigned != 1 || address >= DISK_SIZE) {
fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n");
continue;
} head += scanned;
}
enum command command; { // scan command
char command_buffer[6] = {0};
assigned = sscanf (head, "%5s %n", command_buffer, &scanned);
if (assigned != 1 ||
(!strncmp (command_buffer, "read", 4) &&
!strncmp (command_buffer, "write", 5))) {
fprintf(stderr, "ERROR: command not in set (read; write)\n");
continue;
} head += scanned;
command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE;
}
switch (command) {
case COMMAND_READ: {
uint8_t message[MESSAGE_SIZE];
uint8_t padding[PADDING_SIZE];
uint8_t parity [ PARITY_SIZE];
// parse out message
open_disks (address);
int c = fgetc (disks[0]);
if (c == ' ') {
close_disks ();
printf ("EMPTY!\n");
break;
} ungetc (c, disks[0]);
switch (address % DISK_COUNT) {
case 0:
fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
break;
case 1:
fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
break;
case 2:
fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
break;
case 3:
fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
break;
case 4:
fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
break;
case 5:
fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
break;
} close_disks ();
printf ("%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX "
"%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX\n",
message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5],
message[0x6], message[0x7], message[0x8], message[0x9], message[0xA], message[0xB]);
} break;
case COMMAND_WRITE: {
uint8_t message[MESSAGE_SIZE] = {0}; { // scan message
int message_size = 0;
while (*head == ' ' ||
*head == '\t') ++head;
const char *invalid = NULL;
size_t len = 0;
for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) {
if (!strchr ("0123456789abcdefABCDEF", *c)) {
invalid = c;
break;
} len += 1;
} if (invalid) {
fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid);
break;
} if (len != 2*MESSAGE_SIZE) {
fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len);
break;
}
while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 &&
message_size < MESSAGE_SIZE) {
head += scanned;
message_size += 1;
}
}
// compute parity
uint8_t padding[PADDING_SIZE] = {
0x00,
0x00,
0x00,
};
uint8_t parity[PARITY_SIZE] = {
message[0x0] ^ message[0x3] ^ message[0x6] ^ message[0x9],
message[0x1] ^ message[0x4] ^ message[0x7] ^ message[0xA],
message[0x2] ^ message[0x5] ^ message[0x8] ^ message[0xB],
};
// write blocks
open_disks (address); // open disk files, ensuring their integrity
switch (address % DISK_COUNT) {
case 0:
fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
fprintf (disks[4], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
fprintf (disks[5], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
break;
case 1:
fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
fprintf (disks[4], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
break;
case 2:
fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
fprintf (disks[3], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
break;
case 3:
fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
fprintf (disks[2], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
break;
case 4:
fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
fprintf (disks[1], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
break;
case 5:
fprintf (disks[0], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
break;
} close_disks ();
} break;
} free (line);
}
return EXIT_SUCCESS;
}
|