1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/*\
|*| Parity Archive - A way to restore missing files in a set.
|*|
|*| Copyright (C) 2001 Willem Monsuwe (willem@stack.nl)
|*|
|*| File format by Stefan Wehlus -
|*| initial idea by Tobias Rieper, further suggestions by Kilroy Balore
|*|
|*| Reed-Solomon coding
\*/
#include <stdio.h>
#include <string.h>
#include "types.h"
#include "fileops.h"
#include "rs.h"
#include "util.h"
#include "par.h"
/*\
|*| Calculations over a Galois Field, GF(8)
\*/
u8 gl[0x100], ge[0x200];
/*\ Multiply: a*b = exp(log(a) + log(b)) \*/
static int
gmul(int a, int b)
{
if ((a == 0) || (b == 0)) return 0;
return ge[gl[a] + gl[b]];
}
/*\ Divide: a/b = exp(log(a) - log(b)) \*/
static int
gdiv(int a, int b)
{
if ((a == 0) || (b == 0)) return 0;
return ge[gl[a] - gl[b] + 255];
}
/*\ Power: a^b = exp(log(a) * b) \*/
static int
gpow(int a, int b)
{
if (a == 0) return 0;
return ge[(gl[a] * b) % 255];
}
/*\ Initialise log and exp tables using generator x^8 + x^4 + x^3 + x^2 + 1 \*/
void
ginit(void)
{
unsigned int b, l;
b = 1;
for (l = 0; l < 0xff; l++) {
gl[b] = l;
ge[l] = b;
b += b;
if (b & 0x100) b ^= 0x11d;
}
for (l = 0xff; l < 0x200; l++)
ge[l] = ge[l - 0xff];
}
/*\ Fill in a LUT \*/
static void
make_lut(u8 lut[0x100], int m)
{
int j;
for (j = 0x100; --j; )
lut[j] = ge[gl[m] + gl[j]];
lut[0] = 0;
}
#define MT(i,j) (mt[((i) * Q) + (j)])
#define IMT(i,j) (imt[((i) * N) + (j)])
#define MULS(i,j) (muls[((i) * N) + (j)])
int
recreate(xfile_t *in, xfile_t *out)
{
int i, j, k, l, M, N, Q, R;
u8 *mt, *imt, *muls;
u8 buf[0x10000], *work;
i64 s, size;
i64 perc;
ginit();
/*\ Count number of recovery files \*/
for (i = Q = R = 0; in[i].filenr; i++) {
if (in[i].files) {
R++;
/*\ Get max. matrix row size \*/
for (k = 0; in[i].files[k]; k++) {
if (in[i].files[k] > Q)
Q = in[i].files[k];
}
} else {
if (in[i].filenr > Q)
Q = in[i].filenr;
}
}
N = i;
/*\ Count number of volumes to output \*/
for (i = j = M = 0; out[i].filenr; i++) {
M++;
if (out[i].files) {
j++;
/*\ Get max. matrix row size \*/
for (k = 0; out[i].files[k]; k++) {
if (out[i].files[k] > Q)
Q = out[i].files[k];
}
} else {
if (out[i].filenr > Q)
Q = out[i].filenr;
}
}
R += j;
Q += j;
CNEW(mt, R * Q);
CNEW(imt, R * N);
/*\ Fill in matrix rows for recovery files \*/
for (i = j = 0; in[i].filenr; i++) {
if (!in[i].files)
continue;
for (k = 0; in[i].files[k]; k++)
MT(j, in[i].files[k]-1) = gpow(k+1, in[i].filenr - 1);
IMT(j, i) = 1;
j++;
}
/*\ Fill in matrix rows for output recovery files \*/
for (i = 0, l = Q; out[i].filenr; i++) {
if (!out[i].files)
continue;
for (k = 0; out[i].files[k]; k++)
MT(j, out[i].files[k]-1) = gpow(k+1, out[i].filenr - 1);
--l;
/*\ Fudge filenr \*/
out[i].filenr = l + 1;
MT(j, l) = 1;
j++;
}
if (cmd.loglevel > 0) {
fprintf(stderr, "Matrix input:\n");
for (i = 0; i < R; i++) {
fprintf(stderr, "| ");
for (j = 0; j < Q; j++)
fprintf(stderr, "%02x ", MT(i, j));
fprintf(stderr, "| ");
for (j = 0; j < N; j++)
fprintf(stderr, "%02x ", IMT(i, j));
fprintf(stderr, "|\n");
}
}
/*\ Use (virtual) rows from data files to eliminate columns \*/
for (i = 0; in[i].filenr; i++) {
if (in[i].files)
continue;
k = in[i].filenr - 1;
/*\ MT would have a 1 at (i, k), IMT a 1 at (i, i)
|*| IMT(j,i) -= MT(j,k) * IMT(i,i) (is MT(j, k))
|*| MT(j,k) -= MT(j,k) * MT(i,k) (becomes 0)
\*/
for (j = 0; j < R; j++) {
IMT(j, i) ^= MT(j, k);
MT(j, k) = 0;
}
}
if (cmd.loglevel > 0) {
fprintf(stderr, "Matrix after data file elimination:\n");
for (i = 0; i < R; i++) {
fprintf(stderr, "| ");
for (j = 0; j < Q; j++)
fprintf(stderr, "%02x ", MT(i, j));
fprintf(stderr, "| ");
for (j = 0; j < N; j++)
fprintf(stderr, "%02x ", IMT(i, j));
fprintf(stderr, "|\n");
}
}
/*\ Eliminate columns using the remaining rows, so we get I.
|*| The accompanying matrix will be the inverse
\*/
for (i = 0; i < R; i++) {
int d, l;
/*\ Find first non-zero entry \*/
for (l = 0; (l < Q) && !MT(i, l); l++)
;
if (l == Q) continue;
d = MT(i, l);
/*\ Scale the matrix so MT(i, l) becomes 1 \*/
for (j = 0; j < Q; j++)
MT(i, j) = gdiv(MT(i, j), d);
for (j = 0; j < N; j++)
IMT(i, j) = gdiv(IMT(i, j), d);
/*\ Eliminate the column in the other matrices \*/
for (k = 0; k < R; k++) {
if (k == i) continue;
d = MT(k, l);
for (j = 0; j < Q; j++)
MT(k, j) ^= gmul(MT(i, j), d);
for (j = 0; j < N; j++)
IMT(k, j) ^= gmul(IMT(i, j), d);
}
}
if (cmd.loglevel > 0) {
fprintf(stderr, "Matrix after gaussian elimination:\n");
for (i = 0; i < R; i++) {
fprintf(stderr, "| ");
for (j = 0; j < Q; j++)
fprintf(stderr, "%02x ", MT(i, j));
fprintf(stderr, "| ");
for (j = 0; j < N; j++)
fprintf(stderr, "%02x ", IMT(i, j));
fprintf(stderr, "|\n");
}
}
/*\ Make the multiplication tables \*/
CNEW(muls, M * N);
for (i = 0; out[i].filenr; i++) {
/*\ File #x: The row IMT(j) for which MT(j,x) = 1 \*/
for (j = 0; j < R; j++) {
k = out[i].filenr - 1;
if (MT(j, k) != 1)
continue;
/*\ All other values should be 0 \*/
for (k = 0; !MT(j, k); k++)
;
if (k != out[i].filenr - 1)
continue;
for (k++; (k < Q) && !MT(j, k); k++)
;
if (k != Q)
continue;
break;
}
/*\ Did we find a suitable row ? \*/
if (j == R) {
out[i].size = 0;
continue;
}
for (k = 0; k < N; k++)
MULS(i, k) = IMT(j, k);
}
free(mt);
free(imt);
if (cmd.loglevel > 0) {
fprintf(stderr, "Multipliers:\n");
for (i = 0; i < M; i++) {
fprintf(stderr, "| ");
for (j = 0; j < N; j++)
fprintf(stderr, "%02x ", MULS(i, j));
fprintf(stderr, "|\n");
}
}
/*\ Check for columns with all-zeroes \*/
for (j = 0; j < N; j++) {
for (i = 0; i < M; i++)
if (MULS(i, j))
break;
/*\ This input file isn't used \*/
if (i == M)
in[j].size = 0;
}
/*\ Find out how much we should process in total \*/
size = 0;
for (i = 0; out[i].filenr; i++)
if (size < out[i].size)
size = out[i].size;
/*\ Restore all the files at once \*/
NEW(work, sizeof(buf) * M);
perc = 0;
fprintf(stderr, "0%%"); fflush(stderr);
/*\ Process all files \*/
for (s = 0; s < size; ) {
i64 tr, r, q;
u8 *p;
/*\ Display progress \*/
while (((s * 50) / size) > perc) {
perc++;
if (perc % 5) fprintf(stderr, ".");
else fprintf(stderr, "%lld%%", (perc / 5) * 10);
fflush(stderr);
}
/*\ See how much we should read \*/
memset(work, 0, sizeof(buf) * M);
for (i = 0; in[i].filenr; i++) {
tr = sizeof(buf);
if (tr > (in[i].size - s))
tr = in[i].size - s;
if (tr <= 0)
continue;
r = file_read(in[i].f, buf, tr);
if (r < tr) {
perror("READ ERROR");
free(muls);
free(work);
return 0;
}
for (j = 0; out[j].filenr; j++) {
u8 lut[0x100];
if (s >= out[j].size) continue;
if (!MULS(j, i)) continue;
/*\ Precalc LUT \*/
make_lut(lut, MULS(j, i));
p = work + (j * sizeof(buf));
/*\ XOR it in, passed through the LUTs \*/
for (q = r; --q >= 0; )
p[q] ^= lut[buf[q]];
}
}
for (j = 0; out[j].filenr; j++) {
if (s >= out[j].size) continue;
tr = sizeof(buf);
if (tr > (out[j].size - s))
tr = out[j].size - s;
r = file_write(out[j].f, work + (j * sizeof(buf)), tr);
if (r < tr) {
perror("WRITE ERROR");
free(muls);
free(work);
return 0;
}
}
s += sizeof(buf);
}
fprintf(stderr, "100%%\n"); fflush(stderr);
free(muls);
free(work);
return 1;
}
|