aboutsummaryrefslogtreecommitdiff
path: root/src/raid-4-xor-8b-8d.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/raid-4-xor-8b-8d.c')
-rw-r--r--src/raid-4-xor-8b-8d.c286
1 files changed, 286 insertions, 0 deletions
diff --git a/src/raid-4-xor-8b-8d.c b/src/raid-4-xor-8b-8d.c
new file mode 100644
index 0000000..898fb5d
--- /dev/null
+++ b/src/raid-4-xor-8b-8d.c
@@ -0,0 +1,286 @@
1// nguyen
2// M1M2 M3M4 M5M6 M7M8 0000 0000 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 8
11#define MESSAGE_SIZE 8
12#define BLOCK_SIZE 2
13#define PADDING_SIZE 6
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 5
29#else
30#define LINE_SIZE 6
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, "file opening error: %s\n", filename);
71 exit (EXIT_FAILURE);
72 }
73
74 fprintf (stderr, "missing disk is detected: %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, "damaged disk is 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, "only 1 bad disk detected, recalculate 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 empty = true;
105 } else {
106 ungetc (c, disks[k]);
107 fscanf (disks[k], "%02hhX%02hhX",
108 &block_bytes[k*BLOCK_SIZE + 0],
109 &block_bytes[k*BLOCK_SIZE + 1]);
110 } fgetc (disks[k]);
111 }
112
113 if (empty) { // address empty
114 for (int l = 0; l < BLOCK_SIZE; ++l) {
115 fprintf (disks[bad_disk], " ");
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*BLOCK_SIZE + 0];
126 block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 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, "many unsuitable disks are detected, the array is reinitialized with new disks...\n");
136
137 for (int i = 0; i < DISK_COUNT; ++i) {
138 char filename[] = "disk0";
139 filename[strlen (filename) - 1] += i;
140 fclose (disks[i]);
141 disks[i] = fopen (filename, "w+");
142 for (int j = 0; j < DISK_SIZE; ++j)
143 fputs (" \n", disks[i]);
144 }
145 }
146
147 // position head for writing/reading message
148 for (int i = 0; i < DISK_COUNT; ++i)
149 fseek (disks[i], address*LINE_SIZE, SEEK_SET);
150}
151
152static void
153close_disks (void)
154{
155 for (int i = 0; i < DISK_COUNT; ++i)
156 fclose (disks[i]);
157}
158
159int
160main (void)
161{
162 fprintf (stderr, "RAID 4 XOR initialization on 8 disks with 8-byte messages...\n");
163 // check disk integrity on startup
164 open_disks (0);
165 close_disks ( );
166
167 for (;;) {
168 char *line = NULL;
169 size_t line_size = 0;
170 const char *head;
171 int assigned;
172 int scanned;
173
174 // read whole line
175 printf ("> ");
176 scanned = getline (&line, &line_size, stdin);
177 if (scanned == -1)
178 exit (EXIT_SUCCESS);
179 else if (scanned == 1)
180 continue;
181 head = line;
182
183 uint8_t address; { // scan address
184 assigned = sscanf (head, "%hhx%n", &address, &scanned);
185 if (assigned != 1 || address >= DISK_SIZE) {
186 fprintf(stderr, "invalid address, use: [0x00; 0x40)\n");
187 continue;
188 } head += scanned;
189 }
190
191 enum command command; { // scan command
192 char command_buffer[6] = {0};
193 assigned = sscanf (head, "%5s %n", command_buffer, &scanned);
194 if (assigned != 1 ||
195 (!strncmp (command_buffer, "read", 4) &&
196 !strncmp (command_buffer, "write", 5))) {
197 fprintf(stderr, "command does not exist: expected (read/write)\n");
198 continue;
199 } head += scanned;
200 command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE;
201 }
202
203 switch (command) {
204 case COMMAND_READ: {
205 uint8_t message[MESSAGE_SIZE];
206 uint8_t parity [ BLOCK_SIZE];
207 uint8_t padding[PADDING_SIZE];
208
209 // parse out message
210 open_disks (address);
211 int c = fgetc (disks[0]);
212 if (c == ' ') {
213 close_disks ();
214 printf ("EMPTY!\n");
215 break;
216 } ungetc (c, disks[0]);
217
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", &padding[0x0], &padding[0x1]);
223 fscanf (disks[5], "%02hhX%02hhX\n", &padding[0x2], &padding[0x3]);
224 fscanf (disks[6], "%02hhX%02hhX\n", &padding[0x4], &padding[0x5]);
225 fscanf (disks[7], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]);
226 close_disks ();
227
228 printf ("%02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX\n",
229 message[0x0], message[0x1],
230 message[0x2], message[0x3],
231 message[0x4], message[0x5],
232 message[0x6], message[0x7]);
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[BLOCK_SIZE] = {
266 message[0x0] ^ message[0x2] ^ message[0x4] ^ message[0x6] ^ padding[0x0] ^ padding[0x2] ^ padding[0x4],
267 message[0x1] ^ message[0x3] ^ message[0x5] ^ message[0x7] ^ padding[0x1] ^ padding[0x3] ^ padding[0x5],
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", padding[0x0], padding[0x1]);
277 fprintf (disks[5], "%02hhX%02hhX\n", padding[0x2], padding[0x3]);
278 fprintf (disks[6], "%02hhX%02hhX\n", padding[0x4], padding[0x5]);
279 fprintf (disks[7], "%02hhX%02hhX\n", parity [0x0], parity [0x1]);
280 close_disks ();
281 } break;
282 } free (line);
283 }
284
285 return EXIT_SUCCESS;
286}