From 7fe23e4a8fa6fce19fb3d5e3a8a1f5472496c928 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sat, 7 Nov 2026 10:26:34 +0300 Subject: publish --- src/raid-4-poly-10b-7d.c | 337 ++++++++++++++++++++++++++++++++++++++++ src/raid-4-poly-8b-6d.c | 304 +++++++++++++++++++++++++++++++++++++ src/raid-4-poly-8b-7d.c | 333 ++++++++++++++++++++++++++++++++++++++++ src/raid-4-xor-10b-3d.c | 249 ++++++++++++++++++++++++++++++ src/raid-4-xor-10b-6d.c | 279 ++++++++++++++++++++++++++++++++++ src/raid-4-xor-10b-7d.c | 285 ++++++++++++++++++++++++++++++++++ src/raid-4-xor-12b-4d.c | 289 +++++++++++++++++++++++++++++++++++ src/raid-4-xor-12b-5d.c | 284 ++++++++++++++++++++++++++++++++++ src/raid-4-xor-12b-7d.c | 264 ++++++++++++++++++++++++++++++++ src/raid-4-xor-14b-4d.c | 298 ++++++++++++++++++++++++++++++++++++ src/raid-4-xor-14b-5d.c | 293 +++++++++++++++++++++++++++++++++++ src/raid-4-xor-14b-8d.c | 283 ++++++++++++++++++++++++++++++++++ src/raid-4-xor-8b-8d.c | 286 ++++++++++++++++++++++++++++++++++ src/raid-5-poly-14b-4d.c | 294 +++++++++++++++++++++++++++++++++++ src/raid-5-xor-12b-3d.c | 329 +++++++++++++++++++++++++++++++++++++++ src/raid-5-xor-12b-5d.c | 341 +++++++++++++++++++++++++++++++++++++++++ src/raid-5-xor-12b-6d.c | 380 ++++++++++++++++++++++++++++++++++++++++++++++ src/raid-50-poly-12b-6d.c | 312 +++++++++++++++++++++++++++++++++++++ src/raid-50-poly-8b-6d.c | 305 +++++++++++++++++++++++++++++++++++++ src/raid-50-xor-12b-6d.c | 328 +++++++++++++++++++++++++++++++++++++++ src/raid-50-xor-8b-6d.c | 328 +++++++++++++++++++++++++++++++++++++++ 21 files changed, 6401 insertions(+) create mode 100644 src/raid-4-poly-10b-7d.c create mode 100644 src/raid-4-poly-8b-6d.c create mode 100644 src/raid-4-poly-8b-7d.c create mode 100644 src/raid-4-xor-10b-3d.c create mode 100644 src/raid-4-xor-10b-6d.c create mode 100644 src/raid-4-xor-10b-7d.c create mode 100644 src/raid-4-xor-12b-4d.c create mode 100644 src/raid-4-xor-12b-5d.c create mode 100644 src/raid-4-xor-12b-7d.c create mode 100644 src/raid-4-xor-14b-4d.c create mode 100644 src/raid-4-xor-14b-5d.c create mode 100644 src/raid-4-xor-14b-8d.c create mode 100644 src/raid-4-xor-8b-8d.c create mode 100644 src/raid-5-poly-14b-4d.c create mode 100644 src/raid-5-xor-12b-3d.c create mode 100644 src/raid-5-xor-12b-5d.c create mode 100644 src/raid-5-xor-12b-6d.c create mode 100644 src/raid-50-poly-12b-6d.c create mode 100644 src/raid-50-poly-8b-6d.c create mode 100644 src/raid-50-xor-12b-6d.c create mode 100644 src/raid-50-xor-8b-6d.c (limited to 'src') diff --git a/src/raid-4-poly-10b-7d.c b/src/raid-4-poly-10b-7d.c new file mode 100644 index 0000000..1ca0bf3 --- /dev/null +++ b/src/raid-4-poly-10b-7d.c @@ -0,0 +1,337 @@ +// kira +// M1M2 M3M4 M5M6 M7M8 M9MA 0000 P1P2 +#include +#include +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 7 +#define MESSAGE_SIZE 10 +#define BLOCK_SIZE 2 +#define PADDING_SIZE 2 + +// uint8_t coefficients[DISK_COUNT - 1] = { 1, 3, 5, 7, 11, 13 }; +uint8_t coefficients[DISK_COUNT - 1] = { 1, 1, 1, 1, 1, 1 }; + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_NONE, + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 5 +#else +#define LINE_SIZE 6 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[6]; + snprintf (filename, 6, "disk%d", i); + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR - unable to open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING - found bad disk that is missing: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING - found corrupted disk: %s\n", filename); + bad_disks |= 1 << i; + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + } else rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "NOTICE - only 1 bad disk, recomputing it again \n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + + int c = fgetc (disks[k]); + if (c == ' ') { + empty = true; + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + } else { + ungetc (c, disks[k]); + for (int l = 0; l < BLOCK_SIZE; ++l) { + fscanf (disks[k], "%02hhX", &block_bytes[k*BLOCK_SIZE + l]); + } + } + + fgetc (disks[k]); + } + + if (empty) { + fprintf (disks[bad_disk], " \n"); + continue; + } + + // compute lost block + if (bad_disk == DISK_COUNT - 1) { // lost parity + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] = 0x00; + for (int k = 0; k < DISK_COUNT - 1; ++k) { + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] += + coefficients[k]*block_bytes[k*BLOCK_SIZE + l]; + } + } else { // lost data + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] = block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + l]/coefficients[bad_disk]; + for (int k = 0; k < DISK_COUNT - 1; ++k) { + if (k == bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] -= + coefficients[k]*block_bytes[k*BLOCK_SIZE + l]/coefficients[bad_disk]; + } + } + + // write lost block + for (int l = 0; l < BLOCK_SIZE; ++l) + fprintf (disks[bad_disk], "%02hhX", block_bytes[bad_disk*BLOCK_SIZE + l]); + fprintf (disks[bad_disk], "\n"); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR - more than one bad disks, reinitializing the array with new disks \n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "Initialization RAID 4 with POLY parity on 7 disks. The message size is 10 bytes. \n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + char *line = NULL; + size_t line_size = 0; + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR - address not in set [0x00; 0x40) \n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + while (*head == ' ') head += 1; + if (!strncmp (head, "read", 4)) { + head += 4; + command = COMMAND_READ; + } else if (!strncmp (head, "write", 5)) { + head += 5; + command = COMMAND_WRITE; + } + if (!isspace(*head)) + command = COMMAND_NONE; + } + + switch (command) { + case COMMAND_NONE: { + fprintf(stderr, "ERROR - there is no such command, try entering read or write \n"); + continue; + } break; + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ BLOCK_SIZE]; + uint8_t padding[PADDING_SIZE]; + + while (isspace (*head)) { + head += 1; + if (*head == '\n' || + *head == '\r') { + *head = '\0'; + } + } + if (*head) { + fprintf (stderr, "ERROR - extraneous data after the message: %s", head); + continue; + } + + open_disks (address); + + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("The line is empty \n"); + break; + } ungetc (c, disks[0]); + + // parse out message + fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); + fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); + fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); + fscanf (disks[4], "%02hhX%02hhX\n", &message[0x8], &message[0x9]); + fscanf (disks[5], "%02hhX%02hhX\n", &padding[0x0], &padding[0x1]); + fscanf (disks[6], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); + close_disks (); + + printf ("%02hhX%02hhX " + "%02hhX%02hhX " + "%02hhX%02hhX " + "%02hhX%02hhX " + "%02hhX%02hhX\n", + message[0x0], message[0x1], + message[0x2], message[0x3], + message[0x4], message[0x5], + message[0x6], message[0x7], + message[0x8], message[0x9]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR - message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR - expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + uint8_t padding[PADDING_SIZE] = {0}; + + // compute parity + uint8_t parity[BLOCK_SIZE] = { + coefficients[0]*message[0x0] + + coefficients[1]*message[0x2] + + coefficients[2]*message[0x4] + + coefficients[3]*message[0x6] + + coefficients[4]*message[0x8] + + coefficients[5]*padding[0x0], + + coefficients[0]*message[0x1] + + coefficients[1]*message[0x3] + + coefficients[2]*message[0x5] + + coefficients[3]*message[0x7] + + coefficients[4]*message[0x9] + + coefficients[5]*padding[0x1], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); + fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); + fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); + fprintf (disks[4], "%02hhX%02hhX\n", message[0x8], message[0x9]); + fprintf (disks[5], "%02hhX%02hhX\n", padding[0x0], padding[0x1]); + fprintf (disks[6], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); + close_disks (); + } break; + } + free (line); + line = NULL; + } + + return EXIT_SUCCESS; +} 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 @@ +// jasmin +// M1M2 M3M4 M5M6 M7M8 0000 P1P2 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 6 +#define MESSAGE_SIZE 8 +#define PARITY_SIZE 2 +#define BLOCK_SIZE 2 +#define PADDING_SIZE 2 + +uint8_t coefficients[DISK_COUNT - 1] = { 1, 1, 1, 1, 1 }; + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 5 +#else +#define LINE_SIZE 6 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "The file was not opened:( %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "Missing disk:(: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "Corrupted disk: %s\n", filename); + bad_disks |= 1 << i; + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + } else rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "There is only 1 faulty disk, I'm recalculating it again \n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + + int c = fgetc (disks[k]); + if (c == ' ') { + empty = true; + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX", &block_bytes[k*BLOCK_SIZE + 0], &block_bytes[k*BLOCK_SIZE + 1]); + } fgetc (disks[k]); + } + + if (empty) { + fprintf (disks[bad_disk], " \n"); + continue; + } + + // compute lost block + if (bad_disk == DISK_COUNT - 1) { // lost parity + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + for (int k = 0; k < DISK_COUNT - 1; ++k) { + for (int l = 0; l < PARITY_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] += + coefficients[k]*block_bytes[k*BLOCK_SIZE + l]; + } + } else { // lost data + block_bytes[bad_disk*BLOCK_SIZE + 0] = block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] = block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + 1]; + for (int k = 0; k < DISK_COUNT - 1; ++k) { + if (k == bad_disk) continue; + for (int l = 0; l < PARITY_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] -= + coefficients[k]*block_bytes[k*BLOCK_SIZE + l]; + } + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "Too many damaged disks, reinitialization of the array with new disks...\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "Starting RAID 4 with POLY on 7 disks using 12-byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "The address is not specified [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "Command not specified (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + uint8_t padding[PADDING_SIZE]; + + // parse out message + open_disks (address); + + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("There is no message\n"); + break; + } ungetc (c, disks[0]); + + fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); + fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); + fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); + fscanf (disks[4], "%02hhX%02hhX\n", &padding[0x0], &padding[0x1]); + fscanf (disks[5], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); + close_disks (); + + printf ("%02hhX%02hhX " + "%02hhX%02hhX " + "%02hhX%02hhX " + "%02hhX%02hhX\n", + message[0x0], message[0x1], + message[0x2], message[0x3], + message[0x4], message[0x5], + message[0x6], message[0x7]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "Message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "Expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + // compute parity + uint8_t padding[PADDING_SIZE] = {0}; + + uint8_t parity[PARITY_SIZE] = { + coefficients[0]*message[0x0] + + coefficients[1]*message[0x2] + + coefficients[2]*message[0x4] + + coefficients[3]*message[0x6] + + coefficients[4]*padding[0x0], + + coefficients[0]*message[0x1] + + coefficients[1]*message[0x3] + + coefficients[2]*message[0x5] + + coefficients[3]*message[0x7] + + coefficients[4]*padding[0x1], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); + fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); + fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); + fprintf (disks[4], "%02hhX%02hhX\n", padding[0x0], padding[0x1]); + fprintf (disks[5], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); + close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} 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 @@ +// yana +// M1M2 M3M4 M5M6 M7M8 0000 0000 P1P2 +#include +#include +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 7 +#define MESSAGE_SIZE 8 +#define BLOCK_SIZE 2 +#define PADDING_SIZE 4 + +// uint8_t coefficients[DISK_COUNT - 1] = { 1, 3, 5, 7, 11, 13 }; +uint8_t coefficients[DISK_COUNT - 1] = { 1, 1, 1, 1, 1, 1 }; + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_NONE, + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 5 +#else +#define LINE_SIZE 6 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[6]; + snprintf (filename, 6, "disk%d", i); + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR - unable to open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING - found bad disk that is missing: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING - found corrupted disk: %s\n", filename); + bad_disks |= 1 << i; + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + } else rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "NOTICE - only 1 bad disk, recomputing it again \n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + + int c = fgetc (disks[k]); + if (c == ' ') { + empty = true; + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + } else { + ungetc (c, disks[k]); + for (int l = 0; l < BLOCK_SIZE; ++l) { + fscanf (disks[k], "%02hhX", &block_bytes[k*BLOCK_SIZE + l]); + } + } fgetc (disks[k]); + } + + if (empty) { + fprintf (disks[bad_disk], " \n"); + continue; + } + + // compute lost block + if (bad_disk == DISK_COUNT - 1) { // lost parity + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] = 0x00; + for (int k = 0; k < DISK_COUNT - 1; ++k) { + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] += + coefficients[k]*block_bytes[k*BLOCK_SIZE + l]; + } + } else { // lost data + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] = block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + l]/coefficients[bad_disk]; + for (int k = 0; k < DISK_COUNT - 1; ++k) { + if (k == bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[bad_disk*BLOCK_SIZE + l] -= + coefficients[k]*block_bytes[k*BLOCK_SIZE + l]/coefficients[bad_disk]; + // printf ("%02hhX\n", coefficients[k]*block_bytes[k*BLOCK_SIZE + 0]/coefficients[bad_disk]); + } + } + + // write lost block + for (int l = 0; l < BLOCK_SIZE; ++l) + fprintf (disks[bad_disk], "%02hhX", block_bytes[bad_disk*BLOCK_SIZE + l]); + fprintf (disks[bad_disk], "\n"); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR - more than one bad disks, reinitializing the array with new disks \n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "Initialization RAID 4 on 7 disks. The message size is 8 bytes. \n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + char *line = NULL; + size_t line_size = 0; + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhu%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR - address not in set [0; 64) \n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + while (*head == ' ') head += 1; + if (!strncmp (head, "read", 4)) { + head += 4; + command = COMMAND_READ; + } else if (!strncmp (head, "write", 5)) { + head += 5; + command = COMMAND_WRITE; + } + if (!isspace(*head)) + command = COMMAND_NONE; + } + + switch (command) { + case COMMAND_NONE: { + fprintf(stderr, "ERROR - there is no such command, try entering read or write \n"); + continue; + } break; + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ BLOCK_SIZE]; + uint8_t padding[PADDING_SIZE]; + + while (isspace (*head)) { + head += 1; + if (*head == '\n' || + *head == '\r') { + *head = '\0'; + } + } + if (*head) { + fprintf (stderr, "ERROR - extraneous data after the message: %s", head); + continue; + } + + open_disks (address); + + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("The line is empty \n"); + break; + } ungetc (c, disks[0]); + + // parse out message + fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); + fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); + fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); + fscanf (disks[4], "%02hhX%02hhX\n", &padding[0x0], &padding[0x1]); + fscanf (disks[5], "%02hhX%02hhX\n", &padding[0x2], &padding[0x3]); + fscanf (disks[6], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); + close_disks (); + + printf ("%02hhX%02hhX " + "%02hhX%02hhX " + "%02hhX%02hhX " + "%02hhX%02hhX\n", + message[0x0], message[0x1], + message[0x2], message[0x3], + message[0x4], message[0x5], + message[0x6], message[0x7]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR - message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR - expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + uint8_t padding[PADDING_SIZE] = {0}; + + // compute parity + uint8_t parity[BLOCK_SIZE] = { + coefficients[0]*message[0x0] + + coefficients[1]*message[0x2] + + coefficients[2]*message[0x4] + + coefficients[3]*message[0x6] + + coefficients[4]*padding[0x0] + + coefficients[5]*padding[0x2], + + coefficients[0]*message[0x1] + + coefficients[1]*message[0x3] + + coefficients[2]*message[0x5] + + coefficients[3]*message[0x7] + + coefficients[4]*padding[0x1] + + coefficients[5]*padding[0x3], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); + fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); + fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); + fprintf (disks[4], "%02hhX%02hhX\n", padding[0x0], padding[0x1]); + fprintf (disks[5], "%02hhX%02hhX\n", padding[0x2], padding[0x3]); + fprintf (disks[6], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); + close_disks (); + } break; + } + free (line); + line = NULL; + } + + return EXIT_SUCCESS; +} 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 @@ +// gdluck +// M1M2M3M4M5 M6M7M8M9MA P1P2P3P4P5 +#include +#include +#include +#include +#include + +#define DISK_COUNT 3 +#define MESSAGE_SIZE 10 +#define BLOCK_SIZE 5 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 11 +#else +#define LINE_SIZE 12 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "error: can't open the file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "warning: a bad disk has been found (missing): %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "warning: a bad disk has been found (corrupted): %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "information: there is only 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + fscanf (disks[k], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1], + &block_bytes[k*BLOCK_SIZE + 2], + &block_bytes[k*BLOCK_SIZE + 3], + &block_bytes[k*BLOCK_SIZE + 4]); + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 3] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 4] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2]; + block_bytes[bad_disk*BLOCK_SIZE + 3] ^= block_bytes[k*BLOCK_SIZE + 3]; + block_bytes[bad_disk*BLOCK_SIZE + 4] ^= block_bytes[k*BLOCK_SIZE + 4]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1], + block_bytes[bad_disk*BLOCK_SIZE + 2], + block_bytes[bad_disk*BLOCK_SIZE + 3], + block_bytes[bad_disk*BLOCK_SIZE + 4]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "error: too many damaged disks, reinitializing the array with new disks...\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs ("0000000000\n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "information: initializing RAID 4 on 3 disks with 10 byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "error: the address is not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "error: the command is not in set (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ BLOCK_SIZE]; + + // parse out message + open_disks (address); + fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3], &message[0x4]); + fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x5], &message[0x6], &message[0x7], &message[0x8], &message[0x9]); + fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3], &parity [0x4]); + close_disks (); + + printf ("%02hhX%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX%02hhX\n", + message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], + message[0x5], message[0x6], message[0x7], message[0x8], message[0x9]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + + if (message_size != MESSAGE_SIZE) { + fprintf (stderr, "error: the message is too short, expected %d bytes, received %d\n", + MESSAGE_SIZE, message_size); + continue; + } + + while (isspace (*head)) ++head; + if (*head) { + fprintf (stderr, "error: junk after message: %c...\n", *head); + continue; + } + } + + // compute parity + uint8_t parity[BLOCK_SIZE] = { + message[0x0] ^ message[0x5], + message[0x1] ^ message[0x6], + message[0x2] ^ message[0x7], + message[0x3] ^ message[0x8], + message[0x4] ^ message[0x9], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3], message[0x4]); + fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x5], message[0x6], message[0x7], message[0x8], message[0x9]); + fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3], parity [0x4]); + close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-4-xor-10b-6d.c b/src/raid-4-xor-10b-6d.c new file mode 100644 index 0000000..621625a --- /dev/null +++ b/src/raid-4-xor-10b-6d.c @@ -0,0 +1,279 @@ +// Darya +// M1M2 M3M4 M5M6 M7M8 M9MA P1P2 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 6 +#define MESSAGE_SIZE 10 +#define PARITY_SIZE 2 +#define BLOCK_SIZE 2 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_NONE, + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 5 +#else +#define LINE_SIZE 6 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR: the file could not be opened: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: an inaccessible disk has been detected %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: a corrupted disk has been detected: %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFORMATION: there is only 1 inaccessible disk, a re-calculation is being performed.\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1]); + } fgetc (disks[k]); + } + + if (empty) { // address empty + fprintf (disks[bad_disk], " \n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*2 + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*2 + 1]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: there are too many inaccessible disks, the array is being initialized with new disks.\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFORMATION: initialization of RAID 4 on 6 disks with messages of 10 bytes in length. \n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR: the address is not in the range [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + while (*head == ' ') head += 1; + if (!strncmp (head, "read", 4)) { + head += 4; + command = COMMAND_READ; + } else if (!strncmp (head, "write", 5)) { + head += 5; + command = COMMAND_WRITE; + } + if (!isspace(*head)) + command = COMMAND_NONE; + } + + switch (command) { + case COMMAND_NONE: { + fprintf(stderr, "ERROR: invalid command, must be on of (read, write)\n"); + continue; + } break; + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("EMPTY!\n"); + break; + } ungetc (c, disks[0]); + fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); + fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); + fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); + fscanf (disks[4], "%02hhX%02hhX\n", &message[0x8], &message[0x9]); + fscanf (disks[5], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); + close_disks (); + + printf ("%02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX\n", + message[0x0], message[0x1], + message[0x2], message[0x3], + message[0x4], message[0x5], + message[0x6], message[0x7], + message[0x8], message[0x9]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + // compute parity + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x2] ^ message[0x4] ^ message[0x6] ^ message[0x8], + message[0x1] ^ message[0x3] ^ message[0x5] ^ message[0x7] ^ message[0x9], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); + fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); + fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); + fprintf (disks[4], "%02hhX%02hhX\n", message[0x8], message[0x9]); + fprintf (disks[5], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); + close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-4-xor-10b-7d.c b/src/raid-4-xor-10b-7d.c new file mode 100644 index 0000000..d8edfe5 --- /dev/null +++ b/src/raid-4-xor-10b-7d.c @@ -0,0 +1,285 @@ +// kira +// M1M2 M3M4 M5M6 M7M8 M9MA 0000 P1P2 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 7 +#define MESSAGE_SIZE 10 +#define PARITY_SIZE 2 +#define BLOCK_SIZE 2 +#define PADDING_SIZE 2 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_NONE, + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 5 +#else +#define LINE_SIZE 6 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "error: can't open the file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "warning: a bad disk has been found (missing): %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "warning: a bad disk has been found (corrupted): %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "information: there is only 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1]); + } fgetc (disks[k]); + } + + if (empty) { // address empty + fprintf (disks[bad_disk], " \n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*2 + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*2 + 1]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "error: too many damaged disks, reinitializing the array with new disks...\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "information: initializing RAID 4 on 7 disks with 10 byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "error: the address is not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + while (*head == ' ') head += 1; + if (!strncmp (head, "read", 4)) { + head += 4; + command = COMMAND_READ; + } else if (!strncmp (head, "write", 5)) { + head += 5; + command = COMMAND_WRITE; + } + if (!isspace(*head)) + command = COMMAND_NONE; + } + + switch (command) { + case COMMAND_NONE: { + fprintf(stderr, "Invalid command, must be on of (read, write)\n"); + continue; + } break; + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + uint8_t padding[PADDING_SIZE]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("no data at this address!\n"); + break; + } ungetc (c, disks[0]); + fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); + fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); + fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); + fscanf (disks[4], "%02hhX%02hhX\n", &message[0x8], &message[0x9]); + fscanf (disks[5], "%02hhX%02hhX\n", &padding[0x0], &padding[0x1]); + fscanf (disks[6], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); + close_disks (); + + printf ("%02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX\n", + message[0x0], message[0x1], + message[0x2], message[0x3], + message[0x4], message[0x5], + message[0x6], message[0x7], + message[0x8], message[0x9]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + uint8_t padding[PADDING_SIZE] = {0}; + + // compute parity + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x2] ^ message[0x4] ^ message[0x6] ^ message[0x8] ^ padding[0x0], + message[0x1] ^ message[0x3] ^ message[0x5] ^ message[0x7] ^ message[0x9] ^ padding[0x1], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); + fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); + fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); + fprintf (disks[4], "%02hhX%02hhX\n", message[0x8], message[0x9]); + fprintf (disks[5], "%02hhX%02hhX\n", padding[0x0], padding[0x1]); + fprintf (disks[6], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); + close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-4-xor-12b-4d.c b/src/raid-4-xor-12b-4d.c new file mode 100644 index 0000000..79f98fe --- /dev/null +++ b/src/raid-4-xor-12b-4d.c @@ -0,0 +1,289 @@ +// Dash +// M1M2M3M4 M5M6M7M8 M9MAMBMC P1P2P3P4 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 4 +#define MESSAGE_SIZE 12 +#define PARITY_SIZE 4 +#define BLOCK_SIZE 4 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 9 +#else +#define LINE_SIZE 10 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR! Try another file \n"); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: MISSING DISK\n"); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: CORRUPTED DISK \n"); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFO: detected only one faulty disk; recalculating it\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1], + &block_bytes[k*BLOCK_SIZE + 2], + &block_bytes[k*BLOCK_SIZE + 3]); + } fgetc (disks[k]); + } + + if (empty) { // address empty + fprintf (disks[bad_disk], " \n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 3] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2]; + block_bytes[bad_disk*BLOCK_SIZE + 3] ^= block_bytes[k*BLOCK_SIZE + 3]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1], + block_bytes[bad_disk*BLOCK_SIZE + 2], + block_bytes[bad_disk*BLOCK_SIZE + 3]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: Exceeding the maximum number of faulty disks.\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFO: Setting up RAID 4 with 4 disks, each handling 12-byte messages \n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR: outside the range [0x00; 0x40) \n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "ERROR: The provided command is not valid. Please use either 'read' or 'write\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("No data!\n"); + break; + } ungetc (c, disks[0]); + fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3]); + fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX\n", &message[0x4], &message[0x5], &message[0x6], &message[0x7]); + fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX\n", &message[0x8], &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[3], "%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3]); + close_disks (); + + printf ("%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX\n", + message[0x0], message[0x1], message[0x2], message[0x3], + message[0x4], message[0x5], message[0x6], message[0x7], + message[0x8], message[0x9], message[0xA], message[0xB]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character '%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex-digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + // compute parity + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x4] ^ message[0x8], + message[0x1] ^ message[0x5] ^ message[0x9], + message[0x2] ^ message[0x6] ^ message[0xA], + message[0x3] ^ message[0x7] ^ message[0xB], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3]); + fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX\n", message[0x4], message[0x5], message[0x6], message[0x7]); + fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX\n", message[0x8], message[0x9], message[0xA], message[0xB]); + fprintf (disks[3], "%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3]); + close_disks (); + } break; + } + free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-4-xor-12b-5d.c b/src/raid-4-xor-12b-5d.c new file mode 100644 index 0000000..e2716e0 --- /dev/null +++ b/src/raid-4-xor-12b-5d.c @@ -0,0 +1,284 @@ +// Dash +// M1M2M3 M4M5M6 M7M8M9 MAMBMC P1P2P3 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 5 +#define MESSAGE_SIZE 12 +#define PARITY_SIZE 3 +#define BLOCK_SIZE 3 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 7 +#else +#define LINE_SIZE 8 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR: unable to open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: found bad disk (missing): %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: found bad disk (corrupted): %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1], + &block_bytes[k*BLOCK_SIZE + 2]); + } fgetc (disks[k]); + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2]; + } + + if (empty) { // address empty + fprintf (disks[bad_disk], " \n"); + continue; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1], + block_bytes[bad_disk*BLOCK_SIZE + 2]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFO: initializing RAID 4 on 4 disks with 12 byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "ERROR: command not in set (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("Empty!\n"); + break; + } ungetc (c, disks[0]); + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + close_disks (); + + printf ("%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX %02hhX%02hhX%02hhX %02hhX%02hhX%02hhX\n", + message[0x0], message[0x1], message[0x2], + message[0x3], message[0x4], message[0x5], + message[0x6], message[0x7], message[0x8], + message[0x9], message[0xA], message[0xB]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + // compute parity + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x3] ^ message[0x6] ^ message[0x9], + message[0x1] ^ message[0x4] ^ message[0x7] ^ message[0xA], + message[0x2] ^ message[0x5] ^ message[0x8] ^ message[0xB], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + close_disks (); + } break; + } + free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-4-xor-12b-7d.c b/src/raid-4-xor-12b-7d.c new file mode 100644 index 0000000..6f2a852 --- /dev/null +++ b/src/raid-4-xor-12b-7d.c @@ -0,0 +1,264 @@ +// Dash +// M1M2 M3M4 M5M6 M7M8 M9MA MBMC P1P2 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 7 +#define MESSAGE_SIZE 12 +#define PARITY_SIZE 2 +#define BLOCK_SIZE 2 +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +#ifndef WIN32 +#define LINE_SIZE 5 +#else +#define LINE_SIZE 6 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "Error: can't open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "Warning: found bad disk (missing): %s\n", filename); + bad_disks |= 1 << i; + } else { + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { + fprintf (stderr, "Warning: found bad disk (corrupted): %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { + fprintf (stderr, "there is just 1 bad disk, recomputing it\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1]); + } fgetc (disks[k]); + } + + if (empty) { + fprintf (disks[bad_disk], " \n"); + continue; + } + + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*2 + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*2 + 1]; + } + + fprintf (disks[bad_disk], "%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1]); + } + } else if (bad_disk_count > 1) { + fprintf (stderr, "Error: too many bad disks, reinitializing the array with new disks\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "initializing RAID 4 on 7 disks with 12 byte messages\n"); + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + + const char *head; + int assigned; + int scanned; + + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "Error: address not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "Error: command not in set (use commands write, read)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("There is no data\n"); + break; + } ungetc (c, disks[0]); + fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); + fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); + fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); + fscanf (disks[4], "%02hhX%02hhX\n", &message[0x8], &message[0x9]); + fscanf (disks[5], "%02hhX%02hhX\n", &message[0xA], &message[0xB]); + fscanf (disks[6], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); + close_disks (); + + printf ("%02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX\n", + message[0x0], message[0x1], + message[0x2], message[0x3], + message[0x4], message[0x5], + message[0x6], message[0x7], + message[0x8], message[0x9], + message[0xA], message[0xB]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x2] ^ message[0x4] ^ message[0x6] ^ message[0x8] ^ message[0xA], + message[0x1] ^ message[0x3] ^ message[0x5] ^ message[0x7] ^ message[0x9] ^ message[0xB], + }; + + open_disks (address); + fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); + fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); + fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); + fprintf (disks[4], "%02hhX%02hhX\n", message[0x8], message[0x9]); + fprintf (disks[5], "%02hhX%02hhX\n", message[0xA], message[0xB]); + fprintf (disks[6], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); + close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-4-xor-14b-4d.c b/src/raid-4-xor-14b-4d.c new file mode 100644 index 0000000..f2f098c --- /dev/null +++ b/src/raid-4-xor-14b-4d.c @@ -0,0 +1,298 @@ +// Dash +// M1M2M3M4M5 M6M7M8M9MA MBMCMDME00 P1P2P3P4P5 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 4 +#define MESSAGE_SIZE 14 +#define PARITY_SIZE 5 +#define BLOCK_SIZE 5 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 11 +#else +#define LINE_SIZE 12 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "mistake: the file could not be opened: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "warning: a faulty disk has been found or the disk is missing: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "warning: a faulty/damaged disk has been detected: %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "message: only one faulty disk has been detected, I am detecting it\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX%02hhX%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1], + &block_bytes[k*BLOCK_SIZE + 2], + &block_bytes[k*BLOCK_SIZE + 3], + &block_bytes[k*BLOCK_SIZE + 4]); + } fgetc (disks[k]); + } + + if (empty) { // address empty + fprintf (disks[bad_disk], " \n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 3] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 4] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2]; + block_bytes[bad_disk*BLOCK_SIZE + 3] ^= block_bytes[k*BLOCK_SIZE + 3]; + block_bytes[bad_disk*BLOCK_SIZE + 4] ^= block_bytes[k*BLOCK_SIZE + 4]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1], + block_bytes[bad_disk*BLOCK_SIZE + 2], + block_bytes[bad_disk*BLOCK_SIZE + 3], + block_bytes[bad_disk*BLOCK_SIZE + 4]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "mistake: there are too many bad disks, we are re-initializing arrays with new disks\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "message: Initializing RAID 4 with XOR and 14 byte messages on 4 disks\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + char *line = NULL; + size_t line_size = 0; + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhX%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "mistake: the address must belong to [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "mistake: only the read and write commands are available\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + uint8_t reserved[1]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("No data!\n"); + break; + } ungetc (c, disks[0]); + fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3], &message [0x4]); + fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x5], &message[0x6], &message[0x7], &message[0x8], &message [0x9]); + fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0xA], &message[0xB], &message[0xC], &message[0xD], &reserved[0x0]); + fscanf (disks[3], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3], &parity [0x4]); + close_disks (); + + printf ("%02hhX%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX\n", + message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], + message[0x5], message[0x6], message[0x7], message[0x8], message[0x9], + message[0xA], message[0xB], message[0xC], message[0xD]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "mistake: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "mistake: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhX%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + uint8_t reserved[1] = {0}; + + // compute parity + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x5] ^ message [0xA], + message[0x1] ^ message[0x6] ^ message [0xB], + message[0x2] ^ message[0x7] ^ message [0xC], + message[0x3] ^ message[0x8] ^ message [0xD], + message[0x4] ^ message[0x9] ^ reserved[0x0], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3], message [0x4]); + fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x5], message[0x6], message[0x7], message[0x8], message [0x9]); + fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0xA], message[0xB], message[0xC], message[0xD], reserved[0x0]); + fprintf (disks[3], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3], parity [0x4]); + close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-4-xor-14b-5d.c b/src/raid-4-xor-14b-5d.c new file mode 100644 index 0000000..51b8b4a --- /dev/null +++ b/src/raid-4-xor-14b-5d.c @@ -0,0 +1,293 @@ +// Dash +// M1M2M3M4 M5M6M7M8 M9MAMBMC MDME0000 P1P2P3P4 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 5 +#define MESSAGE_SIZE 14 +#define PARITY_SIZE 4 +#define BLOCK_SIZE 4 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 9 +#else +#define LINE_SIZE 10 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR: The program could not open the file you are trying to use: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: the system has detected that one of the disks in the array is missing: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: the system has detected that one of the disks in the array is corrupted: %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1], + &block_bytes[k*BLOCK_SIZE + 2], + &block_bytes[k*BLOCK_SIZE + 3]); + } + } + + if (empty) { // address empty + fprintf (disks[bad_disk], " \n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 3] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2]; + block_bytes[bad_disk*BLOCK_SIZE + 3] ^= block_bytes[k*BLOCK_SIZE + 3]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1], + block_bytes[bad_disk*BLOCK_SIZE + 2], + block_bytes[bad_disk*BLOCK_SIZE + 3]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: Too many drives in the array have failed and the system is trying to rebuild the array with new drives\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFO: initializing RAID 4 on 5 disks with 14 byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "ERROR: command not in set (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + uint8_t reserved[2]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("No data!\n"); + break; + } ungetc (c, disks[0]); + fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message [0x2], &message [0x3]); + fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX\n", &message[0x4], &message[0x5], &message [0x6], &message [0x7]); + fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX\n", &message[0x8], &message[0x9], &message [0xA], &message [0xB]); + fscanf (disks[3], "%02hhX%02hhX%02hhX%02hhX\n", &message[0xC], &message[0xD], &reserved[0x0], &reserved[0x1]); + fscanf (disks[4], "%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3]); + close_disks (); + + printf ("%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX %02hhX%02hhX\n", + message[0x0], message[0x1], message[0x2], message[0x3], + message[0x4], message[0x5], message[0x6], message[0x7], + message[0x8], message[0x9], message[0xA], message[0xB], + message[0xC], message[0xD]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + uint8_t reserved[2] = {0}; + + // compute parity + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x4] ^ message[0x8] ^ message [0xC], + message[0x1] ^ message[0x5] ^ message[0x9] ^ message [0xD], + message[0x2] ^ message[0x6] ^ message[0xA] ^ reserved[0x0], + message[0x3] ^ message[0x7] ^ message[0xB] ^ reserved[0x1], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message [0x2], message [0x3]); + fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX\n", message[0x4], message[0x5], message [0x6], message [0x7]); + fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX\n", message[0x8], message[0x9], message [0xA], message [0xB]); + fprintf (disks[3], "%02hhX%02hhX%02hhX%02hhX\n", message[0xC], message[0xD], reserved[0x0], reserved[0x1]); + fprintf (disks[4], "%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3]); + close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} 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 @@ +// Dash +// M1M2 M3M4 M5M6 M7M8 M9MA MBMC MDME P1P2 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 8 +#define MESSAGE_SIZE 14 +#define PARITY_SIZE 2 +#define BLOCK_SIZE 2 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 5 +#else +#define LINE_SIZE 6 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "file opening error: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "missing disk is detected: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "damaged disk is detected: %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "only 1 bad disk detected, recalculate it...\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1]); + } fgetc (disks[k]); + } + + if (empty) { // address empty + fprintf (disks[bad_disk], " \n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "many unsuitable disks are detected, the array is reinitialized with new disks...\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "RAID 4 initialization on 5 disks with 14-byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "invalid address, use: [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "command does not exist: expected (read/write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("Empty!\n"); + break; + } ungetc (c, disks[0]); + fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); + fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); + fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); + fscanf (disks[4], "%02hhX%02hhX\n", &message[0x8], &message[0x9]); + fscanf (disks[5], "%02hhX%02hhX\n", &message[0xA], &message[0xB]); + fscanf (disks[6], "%02hhX%02hhX\n", &message[0xC], &message[0xD]); + fscanf (disks[7], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); + close_disks (); + + printf ("%02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX\n", + message[0x0], message[0x1], + message[0x2], message[0x3], + message[0x4], message[0x5], + message[0x6], message[0x7], + message[0x8], message[0x9], + message[0xA], message[0xB], + message[0xC], message[0xD]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + // compute parity + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x2] ^ message[0x4] ^ message[0x6] ^ message[0x8] ^ message[0xA] ^ message[0xC], + message[0x1] ^ message[0x3] ^ message[0x5] ^ message[0x7] ^ message[0x9] ^ message[0xB] ^ message[0xD], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); + fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); + fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); + fprintf (disks[4], "%02hhX%02hhX\n", message[0x8], message[0x9]); + fprintf (disks[5], "%02hhX%02hhX\n", message[0xA], message[0xB]); + fprintf (disks[6], "%02hhX%02hhX\n", message[0xC], message[0xD]); + fprintf (disks[7], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); + close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-4-xor-8b-8d.c b/src/raid-4-xor-8b-8d.c new file mode 100644 index 0000000..898fb5d --- /dev/null +++ b/src/raid-4-xor-8b-8d.c @@ -0,0 +1,286 @@ +// nguyen +// M1M2 M3M4 M5M6 M7M8 0000 0000 0000 P1P2 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 8 +#define MESSAGE_SIZE 8 +#define BLOCK_SIZE 2 +#define PADDING_SIZE 6 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 5 +#else +#define LINE_SIZE 6 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "file opening error: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "missing disk is detected: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "damaged disk is detected: %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "only 1 bad disk detected, recalculate it...\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1]); + } fgetc (disks[k]); + } + + if (empty) { // address empty + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], " "); + } fprintf (disks[bad_disk], "\n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "many unsuitable disks are detected, the array is reinitialized with new disks...\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "RAID 4 XOR initialization on 8 disks with 8-byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "invalid address, use: [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "command does not exist: expected (read/write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ BLOCK_SIZE]; + uint8_t padding[PADDING_SIZE]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("EMPTY!\n"); + break; + } ungetc (c, disks[0]); + + fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); + fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); + fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); + fscanf (disks[4], "%02hhX%02hhX\n", &padding[0x0], &padding[0x1]); + fscanf (disks[5], "%02hhX%02hhX\n", &padding[0x2], &padding[0x3]); + fscanf (disks[6], "%02hhX%02hhX\n", &padding[0x4], &padding[0x5]); + fscanf (disks[7], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); + close_disks (); + + printf ("%02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX\n", + message[0x0], message[0x1], + message[0x2], message[0x3], + message[0x4], message[0x5], + message[0x6], message[0x7]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + uint8_t padding[PADDING_SIZE] = {0}; + + // compute parity + uint8_t parity[BLOCK_SIZE] = { + message[0x0] ^ message[0x2] ^ message[0x4] ^ message[0x6] ^ padding[0x0] ^ padding[0x2] ^ padding[0x4], + message[0x1] ^ message[0x3] ^ message[0x5] ^ message[0x7] ^ padding[0x1] ^ padding[0x3] ^ padding[0x5], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); + fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); + fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); + fprintf (disks[4], "%02hhX%02hhX\n", padding[0x0], padding[0x1]); + fprintf (disks[5], "%02hhX%02hhX\n", padding[0x2], padding[0x3]); + fprintf (disks[6], "%02hhX%02hhX\n", padding[0x4], padding[0x5]); + fprintf (disks[7], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); + close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-5-poly-14b-4d.c b/src/raid-5-poly-14b-4d.c new file mode 100644 index 0000000..f272dea --- /dev/null +++ b/src/raid-5-poly-14b-4d.c @@ -0,0 +1,294 @@ +// g, slock +// M1M2M3M4M5 M6M7M8M9MA MBMCMDME00 P1P2P3P4P5 +// M1M2M3M4M5 M6M7M8M9MA P1P2P3P4P5 MBMCMDME00 +// M1M2M3M4M5 P1P2P3P4P5 M6M7M8M9MA MBMCMDME00 +// P1P2P3P4P5 M1M2M3M4M5 M6M7M8M9MA MBMCMDME00 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 4 +#define MESSAGE_SIZE 14 +#define PADDING_SIZE 1 +#define BLOCK_SIZE 5 + +uint8_t coefficients[DISK_COUNT - 1] = { 1, 1, 1, }; + +int shift_table[][DISK_COUNT] = { + { 0, 1, 2, 3, }, + { 0, 1, 3, 2, }, + { 0, 2, 3, 1, }, + { 1, 2, 3, 0, }, +}; + +#define DISK_SIZE 0x40 +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +#ifndef WIN32 +#define LINE_SIZE (2*BLOCK_SIZE + 1) +#else +#define LINE_SIZE (2*BLOCK_SIZE + 2) +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +static void +recompute_bad_disk (int bad_disk) +{ + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[MESSAGE_SIZE + PADDING_SIZE + BLOCK_SIZE]; + int shift = j % DISK_COUNT; + int logical_bad_disk; + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (shift_table[shift][k] == bad_disk) { + logical_bad_disk = k; + continue; + } + for (int l = 0; l < BLOCK_SIZE; ++l) { + int c = fgetc (disks[shift_table[shift][k]]); + if (c == ' ') { + fgetc (disks[shift_table[shift][k]]); + empty = true; + } else { + ungetc (c, disks[shift_table[shift][k]]); + fscanf (disks[shift_table[shift][k]], "%02hhX", &block_bytes[(k*BLOCK_SIZE + l)]); + } + } fgetc (disks[shift_table[shift][k]]); + } + + if (empty) { // address empty + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], " "); + } fprintf (disks[bad_disk], "\n"); + continue; + } else if (logical_bad_disk % DISK_COUNT == DISK_COUNT - 1) { // lost parity + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l)] = 0x00; + for (int k = 0, c = 0; k < DISK_COUNT; ++k) { + if (k == logical_bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l)] += + coefficients[c]*block_bytes[(k*BLOCK_SIZE + l)]; + c += 1; + } + } else { // lost data + for (int l = 0; l < BLOCK_SIZE; ++l) { + block_bytes[(logical_bad_disk*BLOCK_SIZE + l)] = + block_bytes[((DISK_COUNT - 1)*BLOCK_SIZE + l)]; + } + for (int k = 0, c = 0; k < DISK_COUNT - 1; ++k) { + if (k == logical_bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l)] -= + coefficients[c]*block_bytes[(k*BLOCK_SIZE + l)]; + c += 1; + } + } + + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], "%02hhX", block_bytes[(logical_bad_disk*BLOCK_SIZE + l)]); + } fprintf (disks[bad_disk], "\n"); + } +} + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "Ошибка! Невозможно открыть файл: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "Предупреждение! Найден плохой диск: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "Предупреждение!Найден плохой диск: %s\n", filename); + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + bad_disks |= 1 << i; + } else rewind (disks[i]); + } + } + + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { + fprintf (stderr, "Повторный расчет, один диск плохой...\n"); + int bad_disk = CTZ (bad_disks); + recompute_bad_disk (bad_disk); + } else if (bad_disk_count) { + fprintf (stderr, "Ошибка! Повторная инициализация массива с новыми дисками.\n"); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) { + for (int k = 0; k < BLOCK_SIZE; ++k) { + fputs (" ", disks[i]); + } fputs ("\n", disks[i]); + } + } + } + + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "Инициализация RAID 5 с POLY на 4 дисках с 14 байтовым сообщением.\n"); + + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { + assigned = sscanf (head, "%hhu%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "Ошибка! Адрес не входит в границы! [0; 64)\n"); + continue; + } head += scanned; + } + + enum command command; { + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "Ошибка! Не корректная команда! (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t block_bytes[MESSAGE_SIZE + PADDING_SIZE + BLOCK_SIZE]; + int shift = address % DISK_COUNT; + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("Пусто\n"); + break; + } else ungetc (c, disks[0]); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) { + fscanf (disks[shift_table[shift][i]], "%02hhX", &block_bytes[BLOCK_SIZE*i + j]); + } + } close_disks (); + + for (int i = 0; i < DISK_COUNT - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) { + if (i == DISK_COUNT - 2 && j + PADDING_SIZE >= BLOCK_SIZE) + continue; + printf ("%02hhX", block_bytes[BLOCK_SIZE*i + j]); + } + printf (" "); + } printf ("\n"); + } break; + case COMMAND_WRITE: { + uint8_t block_bytes[MESSAGE_SIZE + PADDING_SIZE + BLOCK_SIZE] = {0}; { // scan message + uint8_t *write_head = block_bytes; + int message_size = 0; + while (sscanf (head, "%2hhx%n", write_head, &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + write_head += 1; + if (message_size % ((DISK_COUNT - 1)*BLOCK_SIZE) == 0) + write_head += BLOCK_SIZE; + } + + if (message_size != MESSAGE_SIZE) { + fprintf (stderr, "Ошибка! Сообщение слишком большое, ожидалось %d байт, восстановленно %d\n", + MESSAGE_SIZE, message_size); + continue; + } + + while (isspace(*head)) ++head; + if (*head) { + fprintf (stderr, "Ошибка! Выводится мусор после сообщения: %c...\n", *head); + continue; + } + } + + for (int i = 0; i < DISK_COUNT - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + j] += coefficients[i]*block_bytes[i*BLOCK_SIZE + j]; + } + + int shift = address % DISK_COUNT; + open_disks (address); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + fprintf (disks[shift_table[shift][i]], "%02hhX", block_bytes[i*BLOCK_SIZE + j]); + fprintf (disks[shift_table[shift][i]], "\n"); + } close_disks (); + } break; + } free (line); + } + return EXIT_SUCCESS; +} diff --git a/src/raid-5-xor-12b-3d.c b/src/raid-5-xor-12b-3d.c new file mode 100644 index 0000000..c52c7bc --- /dev/null +++ b/src/raid-5-xor-12b-3d.c @@ -0,0 +1,329 @@ +// North +// M1M2M3M4M5M6 M7M8M9MAMBMC P1P2P3P4P5P6 +// M1M2M3M4M5M6 P1P2P3P4P5P6 M7M8M9MAMBMC +// P1P2P3P4P5P6 M1M2M3M4M5M6 M7M8M9MAMBMC +// M1M2M3M4M5M6 M7M8M9MAMBMC P1P2P3P4P5P6 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 3 +#define MESSAGE_SIZE 12 +#define PARITY_SIZE 6 +#define BLOCK_SIZE 6 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 13 +#else +#define LINE_SIZE 14 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR: unable to open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: found bad disk (missing): %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: found bad disk (corrupted): %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[MESSAGE_SIZE + PARITY_SIZE]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc (c, disks[k]); + fscanf (disks[k], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX", + &block_bytes[k*BLOCK_SIZE + 0], + &block_bytes[k*BLOCK_SIZE + 1], + &block_bytes[k*BLOCK_SIZE + 2], + &block_bytes[k*BLOCK_SIZE + 3], + &block_bytes[k*BLOCK_SIZE + 4], + &block_bytes[k*BLOCK_SIZE + 5]); + } fgetc (disks[k]); + } + + if (empty) { // address empty + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], " "); + } fprintf (disks[bad_disk], "\n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 3] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 4] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 5] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2]; + block_bytes[bad_disk*BLOCK_SIZE + 3] ^= block_bytes[k*BLOCK_SIZE + 3]; + block_bytes[bad_disk*BLOCK_SIZE + 4] ^= block_bytes[k*BLOCK_SIZE + 4]; + block_bytes[bad_disk*BLOCK_SIZE + 5] ^= block_bytes[k*BLOCK_SIZE + 5]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1], + block_bytes[bad_disk*BLOCK_SIZE + 2], + block_bytes[bad_disk*BLOCK_SIZE + 3], + block_bytes[bad_disk*BLOCK_SIZE + 4], + block_bytes[bad_disk*BLOCK_SIZE + 5]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFO: initializing RAID 5 on 3 disks with 12 byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "ERROR: command not in set (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ PARITY_SIZE]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("No data stored!\n"); + break; + } ungetc (c, disks[0]); + + switch (address % 3) { + case 0: + fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8], &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3], &parity [0x4], &parity [0x5]); + break; + case 1: + fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3], &parity [0x4], &parity [0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8], &message[0x9], &message[0xA], &message[0xB]); + break; + case 2: + fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3], &parity [0x4], &parity [0x5]); + fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8], &message[0x9], &message[0xA], &message[0xB]); + break; + } close_disks (); + + printf ("%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX " + "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", + message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5], + message[0x6], message[0x7], message[0x8], message[0x9], message[0xA], message[0xB]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + // compute parity + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x6], + message[0x1] ^ message[0x7], + message[0x2] ^ message[0x8], + message[0x3] ^ message[0x9], + message[0x4] ^ message[0xA], + message[0x5] ^ message[0xB], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + switch (address % 3) { + case 0: + fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5]); + fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8], message[0x9], message[0xA], message[0xB]); + fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3], parity [0x4], parity [0x5]); + break; + case 1: + fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5]); + fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3], parity [0x4], parity [0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8], message[0x9], message[0xA], message[0xB]); + break; + case 2: + fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3], parity [0x4], parity [0x5]); + fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8], message[0x9], message[0xA], message[0xB]); + break; + } close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} 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 @@ +// Жанг +// M1M2M3 M4M5M6 M7M8M9 MAMBMC P1P2P3 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 5 +#define MESSAGE_SIZE 12 +#define BLOCK_SIZE 3 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE 7 +#else +#define LINE_SIZE 8 +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR: unable to open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: found bad disk (missing): %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: found bad disk (corrupted): %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[MESSAGE_SIZE + BLOCK_SIZE]; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc(c, disks[k]); + fscanf(disks[k], "%02hhX%02hhX%02hhX", + &block_bytes[k * BLOCK_SIZE + 0], + &block_bytes[k * BLOCK_SIZE + 1], + &block_bytes[k * BLOCK_SIZE + 2]); + } fgetc (disks[k]); + } + + if (empty) { // address empty + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], " "); + } fprintf (disks[bad_disk], "\n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1], + block_bytes[bad_disk*BLOCK_SIZE + 2]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFO: initializing RAID 5 XOR on 5 disks with 12 byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "ERROR: command not in set (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t parity [ BLOCK_SIZE]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("EMPTY!\n"); + break; + } ungetc (c, disks[0]); + + switch (address % 5) { + case 0: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + break; + case 1: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + break; + case 2: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + break; + case 3: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + break; + case 4: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + break; + } close_disks (); + + printf ("%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX " + "%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX\n", + message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5], + message[0x6], message[0x7], message[0x8], message[0x9], message[0xA], message[0xB]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + // compute parity + uint8_t parity[BLOCK_SIZE] = { + message[0x0] ^ message[0x3] ^ message[0x6] ^ message[0x9], + message[0x1] ^ message[0x4] ^ message[0x7] ^ message[0xA], + message[0x2] ^ message[0x5] ^ message[0x8] ^ message[0xB], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + switch (address % 5) { + case 0: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + break; + case 1: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + break; + case 2: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + break; + case 3: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + break; + case 4: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + break; + } close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-5-xor-12b-6d.c b/src/raid-5-xor-12b-6d.c new file mode 100644 index 0000000..d10721d --- /dev/null +++ b/src/raid-5-xor-12b-6d.c @@ -0,0 +1,380 @@ +// M1M2M3 M4M5M6 M7M8M9 MAMBMC 000000 P1P2P3 +// M1M2M3 M4M5M6 M7M8M9 MAMBMC P1P2P3 000000 +// M1M2M3 M4M5M6 M7M8M9 P1P2P3 MAMBMC 000000 +// M1M2M3 M4M5M6 P1P2P3 M7M8M9 MAMBMC 000000 +// M1M2M3 P1P2P3 M4M5M6 M7M8M9 MAMBMC 000000 +// P1P2P3 M1M2M3 M4M5M6 M7M8M9 MAMBMC 000000 +// M1M2M3 M4M5M6 M7M8M9 MAMBMC 000000 P1P2P3 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 6 +#define MESSAGE_SIZE 12 +#define BLOCK_SIZE 3 +#define PADDING_SIZE 3 +#define PARITY_SIZE 3 + +#define DISK_SIZE 0x40 + +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +#ifndef WIN32 +#define LINE_SIZE (2*BLOCK_SIZE + 1) +#else +#define LINE_SIZE (2*BLOCK_SIZE + 2) +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR: unable to open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: found bad disk (missing): %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: found bad disk (corrupted): %s\n", filename); + bad_disks |= 1 << i; + } rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[MESSAGE_SIZE + PADDING_SIZE + PARITY_SIZE] = {0}; + + // read blocks from good disks + bool empty = false; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + int c = fgetc (disks[k]); + if (c == ' ') { + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + + fgetc (disks[k]); + fgetc (disks[k]); + empty = true; + } else { + ungetc(c, disks[k]); + fscanf(disks[k], "%02hhX%02hhX%02hhX", + &block_bytes[k * BLOCK_SIZE + 0], + &block_bytes[k * BLOCK_SIZE + 1], + &block_bytes[k * BLOCK_SIZE + 2]); + } fgetc (disks[k]); + } + + if (empty) { // address empty + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], " "); + } fprintf (disks[bad_disk], "\n"); + continue; + } + + // compute lost block + block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; + block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00; + for (int k = 0; k < DISK_COUNT; ++k) { + if (k == bad_disk) continue; + block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; + block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; + block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2]; + } + + // write lost block + fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX\n", + block_bytes[bad_disk*BLOCK_SIZE + 0], + block_bytes[bad_disk*BLOCK_SIZE + 1], + block_bytes[bad_disk*BLOCK_SIZE + 2]); + } + } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n"); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) + fputs (" \n", disks[i]); + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFO: initializing RAID 5 XOR on 5 disks with 12 byte messages...\n"); + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "ERROR: command not in set (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t message[MESSAGE_SIZE]; + uint8_t padding[PADDING_SIZE]; + uint8_t parity [ PARITY_SIZE]; + + // parse out message + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("EMPTY!\n"); + break; + } ungetc (c, disks[0]); + + switch (address % DISK_COUNT) { + case 0: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]); + fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + break; + case 1: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]); + break; + case 2: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + break; + case 3: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]); + break; + case 4: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]); + break; + case 5: + fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]); + fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]); + fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]); + fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]); + fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]); + fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]); + break; + } close_disks (); + + printf ("%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX " + "%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX\n", + message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5], + message[0x6], message[0x7], message[0x8], message[0x9], message[0xA], message[0xB]); + } break; + case COMMAND_WRITE: { + uint8_t message[MESSAGE_SIZE] = {0}; { // scan message + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + } + } + + // compute parity + uint8_t padding[PADDING_SIZE] = { + 0x00, + 0x00, + 0x00, + }; + uint8_t parity[PARITY_SIZE] = { + message[0x0] ^ message[0x3] ^ message[0x6] ^ message[0x9], + message[0x1] ^ message[0x4] ^ message[0x7] ^ message[0xA], + message[0x2] ^ message[0x5] ^ message[0x8] ^ message[0xB], + }; + + // write blocks + open_disks (address); // open disk files, ensuring their integrity + switch (address % DISK_COUNT) { + case 0: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]); + fprintf (disks[5], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + break; + case 1: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]); + break; + case 2: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]); + break; + case 3: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]); + break; + case 4: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]); + break; + case 5: + fprintf (disks[0], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]); + fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]); + fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]); + fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]); + fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]); + fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]); + break; + } close_disks (); + } break; + } free (line); + } + + return EXIT_SUCCESS; +} diff --git a/src/raid-50-poly-12b-6d.c b/src/raid-50-poly-12b-6d.c new file mode 100644 index 0000000..3550688 --- /dev/null +++ b/src/raid-50-poly-12b-6d.c @@ -0,0 +1,312 @@ +// Aksel +// M1M2M3 M4M5M6 P1P2P3 M7M8M9 MAMBMC P4P5P6 +// M1M2M3 P1P2P3 M4M5M6 M7M8M9 P4P5P6 MAMBMC +// P1P2P3 M1M2M3 M4M5M6 P4P5P6 M7M8M9 MAMBMC +// M1M2M3 M4M5M6 P1P2P3 M7M8M9 MAMBMC P4P5P6 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 6 +#define MESSAGE_SIZE 12 +#define BLOCK_SIZE 3 + +uint8_t coefficients[DISK_COUNT - 2] = { 1, 1, 1, 1, }; + +// TODO(cmmm): generate +int shift_table[][DISK_COUNT] = { + { 0, 1, 2, 3, 4, 5 }, + { 0, 2, 1, 3, 5, 4 }, + { 1, 2, 0, 4, 5, 3 }, +}; + +#define DISK_SIZE 0x40 +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +#ifndef WIN32 +#define LINE_SIZE (2*BLOCK_SIZE + 1) +#else +#define LINE_SIZE (2*BLOCK_SIZE + 2) +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +static void +recompute_bad_disk (int bad_disk) +{ + int cluster = bad_disk/(DISK_COUNT/2); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE]; + int shift = j % (DISK_COUNT/2); + int logical_bad_disk; + bool empty = false; + for (int k = cluster*DISK_COUNT/2; k < DISK_COUNT/2 + cluster*DISK_COUNT/2; ++k) { + if (shift_table[shift][k] == bad_disk) { + logical_bad_disk = k; + continue; + } + for (int l = 0; l < BLOCK_SIZE; ++l) { + int c = fgetc (disks[shift_table[shift][k]]); + if (c == ' ') { + fgetc (disks[shift_table[shift][k]]); + empty = true; + } else { + ungetc (c, disks[shift_table[shift][k]]); + fscanf (disks[shift_table[shift][k]], "%02hhX", &block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]); + } + } fgetc (disks[shift_table[shift][k]]); + } + + if (empty) { // address empty + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], " "); + } fprintf (disks[bad_disk], "\n"); + continue; + } else if (logical_bad_disk % (DISK_COUNT/2) == DISK_COUNT/2 - 1) { // lost parity + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] = 0x00; + for (int k = cluster*DISK_COUNT/2, c = 0; k < DISK_COUNT/2 + cluster*DISK_COUNT/2; ++k) { + if (k == logical_bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] += + coefficients[c]*block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]; + c += 1; + } + } else { + for (int l = 0; l < BLOCK_SIZE; ++l) { + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] = + block_bytes[((DISK_COUNT/2 + cluster*DISK_COUNT/2 - 1)*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]; + } + for (int k = cluster*DISK_COUNT/2, c = 0; k < DISK_COUNT/2 + cluster*DISK_COUNT/2 - 1; ++k) { + if (k == logical_bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] -= + coefficients[c]*block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]; + c += 1; + } + } + + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], "%02hhX", block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]); + } fprintf (disks[bad_disk], "\n"); + } +} + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "Ошибка! Невозможно открыть файл: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "Предупреждение! Найден плохой диск: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "Предупреждение!Найден плохой диск: %s\n", filename); + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + bad_disks |= 1 << i; + } else rewind (disks[i]); + } + } + + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { + fprintf (stderr, "Повторный расчет, один диск плохой...\n"); + int bad_disk = CTZ (bad_disks); + recompute_bad_disk (bad_disk); + } else if (bad_disk_count == 2 && bad_disks & ((1 << DISK_COUNT/2) - 1) && bad_disks >> DISK_COUNT/2) { + fprintf (stderr, "2 плохих диска в разделяющемся кластере, повторный расчет.\n"); + int bad_disk0 = CTZ (bad_disks); + int bad_disk1 = CTZ (bad_disks ^ (1 << bad_disk0)); + recompute_bad_disk (bad_disk0); + recompute_bad_disk (bad_disk1); + } else if (bad_disk_count) { + fprintf (stderr, "Ошибка! Повторная инициализация массива с новыми дисками.\n"); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) { + for (int k = 0; k < BLOCK_SIZE; ++k) { + fputs (" ", disks[i]); + } fputs ("\n", disks[i]); + } + } + } + + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "Инициализация RAID 50 с POLY на 6 дисках с 12 байтовым сообщением.\n"); + + + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { + assigned = sscanf (head, "%hhu%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "Ошибка! Адрес не входит в границы! [0; 64)\n"); + continue; + } head += scanned; + } + + enum command command; { + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "Ошибка! Не корректная команда! (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE + MESSAGE_SIZE/2 + BLOCK_SIZE]; + int shift = address % (DISK_COUNT/2); + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("Пусто\n"); + break; + } else ungetc (c, disks[0]); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) { + fscanf (disks[shift_table[shift][i]], "%02hhX", &block_bytes[BLOCK_SIZE*i + j]); + } + } close_disks (); + + for (int i = 0; i < DISK_COUNT; ++i) { + if (i % (DISK_COUNT/2) == DISK_COUNT/2 - 1) continue; + for (int j = 0; j < BLOCK_SIZE; ++j) + printf ("%02hhX", block_bytes[BLOCK_SIZE*i + j]); + printf (" "); + } printf ("\n"); + } break; + case COMMAND_WRITE: { + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE + MESSAGE_SIZE/2 + BLOCK_SIZE] = {0}; { // scan message + uint8_t *write_head = block_bytes; + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "Ошибка! В сообщении запрещён символ `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "Ошибка! Ожидалось %d hex цифр для сообщения, получилось %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", write_head, &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + write_head += 1; + if (message_size % ((DISK_COUNT/2 - 1)*BLOCK_SIZE) == 0) + write_head += BLOCK_SIZE; + } + } + + int shift = address % (DISK_COUNT/2); + + for (int i = 0; i < DISK_COUNT/2 - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + block_bytes[(DISK_COUNT/2 - 1)*BLOCK_SIZE + j] += coefficients[i]*block_bytes[i*BLOCK_SIZE + j]; + } + + for (int i = DISK_COUNT/2; i < DISK_COUNT - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + j] += coefficients[i - 1]*block_bytes[i*BLOCK_SIZE + j]; + } + + open_disks (address); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + fprintf (disks[shift_table[shift][i]], "%02hhX", block_bytes[i*BLOCK_SIZE + j]); + fprintf (disks[shift_table[shift][i]], "\n"); + } close_disks (); + } break; + } free (line); + } + return EXIT_SUCCESS; +} diff --git a/src/raid-50-poly-8b-6d.c b/src/raid-50-poly-8b-6d.c new file mode 100644 index 0000000..351ebbb --- /dev/null +++ b/src/raid-50-poly-8b-6d.c @@ -0,0 +1,305 @@ +// feld marshal +// M1M2 M3M4 P1P2 M5M6 M7M8 P3P4 +// M1M2 P1P2 M3M4 M5M6 P3P4 M7M8 +// P1P2 M1M2 M3M4 P3P4 M5M6 M7M8 +// M1M2 M3M4 P1P2 M5M6 M7M8 P3P4 +#include +#include +#include +#include +#include + +#define DISK_COUNT 6 +#define MESSAGE_SIZE 8 +#define BLOCK_SIZE 2 + +uint8_t coefficients[DISK_COUNT - 2] = { 1, 1, 1, 1, }; + +// TODO(cmmm): generate +int shift_table[][DISK_COUNT] = { + { 0, 1, 2, 3, 4, 5 }, + { 0, 2, 1, 3, 5, 4 }, + { 1, 2, 0, 4, 5, 3 }, +}; + +#define DISK_SIZE 0x40 +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +#ifndef WIN32 +#define LINE_SIZE (2*BLOCK_SIZE + 1) +#else +#define LINE_SIZE (2*BLOCK_SIZE + 2) +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +enum command { + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +static void +recompute_bad_disk (int bad_disk) +{ + int cluster = bad_disk/(DISK_COUNT/2); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE]; + int shift = j % (DISK_COUNT/2); + + // read blocks from good disks + int logical_bad_disk; + for (int k = cluster*DISK_COUNT/2; k < DISK_COUNT/2 + cluster*DISK_COUNT/2; ++k) { + if (shift_table[shift][k] == bad_disk) { + logical_bad_disk = k; + continue; + } + for (int l = 0; l < BLOCK_SIZE; ++l) { + fscanf (disks[shift_table[shift][k]], "%02hhX", &block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]); + } fscanf (disks[shift_table[shift][k]], "\n"); + } + + // compute lost block + if (logical_bad_disk % (DISK_COUNT/2) == DISK_COUNT/2 - 1) { // lost parity + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] = 0x00; + for (int k = cluster*DISK_COUNT/2, c = 0; k < DISK_COUNT/2 + cluster*DISK_COUNT/2; ++k) { + if (k == logical_bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] += + coefficients[c]*block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]; + c += 1; + } + } else { // lost data + for (int l = 0; l < BLOCK_SIZE; ++l) { + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] = + block_bytes[((DISK_COUNT/2 + cluster*DISK_COUNT/2 - 1)*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]; + } + for (int k = cluster*DISK_COUNT/2, c = 0; k < DISK_COUNT/2 + cluster*DISK_COUNT/2 - 1; ++k) { + if (k == logical_bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] -= + coefficients[c]*block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]; + c += 1; + } + } + + // write lost block + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], "%02hhX", block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]); + } fprintf (disks[bad_disk], "\n"); + } +} + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR: unable to open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: found bad disk: %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: found bad disk: %s\n", filename); + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + bad_disks |= 1 << i; + } else rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + recompute_bad_disk (bad_disk); + } else if (bad_disk_count == 2 && bad_disks & ((1 << DISK_COUNT/2) - 1) && bad_disks >> DISK_COUNT/2) { + fprintf (stderr, "INFO: there are 2 bad disk in separate clusters, recomputing them...\n"); + int bad_disk0 = CTZ (bad_disks); + int bad_disk1 = CTZ (bad_disks ^ (1 << bad_disk0)); + recompute_bad_disk (bad_disk0); + recompute_bad_disk (bad_disk1); + } else if (bad_disk_count) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n"); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) { + for (int k = 0; k < BLOCK_SIZE; ++k) { + fputs ("00", disks[i]); + } fputs ("\n", disks[i]); + } + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFO: initializing RAID 50 on 6 disks with 8 byte messages...\n"); + + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + const char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE) { + fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command; { // scan command + char command_buffer[6] = {0}; + assigned = sscanf (head, "%5s %n", command_buffer, &scanned); + if (assigned != 1 || + (!strncmp (command_buffer, "read", 4) && + !strncmp (command_buffer, "write", 5))) { + fprintf(stderr, "ERROR: command not in set (read; write)\n"); + continue; + } head += scanned; + command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; + } + + switch (command) { + case COMMAND_READ: { + // parse out message + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE + MESSAGE_SIZE/2 + BLOCK_SIZE]; + int shift = address % (DISK_COUNT/2); + open_disks (address); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + fscanf (disks[shift_table[shift][i]], "%02hhX", &block_bytes[2*i + j]); + fscanf (disks[shift_table[shift][i]], "\n"); + } close_disks (); + + for (int i = 0; i < DISK_COUNT; ++i) { + if (i % (DISK_COUNT/2) == DISK_COUNT/2 - 1) continue; + for (int j = 0; j < BLOCK_SIZE; ++j) + printf ("%02hhX", block_bytes[2*i + j]); + printf (" "); + } printf ("\n"); + } break; + case COMMAND_WRITE: { + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE + MESSAGE_SIZE/2 + BLOCK_SIZE] = {0}; { // scan message + uint8_t *write_head = block_bytes; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + int message_size = 0; + while (sscanf (head, "%2hhx%n", write_head, &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + write_head += 1; + if (message_size % ((DISK_COUNT/2 - 1)*BLOCK_SIZE) == 0) + write_head += BLOCK_SIZE; + } + } + + int shift = address % (DISK_COUNT/2); + + // compute parity of first cluster + for (int i = 0; i < DISK_COUNT/2 - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + block_bytes[(DISK_COUNT/2 - 1)*BLOCK_SIZE + j] += coefficients[i]*block_bytes[i*BLOCK_SIZE + j]; + } + + // compute parity of second cluster + for (int i = DISK_COUNT/2; i < DISK_COUNT - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + j] += coefficients[i - 1]*block_bytes[i*BLOCK_SIZE + j]; + } + + // write needed blocks in each file in the necessary order + open_disks (address); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + fprintf (disks[shift_table[shift][i]], "%02hhX", block_bytes[i*BLOCK_SIZE + j]); + fprintf (disks[shift_table[shift][i]], "\n"); + } close_disks (); + } break; + } free (line); + } + return EXIT_SUCCESS; +} diff --git a/src/raid-50-xor-12b-6d.c b/src/raid-50-xor-12b-6d.c new file mode 100644 index 0000000..0aac492 --- /dev/null +++ b/src/raid-50-xor-12b-6d.c @@ -0,0 +1,328 @@ +// M1M2M3 M4M5M6 P1P2P3 M7M8M9 MAMBMC P4P5P6 +// M1M2M3 P1P2P3 M4M5M6 M7M8M9 P4P5P6 MAMBMC +// P1P2P3 M1M2M3 M4M5M6 P4P5P6 M7M8M9 MAMBMC +// M1M2M3 M4M5M6 P1P2P3 M7M8M9 MAMBMC P4P5P6 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 6 +#define MESSAGE_SIZE 12 +#define BLOCK_SIZE 3 + +// TODO(cmmm): generate +int shift_table[][DISK_COUNT] = { + { 0, 1, 2, 3, 4, 5 }, + { 0, 2, 1, 3, 5, 4 }, + { 1, 2, 0, 4, 5, 3 }, +}; + +#define DISK_SIZE 0x40 +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +#ifndef WIN32 +#define LINE_SIZE (2*BLOCK_SIZE + 1) +#else +#define LINE_SIZE (2*BLOCK_SIZE + 2) +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +enum command { + COMMAND_NONE, + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +static void +recompute_bad_disk (int bad_disk) +{ + int cluster = bad_disk/(DISK_COUNT/2); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE]; + int shift = j % (DISK_COUNT/2); + + // read blocks from good disks + int logical_bad_disk; + bool empty = false; + for (int k = cluster*DISK_COUNT/2; k < DISK_COUNT/2 + cluster*DISK_COUNT/2; ++k) { + if (shift_table[shift][k] == bad_disk) { + logical_bad_disk = k; + continue; + } + for (int l = 0; l < BLOCK_SIZE; ++l) { + int c = fgetc (disks[shift_table[shift][k]]); + if (c == ' ') { + fgetc (disks[shift_table[shift][k]]); + empty = true; + } else { + ungetc (c, disks[shift_table[shift][k]]); + fscanf (disks[shift_table[shift][k]], "%02hhX", + &block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]); + } + } fgetc (disks[shift_table[shift][k]]); + } + + if (empty) { // address empty + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], " "); + } fprintf (disks[bad_disk], "\n"); + continue; + } + + // compute lost block + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] = 0x00; + for (int k = cluster*DISK_COUNT/2; k < DISK_COUNT/2 + cluster*DISK_COUNT/2; ++k) { + if (k == logical_bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] ^= + block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]; + } + + // write lost block + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], "%02hhX", + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]); + } fprintf (disks[bad_disk], "\n"); + } +} + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR: unable to open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: found bad disk (missing): %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: found bad disk (corrupted): %s\n", filename); + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + bad_disks |= 1 << i; + } else rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + recompute_bad_disk (bad_disk); + } else if (bad_disk_count == 2 && bad_disks & ((1 << DISK_COUNT/2) - 1) && bad_disks >> DISK_COUNT/2) { + fprintf (stderr, "INFO: there are 2 bad disk in separate clusters, recomputing them...\n"); + int bad_disk0 = CTZ (bad_disks); + int bad_disk1 = CTZ (bad_disks ^ (1 << bad_disk0)); + recompute_bad_disk (bad_disk0); + recompute_bad_disk (bad_disk1); + } else if (bad_disk_count) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n"); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) { + for (int k = 0; k < BLOCK_SIZE; ++k) { + fputs (" ", disks[i]); + } fputs ("\n", disks[i]); + } + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFO: initializing RAID 50 with XOR on 6 disks with 8 byte messages...\n"); + + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE || scanned > 2) { + fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command = COMMAND_NONE; { // scan command + while (*head == ' ') head += 1; + if (!strncmp (head, "read", 4)) { + head += 4; + command = COMMAND_READ; + } else if (!strncmp (head, "write", 5)) { + head += 5; + command = COMMAND_WRITE; + } + if (!isspace(*head)) + command = COMMAND_NONE; + } + + switch (command) { + case COMMAND_NONE: { + fprintf(stderr, "ERROR: invalid command, must be on of (read, write)\n"); + continue; + } break; + case COMMAND_READ: { + // parse out message + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE + MESSAGE_SIZE/2 + BLOCK_SIZE]; + int shift = address % (DISK_COUNT/2); + + while (isspace (*head)) { + head += 1; + if (*head == '\n' || + *head == '\r') { + *head = '\0'; + } + } if (*head) { + fprintf (stderr, "ERROR: trailing junk after command: %s", head); + continue; + } + + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("No data stored!\n"); + break; + } ungetc (c, disks[0]); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + fscanf (disks[shift_table[shift][i]], "%02hhX", &block_bytes[i*BLOCK_SIZE + j]); + fscanf (disks[shift_table[shift][i]], "\n"); + } close_disks (); + + for (int i = 0; i < DISK_COUNT; ++i) { + if ((i + 1) % (DISK_COUNT/2) == 0) continue; + for (int j = 0; j < BLOCK_SIZE; ++j) + printf ("%02hhX", block_bytes[i*BLOCK_SIZE + j]); + } printf ("\n"); + } break; + case COMMAND_WRITE: { + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE + MESSAGE_SIZE/2 + BLOCK_SIZE] = {0}; { // scan message + uint8_t *write_head = block_bytes; + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", write_head, &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + write_head += 1; + if (message_size % ((DISK_COUNT/2 - 1)*BLOCK_SIZE) == 0) + write_head += BLOCK_SIZE; + } + } + + int shift = address % (DISK_COUNT/2); + + // compute parity of first cluster + for (int i = 0; i < DISK_COUNT/2 - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + block_bytes[(DISK_COUNT/2 - 1)*BLOCK_SIZE + j] ^= block_bytes[i*BLOCK_SIZE + j]; + } + + // compute parity of second cluster + for (int i = DISK_COUNT/2; i < DISK_COUNT - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + j] ^= block_bytes[i*BLOCK_SIZE + j]; + } + + // write needed blocks in each file in the necessary order + open_disks (address); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + fprintf (disks[shift_table[shift][i]], "%02hhX", block_bytes[i*BLOCK_SIZE + j]); + fprintf (disks[shift_table[shift][i]], "\n"); + } close_disks (); + } break; + } free (line); + } return EXIT_SUCCESS; +} diff --git a/src/raid-50-xor-8b-6d.c b/src/raid-50-xor-8b-6d.c new file mode 100644 index 0000000..486e787 --- /dev/null +++ b/src/raid-50-xor-8b-6d.c @@ -0,0 +1,328 @@ +// M1M2 M3M4 P1P2 M5M6 M7M8 P3P4 +// M1M2 P1P2 M3M4 M5M6 P3P4 M7M8 +// P1P2 M1M2 M3M4 P3P4 M5M6 M7M8 +// M1M2 M3M4 P1P2 M5M6 M7M8 P3P4 +#include +#include +#include +#include +#include +#include + +#define DISK_COUNT 6 +#define MESSAGE_SIZE 8 +#define BLOCK_SIZE 2 + +// TODO(cmmm): generate +int shift_table[][DISK_COUNT] = { + { 0, 1, 2, 3, 4, 5 }, + { 0, 2, 1, 3, 5, 4 }, + { 1, 2, 0, 4, 5, 3 }, +}; + +#define DISK_SIZE 0x40 +#define POPCOUNT __builtin_popcount +#define CTZ __builtin_ctz + +#ifndef WIN32 +#define LINE_SIZE (2*BLOCK_SIZE + 1) +#else +#define LINE_SIZE (2*BLOCK_SIZE + 2) +static ssize_t +getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { + register char c; + register char *cs = NULL; + register int length = 0; + while ((c = getc (stream)) != EOF) { + cs = (char *) realloc (cs, ++length + 1); + if ((*(cs + length - 1) = c) == '\n') { + *(cs + length) = '\0'; + break; + } + } + + // return the allocated memory if lineptr is null + if ((*lineptr) == NULL) { + *lineptr = cs; + } else { + // check if enough memory is allocated + if ((length + 1) < *n) + *lineptr = (char *) realloc (*lineptr, length + 1); + memcpy (*lineptr, cs, length); + free (cs); + } + + return (ssize_t) (*n = strlen (*lineptr)); +} +#endif + +enum command { + COMMAND_NONE, + COMMAND_WRITE, + COMMAND_READ, +}; + +FILE *disks[DISK_COUNT]; + +static void +recompute_bad_disk (int bad_disk) +{ + int cluster = bad_disk/(DISK_COUNT/2); + + for (int j = 0; j < DISK_SIZE; ++j) { + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE]; + int shift = j % (DISK_COUNT/2); + + // read blocks from good disks + int logical_bad_disk; + bool empty = false; + for (int k = cluster*DISK_COUNT/2; k < DISK_COUNT/2 + cluster*DISK_COUNT/2; ++k) { + if (shift_table[shift][k] == bad_disk) { + logical_bad_disk = k; + continue; + } + for (int l = 0; l < BLOCK_SIZE; ++l) { + int c = fgetc (disks[shift_table[shift][k]]); + if (c == ' ') { + fgetc (disks[shift_table[shift][k]]); + empty = true; + } else { + ungetc (c, disks[shift_table[shift][k]]); + fscanf (disks[shift_table[shift][k]], "%02hhX", + &block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]); + } + } fgetc (disks[shift_table[shift][k]]); + } + + if (empty) { // address empty + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], " "); + } fprintf (disks[bad_disk], "\n"); + continue; + } + + // compute lost block + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] = 0x00; + for (int k = cluster*DISK_COUNT/2; k < DISK_COUNT/2 + cluster*DISK_COUNT/2; ++k) { + if (k == logical_bad_disk) continue; + for (int l = 0; l < BLOCK_SIZE; ++l) + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)] ^= + block_bytes[(k*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]; + } + + // write lost block + for (int l = 0; l < BLOCK_SIZE; ++l) { + fprintf (disks[bad_disk], "%02hhX", + block_bytes[(logical_bad_disk*BLOCK_SIZE + l) % (MESSAGE_SIZE/2 + BLOCK_SIZE)]); + } fprintf (disks[bad_disk], "\n"); + } +} + +static void +open_disks (uint8_t address) +{ + uint16_t bad_disks = 0; + for (int i = 0; i < DISK_COUNT; ++i) { + char filename[] = "disk0"; + filename[strlen (filename) - 1] += i; + disks[i] = fopen (filename, "r+"); + if (!disks[i]) { // disk file was deleted + disks[i] = fopen (filename, "w+"); + if (!disks[i]) { + fprintf (stderr, "ERROR: unable to open file: %s\n", filename); + exit (EXIT_FAILURE); + } + + fprintf (stderr, "WARNING: found bad disk (missing): %s\n", filename); + bad_disks |= 1 << i; + } else { // disk file wasn't deleted + fseek (disks[i], 0, SEEK_END); + if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format + fprintf (stderr, "WARNING: found bad disk (corrupted): %s\n", filename); + fclose (disks[i]); + disks[i] = fopen (filename, "w+"); + bad_disks |= 1 << i; + } else rewind (disks[i]); + } + } + + // there were bad disks + int bad_disk_count = POPCOUNT (bad_disks); + if (bad_disk_count == 1) { // recompute lost disk + fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n"); + int bad_disk = CTZ (bad_disks); + recompute_bad_disk (bad_disk); + } else if (bad_disk_count == 2 && bad_disks & ((1 << DISK_COUNT/2) - 1) && bad_disks >> DISK_COUNT/2) { + fprintf (stderr, "INFO: there are 2 bad disk in separate clusters, recomputing them...\n"); + int bad_disk0 = CTZ (bad_disks); + int bad_disk1 = CTZ (bad_disks ^ (1 << bad_disk0)); + recompute_bad_disk (bad_disk0); + recompute_bad_disk (bad_disk1); + } else if (bad_disk_count) { // replace all disks with empty ones, all data is lost + fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n"); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < DISK_SIZE; ++j) { + for (int k = 0; k < BLOCK_SIZE; ++k) { + fputs (" ", disks[i]); + } fputs ("\n", disks[i]); + } + } + } + + // position head for writing/reading message + for (int i = 0; i < DISK_COUNT; ++i) + fseek (disks[i], address*LINE_SIZE, SEEK_SET); +} + +static void +close_disks (void) +{ + for (int i = 0; i < DISK_COUNT; ++i) + fclose (disks[i]); +} + +int +main (void) +{ + fprintf (stderr, "INFO: initializing RAID 50 with XOR on 6 disks with 8 byte messages...\n"); + + // check disk integrity on startup + open_disks (0); + close_disks ( ); + + for (;;) { + char *line = NULL; + size_t line_size = 0; + char *head; + int assigned; + int scanned; + + // read whole line + printf ("> "); + scanned = getline (&line, &line_size, stdin); + if (scanned == -1) + exit (EXIT_SUCCESS); + else if (scanned == 1) + continue; + head = line; + + uint8_t address; { // scan address + assigned = sscanf (head, "%hhx%n", &address, &scanned); + if (assigned != 1 || address >= DISK_SIZE || scanned > 2) { + fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n"); + continue; + } head += scanned; + } + + enum command command = COMMAND_NONE; { // scan command + while (*head == ' ') head += 1; + if (!strncmp (head, "read", 4)) { + head += 4; + command = COMMAND_READ; + } else if (!strncmp (head, "write", 5)) { + head += 5; + command = COMMAND_WRITE; + } + if (!isspace(*head)) + command = COMMAND_NONE; + } + + switch (command) { + case COMMAND_NONE: { + fprintf(stderr, "ERROR: invalid command, must be on of (read, write)\n"); + continue; + } break; + case COMMAND_READ: { + // parse out message + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE + MESSAGE_SIZE/2 + BLOCK_SIZE]; + int shift = address % (DISK_COUNT/2); + + while (isspace (*head)) { + head += 1; + if (*head == '\n' || + *head == '\r') { + *head = '\0'; + } + } if (*head) { + fprintf (stderr, "ERROR: trailing junk after command: %s", head); + continue; + } + + open_disks (address); + int c = fgetc (disks[0]); + if (c == ' ') { + close_disks (); + printf ("No data stored!\n"); + break; + } ungetc (c, disks[0]); + + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + fscanf (disks[shift_table[shift][i]], "%02hhX", &block_bytes[2*i + j]); + fscanf (disks[shift_table[shift][i]], "\n"); + } close_disks (); + + for (int i = 0; i < DISK_COUNT; ++i) { + if (i % (DISK_COUNT/2) == DISK_COUNT/2 - 1) continue; + for (int j = 0; j < BLOCK_SIZE; ++j) + printf ("%02hhX", block_bytes[2*i + j]); + } printf ("\n"); + } break; + case COMMAND_WRITE: { + uint8_t block_bytes[MESSAGE_SIZE/2 + BLOCK_SIZE + MESSAGE_SIZE/2 + BLOCK_SIZE] = {0}; { // scan message + uint8_t *write_head = block_bytes; + int message_size = 0; + + while (*head == ' ' || + *head == '\t') ++head; + const char *invalid = NULL; + size_t len = 0; + for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { + if (!strchr ("0123456789abcdefABCDEF", *c)) { + invalid = c; + break; + } len += 1; + } if (invalid) { + fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); + break; + } if (len != 2*MESSAGE_SIZE) { + fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); + break; + } + + while (sscanf (head, "%2hhx%n", write_head, &scanned) == 1 && + message_size < MESSAGE_SIZE) { + head += scanned; + message_size += 1; + write_head += 1; + if (message_size % ((DISK_COUNT/2 - 1)*BLOCK_SIZE) == 0) + write_head += BLOCK_SIZE; + } + } + + int shift = address % (DISK_COUNT/2); + + // compute parity of first cluster + for (int i = 0; i < DISK_COUNT/2 - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + block_bytes[(DISK_COUNT/2 - 1)*BLOCK_SIZE + j] ^= block_bytes[i*BLOCK_SIZE + j]; + } + + // compute parity of second cluster + for (int i = DISK_COUNT/2; i < DISK_COUNT - 1; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + j] ^= block_bytes[i*BLOCK_SIZE + j]; + } + + // write needed blocks in each file in the necessary order + open_disks (address); + for (int i = 0; i < DISK_COUNT; ++i) { + for (int j = 0; j < BLOCK_SIZE; ++j) + fprintf (disks[shift_table[shift][i]], "%02hhX", block_bytes[i*BLOCK_SIZE + j]); + fprintf (disks[shift_table[shift][i]], "\n"); + } close_disks (); + } break; + } free (line); + } return EXIT_SUCCESS; +} -- cgit v1.3.1