aboutsummaryrefslogtreecommitdiff
path: root/src/raid-4-xor-12b-4d.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-07 07:26:34 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-07 08:38:33 +0000
commit7fe23e4a8fa6fce19fb3d5e3a8a1f5472496c928 (patch)
tree64c163a368fa600d6054796e05a4d8847ed1cd53 /src/raid-4-xor-12b-4d.c
downloadlab-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/raid-4-xor-12b-4d.c')
-rw-r--r--src/raid-4-xor-12b-4d.c289
1 files changed, 289 insertions, 0 deletions
diff --git a/src/raid-4-xor-12b-4d.c b/src/raid-4-xor-12b-4d.c
new file mode 100644
index 0000000..79f98fe
--- /dev/null
+++ b/src/raid-4-xor-12b-4d.c
@@ -0,0 +1,289 @@
1// Dash
2// M1M2M3M4 M5M6M7M8 M9MAMBMC P1P2P3P4
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 12
12#define PARITY_SIZE 4
13#define BLOCK_SIZE 4
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 9
29#else
30#define LINE_SIZE 10
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, "ERROR! Try another file \n");
71 exit (EXIT_FAILURE);
72 }
73
74 fprintf (stderr, "WARNING: MISSING DISK\n");
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: CORRUPTED DISK \n");
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, "INFO: detected only one faulty disk; recalculating 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 empty = true;
111 } else {
112 ungetc (c, disks[k]);
113 fscanf (disks[k], "%02hhX%02hhX%02hhX%02hhX",
114 &block_bytes[k*BLOCK_SIZE + 0],
115 &block_bytes[k*BLOCK_SIZE + 1],
116 &block_bytes[k*BLOCK_SIZE + 2],
117 &block_bytes[k*BLOCK_SIZE + 3]);
118 } fgetc (disks[k]);
119 }
120
121 if (empty) { // address empty
122 fprintf (disks[bad_disk], " \n");
123 continue;
124 }
125
126 // compute lost block
127 block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00;
128 block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00;
129 block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00;
130 block_bytes[bad_disk*BLOCK_SIZE + 3] = 0x00;
131 for (int k = 0; k < DISK_COUNT; ++k) {
132 if (k == bad_disk) continue;
133 block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0];
134 block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1];
135 block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2];
136 block_bytes[bad_disk*BLOCK_SIZE + 3] ^= block_bytes[k*BLOCK_SIZE + 3];
137 }
138
139 // write lost block
140 fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX%02hhX\n",
141 block_bytes[bad_disk*BLOCK_SIZE + 0],
142 block_bytes[bad_disk*BLOCK_SIZE + 1],
143 block_bytes[bad_disk*BLOCK_SIZE + 2],
144 block_bytes[bad_disk*BLOCK_SIZE + 3]);
145 }
146 } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost
147 fprintf (stderr, "ERROR: Exceeding the maximum number of faulty disks.\n");
148
149 for (int i = 0; i < DISK_COUNT; ++i) {
150 char filename[] = "disk0";
151 filename[strlen (filename) - 1] += i;
152 fclose (disks[i]);
153 disks[i] = fopen (filename, "w+");
154 for (int j = 0; j < DISK_SIZE; ++j)
155 fputs (" \n", disks[i]);
156 }
157 }
158
159 // position head for writing/reading message
160 for (int i = 0; i < DISK_COUNT; ++i)
161 fseek (disks[i], address*LINE_SIZE, SEEK_SET);
162}
163
164static void
165close_disks (void)
166{
167 for (int i = 0; i < DISK_COUNT; ++i)
168 fclose (disks[i]);
169}
170
171int
172main (void)
173{
174 fprintf (stderr, "INFO: Setting up RAID 4 with 4 disks, each handling 12-byte messages \n");
175 // check disk integrity on startup
176 open_disks (0);
177 close_disks ( );
178
179 for (;;) {
180 char *line = NULL;
181 size_t line_size = 0;
182
183 const char *head;
184 int assigned;
185 int scanned;
186
187 // read whole line
188 printf ("> ");
189 scanned = getline (&line, &line_size, stdin);
190 if (scanned == -1)
191 exit (EXIT_SUCCESS);
192 else if (scanned == 1)
193 continue;
194 head = line;
195
196 uint8_t address; { // scan address
197 assigned = sscanf (head, "%hhx%n", &address, &scanned);
198 if (assigned != 1 || address >= DISK_SIZE) {
199 fprintf(stderr, "ERROR: outside the range [0x00; 0x40) \n");
200 continue;
201 } head += scanned;
202 }
203
204 enum command command; { // scan command
205 char command_buffer[6] = {0};
206 assigned = sscanf (head, "%5s %n", command_buffer, &scanned);
207 if (assigned != 1 ||
208 (!strncmp (command_buffer, "read", 4) &&
209 !strncmp (command_buffer, "write", 5))) {
210 fprintf(stderr, "ERROR: The provided command is not valid. Please use either 'read' or 'write\n");
211 continue;
212 } head += scanned;
213 command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE;
214 }
215
216 switch (command) {
217 case COMMAND_READ: {
218 uint8_t message[MESSAGE_SIZE];
219 uint8_t parity [ PARITY_SIZE];
220
221 // parse out message
222 open_disks (address);
223 int c = fgetc (disks[0]);
224 if (c == ' ') {
225 close_disks ();
226 printf ("No data!\n");
227 break;
228 } ungetc (c, disks[0]);
229 fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3]);
230 fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX\n", &message[0x4], &message[0x5], &message[0x6], &message[0x7]);
231 fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX\n", &message[0x8], &message[0x9], &message[0xA], &message[0xB]);
232 fscanf (disks[3], "%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3]);
233 close_disks ();
234
235 printf ("%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX\n",
236 message[0x0], message[0x1], message[0x2], message[0x3],
237 message[0x4], message[0x5], message[0x6], message[0x7],
238 message[0x8], message[0x9], message[0xA], message[0xB]);
239 } break;
240 case COMMAND_WRITE: {
241 uint8_t message[MESSAGE_SIZE] = {0}; { // scan message
242 int message_size = 0;
243
244 while (*head == ' ' ||
245 *head == '\t') ++head;
246 const char *invalid = NULL;
247 size_t len = 0;
248 for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) {
249 if (!strchr ("0123456789abcdefABCDEF", *c)) {
250 invalid = c;
251 break;
252 } len += 1;
253 } if (invalid) {
254 fprintf (stderr, "ERROR: message contains invalid character '%c'\n", *invalid);
255 break;
256 } if (len != 2*MESSAGE_SIZE) {
257 fprintf (stderr, "ERROR: expected %d hex-digits for message, received %zu\n", 2*MESSAGE_SIZE, len);
258 break;
259 }
260
261 while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 &&
262 message_size < MESSAGE_SIZE) {
263 head += scanned;
264 message_size += 1;
265 }
266 }
267
268 // compute parity
269 uint8_t parity[PARITY_SIZE] = {
270 message[0x0] ^ message[0x4] ^ message[0x8],
271 message[0x1] ^ message[0x5] ^ message[0x9],
272 message[0x2] ^ message[0x6] ^ message[0xA],
273 message[0x3] ^ message[0x7] ^ message[0xB],
274 };
275
276 // write blocks
277 open_disks (address); // open disk files, ensuring their integrity
278 fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3]);
279 fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX\n", message[0x4], message[0x5], message[0x6], message[0x7]);
280 fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX\n", message[0x8], message[0x9], message[0xA], message[0xB]);
281 fprintf (disks[3], "%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3]);
282 close_disks ();
283 } break;
284 }
285 free (line);
286 }
287
288 return EXIT_SUCCESS;
289}