2014-11-05 06:09:40 -05:00
|
|
|
/*
|
|
|
|
* Mini Object Storage, (C) 2014 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-27 22:43:54 -05:00
|
|
|
#include <string.h>
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
#include <erasure-code.h>
|
2014-11-27 22:43:54 -05:00
|
|
|
#include "decode.h"
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
static int src_in_err (int r, int *src_err_list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; src_err_list[i] != -1; i++) {
|
|
|
|
if (src_err_list[i] == r) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// false
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Generate decode matrix during the decoding phase
|
|
|
|
*/
|
|
|
|
|
2014-11-27 22:43:54 -05:00
|
|
|
int minio_init_decoder (int *src_err_list,
|
|
|
|
unsigned char *encode_matrix,
|
|
|
|
unsigned char **decode_matrix,
|
|
|
|
unsigned char **decode_tbls,
|
|
|
|
int k, int n, int errs)
|
2014-11-05 06:09:40 -05:00
|
|
|
{
|
|
|
|
int i, j, r, s, l, z;
|
2014-11-27 22:43:54 -05:00
|
|
|
unsigned char input_matrix[k * n];
|
|
|
|
unsigned char inverse_matrix[k * n];
|
|
|
|
unsigned char *tmp_decode_matrix;
|
|
|
|
unsigned char *tmp_decode_tbls;
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2014-11-27 22:43:54 -05:00
|
|
|
tmp_decode_matrix = (unsigned char *) malloc (k * n);
|
|
|
|
if (!tmp_decode_matrix)
|
2014-11-05 06:09:40 -05:00
|
|
|
return -1;
|
|
|
|
|
2014-11-27 22:43:54 -05:00
|
|
|
tmp_decode_tbls = (unsigned char *) malloc (k * n * 32);
|
|
|
|
if (!tmp_decode_tbls)
|
2014-11-05 06:09:40 -05:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0, r = 0; i < k; i++, r++) {
|
|
|
|
while (src_in_err(r, src_err_list))
|
|
|
|
r++;
|
|
|
|
for (j = 0; j < k; j++) {
|
|
|
|
input_matrix[k * i + j] = encode_matrix[k * r + j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not all Vandermonde matrix can be inverted
|
|
|
|
if (gf_invert_matrix(input_matrix, inverse_matrix, k) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (l = 0; l < errs; l++) {
|
|
|
|
if (src_err_list[l] < k) {
|
|
|
|
// decoding matrix elements for data chunks
|
|
|
|
for (j = 0; j < k; j++) {
|
2014-11-27 22:43:54 -05:00
|
|
|
tmp_decode_matrix[k * l + j] =
|
2014-11-05 06:09:40 -05:00
|
|
|
inverse_matrix[k *
|
|
|
|
src_err_list[l] + j];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int s = 0;
|
|
|
|
// decoding matrix element for coding chunks
|
|
|
|
for (i = 0; i < k; i++) {
|
|
|
|
s = 0;
|
|
|
|
for (j = 0; j < k; j++) {
|
|
|
|
s ^= gf_mul(inverse_matrix[j * k + i],
|
|
|
|
encode_matrix[k *
|
|
|
|
src_err_list[l] + j]);
|
|
|
|
}
|
2014-11-27 22:43:54 -05:00
|
|
|
tmp_decode_matrix[k * l + i] = s;
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 22:43:54 -05:00
|
|
|
|
|
|
|
ec_init_tables(k, errs, tmp_decode_matrix, tmp_decode_tbls);
|
|
|
|
|
|
|
|
*decode_matrix = tmp_decode_matrix;
|
|
|
|
*decode_tbls = tmp_decode_tbls;
|
2014-11-05 06:09:40 -05:00
|
|
|
return 0;
|
|
|
|
}
|