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