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