2014-11-05 06:09:40 -05:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2014 Minio, Inc.
|
2014-11-05 06:09:40 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package erasure
|
|
|
|
|
2015-01-26 15:21:56 -05:00
|
|
|
// #cgo CFLAGS: -O0
|
2014-11-05 06:09:40 -05:00
|
|
|
// #include <stdlib.h>
|
2015-01-27 15:36:44 -05:00
|
|
|
// #include "ec-code.h"
|
|
|
|
// #include "ec-common.h"
|
2014-11-05 06:09:40 -05:00
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2015-02-23 16:39:16 -05:00
|
|
|
// Decode decodes 2 tuple data containing (k + m) chunks back into its original form.
|
|
|
|
// Additionally original block length should also be provided as input.
|
|
|
|
//
|
|
|
|
// Decoded data is exactly similar in length and content as the original data.
|
2014-11-05 06:09:40 -05:00
|
|
|
func (e *Encoder) Decode(chunks [][]byte, length int) ([]byte, error) {
|
2015-03-24 21:44:21 -04:00
|
|
|
var decodeMatrix *C.uint8_t
|
|
|
|
var decodeTbls *C.uint8_t
|
|
|
|
var decodeIndex *C.uint32_t
|
2014-12-07 03:09:24 -05:00
|
|
|
var source, target **C.uint8_t
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-24 21:44:21 -04:00
|
|
|
k := int(e.params.K)
|
|
|
|
m := int(e.params.M)
|
2015-03-22 21:21:35 -04:00
|
|
|
n := k + m
|
2015-03-24 21:44:21 -04:00
|
|
|
if len(chunks) != n {
|
2014-11-05 06:09:40 -05:00
|
|
|
return nil, errors.New(fmt.Sprintf("chunks length must be %d", n))
|
|
|
|
}
|
2015-03-24 21:44:21 -04:00
|
|
|
chunkLen := GetEncodedBlockLen(length, uint8(k))
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-24 21:44:21 -04:00
|
|
|
errorIndex := make([]int, n+1)
|
|
|
|
var errCount int = 0
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
for i := range chunks {
|
|
|
|
// Check of chunks are really null
|
|
|
|
if chunks[i] == nil || len(chunks[i]) == 0 {
|
2015-03-24 21:44:21 -04:00
|
|
|
errorIndex[errCount] = i
|
|
|
|
errCount++
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 21:44:21 -04:00
|
|
|
errorIndex[errCount] = -1
|
|
|
|
errCount++
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
// Too many missing chunks, cannot be more than parity `m`
|
2015-03-24 21:44:21 -04:00
|
|
|
if errCount-1 > int(n-k) {
|
2014-11-05 06:09:40 -05:00
|
|
|
return nil, errors.New("too many erasures requested, can't decode")
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:44:21 -04:00
|
|
|
errorIndex_ptr := int2CInt(errorIndex[:errCount])
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
for i := range chunks {
|
|
|
|
if chunks[i] == nil || len(chunks[i]) == 0 {
|
2015-03-24 21:44:21 -04:00
|
|
|
chunks[i] = make([]byte, chunkLen)
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:44:21 -04:00
|
|
|
C.minio_init_decoder(errorIndex_ptr, C.int(k), C.int(n), C.int(errCount-1),
|
|
|
|
e.encodeMatrix, &decodeMatrix, &decodeTbls, &decodeIndex)
|
2014-11-05 06:09:40 -05:00
|
|
|
|
|
|
|
pointers := make([]*byte, n)
|
|
|
|
for i := range chunks {
|
|
|
|
pointers[i] = &chunks[i][0]
|
|
|
|
}
|
2014-11-14 00:48:04 -05:00
|
|
|
|
2014-12-07 03:09:24 -05:00
|
|
|
data := (**C.uint8_t)(unsafe.Pointer(&pointers[0]))
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-24 21:44:21 -04:00
|
|
|
ret := C.minio_get_source_target(C.int(errCount-1), C.int(k), C.int(m), errorIndex_ptr,
|
|
|
|
decodeIndex, data, &source, &target)
|
2014-11-29 17:58:40 -05:00
|
|
|
|
2015-01-26 15:21:56 -05:00
|
|
|
if int(ret) == -1 {
|
|
|
|
return nil, errors.New("Decoding source target failed")
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:44:21 -04:00
|
|
|
C.ec_encode_data(C.int(chunkLen), C.int(k), C.int(errCount-1), decodeTbls,
|
2014-11-29 17:58:40 -05:00
|
|
|
source, target)
|
2014-11-05 06:09:40 -05:00
|
|
|
|
2015-03-24 21:44:21 -04:00
|
|
|
recoveredOutput := make([]byte, 0, chunkLen*int(k))
|
2015-03-22 20:17:53 -04:00
|
|
|
for i := 0; i < int(k); i++ {
|
2015-03-24 21:44:21 -04:00
|
|
|
recoveredOutput = append(recoveredOutput, chunks[i]...)
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|
2014-11-27 22:43:54 -05:00
|
|
|
|
2015-03-12 05:00:36 -04:00
|
|
|
// TODO cache this if necessary
|
2015-03-24 21:44:21 -04:00
|
|
|
e.decodeMatrix = decodeMatrix
|
|
|
|
e.decodeTbls = decodeTbls
|
2014-11-27 22:43:54 -05:00
|
|
|
|
2015-03-24 21:44:21 -04:00
|
|
|
return recoveredOutput[:length], nil
|
2014-11-05 06:09:40 -05:00
|
|
|
}
|