-
Notifications
You must be signed in to change notification settings - Fork 7
/
frag.c
491 lines (431 loc) · 14.8 KB
/
frag.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* Copyright (c) 2022 Jiapeng Li <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "frag.h"
#define FRAGDBG(x...) printf(x)
#define FRAGLOG(x...) printf(x)
#define DEBUG
#define FRAG_COMPRESS_MATRIX_SIZE
static bool is_power2(uint32_t num)
{
return (num != 0) && ((num & (num-1)) == 0);
}
static uint32_t prbs23(uint32_t x)
{
uint32_t b0, b1;
b0 = (x & 0x00000001);
b1 = (x & 0x00000020) >> 5;
return (x >> 1) + ((b0 ^ b1) << 22);
}
/*
n: FragIndex
m: NubFrag
buf: length is greater than m
*/
static int matrix_line(uint8_t *buf, int n, int m)
{
int mm, x, nbCoeff, r;
mm = 0;
if (is_power2(m)) {
mm = 1;
}
mm += m;
x = 1 + (1001 * n);
for (nbCoeff = 0; nbCoeff < (m/2); nbCoeff++) {
r = (1 << 16);
while (r >= m) {
x = prbs23(x);
r = x % mm;
}
buf[r] = 1;
}
return 0;
}
// n: index of the uncoded and coded fragmentations, maximum N - 1, starts from 0
static int matrix_line_bm(bm_t *bm, int n, int m)
{
int mm, x, nbCoeff, r;
bit_clear_all(bm, m);
/* from 0 to m - 1 */
if (n < m) {
bit_set(bm, n);
return 0;
}
/* from m to N */
n = n - m + 1;
mm = 0;
if (is_power2(m)) {
mm = 1;
}
mm += m;
x = 1 + (1001 * n);
for (nbCoeff = 0; nbCoeff < (m/2); nbCoeff++) {
r = (1 << 16);
while (r >= m) {
x = prbs23(x);
r = x % mm;
}
bit_set(bm, r);
}
return 0;
}
static int buf_xor(uint8_t *des, uint8_t *src, int len)
{
int i;
for (i = 0; i < len; i++) {
des[i] ^= src[i];
}
return 0;
}
/*
unit: m
*/
int frag_enc(frag_enc_t *obj, uint8_t *buf, int len, int unit, int cr)
{
int i, j, k;
int num, maxlen;
uint8_t *mline;
uint8_t *rline;
if ((len % unit) != 0) {
return -1;
}
num = len / unit;
maxlen = len + cr * unit + num * cr;
if (maxlen > obj->maxlen) {
FRAGLOG("maxlen: %d, input buffer: %d", maxlen, obj->maxlen);
return -2;
}
obj->unit = unit;
obj->num = num;
obj->cr = cr;
obj->line = obj->dt;
obj->rline = obj->dt + len;
obj->mline = obj->dt + len + cr * unit;
memcpy(obj->dt + 0, buf, len);
mline = obj->mline;
rline = obj->rline;
for (i = 0; i < cr; i++, mline += num, rline += unit) {
matrix_line(mline, i + 1, num);
for (j = 0; j < num; j++) {
if (mline[j] == 1) {
for (k = 0; k < unit; k++) {
rline[k] ^= buf[j * unit + k];
}
}
}
}
return 0;
}
//#define ALIGN4(x) (x) = (((x) + 0x03) & ~0x03)
#define ALIGN4(x) (x) = (x)
int frag_dec_init(frag_dec_t *obj)
{
int i, j;
i = 0;
/* TODO: check if obj->cfg.dt is aligned */
memset(obj->cfg.dt, 0, obj->cfg.maxlen);
ALIGN4(i);
obj->lost_frm_bm = (bm_t *)(obj->cfg.dt + i);
i += (obj->cfg.nb + BM_UNIT - 1) / BM_UNIT * sizeof(bm_t);
ALIGN4(i);
obj->lost_frm_matrix_bm = (bm_t *)(obj->cfg.dt + i);
#ifdef FRAG_COMPRESS_MATRIX_SIZE
/* left below of the matrix is useless compress used memory */
i += (obj->cfg.tolerence * (obj->cfg.tolerence + 1) / 2 + BM_UNIT - 1) / BM_UNIT * sizeof(bm_t);
#else
i += (obj->cfg.tolerence * obj->cfg.tolerence + BM_UNIT - 1) / BM_UNIT * sizeof(bm_t);
#endif // FRAG_COMPRESS_MATRIX_SIZE
ALIGN4(i);
obj->matched_lost_frm_bm0 = (bm_t *)(obj->cfg.dt + i);
i += (obj->cfg.tolerence + BM_UNIT - 1) / BM_UNIT * sizeof(bm_t);
ALIGN4(i);
obj->matched_lost_frm_bm1 = (bm_t *)(obj->cfg.dt + i);
i += (obj->cfg.tolerence + BM_UNIT - 1) / BM_UNIT * sizeof(bm_t);
ALIGN4(i);
obj->matrix_line_bm = (bm_t *)(obj->cfg.dt + i);
i += (obj->cfg.nb + BM_UNIT - 1) / BM_UNIT * sizeof(bm_t);
ALIGN4(i);
obj->row_data_buf = obj->cfg.dt + i;
i += obj->cfg.size;
ALIGN4(i);
obj->xor_row_data_buf = obj->cfg.dt + i;
i += obj->cfg.size;
ALIGN4(i);
if (i > obj->cfg.maxlen) {
return -1;
}
/* set all frame lost, from 0 to nb-1 */
obj->lost_frm_count = obj->cfg.nb;
for (j = 0; j < obj->cfg.nb; j++) {
bit_set(obj->lost_frm_bm, j);
}
obj->filled_lost_frm_count = 0;
obj->sta = FRAG_DEC_STA_UNCODED;
return i;
}
void frag_dec_frame_received(frag_dec_t *obj, uint16_t index)
{
if (bit_get(obj->lost_frm_bm, index)) {
bit_clr(obj->lost_frm_bm, index);
obj->lost_frm_count--;
/* TODO: check and update other maps */
}
}
void frag_dec_flash_wr(frag_dec_t *obj, uint16_t index, uint8_t *buf)
{
obj->cfg.fwr_func(index * obj->cfg.size, buf, obj->cfg.size);
#ifdef DEBUG
FRAGDBG("-> index %d, ", index);
frag_dec_log_buf(buf, obj->cfg.size);
#endif
}
void frag_dec_flash_rd(frag_dec_t *obj, uint16_t index, uint8_t *buf)
{
obj->cfg.frd_func(index * obj->cfg.size, buf, obj->cfg.size);
#ifdef DEBUG
FRAGDBG("<- index %d, ", index);
frag_dec_log_buf(buf, obj->cfg.size);
#endif
}
#ifdef FRAG_COMPRESS_MATRIX_SIZE
void frag_dec_lost_frm_matrix_save(frag_dec_t *obj, uint16_t lindex, bm_t *map, int len)
{
int i;
for (i = 0; i < len; i++) {
if (bit_get(map, i)) {
m2t_set(obj->lost_frm_matrix_bm, i, lindex, len);
} else {
m2t_clr(obj->lost_frm_matrix_bm, i, lindex, len);
}
}
}
void frag_dec_lost_frm_matrix_load(frag_dec_t *obj, uint16_t lindex, bm_t *map, int len)
{
int i;
for (i = 0; i < len; i++) {
if (m2t_get(obj->lost_frm_matrix_bm, i, lindex, len)) {
bit_set(map, i);
} else {
bit_clr(map, i);
}
}
}
bool frag_dec_lost_frm_matrix_is_diagonal(frag_dec_t *obj, uint16_t lindex, int len)
{
return m2t_get(obj->lost_frm_matrix_bm, lindex, lindex, len);
}
#else
void frag_dec_lost_frm_matrix_save(frag_dec_t *obj, uint16_t lindex, bm_t *map, int len)
{
int i, offset;
offset = lindex * len;
for (i = 0; i < len; i++) {
if (bit_get(map, i)) {
bit_set(obj->lost_frm_matrix_bm, offset + i);
} else {
bit_clr(obj->lost_frm_matrix_bm, offset + i);
}
}
}
void frag_dec_lost_frm_matrix_load(frag_dec_t *obj, uint16_t lindex, bm_t *map, int len)
{
int i, offset;
offset = lindex * len;
for (i = 0; i < len; i++) {
if (bit_get(obj->lost_frm_matrix_bm, offset + i)) {
bit_set(map, i);
} else {
bit_clr(map, i);
}
}
}
bool frag_dec_lost_frm_matrix_is_diagonal(frag_dec_t *obj, uint16_t lindex, int len)
{
return bit_get(obj->lost_frm_matrix_bm, lindex * len + lindex);
}
#endif
/* fcnt from 1 to nb */
int frag_dec(frag_dec_t *obj, uint16_t fcnt, uint8_t *buf, int len)
{
int i, j;
int index, unmatched_frame_cnt;
int lost_frame_index, frame_index, frame_index1;
bool no_info;
if (obj->sta == FRAG_DEC_STA_DONE) {
return obj->lost_frm_count;
}
if (len != obj->cfg.size) {
return FRAG_DEC_ERR_INVALID_FRAME;
}
/* clear all temporary bm and buf */
bit_clear_all(obj->matched_lost_frm_bm0, obj->lost_frm_count);
bit_clear_all(obj->matched_lost_frm_bm1, obj->lost_frm_count);
/* back up input data so that not to mess input data */
memcpy(obj->xor_row_data_buf, buf, obj->cfg.size);
index = fcnt - 1;
if ((index < obj->cfg.nb) && (obj->sta == FRAG_DEC_STA_UNCODED)) {
/* uncoded frames under uncoded process */
/* mark new received frame */
frag_dec_frame_received(obj, index);
/* save data to flash */
frag_dec_flash_wr(obj, index, buf);
/* if no frame lost finish decode process */
if (obj->lost_frm_count == 0) {
obj->sta = FRAG_DEC_STA_DONE;
return obj->lost_frm_count;
}
} else {
obj->sta = FRAG_DEC_STA_CODED;
/* coded frames start processing, lost_frm_count is now frozen and should be not changed (!!!) */
/* too many packets are lost, it is not possible to reconstruct data block */
if (obj->lost_frm_count > obj->cfg.tolerence) {
/* too many frames are lost, memory is not enough to reconstruct the packets */
return FRAG_DEC_ERR_TOO_MANY_FRAME_LOST;
}
unmatched_frame_cnt = 0;
matrix_line_bm(obj->matrix_line_bm, index, obj->cfg.nb);
for (i = 0; i < obj->cfg.nb; i++) {
if (bit_get(obj->matrix_line_bm, i) == true) {
if (bit_get(obj->lost_frm_bm, i) == false) {
/* coded frame is matched one received uncoded frame */
frag_dec_flash_rd(obj, i, obj->row_data_buf);
buf_xor(obj->xor_row_data_buf, obj->row_data_buf, obj->cfg.size);
} else {
/* coded frame is not matched one received uncoded frame */
/* matched_lost_frm_bm0 index is the nth lost frame */
bit_set(obj->matched_lost_frm_bm0, bit_count_ones(obj->lost_frm_bm, i) - 1);
unmatched_frame_cnt++;
}
}
}
if (unmatched_frame_cnt <= 0) {
return FRAG_DEC_ONGOING;
}
#ifdef DEBUG
FRAGDBG("matrix_line_bm: %d, ", index);
frag_dec_log_bits(obj->matrix_line_bm, obj->cfg.nb);
FRAGDBG("matched_lost_frm_bm0: ");
frag_dec_log_bits(obj->matched_lost_frm_bm0, obj->lost_frm_count);
#endif
/* obj->matched_lost_frm_bm0 now saves new coded frame which excludes all received frames content */
/* start to diagonal obj->matched_lost_frm_bm0 */
no_info = false;
do {
lost_frame_index = bit_ffs(obj->matched_lost_frm_bm0, obj->lost_frm_count);
frame_index = bit_fns(obj->lost_frm_bm, obj->cfg.nb, lost_frame_index + 1);
if (frame_index == -1) {
FRAGLOG("matched_lost_frm_bm0: ");
frag_dec_log_bits(obj->matched_lost_frm_bm0, obj->lost_frm_count);
FRAGLOG("lost_frm_bm: ");
frag_dec_log_bits(obj->lost_frm_bm, obj->cfg.nb);
FRAGLOG("frame_index: %d, lost_frame_index: %d\n", frame_index, lost_frame_index);
}
#ifdef DEBUG
FRAGDBG("matched_lost_frm_bm0: ");
frag_dec_log_bits(obj->matched_lost_frm_bm0, obj->lost_frm_count);
FRAGDBG("lost_frm_bm: ");
frag_dec_log_bits(obj->lost_frm_bm, obj->cfg.nb);
FRAGDBG("frame_index: %d, lost_frame_index: %d\n", frame_index, lost_frame_index);
#endif
if (frag_dec_lost_frm_matrix_is_diagonal(obj, lost_frame_index, obj->lost_frm_count) == false) {
break;
}
frag_dec_lost_frm_matrix_load(obj, lost_frame_index, obj->matched_lost_frm_bm1, obj->lost_frm_count);
bit_xor(obj->matched_lost_frm_bm0, obj->matched_lost_frm_bm1, obj->lost_frm_count);
frag_dec_flash_rd(obj, frame_index, obj->row_data_buf);
buf_xor(obj->xor_row_data_buf, obj->row_data_buf, obj->cfg.size);
if (bit_is_all_clear(obj->matched_lost_frm_bm0, obj->lost_frm_count)) {
no_info = true;
break;
}
} while (1);
if (!no_info) {
/* current frame contains new information, save it */
frag_dec_lost_frm_matrix_save(obj, lost_frame_index, obj->matched_lost_frm_bm0, obj->lost_frm_count);
frag_dec_flash_wr(obj, frame_index, obj->xor_row_data_buf);
obj->filled_lost_frm_count++;
}
if (obj->filled_lost_frm_count == obj->lost_frm_count) {
/* all frame content is received, now to reconstruct the whole frame */
if (obj->lost_frm_count > 1) {
for (i = (obj->lost_frm_count - 2); i >= 0; i--) {
frame_index = bit_fns(obj->lost_frm_bm, obj->cfg.nb, i + 1);
frag_dec_flash_rd(obj, frame_index, obj->xor_row_data_buf);
for (j = (obj->lost_frm_count - 1); j > i; j--) {
frag_dec_lost_frm_matrix_load(obj, i, obj->matched_lost_frm_bm1, obj->lost_frm_count);
frag_dec_lost_frm_matrix_load(obj, j, obj->matched_lost_frm_bm0, obj->lost_frm_count);
if (bit_get(obj->matched_lost_frm_bm1, j)) {
frame_index1 = bit_fns(obj->lost_frm_bm, obj->cfg.nb, j + 1);
frag_dec_flash_rd(obj, frame_index1, obj->row_data_buf);
bit_xor(obj->matched_lost_frm_bm1, obj->matched_lost_frm_bm0, obj->lost_frm_count);
buf_xor(obj->xor_row_data_buf, obj->row_data_buf, obj->cfg.size);
frag_dec_lost_frm_matrix_save(obj, i, obj->matched_lost_frm_bm1, obj->lost_frm_count);
}
}
frag_dec_flash_wr(obj, frame_index, obj->xor_row_data_buf);
}
}
obj->sta = FRAG_DEC_STA_DONE;
return obj->lost_frm_count;
}
}
/* process ongoing */
return FRAG_DEC_ONGOING;
}
void frag_dec_log_buf(uint8_t *buf, int len)
{
int i;
for (i = 0; i < len; i++) {
FRAGLOG("%02X, ", buf[i]);
}
FRAGLOG("\n");
}
void frag_dec_log_bits(bm_t *bitmap, int len)
{
int i;
for (i = 0; i < len; i++) {
if (bit_get(bitmap, i)) {
FRAGLOG("1 ");
} else {
FRAGLOG("0 ");
}
}
FRAGLOG("\n");
}
void frag_dec_log_matrix_bits(bm_t *bitmap, int len)
{
int i, j;
for (i = 0; i < len; i++) {
for (j = 0; j < len; j++) {
#ifdef FRAG_COMPRESS_MATRIX_SIZE
if (m2t_get(bitmap, j, i, len)) {
#else
if (bit_get(bitmap, j + i * len)) {
#endif
FRAGLOG("1 ");
} else {
FRAGLOG("0 ");
}
}
FRAGLOG("\n");
}
if (i == 0) {
FRAGLOG("\n");
}
}
void frag_dec_log(frag_dec_t *obj)
{
int i, j;
FRAGLOG("Decode %s\n", (obj->sta == FRAG_DEC_STA_DONE) ? "ok" : "ng");
for (i = 0; i < obj->cfg.nb; i++) {
frag_dec_flash_rd(obj, i, obj->row_data_buf);
for (j = 0; j < obj->cfg.size; j++) {
FRAGLOG("%02X ", obj->row_data_buf[j]);
}
FRAGLOG("\n");
}
FRAGLOG("lost_frm_matrix_bm: (%d) \n", obj->lost_frm_count);
frag_dec_log_matrix_bits(obj->lost_frm_matrix_bm, obj->lost_frm_count);
}