aboutsummaryrefslogtreecommitdiff
path: root/src/raid-4-xor-14b-4d.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/raid-4-xor-14b-4d.c')
-rw-r--r--src/raid-4-xor-14b-4d.c298
1 files changed, 298 insertions, 0 deletions
diff --git a/src/raid-4-xor-14b-4d.c b/src/raid-4-xor-14b-4d.c
new file mode 100644
index 0000000..f2f098c
--- /dev/null
+++ b/src/raid-4-xor-14b-4d.c
@@ -0,0 +1,298 @@
1// Dash
2// M1M2M3M4M5 M6M7M8M9MA MBMCMDME00 P1P2P3P4P5
3#include <stdlib.h>
4#include <stdio.h>
5#include <stdint.h>
6#include <stdbool.h>
7#include <string.h>
8#include <ctype.h>
9
10#define DISK_COUNT 4
11#define MESSAGE_SIZE 14
12#define PARITY_SIZE 5
13#define BLOCK_SIZE 5
14
15#define DISK_SIZE 0x40
16
17#define POPCOUNT __builtin_popcount
18#define CTZ __builtin_ctz
19
20enum command {
21 COMMAND_WRITE,
22 COMMAND_READ,
23};
24
25FILE *disks[DISK_COUNT];
26
27#ifndef WIN32
28#define LINE_SIZE 11
29#else
30#define LINE_SIZE 12
31static ssize_t
32getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) {
33 register char c;
34 register char *cs = NULL;
35 register int length = 0;
36 while ((c = getc (stream)) != EOF) {
37 cs = (char *) realloc (cs, ++length + 1);
38 if ((*(cs + length - 1) = c) == '\n') {
39 *(cs + length) = '\0';
40 break;
41 }
42 }
43
44 // return the allocated memory if lineptr is null
45 if ((*lineptr) == NULL) {
46 *lineptr = cs;
47 } else {
48 // check if enough memory is allocated
49 if ((length + 1) < *n)
50 *lineptr = (char *) realloc (*lineptr, length + 1);
51 memcpy (*lineptr, cs, length);
52 free (cs);
53 }
54
55 return (ssize_t) (*n = strlen (*lineptr));
56}
57#endif
58
59static void
60open_disks (uint8_t address)
61{
62 uint16_t bad_disks = 0;
63 for (int i = 0; i < DISK_COUNT; ++i) {
64 char filename[] = "disk0";
65 filename[strlen (filename) - 1] += i;
66 disks[i] = fopen (filename, "r+");
67 if (!disks[i]) { // disk file was deleted
68 disks[i] = fopen (filename, "w+");
69 if (!disks[i]) {
70 fprintf (stderr, "mistake: the file could not be opened: %s\n", filename);
71 exit (EXIT_FAILURE);
72 }
73
74 fprintf (stderr, "warning: a faulty disk has been found or the disk is missing: %s\n", filename);
75 bad_disks |= 1 << i;
76 } else { // disk file wasn't deleted
77 fseek (disks[i], 0, SEEK_END);
78 if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format
79 fprintf (stderr, "warning: a faulty/damaged disk has been detected: %s\n", filename);
80 bad_disks |= 1 << i;
81 } rewind (disks[i]);
82 }
83 }
84
85 // there were bad disks
86 int bad_disk_count = POPCOUNT (bad_disks);
87 if (bad_disk_count == 1) { // recompute lost disk
88 fprintf (stderr, "message: only one faulty disk has been detected, I am detecting it\n");
89 int bad_disk = CTZ (bad_disks);
90
91 for (int j = 0; j < DISK_SIZE; ++j) {
92 uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT];
93
94 // read blocks from good disks
95 bool empty = false;
96 for (int k = 0; k < DISK_COUNT; ++k) {
97 if (k == bad_disk) continue;
98 int c = fgetc (disks[k]);
99 if (c == ' ') {
100 fgetc (disks[k]);
101
102 fgetc (disks[k]);
103 fgetc (disks[k]);
104
105 fgetc (disks[k]);
106 fgetc (disks[k]);
107
108 fgetc (disks[k]);
109 fgetc (disks[k]);
110
111 fgetc (disks[k]);
112 fgetc (disks[k]);
113 empty = true;
114 } else {
115 ungetc (c, disks[k]);
116 fscanf (disks[k], "%02hhX%02hhX%02hhX%02hhX%02hhX",
117 &block_bytes[k*BLOCK_SIZE + 0],
118 &block_bytes[k*BLOCK_SIZE + 1],
119 &block_bytes[k*BLOCK_SIZE + 2],
120 &block_bytes[k*BLOCK_SIZE + 3],
121 &block_bytes[k*BLOCK_SIZE + 4]);
122 } fgetc (disks[k]);
123 }
124
125 if (empty) { // address empty
126 fprintf (disks[bad_disk], " \n");
127 continue;
128 }
129
130 // compute lost block
131 block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00;
132 block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00;
133 block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00;
134 block_bytes[bad_disk*BLOCK_SIZE + 3] = 0x00;
135 block_bytes[bad_disk*BLOCK_SIZE + 4] = 0x00;
136 for (int k = 0; k < DISK_COUNT; ++k) {
137 if (k == bad_disk) continue;
138 block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0];
139 block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1];
140 block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2];
141 block_bytes[bad_disk*BLOCK_SIZE + 3] ^= block_bytes[k*BLOCK_SIZE + 3];
142 block_bytes[bad_disk*BLOCK_SIZE + 4] ^= block_bytes[k*BLOCK_SIZE + 4];
143 }
144
145 // write lost block
146 fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX%02hhX%02hhX\n",
147 block_bytes[bad_disk*BLOCK_SIZE + 0],
148 block_bytes[bad_disk*BLOCK_SIZE + 1],
149 block_bytes[bad_disk*BLOCK_SIZE + 2],
150 block_bytes[bad_disk*BLOCK_SIZE + 3],
151 block_bytes[bad_disk*BLOCK_SIZE + 4]);
152 }
153 } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost
154 fprintf (stderr, "mistake: there are too many bad disks, we are re-initializing arrays with new disks\n");
155
156 for (int i = 0; i < DISK_COUNT; ++i) {
157 char filename[] = "disk0";
158 filename[strlen (filename) - 1] += i;
159 fclose (disks[i]);
160 disks[i] = fopen (filename, "w+");
161 for (int j = 0; j < DISK_SIZE; ++j)
162 fputs (" \n", disks[i]);
163 }
164 }
165
166 // position head for writing/reading message
167 for (int i = 0; i < DISK_COUNT; ++i)
168 fseek (disks[i], address*LINE_SIZE, SEEK_SET);
169}
170
171static void
172close_disks (void)
173{
174 for (int i = 0; i < DISK_COUNT; ++i)
175 fclose (disks[i]);
176}
177
178int
179main (void)
180{
181 fprintf (stderr, "message: Initializing RAID 4 with XOR and 14 byte messages on 4 disks\n");
182 // check disk integrity on startup
183 open_disks (0);
184 close_disks ( );
185
186 for (;;) {
187 const char *head;
188 int assigned;
189 int scanned;
190
191 // read whole line
192 printf ("> ");
193 char *line = NULL;
194 size_t line_size = 0;
195 scanned = getline (&line, &line_size, stdin);
196 if (scanned == -1)
197 exit (EXIT_SUCCESS);
198 else if (scanned == 1)
199 continue;
200 head = line;
201
202 uint8_t address; { // scan address
203 assigned = sscanf (head, "%hhX%n", &address, &scanned);
204 if (assigned != 1 || address >= DISK_SIZE) {
205 fprintf(stderr, "mistake: the address must belong to [0x00; 0x40)\n");
206 continue;
207 } head += scanned;
208 }
209
210 enum command command; { // scan command
211 char command_buffer[6] = {0};
212 assigned = sscanf (head, "%5s %n", command_buffer, &scanned);
213 if (assigned != 1 ||
214 (!strncmp (command_buffer, "read", 4) &&
215 !strncmp (command_buffer, "write", 5))) {
216 fprintf(stderr, "mistake: only the read and write commands are available\n");
217 continue;
218 } head += scanned;
219 command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE;
220 }
221
222 switch (command) {
223 case COMMAND_READ: {
224 uint8_t message[MESSAGE_SIZE];
225 uint8_t parity [ PARITY_SIZE];
226 uint8_t reserved[1];
227
228 // parse out message
229 open_disks (address);
230 int c = fgetc (disks[0]);
231 if (c == ' ') {
232 close_disks ();
233 printf ("No data!\n");
234 break;
235 } ungetc (c, disks[0]);
236 fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3], &message [0x4]);
237 fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x5], &message[0x6], &message[0x7], &message[0x8], &message [0x9]);
238 fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0xA], &message[0xB], &message[0xC], &message[0xD], &reserved[0x0]);
239 fscanf (disks[3], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3], &parity [0x4]);
240 close_disks ();
241
242 printf ("%02hhX%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX\n",
243 message[0x0], message[0x1], message[0x2], message[0x3], message[0x4],
244 message[0x5], message[0x6], message[0x7], message[0x8], message[0x9],
245 message[0xA], message[0xB], message[0xC], message[0xD]);
246 } break;
247 case COMMAND_WRITE: {
248 uint8_t message[MESSAGE_SIZE] = {0}; { // scan message
249 int message_size = 0;
250
251 while (*head == ' ' ||
252 *head == '\t') ++head;
253 const char *invalid = NULL;
254 size_t len = 0;
255 for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) {
256 if (!strchr ("0123456789abcdefABCDEF", *c)) {
257 invalid = c;
258 break;
259 } len += 1;
260 } if (invalid) {
261 fprintf (stderr, "mistake: message contains invalid character `%c'\n", *invalid);
262 break;
263 } if (len != 2*MESSAGE_SIZE) {
264 fprintf (stderr, "mistake: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len);
265 break;
266 }
267
268 while (sscanf (head, "%2hhX%n", &message[message_size], &scanned) == 1 &&
269 message_size < MESSAGE_SIZE) {
270 head += scanned;
271 message_size += 1;
272 }
273 }
274
275 uint8_t reserved[1] = {0};
276
277 // compute parity
278 uint8_t parity[PARITY_SIZE] = {
279 message[0x0] ^ message[0x5] ^ message [0xA],
280 message[0x1] ^ message[0x6] ^ message [0xB],
281 message[0x2] ^ message[0x7] ^ message [0xC],
282 message[0x3] ^ message[0x8] ^ message [0xD],
283 message[0x4] ^ message[0x9] ^ reserved[0x0],
284 };
285
286 // write blocks
287 open_disks (address); // open disk files, ensuring their integrity
288 fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3], message [0x4]);
289 fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x5], message[0x6], message[0x7], message[0x8], message [0x9]);
290 fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0xA], message[0xB], message[0xC], message[0xD], reserved[0x0]);
291 fprintf (disks[3], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3], parity [0x4]);
292 close_disks ();
293 } break;
294 } free (line);
295 }
296
297 return EXIT_SUCCESS;
298}