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