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