aboutsummaryrefslogtreecommitdiff
path: root/src/raid-5-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-5-xor-12b-5d.c
downloadlab-7fe23e4a8fa6fce19fb3d5e3a8a1f5472496c928.tar.gz
publishHEADmaster
Diffstat (limited to 'src/raid-5-xor-12b-5d.c')
-rw-r--r--src/raid-5-xor-12b-5d.c341
1 files changed, 341 insertions, 0 deletions
diff --git a/src/raid-5-xor-12b-5d.c b/src/raid-5-xor-12b-5d.c
new file mode 100644
index 0000000..66afa7b
--- /dev/null
+++ b/src/raid-5-xor-12b-5d.c
@@ -0,0 +1,341 @@
1// Жанг
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 BLOCK_SIZE 3
13
14#define DISK_SIZE 0x40
15
16#define POPCOUNT __builtin_popcount
17#define CTZ __builtin_ctz
18
19enum command {
20 COMMAND_WRITE,
21 COMMAND_READ,
22};
23
24FILE *disks[DISK_COUNT];
25
26#ifndef WIN32
27#define LINE_SIZE 7
28#else
29#define LINE_SIZE 8
30static ssize_t
31getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) {
32 register char c;
33 register char *cs = NULL;
34 register int length = 0;
35 while ((c = getc (stream)) != EOF) {
36 cs = (char *) realloc (cs, ++length + 1);
37 if ((*(cs + length - 1) = c) == '\n') {
38 *(cs + length) = '\0';
39 break;
40 }
41 }
42
43 // return the allocated memory if lineptr is null
44 if ((*lineptr) == NULL) {
45 *lineptr = cs;
46 } else {
47 // check if enough memory is allocated
48 if ((length + 1) < *n)
49 *lineptr = (char *) realloc (*lineptr, length + 1);
50 memcpy (*lineptr, cs, length);
51 free (cs);
52 }
53
54 return (ssize_t) (*n = strlen (*lineptr));
55}
56#endif
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]) { // disk file was deleted
67 disks[i] = fopen (filename, "w+");
68 if (!disks[i]) {
69 fprintf (stderr, "ERROR: unable to 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 { // disk file wasn't deleted
76 fseek (disks[i], 0, SEEK_END);
77 if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format
78 fprintf (stderr, "WARNING: found bad disk (corrupted): %s\n", filename);
79 bad_disks |= 1 << i;
80 } rewind (disks[i]);
81 }
82 }
83
84 // there were bad disks
85 int bad_disk_count = POPCOUNT (bad_disks);
86 if (bad_disk_count == 1) { // recompute lost disk
87 fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n");
88 int bad_disk = CTZ (bad_disks);
89
90 for (int j = 0; j < DISK_SIZE; ++j) {
91 uint8_t block_bytes[MESSAGE_SIZE + BLOCK_SIZE];
92
93 // read blocks from good disks
94 bool empty = false;
95 for (int k = 0; k < DISK_COUNT; ++k) {
96 if (k == bad_disk) continue;
97 int c = fgetc (disks[k]);
98 if (c == ' ') {
99 fgetc (disks[k]);
100
101 fgetc (disks[k]);
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%02hhX",
110 &block_bytes[k * BLOCK_SIZE + 0],
111 &block_bytes[k * BLOCK_SIZE + 1],
112 &block_bytes[k * BLOCK_SIZE + 2]);
113 } fgetc (disks[k]);
114 }
115
116 if (empty) { // address empty
117 for (int l = 0; l < BLOCK_SIZE; ++l) {
118 fprintf (disks[bad_disk], " ");
119 } fprintf (disks[bad_disk], "\n");
120 continue;
121 }
122
123 // compute lost block
124 block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00;
125 block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00;
126 block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00;
127 for (int k = 0; k < DISK_COUNT; ++k) {
128 if (k == bad_disk) continue;
129 block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0];
130 block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1];
131 block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2];
132 }
133
134 // write lost block
135 fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX\n",
136 block_bytes[bad_disk*BLOCK_SIZE + 0],
137 block_bytes[bad_disk*BLOCK_SIZE + 1],
138 block_bytes[bad_disk*BLOCK_SIZE + 2]);
139 }
140 } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost
141 fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n");
142
143 for (int i = 0; i < DISK_COUNT; ++i) {
144 for (int j = 0; j < DISK_SIZE; ++j)
145 fputs (" \n", disks[i]);
146 }
147 }
148
149 // position head for writing/reading message
150 for (int i = 0; i < DISK_COUNT; ++i)
151 fseek (disks[i], address*LINE_SIZE, SEEK_SET);
152}
153
154static void
155close_disks (void)
156{
157 for (int i = 0; i < DISK_COUNT; ++i)
158 fclose (disks[i]);
159}
160
161int
162main (void)
163{
164 fprintf (stderr, "INFO: initializing RAID 5 XOR on 5 disks with 12 byte messages...\n");
165 // check disk integrity on startup
166 open_disks (0);
167 close_disks ( );
168
169 for (;;) {
170 char *line = NULL;
171 size_t line_size = 0;
172 const char *head;
173 int assigned;
174 int scanned;
175
176 // read whole line
177 printf ("> ");
178 scanned = getline (&line, &line_size, stdin);
179 if (scanned == -1)
180 exit (EXIT_SUCCESS);
181 else if (scanned == 1)
182 continue;
183 head = line;
184
185 uint8_t address; { // scan address
186 assigned = sscanf (head, "%hhx%n", &address, &scanned);
187 if (assigned != 1 || address >= DISK_SIZE) {
188 fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n");
189 continue;
190 } head += scanned;
191 }
192
193 enum command command; { // scan command
194 char command_buffer[6] = {0};
195 assigned = sscanf (head, "%5s %n", command_buffer, &scanned);
196 if (assigned != 1 ||
197 (!strncmp (command_buffer, "read", 4) &&
198 !strncmp (command_buffer, "write", 5))) {
199 fprintf(stderr, "ERROR: command not in set (read; write)\n");
200 continue;
201 } head += scanned;
202 command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE;
203 }
204
205 switch (command) {
206 case COMMAND_READ: {
207 uint8_t message[MESSAGE_SIZE];
208 uint8_t parity [ BLOCK_SIZE];
209
210 // parse out message
211 open_disks (address);
212 int c = fgetc (disks[0]);
213 if (c == ' ') {
214 close_disks ();
215 printf ("EMPTY!\n");
216 break;
217 } ungetc (c, disks[0]);
218
219 switch (address % 5) {
220 case 0:
221 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
222 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
223 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
224 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
225 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
226 break;
227 case 1:
228 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
229 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
230 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
231 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
232 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
233 break;
234 case 2:
235 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
236 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
237 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
238 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
239 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
240 break;
241 case 3:
242 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
243 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
244 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
245 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
246 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
247 break;
248 case 4:
249 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
250 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
251 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
252 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
253 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
254 break;
255 } close_disks ();
256
257 printf ("%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX "
258 "%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX\n",
259 message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5],
260 message[0x6], message[0x7], message[0x8], message[0x9], message[0xA], message[0xB]);
261 } break;
262 case COMMAND_WRITE: {
263 uint8_t message[MESSAGE_SIZE] = {0}; { // scan message
264 int message_size = 0;
265
266 while (*head == ' ' ||
267 *head == '\t') ++head;
268 const char *invalid = NULL;
269 size_t len = 0;
270 for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) {
271 if (!strchr ("0123456789abcdefABCDEF", *c)) {
272 invalid = c;
273 break;
274 } len += 1;
275 } if (invalid) {
276 fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid);
277 break;
278 } if (len != 2*MESSAGE_SIZE) {
279 fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len);
280 break;
281 }
282
283 while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 &&
284 message_size < MESSAGE_SIZE) {
285 head += scanned;
286 message_size += 1;
287 }
288 }
289
290 // compute parity
291 uint8_t parity[BLOCK_SIZE] = {
292 message[0x0] ^ message[0x3] ^ message[0x6] ^ message[0x9],
293 message[0x1] ^ message[0x4] ^ message[0x7] ^ message[0xA],
294 message[0x2] ^ message[0x5] ^ message[0x8] ^ message[0xB],
295 };
296
297 // write blocks
298 open_disks (address); // open disk files, ensuring their integrity
299 switch (address % 5) {
300 case 0:
301 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
302 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
303 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
304 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
305 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
306 break;
307 case 1:
308 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
309 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
310 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
311 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
312 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
313 break;
314 case 2:
315 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
316 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
317 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
318 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
319 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
320 break;
321 case 3:
322 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
323 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
324 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
325 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
326 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
327 break;
328 case 4:
329 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
330 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
331 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
332 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
333 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
334 break;
335 } close_disks ();
336 } break;
337 } free (line);
338 }
339
340 return EXIT_SUCCESS;
341}