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