ruby-libstorj 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.gitmodules +3 -0
- data/.rspec +1 -0
- data/Gemfile +23 -0
- data/Gemfile.lock +111 -0
- data/Guardfile +21 -0
- data/LICENSE +502 -0
- data/README.md +262 -0
- data/Rakefile +76 -0
- data/ext/libstorj/.gitignore +47 -0
- data/ext/libstorj/.travis.yml +27 -0
- data/ext/libstorj/Doxyfile +2427 -0
- data/ext/libstorj/LICENSE +502 -0
- data/ext/libstorj/Makefile.am +6 -0
- data/ext/libstorj/README.md +198 -0
- data/ext/libstorj/autogen.sh +3 -0
- data/ext/libstorj/configure.ac +64 -0
- data/ext/libstorj/depends/Makefile +153 -0
- data/ext/libstorj/depends/config.guess +1462 -0
- data/ext/libstorj/depends/config.sub +1823 -0
- data/ext/libstorj/depends/extract-osx-sdk.sh +33 -0
- data/ext/libstorj/depends/packages/cctools.mk +7 -0
- data/ext/libstorj/depends/packages/clang.mk +7 -0
- data/ext/libstorj/depends/packages/gmp.mk +23 -0
- data/ext/libstorj/depends/packages/gnutls.mk +25 -0
- data/ext/libstorj/depends/packages/json-c.mk +7 -0
- data/ext/libstorj/depends/packages/libcurl.mk +39 -0
- data/ext/libstorj/depends/packages/libmicrohttpd.mk +7 -0
- data/ext/libstorj/depends/packages/libuv.mk +7 -0
- data/ext/libstorj/depends/packages/nettle.mk +30 -0
- data/ext/libstorj/libstorj.pc.in +11 -0
- data/ext/libstorj/src/Makefile.am +23 -0
- data/ext/libstorj/src/bip39.c +233 -0
- data/ext/libstorj/src/bip39.h +64 -0
- data/ext/libstorj/src/bip39_english.h +2074 -0
- data/ext/libstorj/src/cli.c +1494 -0
- data/ext/libstorj/src/crypto.c +525 -0
- data/ext/libstorj/src/crypto.h +178 -0
- data/ext/libstorj/src/downloader.c +1923 -0
- data/ext/libstorj/src/downloader.h +163 -0
- data/ext/libstorj/src/http.c +688 -0
- data/ext/libstorj/src/http.h +175 -0
- data/ext/libstorj/src/rs.c +962 -0
- data/ext/libstorj/src/rs.h +99 -0
- data/ext/libstorj/src/storj.c +1523 -0
- data/ext/libstorj/src/storj.h +1014 -0
- data/ext/libstorj/src/uploader.c +2736 -0
- data/ext/libstorj/src/uploader.h +181 -0
- data/ext/libstorj/src/utils.c +336 -0
- data/ext/libstorj/src/utils.h +65 -0
- data/ext/libstorj/test/Makefile.am +27 -0
- data/ext/libstorj/test/mockbridge.c +260 -0
- data/ext/libstorj/test/mockbridge.json +687 -0
- data/ext/libstorj/test/mockbridgeinfo.json +1836 -0
- data/ext/libstorj/test/mockfarmer.c +358 -0
- data/ext/libstorj/test/storjtests.h +41 -0
- data/ext/libstorj/test/tests.c +1617 -0
- data/ext/libstorj/test/tests_rs.c +869 -0
- data/ext/ruby-libstorj/extconf.rb +8 -0
- data/ext/ruby-libstorj/ruby-libstorj.cc +17 -0
- data/lib/ruby-libstorj.rb +1 -0
- data/lib/ruby-libstorj/arg_forwarding_task.rb +58 -0
- data/lib/ruby-libstorj/env.rb +178 -0
- data/lib/ruby-libstorj/ext/bucket.rb +71 -0
- data/lib/ruby-libstorj/ext/create_bucket_request.rb +53 -0
- data/lib/ruby-libstorj/ext/curl_code.rb +139 -0
- data/lib/ruby-libstorj/ext/ext.rb +71 -0
- data/lib/ruby-libstorj/ext/file.rb +84 -0
- data/lib/ruby-libstorj/ext/get_bucket_request.rb +45 -0
- data/lib/ruby-libstorj/ext/json_request.rb +51 -0
- data/lib/ruby-libstorj/ext/list_files_request.rb +63 -0
- data/lib/ruby-libstorj/ext/types.rb +226 -0
- data/lib/ruby-libstorj/ext/upload_options.rb +38 -0
- data/lib/ruby-libstorj/libstorj.rb +22 -0
- data/lib/ruby-libstorj/mixins/storj.rb +27 -0
- data/lib/ruby-libstorj/struct.rb +42 -0
- data/ruby-libstorj.gemspec +57 -0
- data/spec/helpers/options.yml.example +22 -0
- data/spec/helpers/shared_rake_examples.rb +132 -0
- data/spec/helpers/storj_options.rb +96 -0
- data/spec/helpers/upload.data +3 -0
- data/spec/helpers/upload.data.sha256 +1 -0
- data/spec/libstorj_spec.rb +0 -0
- data/spec/ruby-libstorj/arg_forwarding_task_spec.rb +311 -0
- data/spec/ruby-libstorj/env_spec.rb +353 -0
- data/spec/ruby-libstorj/ext_spec.rb +75 -0
- data/spec/ruby-libstorj/json_request_spec.rb +13 -0
- data/spec/ruby-libstorj/libstorj_spec.rb +81 -0
- data/spec/ruby-libstorj/struct_spec.rb +64 -0
- data/spec/spec_helper.rb +113 -0
- metadata +136 -0
|
@@ -0,0 +1,869 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
* Copyright (c) 2016 janson
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
#include <assert.h>
|
|
25
|
+
#include <stdint.h>
|
|
26
|
+
#include <stdio.h>
|
|
27
|
+
#include <stdlib.h>
|
|
28
|
+
#include <string.h>
|
|
29
|
+
#include <sys/stat.h>
|
|
30
|
+
#include <sys/time.h>
|
|
31
|
+
#include <time.h>
|
|
32
|
+
#include <unistd.h>
|
|
33
|
+
|
|
34
|
+
#define PROFILE
|
|
35
|
+
#include "../src/rs.h"
|
|
36
|
+
#include "../src/rs.c"
|
|
37
|
+
|
|
38
|
+
void print_matrix1(gf* matrix, int nrows, int ncols);
|
|
39
|
+
void print_matrix2(gf** matrix, int nrows, int ncols);
|
|
40
|
+
|
|
41
|
+
void print_buf(gf* buf, char *fmt, size_t len) {
|
|
42
|
+
size_t i = 0;
|
|
43
|
+
while(i < len) {
|
|
44
|
+
printf(fmt, buf[i]);
|
|
45
|
+
i++;
|
|
46
|
+
if((i % 16) == 0) {
|
|
47
|
+
printf("\n");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
printf("\n");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
void test_galois(void) {
|
|
54
|
+
printf("%s:\n", __FUNCTION__);
|
|
55
|
+
|
|
56
|
+
//copy from golang rs version
|
|
57
|
+
assert(galMultiply(3, 4) == 12);
|
|
58
|
+
assert(galMultiply(7, 7) == 21);
|
|
59
|
+
assert(galMultiply(23, 45) == 41);
|
|
60
|
+
|
|
61
|
+
{
|
|
62
|
+
gf in[] = {0, 1, 2, 3, 4, 5, 6, 10, 50, 100, 150, 174, 201, 255, 99, 32, 67, 85};
|
|
63
|
+
gf out[sizeof(in)/sizeof(gf)] = {0};
|
|
64
|
+
gf expect[] = {0x0, 0x19, 0x32, 0x2b, 0x64, 0x7d, 0x56, 0xfa, 0xb8, 0x6d, 0xc7, 0x85, 0xc3, 0x1f, 0x22, 0x7, 0x25, 0xfe};
|
|
65
|
+
gf expect2[] = {0x0, 0xb1, 0x7f, 0xce, 0xfe, 0x4f, 0x81, 0x9e, 0x3, 0x6, 0xe8, 0x75, 0xbd, 0x40, 0x36, 0xa3, 0x95, 0xcb};
|
|
66
|
+
int rlt;
|
|
67
|
+
int sz = sizeof(int)/sizeof(gf);
|
|
68
|
+
addmul(out, in, 25, sz, sz, sz);
|
|
69
|
+
rlt = memcmp(out, expect, sizeof(int)/sizeof(gf));
|
|
70
|
+
assert(0 == rlt);
|
|
71
|
+
|
|
72
|
+
memset(out, 0, sizeof(in)/sizeof(gf));
|
|
73
|
+
sz = sizeof(in)/sizeof(gf);
|
|
74
|
+
addmul(out, in, 177, sz, sz, sz);
|
|
75
|
+
rlt = memcmp(out, expect2, sizeof(int)/sizeof(gf));
|
|
76
|
+
assert(0 == rlt);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
assert(galExp(2,2) == 4);
|
|
80
|
+
assert(galExp(5,20) == 235);
|
|
81
|
+
assert(galExp(13,7) == 43);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
void test_sub_matrix(void) {
|
|
85
|
+
int r, c, ptr, nrows = 10, ncols = 20;
|
|
86
|
+
gf* m1 = (gf*)RS_MALLOC(nrows * ncols);
|
|
87
|
+
gf *test1;
|
|
88
|
+
|
|
89
|
+
printf("%s:\n", __FUNCTION__);
|
|
90
|
+
|
|
91
|
+
ptr = 0;
|
|
92
|
+
for(r = 0; r < nrows; r++) {
|
|
93
|
+
for(c = 0; c < ncols; c++) {
|
|
94
|
+
m1[ptr] = ptr;
|
|
95
|
+
ptr++;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
test1 = sub_matrix(m1, 0, 0, 3, 4, nrows, ncols);
|
|
99
|
+
for(r = 0; r < 3; r++) {
|
|
100
|
+
for(c = 0; c < 4; c++) {
|
|
101
|
+
assert(test1[r*4 + c] == (r*ncols + c));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
free(test1);
|
|
105
|
+
|
|
106
|
+
test1 = sub_matrix(m1, 3, 2, 7, 9, nrows, ncols);
|
|
107
|
+
for(r = 0; r < (7-3); r++) {
|
|
108
|
+
for(c = 0; c < (9-2); c++) {
|
|
109
|
+
assert(test1[r*(9-2) + c] == ((r+3)*ncols + (c+2)));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
free(m1);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
void test_multiply(void) {
|
|
117
|
+
gf a[] = {1,2,3,4};
|
|
118
|
+
gf b[] = {5,6,7,8};
|
|
119
|
+
gf exp[] = {11,22,19,42};
|
|
120
|
+
gf *out;
|
|
121
|
+
int rlt;
|
|
122
|
+
|
|
123
|
+
printf("%s:\n", __FUNCTION__);
|
|
124
|
+
|
|
125
|
+
out = multiply1(a, 2, 2, b, 2, 2);
|
|
126
|
+
rlt = memcmp(out, exp, 4);
|
|
127
|
+
assert(0 == rlt);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
void test_inverse(void) {
|
|
131
|
+
printf("%s:\n", __FUNCTION__);
|
|
132
|
+
{
|
|
133
|
+
gf a[] = {56, 23, 98, 3, 100, 200, 45, 201, 123};
|
|
134
|
+
gf ae[] = {175, 133, 33, 130, 13, 245, 112, 35, 126};
|
|
135
|
+
int rlt = invert_mat(a, 3);
|
|
136
|
+
assert(0 == rlt);
|
|
137
|
+
rlt = memcmp(a, ae, 3*3);
|
|
138
|
+
assert(0 == rlt);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
{
|
|
142
|
+
gf a[] = { 1, 0, 0, 0, 0,
|
|
143
|
+
0, 1, 0, 0, 0,
|
|
144
|
+
0, 0, 0, 1, 0,
|
|
145
|
+
0, 0, 0, 0, 1,
|
|
146
|
+
7, 7, 6, 6, 1};
|
|
147
|
+
gf ae[] = {1, 0, 0, 0, 0,
|
|
148
|
+
0, 1, 0, 0, 0,
|
|
149
|
+
123, 123, 1, 122, 122,
|
|
150
|
+
0, 0, 1, 0, 0,
|
|
151
|
+
0, 0, 0, 1, 0};
|
|
152
|
+
int rlt = invert_mat(a, 5);
|
|
153
|
+
assert(0 == rlt);
|
|
154
|
+
rlt = memcmp(a, ae, 5*5);
|
|
155
|
+
assert(0 == rlt);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
{
|
|
159
|
+
/* error matrix */
|
|
160
|
+
gf a[] = {4,2,12,6};
|
|
161
|
+
int rlt = invert_mat(a, 2);
|
|
162
|
+
assert(0 != rlt);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
uint8_t* test_create_random(reed_solomon *rs, int data_size, int block_size) {
|
|
167
|
+
struct timeval tv;
|
|
168
|
+
uint8_t* data;
|
|
169
|
+
int i, n, seed, nr_blocks;
|
|
170
|
+
|
|
171
|
+
gettimeofday(&tv, 0);
|
|
172
|
+
seed = tv.tv_sec ^ tv.tv_usec;
|
|
173
|
+
srand(seed);
|
|
174
|
+
|
|
175
|
+
nr_blocks = (data_size+block_size-1)/block_size;
|
|
176
|
+
nr_blocks = ((nr_blocks + rs->data_shards - 1)/ rs->data_shards) * rs->data_shards;
|
|
177
|
+
n = nr_blocks / rs->data_shards;
|
|
178
|
+
nr_blocks += n * rs->parity_shards;
|
|
179
|
+
|
|
180
|
+
data = malloc(nr_blocks * block_size);
|
|
181
|
+
for(i = 0; i < data_size; i++) {
|
|
182
|
+
data[i] = (uint8_t)(rand() % 255);
|
|
183
|
+
}
|
|
184
|
+
memset(data + data_size, 0, nr_blocks*block_size - data_size);
|
|
185
|
+
|
|
186
|
+
return data;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
int test_create_encoding(
|
|
190
|
+
reed_solomon *rs,
|
|
191
|
+
uint8_t *data,
|
|
192
|
+
int data_size,
|
|
193
|
+
int block_size
|
|
194
|
+
) {
|
|
195
|
+
uint8_t **data_blocks;
|
|
196
|
+
uint8_t **fec_blocks;
|
|
197
|
+
int data_shards, parity_shards;
|
|
198
|
+
int i, n, nr_shards, nr_blocks, nr_fec_blocks;
|
|
199
|
+
|
|
200
|
+
data_shards = rs->data_shards;
|
|
201
|
+
parity_shards = rs->parity_shards;
|
|
202
|
+
nr_blocks = (data_size+block_size-1)/block_size;
|
|
203
|
+
nr_blocks = ((nr_blocks+data_shards-1)/data_shards) * data_shards;
|
|
204
|
+
n = nr_blocks / data_shards;
|
|
205
|
+
nr_fec_blocks = n * parity_shards;
|
|
206
|
+
nr_shards = nr_blocks + nr_fec_blocks;
|
|
207
|
+
|
|
208
|
+
data_blocks = (uint8_t**)malloc(nr_blocks * sizeof(uint8_t*));
|
|
209
|
+
for(i = 0; i < nr_blocks; i++) {
|
|
210
|
+
data_blocks[i] = data + i*block_size;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
fec_blocks = (uint8_t**)malloc(nr_fec_blocks * sizeof(uint8_t*));
|
|
214
|
+
for(i = 0; i < nr_fec_blocks; i++) {
|
|
215
|
+
fec_blocks[i] = data + (nr_blocks + i)*block_size;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
n = reed_solomon_encode2(rs, data_blocks, fec_blocks, nr_shards,
|
|
219
|
+
block_size, data_size);
|
|
220
|
+
free(data_blocks);
|
|
221
|
+
free(fec_blocks);
|
|
222
|
+
|
|
223
|
+
return n;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
int test_data_decode(
|
|
227
|
+
reed_solomon *rs,
|
|
228
|
+
uint8_t *data,
|
|
229
|
+
int data_size,
|
|
230
|
+
int block_size,
|
|
231
|
+
int *erases,
|
|
232
|
+
int erase_count) {
|
|
233
|
+
uint8_t **data_blocks;
|
|
234
|
+
uint8_t **fec_blocks;
|
|
235
|
+
uint8_t *zilch;
|
|
236
|
+
int data_shards, parity_shards;
|
|
237
|
+
int i, j, n, nr_shards, nr_blocks, nr_fec_blocks;
|
|
238
|
+
|
|
239
|
+
data_shards = rs->data_shards;
|
|
240
|
+
parity_shards = rs->parity_shards;
|
|
241
|
+
nr_blocks = (data_size+block_size-1)/block_size;
|
|
242
|
+
nr_blocks = ((nr_blocks+data_shards-1)/data_shards) * data_shards;
|
|
243
|
+
n = nr_blocks / data_shards;
|
|
244
|
+
nr_fec_blocks = n * parity_shards;
|
|
245
|
+
nr_shards = nr_blocks + nr_fec_blocks;
|
|
246
|
+
|
|
247
|
+
data_blocks = (uint8_t**)malloc(nr_blocks * sizeof(uint8_t*));
|
|
248
|
+
for(i = 0; i < nr_blocks; i++) {
|
|
249
|
+
data_blocks[i] = data + i*block_size;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
fec_blocks = (uint8_t**)malloc(nr_fec_blocks * sizeof(uint8_t*));
|
|
253
|
+
for(i = 0; i < nr_fec_blocks; i++) {
|
|
254
|
+
fec_blocks[i] = data + (nr_blocks + i)*block_size;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
zilch = (uint8_t*)calloc(1, nr_shards);
|
|
258
|
+
for(i = 0; i < erase_count; i++) {
|
|
259
|
+
j = erases[i];
|
|
260
|
+
memset(data + j*block_size, 137, block_size);
|
|
261
|
+
zilch[j] = 1; //mark as erased
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
n = reed_solomon_reconstruct(rs, data_blocks, fec_blocks, zilch,
|
|
265
|
+
nr_shards, block_size, data_size);
|
|
266
|
+
free(data_blocks);
|
|
267
|
+
free(zilch);
|
|
268
|
+
|
|
269
|
+
return n;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
void test_one_encoding(void) {
|
|
273
|
+
reed_solomon *rs;
|
|
274
|
+
uint8_t* data;
|
|
275
|
+
int block_size = 50000;
|
|
276
|
+
int data_size = 10*block_size;
|
|
277
|
+
int err;
|
|
278
|
+
|
|
279
|
+
printf("%s:\n", __FUNCTION__);
|
|
280
|
+
|
|
281
|
+
rs = reed_solomon_new(10, 3);
|
|
282
|
+
data = test_create_random(rs, data_size, block_size);
|
|
283
|
+
err = test_create_encoding(rs, data, data_size, block_size);
|
|
284
|
+
|
|
285
|
+
free(data);
|
|
286
|
+
reed_solomon_release(rs);
|
|
287
|
+
|
|
288
|
+
assert(0 == err);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
int test_one_decoding_13(int *erases, int erase_count) {
|
|
292
|
+
reed_solomon *rs;
|
|
293
|
+
uint8_t *data, *origin;
|
|
294
|
+
int block_size = 50000;
|
|
295
|
+
int data_size = 10*block_size;
|
|
296
|
+
int err, err2;
|
|
297
|
+
|
|
298
|
+
rs = reed_solomon_new(10, 3);
|
|
299
|
+
data = test_create_random(rs, data_size, block_size);
|
|
300
|
+
err = test_create_encoding(rs, data, data_size, block_size);
|
|
301
|
+
assert(0 == err);
|
|
302
|
+
|
|
303
|
+
origin = (uint8_t*)malloc(data_size);
|
|
304
|
+
memcpy(origin, data, data_size);
|
|
305
|
+
|
|
306
|
+
err = test_data_decode(rs, data, data_size, block_size, erases, erase_count);
|
|
307
|
+
if(0 == err) {
|
|
308
|
+
err2 = memcmp(origin, data, data_size);
|
|
309
|
+
assert(0 == err2);
|
|
310
|
+
} else {
|
|
311
|
+
//failed here
|
|
312
|
+
err2 = memcmp(origin, data, data_size);
|
|
313
|
+
assert(0 != err2);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
free(data);
|
|
317
|
+
free(origin);
|
|
318
|
+
reed_solomon_release(rs);
|
|
319
|
+
|
|
320
|
+
return err;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
void test_one_decoding(void) {
|
|
324
|
+
printf("%s:\n", __FUNCTION__);
|
|
325
|
+
|
|
326
|
+
{
|
|
327
|
+
int erases[] = {0};
|
|
328
|
+
int err;
|
|
329
|
+
|
|
330
|
+
// lost nothing
|
|
331
|
+
err = test_one_decoding_13(erases, 0);
|
|
332
|
+
assert(0 == err);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
{
|
|
336
|
+
int erases[] = {0};
|
|
337
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
338
|
+
int err;
|
|
339
|
+
|
|
340
|
+
// lost only one
|
|
341
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
342
|
+
assert(0 == err);
|
|
343
|
+
|
|
344
|
+
erases[0] = 5;
|
|
345
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
346
|
+
assert(0 == err);
|
|
347
|
+
|
|
348
|
+
erases[0] = 9;
|
|
349
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
350
|
+
assert(0 == err);
|
|
351
|
+
|
|
352
|
+
erases[0] = 11;
|
|
353
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
354
|
+
assert(0 == err);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
{
|
|
358
|
+
int erases[] = {0, 1};
|
|
359
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
360
|
+
int err;
|
|
361
|
+
|
|
362
|
+
// lost two
|
|
363
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
364
|
+
assert(0 == err);
|
|
365
|
+
|
|
366
|
+
erases[0] = 3;
|
|
367
|
+
erases[1] = 7;
|
|
368
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
369
|
+
assert(0 == err);
|
|
370
|
+
|
|
371
|
+
erases[0] = 11;
|
|
372
|
+
erases[1] = 9;
|
|
373
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
374
|
+
assert(0 == err);
|
|
375
|
+
|
|
376
|
+
erases[0] = 11;
|
|
377
|
+
erases[1] = 12;
|
|
378
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
379
|
+
assert(0 == err);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
{
|
|
383
|
+
int erases[] = {0, 1, 4};
|
|
384
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
385
|
+
int err;
|
|
386
|
+
|
|
387
|
+
// lost three
|
|
388
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
389
|
+
assert(0 == err);
|
|
390
|
+
|
|
391
|
+
erases[0] = 3;
|
|
392
|
+
erases[1] = 8;
|
|
393
|
+
erases[2] = 7;
|
|
394
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
395
|
+
assert(0 == err);
|
|
396
|
+
|
|
397
|
+
erases[0] = 11;
|
|
398
|
+
erases[1] = 9;
|
|
399
|
+
erases[2] = 1;
|
|
400
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
401
|
+
assert(0 == err);
|
|
402
|
+
|
|
403
|
+
erases[0] = 11;
|
|
404
|
+
erases[1] = 12;
|
|
405
|
+
erases[2] = 9;
|
|
406
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
407
|
+
assert(0 == err);
|
|
408
|
+
|
|
409
|
+
erases[0] = 11;
|
|
410
|
+
erases[1] = 12;
|
|
411
|
+
erases[2] = 9;
|
|
412
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
413
|
+
assert(0 == err);
|
|
414
|
+
|
|
415
|
+
erases[0] = 11;
|
|
416
|
+
erases[1] = 12;
|
|
417
|
+
erases[2] = 10;
|
|
418
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
419
|
+
assert(0 == err);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
{
|
|
423
|
+
int erases[] = {0, 1, 4, 8};
|
|
424
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
425
|
+
int err;
|
|
426
|
+
|
|
427
|
+
// lost 4, failed!
|
|
428
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
429
|
+
assert(0 != err);
|
|
430
|
+
|
|
431
|
+
erases[0] = 11;
|
|
432
|
+
erases[1] = 12;
|
|
433
|
+
erases[2] = 10;
|
|
434
|
+
erases[3] = 9;
|
|
435
|
+
err = test_one_decoding_13(erases, erases_count);
|
|
436
|
+
assert(0 != err);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
int test_one_decoding_13_6(int *erases, int erase_count) {
|
|
441
|
+
reed_solomon *rs;
|
|
442
|
+
uint8_t *data, *origin;
|
|
443
|
+
int block_size = 50000;
|
|
444
|
+
int data_size = 10*block_size*6;
|
|
445
|
+
int err, err2;
|
|
446
|
+
|
|
447
|
+
rs = reed_solomon_new(10, 3);
|
|
448
|
+
data = test_create_random(rs, data_size, block_size);
|
|
449
|
+
err = test_create_encoding(rs, data, data_size, block_size);
|
|
450
|
+
assert(0 == err);
|
|
451
|
+
|
|
452
|
+
origin = (uint8_t*)malloc(data_size);
|
|
453
|
+
memcpy(origin, data, data_size);
|
|
454
|
+
|
|
455
|
+
err = test_data_decode(rs, data, data_size, block_size, erases, erase_count);
|
|
456
|
+
if(0 == err) {
|
|
457
|
+
err2 = memcmp(origin, data, data_size);
|
|
458
|
+
assert(0 == err2);
|
|
459
|
+
} else {
|
|
460
|
+
//failed here
|
|
461
|
+
err2 = memcmp(origin, data, data_size);
|
|
462
|
+
assert(0 != err2);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
free(data);
|
|
466
|
+
free(origin);
|
|
467
|
+
reed_solomon_release(rs);
|
|
468
|
+
|
|
469
|
+
return err;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
void test_encoding(void) {
|
|
473
|
+
reed_solomon *rs;
|
|
474
|
+
uint8_t *data;
|
|
475
|
+
int block_size = 50000;
|
|
476
|
+
//multi shards encoding
|
|
477
|
+
int data_size = 13*block_size*6;
|
|
478
|
+
int err;
|
|
479
|
+
|
|
480
|
+
printf("%s:\n", __FUNCTION__);
|
|
481
|
+
|
|
482
|
+
rs = reed_solomon_new(10, 3);
|
|
483
|
+
data = test_create_random(rs, data_size, block_size);
|
|
484
|
+
err = test_create_encoding(rs, data, data_size, block_size);
|
|
485
|
+
|
|
486
|
+
free(data);
|
|
487
|
+
reed_solomon_release(rs);
|
|
488
|
+
|
|
489
|
+
assert(0 == err);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
void test_reconstruct(void) {
|
|
493
|
+
#define FEC_START (10*6)
|
|
494
|
+
printf("%s:\n", __FUNCTION__);
|
|
495
|
+
|
|
496
|
+
{
|
|
497
|
+
int erases[] = {0};
|
|
498
|
+
int err;
|
|
499
|
+
|
|
500
|
+
// lost nothing
|
|
501
|
+
err = test_one_decoding_13_6(erases, 0);
|
|
502
|
+
assert(0 == err);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
{
|
|
506
|
+
int erases[] = {0, 1, 9, 10+2, 10+4, 10+9};
|
|
507
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
508
|
+
int err;
|
|
509
|
+
|
|
510
|
+
// shard1 shard2 both lost three
|
|
511
|
+
err = test_one_decoding_13_6(erases, erases_count);
|
|
512
|
+
assert(0 == err);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
{
|
|
516
|
+
int erases[] = {0, 9, FEC_START + 1, 10+2, 10+9, FEC_START + 3 + 2};
|
|
517
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
518
|
+
int err;
|
|
519
|
+
|
|
520
|
+
// shard1 shard2 both lost three, and both lost one in fec
|
|
521
|
+
err = test_one_decoding_13_6(erases, erases_count);
|
|
522
|
+
assert(0 == err);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
{
|
|
526
|
+
int erases[] = {11, 12, 10, 9};
|
|
527
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
528
|
+
int err;
|
|
529
|
+
|
|
530
|
+
/* this is ok. not lost 4 but shard1 lost 1, shard2 lost 3, we can reconstruct it */
|
|
531
|
+
err = test_one_decoding_13_6(erases, erases_count);
|
|
532
|
+
assert(0 == err);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
{
|
|
536
|
+
int erases[] = {0, 1, 4, 8};
|
|
537
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
538
|
+
int err;
|
|
539
|
+
|
|
540
|
+
// shard1 lost 4, failed!
|
|
541
|
+
err = test_one_decoding_13_6(erases, erases_count);
|
|
542
|
+
assert(0 != err);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
{
|
|
546
|
+
int erases[] = {10, 11, 14, 18};
|
|
547
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
548
|
+
int err;
|
|
549
|
+
|
|
550
|
+
// shard2 lost 4, failed!
|
|
551
|
+
err = test_one_decoding_13_6(erases, erases_count);
|
|
552
|
+
assert(0 != err);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
{
|
|
556
|
+
int erases[] = {0, 1, 4, 8, 10, 11, 14, 18};
|
|
557
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
558
|
+
int err;
|
|
559
|
+
|
|
560
|
+
// shard1 and shard2 both lost 4, failed!
|
|
561
|
+
err = test_one_decoding_13_6(erases, erases_count);
|
|
562
|
+
assert(0 != err);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
{
|
|
566
|
+
int erases[] = {11, 12, 10, 9, FEC_START+3+0, FEC_START+3+1, FEC_START+3+2};
|
|
567
|
+
int erases_count = sizeof(erases)/sizeof(int);
|
|
568
|
+
int err;
|
|
569
|
+
|
|
570
|
+
err = test_one_decoding_13_6(erases, erases_count);
|
|
571
|
+
assert(0 != err);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
double benchmarkEncodeTest(int n, int dataShards, int parityShards, int shardSize) {
|
|
576
|
+
clock_t start, end;
|
|
577
|
+
double millis;
|
|
578
|
+
uint8_t* data;
|
|
579
|
+
int i;
|
|
580
|
+
int dataSize = shardSize*dataShards;
|
|
581
|
+
reed_solomon* rs = reed_solomon_new(dataShards, parityShards);
|
|
582
|
+
|
|
583
|
+
data = test_create_random(rs, dataSize, shardSize);
|
|
584
|
+
|
|
585
|
+
start = clock();
|
|
586
|
+
for(i = 0; i < n; i++) {
|
|
587
|
+
test_create_encoding(rs, data, dataSize, shardSize);
|
|
588
|
+
}
|
|
589
|
+
end = clock();
|
|
590
|
+
millis = (double)(end - start) * 1000.0 / CLOCKS_PER_SEC;
|
|
591
|
+
return (millis);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/* TODO please check, is this benchmark ok? */
|
|
595
|
+
void benchmarkEncode(void) {
|
|
596
|
+
double millis;
|
|
597
|
+
double per_sec_in_bytes;
|
|
598
|
+
double MB = 1024.0 * 1024.0;
|
|
599
|
+
double millis_to_sec = 1000*1000;
|
|
600
|
+
int n;
|
|
601
|
+
int size;
|
|
602
|
+
|
|
603
|
+
printf("%s:\n", __FUNCTION__);
|
|
604
|
+
|
|
605
|
+
n = 20000;
|
|
606
|
+
size = 10000;
|
|
607
|
+
millis = benchmarkEncodeTest(n, 10, 2, size);
|
|
608
|
+
|
|
609
|
+
per_sec_in_bytes = (10*2*size/MB) * millis_to_sec * n / millis;
|
|
610
|
+
printf("10x2x10000, test_count=%d millis=%lf per_sec_in_bytes=%lfMB/s\n", n, millis, per_sec_in_bytes);
|
|
611
|
+
|
|
612
|
+
n = 200;
|
|
613
|
+
millis = benchmarkEncodeTest(n, 100, 20, size);
|
|
614
|
+
per_sec_in_bytes = (100*20*size/MB) * millis_to_sec * n / millis;
|
|
615
|
+
printf("100x20x10000, test_count=%d millis=%lf per_sec_in_bytes=%lfMB/s\n", n, millis, per_sec_in_bytes);
|
|
616
|
+
|
|
617
|
+
n = 200;
|
|
618
|
+
size = 1024*1024;
|
|
619
|
+
millis = benchmarkEncodeTest(n, 17, 3, size);
|
|
620
|
+
per_sec_in_bytes = (17*3*size/MB) * millis_to_sec * n / millis;
|
|
621
|
+
printf("17x3x(1024*1024), test_count=%d millis=%lf per_sec_in_bytes=%lfMB/s\n", n, millis, per_sec_in_bytes);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
void test_001(void) {
|
|
625
|
+
reed_solomon* rs = reed_solomon_new(11, 6);
|
|
626
|
+
print_matrix1(rs->m, rs->data_shards, rs->data_shards);
|
|
627
|
+
print_matrix1(rs->parity, rs->parity_shards, rs->data_shards);
|
|
628
|
+
reed_solomon_release(rs);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
void test_002(void) {
|
|
632
|
+
char text[] = "hello world", output[256];
|
|
633
|
+
int data_size = strlen(text);
|
|
634
|
+
int block_size = 1;
|
|
635
|
+
int nrDataBlocks = sizeof(text)/sizeof(char) - 1;
|
|
636
|
+
uint8_t* data_blocks[128];
|
|
637
|
+
uint8_t* fec_blocks[128];
|
|
638
|
+
int nrFecBlocks = 6;
|
|
639
|
+
|
|
640
|
+
//decode
|
|
641
|
+
unsigned int fec_block_nos[128], erased_blocks[128];
|
|
642
|
+
uint8_t* dec_fec_blocks[128];
|
|
643
|
+
int nr_fec_blocks;
|
|
644
|
+
|
|
645
|
+
int i;
|
|
646
|
+
reed_solomon* rs = reed_solomon_new(nrDataBlocks, nrFecBlocks);
|
|
647
|
+
|
|
648
|
+
printf("%s:\n", __FUNCTION__);
|
|
649
|
+
|
|
650
|
+
for(i = 0; i < nrDataBlocks; i++) {
|
|
651
|
+
data_blocks[i] = (uint8_t*)&text[i];
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
memset(output, 0, sizeof(output));
|
|
655
|
+
memcpy(output, text, nrDataBlocks);
|
|
656
|
+
for(i = 0; i < nrFecBlocks; i++) {
|
|
657
|
+
fec_blocks[i] = (uint8_t*)&output[i + nrDataBlocks];
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
reed_solomon_encode(rs, data_blocks, fec_blocks, block_size, data_size);
|
|
661
|
+
print_buf((gf*)output, "%d ", nrFecBlocks+nrDataBlocks);
|
|
662
|
+
|
|
663
|
+
text[1] = 'x';
|
|
664
|
+
text[3] = 'y';
|
|
665
|
+
text[4] = 'z';
|
|
666
|
+
erased_blocks[0] = 4;
|
|
667
|
+
erased_blocks[1] = 1;
|
|
668
|
+
erased_blocks[2] = 3;
|
|
669
|
+
|
|
670
|
+
fec_block_nos[0] = 1;
|
|
671
|
+
fec_block_nos[1] = 3;
|
|
672
|
+
fec_block_nos[2] = 5;
|
|
673
|
+
dec_fec_blocks[0] = fec_blocks[1];
|
|
674
|
+
dec_fec_blocks[1] = fec_blocks[3];
|
|
675
|
+
dec_fec_blocks[2] = fec_blocks[5];
|
|
676
|
+
nr_fec_blocks = 3;
|
|
677
|
+
|
|
678
|
+
printf("erased:%s\n", text);
|
|
679
|
+
|
|
680
|
+
reed_solomon_decode(rs, data_blocks, block_size, dec_fec_blocks,
|
|
681
|
+
fec_block_nos, erased_blocks, nr_fec_blocks, data_size);
|
|
682
|
+
|
|
683
|
+
printf("fixed:%s\n", text);
|
|
684
|
+
|
|
685
|
+
reed_solomon_release(rs);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
void test_003(void) {
|
|
689
|
+
char text[] = "hello world hello world ", output[256];
|
|
690
|
+
int data_size = strlen(text);
|
|
691
|
+
int block_size = 2;
|
|
692
|
+
int nrDataBlocks = (sizeof(text)/sizeof(char) - 1) / block_size;
|
|
693
|
+
uint8_t* data_blocks[128];
|
|
694
|
+
uint8_t* fec_blocks[128];
|
|
695
|
+
int nrFecBlocks = 6;
|
|
696
|
+
|
|
697
|
+
//decode
|
|
698
|
+
unsigned int fec_block_nos[128], erased_blocks[128];
|
|
699
|
+
uint8_t* dec_fec_blocks[128];
|
|
700
|
+
int nr_fec_blocks;
|
|
701
|
+
|
|
702
|
+
int i;
|
|
703
|
+
reed_solomon* rs = reed_solomon_new(nrDataBlocks, nrFecBlocks);
|
|
704
|
+
|
|
705
|
+
printf("%s:\n", __FUNCTION__);
|
|
706
|
+
//printf("text size=%d\n", (int)(sizeof(text)/sizeof(char) - 1) );
|
|
707
|
+
|
|
708
|
+
for(i = 0; i < nrDataBlocks; i++) {
|
|
709
|
+
data_blocks[i] = (uint8_t*)&text[i*block_size];
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
memset(output, 0, sizeof(output));
|
|
713
|
+
memcpy(output, text, nrDataBlocks*block_size);
|
|
714
|
+
//print_matrix1((gf*)output, nrDataBlocks + nrFecBlocks, block_size);
|
|
715
|
+
for(i = 0; i < nrFecBlocks; i++) {
|
|
716
|
+
fec_blocks[i] = (uint8_t*)&output[i*block_size + nrDataBlocks*block_size];
|
|
717
|
+
}
|
|
718
|
+
reed_solomon_encode(rs, data_blocks, fec_blocks, block_size, data_size);
|
|
719
|
+
printf("golang output(example/test_rs.go):\n [[104 101] [108 108] [111 32] [119 111] [114 108] [100 32] [104 101] [108 108] [111 32] [119 111] [114 108] [100 32] \n[157 178] [83 31] [48 240] [254 93] [31 89] [151 184]]\n");
|
|
720
|
+
printf("c verion output:\n");
|
|
721
|
+
print_buf((gf*)output, "%d ", nrFecBlocks*block_size + nrDataBlocks*block_size);
|
|
722
|
+
//print_matrix1((gf*)output, nrDataBlocks + nrFecBlocks, block_size);
|
|
723
|
+
|
|
724
|
+
//decode
|
|
725
|
+
text[1*block_size] = 'x';
|
|
726
|
+
text[10*block_size+1] = 'y';
|
|
727
|
+
text[4*block_size] = 'z';
|
|
728
|
+
erased_blocks[0] = 4;
|
|
729
|
+
erased_blocks[1] = 1;
|
|
730
|
+
erased_blocks[2] = 10;
|
|
731
|
+
|
|
732
|
+
fec_block_nos[0] = 1;
|
|
733
|
+
fec_block_nos[1] = 3;
|
|
734
|
+
fec_block_nos[2] = 5;
|
|
735
|
+
dec_fec_blocks[0] = fec_blocks[1];
|
|
736
|
+
dec_fec_blocks[1] = fec_blocks[3];
|
|
737
|
+
dec_fec_blocks[2] = fec_blocks[5];
|
|
738
|
+
nr_fec_blocks = 3;
|
|
739
|
+
|
|
740
|
+
printf("erased:%s\n", text);
|
|
741
|
+
|
|
742
|
+
reed_solomon_decode(rs, data_blocks, block_size, dec_fec_blocks,
|
|
743
|
+
fec_block_nos, erased_blocks, nr_fec_blocks, data_size);
|
|
744
|
+
|
|
745
|
+
printf("fixed:%s\n", text);
|
|
746
|
+
|
|
747
|
+
reed_solomon_release(rs);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
void test_004(void) {
|
|
751
|
+
//char text[] = "hello world hello world ";
|
|
752
|
+
int dataShards = 30;
|
|
753
|
+
int parityShards = 21;
|
|
754
|
+
int blockSize = 280;
|
|
755
|
+
struct timeval tv;
|
|
756
|
+
int i, j, n, seed, size, nrShards, nrBlocks, nrFecBlocks;
|
|
757
|
+
uint8_t *origin, *data;
|
|
758
|
+
uint8_t **data_blocks;
|
|
759
|
+
uint8_t **fec_blocks;
|
|
760
|
+
uint8_t *zilch;
|
|
761
|
+
reed_solomon *rs;
|
|
762
|
+
|
|
763
|
+
gettimeofday(&tv, 0);
|
|
764
|
+
seed = tv.tv_sec ^ tv.tv_usec;
|
|
765
|
+
srand(seed);
|
|
766
|
+
|
|
767
|
+
fec_init();
|
|
768
|
+
|
|
769
|
+
//size = sizeof(text)/sizeof(char)-1;
|
|
770
|
+
size = 1024*1024;
|
|
771
|
+
origin = malloc(size);
|
|
772
|
+
//memcpy(origin, text, size);
|
|
773
|
+
for(i = 0; i < size; i++) {
|
|
774
|
+
origin[i] = (uint8_t)(rand() % 255);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
nrBlocks = (size+blockSize-1) / blockSize;
|
|
778
|
+
nrBlocks = ((nrBlocks+dataShards-1)/dataShards) * dataShards;
|
|
779
|
+
n = nrBlocks / dataShards;
|
|
780
|
+
nrFecBlocks = n*parityShards;
|
|
781
|
+
nrShards = nrBlocks + nrFecBlocks;
|
|
782
|
+
int data_size = nrShards * blockSize;
|
|
783
|
+
data = malloc(data_size);
|
|
784
|
+
memcpy(data, origin, size);
|
|
785
|
+
memset(data + size, 0, nrShards*blockSize - size);
|
|
786
|
+
printf("nrBlocks=%d nrFecBlocks=%d nrShards=%d n=%d left=%d\n", nrBlocks, nrFecBlocks, nrShards, n, nrShards*blockSize - size);
|
|
787
|
+
//print_buf(origin, "%d ", size);
|
|
788
|
+
//print_buf(data, "%d ", nrShards*blockSize);
|
|
789
|
+
|
|
790
|
+
data_blocks = (uint8_t**)malloc(nrBlocks * sizeof(uint8_t**));
|
|
791
|
+
for(i = 0; i < nrBlocks; i++) {
|
|
792
|
+
data_blocks[i] = data + i*blockSize;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
fec_blocks = (uint8_t**)malloc(nrFecBlocks * sizeof(uint8_t*));
|
|
796
|
+
for(i = 0; i < nrFecBlocks; i++) {
|
|
797
|
+
fec_blocks[i] = data + (nrBlocks + i)*blockSize;
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
rs = reed_solomon_new(dataShards, parityShards);
|
|
801
|
+
reed_solomon_encode2(rs, data_blocks, fec_blocks, nrShards, blockSize,
|
|
802
|
+
data_size);
|
|
803
|
+
i = memcmp(origin, data, size);
|
|
804
|
+
assert(0 == i);
|
|
805
|
+
//print_matrix2(data_blocks, nrShards, blockSize);
|
|
806
|
+
|
|
807
|
+
zilch = (uint8_t*)calloc(1, nrShards);
|
|
808
|
+
n = parityShards;
|
|
809
|
+
|
|
810
|
+
/* int es[100];
|
|
811
|
+
es[0] = 3;
|
|
812
|
+
es[1] = 3;
|
|
813
|
+
es[2] = 2;
|
|
814
|
+
es[3] = 8; */
|
|
815
|
+
|
|
816
|
+
for(i = 0; i < n-2; i++) {
|
|
817
|
+
j = rand() % (nrBlocks-1);
|
|
818
|
+
//j = es[i];
|
|
819
|
+
memset(data + j*blockSize, 137, blockSize);
|
|
820
|
+
zilch[j] = 1; //erased!
|
|
821
|
+
printf("erased %d\n", j);
|
|
822
|
+
}
|
|
823
|
+
if(nrFecBlocks > 2) {
|
|
824
|
+
for(i = 0; i < 2; i++) {
|
|
825
|
+
j = nrBlocks + (rand() % nrFecBlocks);
|
|
826
|
+
memset(data + j*blockSize, 139, blockSize);
|
|
827
|
+
zilch[j] = 1;
|
|
828
|
+
printf("erased %d\n", j);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
reed_solomon_reconstruct(rs, data_blocks, fec_blocks, zilch,
|
|
833
|
+
nrShards, blockSize, data_size);
|
|
834
|
+
i = memcmp(origin, data, size);
|
|
835
|
+
//print_buf(origin, "%d ", nrBlocks);
|
|
836
|
+
//print_buf(data, "%d ", nrBlocks);
|
|
837
|
+
printf("rlt=%d\n", i);
|
|
838
|
+
assert(0 == i);
|
|
839
|
+
|
|
840
|
+
free(origin);
|
|
841
|
+
free(data);
|
|
842
|
+
free(data_blocks);
|
|
843
|
+
free(fec_blocks);
|
|
844
|
+
free(zilch);
|
|
845
|
+
reed_solomon_release(rs);
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
int main(void) {
|
|
849
|
+
fec_init();
|
|
850
|
+
|
|
851
|
+
test_galois();
|
|
852
|
+
test_sub_matrix();
|
|
853
|
+
test_multiply();
|
|
854
|
+
test_inverse();
|
|
855
|
+
test_one_encoding();
|
|
856
|
+
test_one_decoding();
|
|
857
|
+
test_encoding();
|
|
858
|
+
test_reconstruct();
|
|
859
|
+
printf("reach here means test all ok\n");
|
|
860
|
+
|
|
861
|
+
benchmarkEncode();
|
|
862
|
+
|
|
863
|
+
//test_001();
|
|
864
|
+
//test_002();
|
|
865
|
+
test_003();
|
|
866
|
+
//test_004();
|
|
867
|
+
|
|
868
|
+
return 0;
|
|
869
|
+
}
|