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-29 17:58:40 -05:00
|
|
|
#include "common.h"
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2014-12-07 03:09:24 -05:00
|
|
|
static
|
|
|
|
int32_t _minio_src_index_in_error (int r, int32_t *error_index)
|
2014-11-05 06:09:40 -05:00
|
|
|
{
|
|
|
|
int i;
|
2014-12-07 03:09:24 -05:00
|
|
|
for (i = 0; error_index[i] != -1; i++) {
|
|
|
|
if (error_index[i] == r) {
|
2014-11-29 17:58:40 -05:00
|
|
|
// true
|
2014-11-05 06:09:40 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// false
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-07 03:09:24 -05:00
|
|
|
int32_t minio_get_source_target (int errs, int k, int m,
|
|
|
|
int32_t *error_index,
|
|
|
|
uint32_t *decode_index,
|
|
|
|
uint8_t **buffs,
|
|
|
|
uint8_t ***source,
|
|
|
|
uint8_t ***target)
|
2014-11-29 17:58:40 -05:00
|
|
|
{
|
2014-12-07 03:09:24 -05:00
|
|
|
int i;
|
|
|
|
uint8_t *tmp_source[k];
|
|
|
|
uint8_t *tmp_target[m];
|
|
|
|
|
|
|
|
memset (tmp_source, 0, k);
|
|
|
|
memset (tmp_target, 0, m);
|
|
|
|
|
|
|
|
for (i = 0; i < k; i++) {
|
|
|
|
tmp_source[i] = (uint8_t *) buffs[decode_index[i]];
|
2014-11-29 17:58:40 -05:00
|
|
|
}
|
2014-12-07 03:09:24 -05:00
|
|
|
|
|
|
|
for (i = 0; i < m; i++) {
|
|
|
|
if (i < errs)
|
|
|
|
tmp_target[i] = (uint8_t *) buffs[error_index[i]];
|
|
|
|
}
|
|
|
|
|
2014-11-29 17:58:40 -05:00
|
|
|
*source = tmp_source;
|
|
|
|
*target = tmp_target;
|
2015-01-11 01:50:51 -05:00
|
|
|
|
|
|
|
return 0;
|
2014-11-29 17:58:40 -05:00
|
|
|
}
|
|
|
|
|
2014-11-05 06:09:40 -05:00
|
|
|
/*
|
|
|
|
Generate decode matrix during the decoding phase
|
|
|
|
*/
|
|
|
|
|
2014-12-07 03:09:24 -05:00
|
|
|
int minio_init_decoder (int32_t *error_index,
|
|
|
|
int k, int n, int errs,
|
|
|
|
uint8_t *encode_matrix,
|
|
|
|
uint8_t **decode_matrix,
|
|
|
|
uint8_t **decode_tbls,
|
|
|
|
uint32_t **decode_index)
|
2014-11-05 06:09:40 -05:00
|
|
|
{
|
|
|
|
int i, j, r, s, l, z;
|
2014-12-07 03:09:24 -05:00
|
|
|
uint8_t input_matrix[k * n];
|
|
|
|
uint8_t inverse_matrix[k * n];
|
|
|
|
uint8_t tmp_decode_matrix[k * n];
|
|
|
|
uint8_t tmp_decode_tbls[k * n * 32];
|
|
|
|
uint32_t tmp_decode_index[k];
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
for (i = 0, r = 0; i < k; i++, r++) {
|
2014-12-07 03:09:24 -05:00
|
|
|
while (_minio_src_index_in_error(r, error_index))
|
2014-11-05 06:09:40 -05:00
|
|
|
r++;
|
|
|
|
for (j = 0; j < k; j++) {
|
|
|
|
input_matrix[k * i + j] = encode_matrix[k * r + j];
|
|
|
|
}
|
2014-12-07 03:09:24 -05:00
|
|
|
tmp_decode_index[i] = r;
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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++) {
|
2014-12-07 03:09:24 -05:00
|
|
|
if (error_index[l] < k) {
|
2014-11-05 06:09:40 -05:00
|
|
|
// 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 *
|
2014-12-07 03:09:24 -05:00
|
|
|
error_index[l] + j];
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
} 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 *
|
2014-12-07 03:09:24 -05:00
|
|
|
error_index[l] + j]);
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
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
|
|
|
|
2014-12-07 03:09:24 -05:00
|
|
|
ec_init_tables (k, errs, tmp_decode_matrix, tmp_decode_tbls);
|
2014-11-27 22:43:54 -05:00
|
|
|
|
|
|
|
*decode_matrix = tmp_decode_matrix;
|
|
|
|
*decode_tbls = tmp_decode_tbls;
|
2014-12-07 03:09:24 -05:00
|
|
|
*decode_index = tmp_decode_index;
|
|
|
|
|
2014-11-05 06:09:40 -05:00
|
|
|
return 0;
|
|
|
|
}
|