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