static void ecc_1(void) { int size[] = {7, 8, 16, 1024+16, 4096+32, 8*1024*4+64, 0}; for (int * s = size; *s != 0; s++) { unsigned char in[*s]; unsigned char out[*s + (*s / 8)]; ssize_t in_sz = sizeof in; ssize_t out_sz = sizeof out; memset(in, 0, in_sz); memset(out, 0, out_sz); FILE * f = fopen("/dev/urandom", "r"); CU_ASSERT_FATAL(f != NULL); CU_ASSERT_FATAL(fread(in, in_sz, 1, f) == 1); fclose(f); ssize_t rc = sfc_ecc_inject(out, out_sz, in, in_sz); if ((*s % 8) != 0) { CU_ASSERT(rc == -1); } else { CU_ASSERT(rc == in_sz + (in_sz / 8)); } unsigned char cmp[*s]; ssize_t cmp_sz = sizeof cmp; memset(cmp, 0, cmp_sz); rc = sfc_ecc_remove(cmp, cmp_sz, out, out_sz); if ((out_sz % 9) != 0) { CU_ASSERT(rc == -1); } else { CU_ASSERT(rc == in_sz) CU_ASSERT_FATAL(memcmp(in, cmp, in_sz) == 0); #ifdef DEBUG dump_memory(stdout, 0, in, in_sz); dump_memory(stdout, 0, cmp, cmp_sz); #endif } } }
static int command_inject(args_t * args) { assert(args != NULL); struct stat st; if (stat(args->path, &st) != 0) { ERRNO(errno); return -1; } if (!S_ISREG(st.st_mode)) { ERRNO(errno); return -1; } FILE *i = fopen(args->path, "r"); if (i == NULL) { ERRNO(errno); return-1; } FILE *o = fopen(args->file, "w"); if (o == NULL) { ERRNO(errno); return -1; } #define INPUT_SIZE (4096 - (4096 / ECC_SIZE)) char input[INPUT_SIZE]; // 4KB less 512 ECC bytes #undef INPUT_SIZE size_t count = 0; while (count < st.st_size) { clearerr(i); size_t rc = fread(input, 1, sizeof input, i); if (rc == 0) { int err = ferror(i); if (err) { ERRNO(errno); return -1; } else break; } count += rc; #define OUTPUT_SIZE 4096 char output[OUTPUT_SIZE]; #undef OUTPUT_SIZE memset(output + sizeof input, 0, sizeof output - sizeof input); rc = (rc + 7) & ~7; // 8-byte alignment ssize_t injected_size = 0; if (args->p8 == f_P8) injected_size = p8_ecc_inject(output, sizeof output, input, rc); else injected_size = sfc_ecc_inject(output, sizeof output, input, rc); if (injected_size < 0) { ERRNO(errno); return -1; } clearerr(o); rc = fwrite(output, 1, injected_size, o); if (rc == 0) { int err = ferror(o); if (err) { ERRNO(errno); return -1; } } } if (fclose(i) == EOF) { ERRNO(errno); return -1; } if (fclose(o) == EOF) { ERRNO(errno); return -1; } return 0; }