aboutsummaryrefslogtreecommitdiff
path: root/src/raid-4-xor-10b-3d.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-10b-3d.c
downloadlab-7fe23e4a8fa6fce19fb3d5e3a8a1f5472496c928.tar.gz
publishHEADmaster
Diffstat (limited to 'src/raid-4-xor-10b-3d.c')
-rw-r--r--src/raid-4-xor-10b-3d.c249
1 files changed, 249 insertions, 0 deletions
diff --git a/src/raid-4-xor-10b-3d.c b/src/raid-4-xor-10b-3d.c
new file mode 100644
index 0000000..d752c10
--- /dev/null
+++ b/src/raid-4-xor-10b-3d.c
@@ -0,0 +1,249 @@
1// gdluck
2// M1M2M3M4M5 M6M7M8M9MA P1P2P3P4P5
3#include <stdlib.h>
4#include <stdio.h>
5#include <stdint.h>
6#include <string.h>
7#include <ctype.h>
8
9#define DISK_COUNT 3
10#define MESSAGE_SIZE 10
11#define BLOCK_SIZE 5
12
13#define DISK_SIZE 0x40
14
15#define POPCOUNT __builtin_popcount
16#define CTZ __builtin_ctz
17
18enum command {
19 COMMAND_WRITE,
20 COMMAND_READ,
21};
22
23FILE *disks[DISK_COUNT];
24
25#ifndef WIN32
26#define LINE_SIZE 11
27#else
28#define LINE_SIZE 12
29static ssize_t
30getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) {
31 register char c;
32 register char *cs = NULL;
33 register int length = 0;
34 while ((c = getc (stream)) != EOF) {
35 cs = (char *) realloc (cs, ++length + 1);
36 if ((*(cs + length - 1) = c) == '\n') {
37 *(cs + length) = '\0';
38 break;
39 }
40 }
41
42 // return the allocated memory if lineptr is null
43 if ((*lineptr) == NULL) {
44 *lineptr = cs;
45 } else {
46 // check if enough memory is allocated
47 if ((length + 1) < *n)
48 *lineptr = (char *) realloc (*lineptr, length + 1);
49 memcpy (*lineptr, cs, length);
50 free (cs);
51 }
52
53 return (ssize_t) (*n = strlen (*lineptr));
54}
55#endif
56
57static void
58open_disks (uint8_t address)
59{
60 uint16_t bad_disks = 0;
61 for (int i = 0; i < DISK_COUNT; ++i) {
62 char filename[] = "disk0";
63 filename[strlen (filename) - 1] += i;
64 disks[i] = fopen (filename, "r+");
65 if (!disks[i]) { // disk file was deleted
66 disks[i] = fopen (filename, "w+");
67 if (!disks[i]) {
68 fprintf (stderr, "error: can't open the file: %s\n", filename);
69 exit (EXIT_FAILURE);
70 }
71
72 fprintf (stderr, "warning: a bad disk has been found (missing): %s\n", filename);
73 bad_disks |= 1 << i;
74 } else { // disk file wasn't deleted
75 fseek (disks[i], 0, SEEK_END);
76 if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format
77 fprintf (stderr, "warning: a bad disk has been found (corrupted): %s\n", filename);
78 bad_disks |= 1 << i;
79 } rewind (disks[i]);
80 }
81 }
82
83 // there were bad disks
84 int bad_disk_count = POPCOUNT (bad_disks);
85 if (bad_disk_count == 1) { // recompute lost disk
86 fprintf (stderr, "information: there is only 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 // read blocks from good disks
93 for (int k = 0; k < DISK_COUNT; ++k) {
94 if (k == bad_disk) continue;
95 fscanf (disks[k], "%02hhX%02hhX%02hhX%02hhX%02hhX\n",
96 &block_bytes[k*BLOCK_SIZE + 0],
97 &block_bytes[k*BLOCK_SIZE + 1],
98 &block_bytes[k*BLOCK_SIZE + 2],
99 &block_bytes[k*BLOCK_SIZE + 3],
100 &block_bytes[k*BLOCK_SIZE + 4]);
101 }
102
103 // compute lost block
104 block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00;
105 block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00;
106 block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00;
107 block_bytes[bad_disk*BLOCK_SIZE + 3] = 0x00;
108 block_bytes[bad_disk*BLOCK_SIZE + 4] = 0x00;
109 for (int k = 0; k < DISK_COUNT; ++k) {
110 if (k == bad_disk) continue;
111 block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0];
112 block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1];
113 block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2];
114 block_bytes[bad_disk*BLOCK_SIZE + 3] ^= block_bytes[k*BLOCK_SIZE + 3];
115 block_bytes[bad_disk*BLOCK_SIZE + 4] ^= block_bytes[k*BLOCK_SIZE + 4];
116 }
117
118 // write lost block
119 fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX%02hhX%02hhX\n",
120 block_bytes[bad_disk*BLOCK_SIZE + 0],
121 block_bytes[bad_disk*BLOCK_SIZE + 1],
122 block_bytes[bad_disk*BLOCK_SIZE + 2],
123 block_bytes[bad_disk*BLOCK_SIZE + 3],
124 block_bytes[bad_disk*BLOCK_SIZE + 4]);
125 }
126 } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost
127 fprintf (stderr, "error: too many damaged 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 ("0000000000\n", disks[i]);
132 }
133 }
134
135 // position head for writing/reading message
136 for (int i = 0; i < DISK_COUNT; ++i)
137 fseek (disks[i], address*LINE_SIZE, SEEK_SET);
138}
139
140static void
141close_disks (void)
142{
143 for (int i = 0; i < DISK_COUNT; ++i)
144 fclose (disks[i]);
145}
146
147int
148main (void)
149{
150 fprintf (stderr, "information: initializing RAID 4 on 3 disks with 10 byte messages...\n");
151 // check disk integrity on startup
152 open_disks (0);
153 close_disks ( );
154
155 for (;;) {
156 char *line = NULL;
157 size_t line_size = 0;
158 const char *head;
159 int assigned;
160 int scanned;
161
162 // read whole line
163 printf ("> ");
164 scanned = getline (&line, &line_size, stdin);
165 if (scanned == -1)
166 exit (EXIT_SUCCESS);
167 else if (scanned == 1)
168 continue;
169 head = line;
170
171 uint8_t address; { // scan address
172 assigned = sscanf (head, "%hhx%n", &address, &scanned);
173 if (assigned != 1 || address >= DISK_SIZE) {
174 fprintf(stderr, "error: the address is not in set [0x00; 0x40)\n");
175 continue;
176 } head += scanned;
177 }
178
179 enum command command; { // scan command
180 char command_buffer[6] = {0};
181 assigned = sscanf (head, "%5s %n", command_buffer, &scanned);
182 if (assigned != 1 ||
183 (!strncmp (command_buffer, "read", 4) &&
184 !strncmp (command_buffer, "write", 5))) {
185 fprintf(stderr, "error: the command is not in set (read; write)\n");
186 continue;
187 } head += scanned;
188 command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE;
189 }
190
191 switch (command) {
192 case COMMAND_READ: {
193 uint8_t message[MESSAGE_SIZE];
194 uint8_t parity [ BLOCK_SIZE];
195
196 // parse out message
197 open_disks (address);
198 fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3], &message[0x4]);
199 fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x5], &message[0x6], &message[0x7], &message[0x8], &message[0x9]);
200 fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3], &parity [0x4]);
201 close_disks ();
202
203 printf ("%02hhX%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX%02hhX\n",
204 message[0x0], message[0x1], message[0x2], message[0x3], message[0x4],
205 message[0x5], message[0x6], message[0x7], message[0x8], message[0x9]);
206 } break;
207 case COMMAND_WRITE: {
208 uint8_t message[MESSAGE_SIZE] = {0}; { // scan message
209 int message_size = 0;
210 while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 &&
211 message_size < MESSAGE_SIZE) {
212 head += scanned;
213 message_size += 1;
214 }
215
216 if (message_size != MESSAGE_SIZE) {
217 fprintf (stderr, "error: the message is too short, expected %d bytes, received %d\n",
218 MESSAGE_SIZE, message_size);
219 continue;
220 }
221
222 while (isspace (*head)) ++head;
223 if (*head) {
224 fprintf (stderr, "error: junk after message: %c...\n", *head);
225 continue;
226 }
227 }
228
229 // compute parity
230 uint8_t parity[BLOCK_SIZE] = {
231 message[0x0] ^ message[0x5],
232 message[0x1] ^ message[0x6],
233 message[0x2] ^ message[0x7],
234 message[0x3] ^ message[0x8],
235 message[0x4] ^ message[0x9],
236 };
237
238 // write blocks
239 open_disks (address); // open disk files, ensuring their integrity
240 fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3], message[0x4]);
241 fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x5], message[0x6], message[0x7], message[0x8], message[0x9]);
242 fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3], parity [0x4]);
243 close_disks ();
244 } break;
245 } free (line);
246 }
247
248 return EXIT_SUCCESS;
249}