Exemplo n.º 1
0
// Encode takes a data_block as input and produces base + parity chunks from it
// Original data block can then be recovered from any of base chunks
func (e *CauchyEncoder) Encode(data_block []byte) (chunks [][]byte, length int) {
	// save original block length
	original_length := len(data_block)

	// extract required parameters
	b := e.p.b
	n := e.p.n
	word_size := e.p.word_size
	packet_size := e.p.packet_size
	k := e.k
	m := e.m
	w := e.w
	bitmatrix := e.bitmatrix

	// data block length must to be multiple of base, word size, block size
	var multiplier int = b * word_size * packet_size

	// pad data block if necessary
	if r := original_length % multiplier; r != 0 {
		// sparse block of length r
		// TODO: implement PKCS7 padding
		s := make([]byte, multiplier-r)
		data_block = append(data_block, s...)
	}

	// calculate chunk length
	chunk_length := len(data_block) / int(b)

	// virtually split data_block into k slices
	// the last m slices are for coding chunks
	chunks = make([][]byte, n)
	// aux structure, used to pass chunks into codec
	pointers := make([]*byte, n)

	var i int
	for i = 0; i < b; i++ {
		chunks[i] = data_block[i*chunk_length : (i+1)*chunk_length]
		pointers[i] = &chunks[i][0]
	}
	// allocate space for coding blocks
	for i = b; i < n; i++ {
		chunks[i] = make([]byte, chunk_length)
		pointers[i] = &chunks[i][0]
	}

	// write coding c
	data_ptrs := (**C.char)(unsafe.Pointer(&pointers[:b][0]))
	coding_ptrs := (**C.char)(unsafe.Pointer(&pointers[b:][0]))
	C.jerasure_bitmatrix_encode(k, m, w, bitmatrix, data_ptrs, coding_ptrs, C.int(chunk_length), e.packet_size)
	return chunks, original_length
}
Exemplo n.º 2
0
// void jerasure_bitmatrix_encode(int k, int m, int w, int *bitmatrix,char **data_ptrs, char **coding_ptrs, int size, int packetsize);
func EncodeBitMatrix(k, m, w int, bitmatrix []int, data, coding [][]byte, stripesize, packetsize int) {
	cdata := make([]*C.char, len(data))
	for i := range cdata {
		cdata[i] = (*C.char)(unsafe.Pointer(&data[i][0]))
	}
	ccoding := make([]*C.char, len(coding))
	for i := range ccoding {
		ccoding[i] = (*C.char)(unsafe.Pointer(&coding[i][0]))
	}
	cints := make([]C.int, len(bitmatrix))
	for i := range bitmatrix {
		cints[i] = C.int(bitmatrix[i])
	}

	C.jerasure_bitmatrix_encode(C.int(k), C.int(m), C.int(w), (*C.int)(unsafe.Pointer(&cints[0])),
		(**C.char)(unsafe.Pointer(&cdata[0])), (**C.char)(unsafe.Pointer(&ccoding[0])), C.int(stripesize), C.int(packetsize))
}