-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfec.c
More file actions
152 lines (125 loc) · 3.2 KB
/
fec.c
File metadata and controls
152 lines (125 loc) · 3.2 KB
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
//
// fec.c
// fec
//
// Created by fanzy on 14-9-7.
// Copyright (c) 2014年 fanzy. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "fec.h"
fec* fecCreate(int k, int n, int blockSize)
{
fec* f = malloc(sizeof(fec) + sizeof(fec_data_t) * n);
memset(f, 0, sizeof(fec) + sizeof(fec_data_t) * n);
f->k = k;
f->n = n;
f->blockSize = blockSize;
return f;
}
void fecClose(fec* f)
{
if(f) {
for (int i = 0; i < f-> n; i++) {
if (f->data[i].d != NULL && (f->data[i].flag & FEC_FLAG_USERMEM) == 0) {
free(f->data[i].d);
}
}
_fecDecoderClose(&f->d);
_fecMatrixClose(&f->m);
free(f);
}
}
static void fecAppendWithFlag(fec* f, int idx, void* d, int flag)
{
assert(idx >=0 && idx < f->n);
assert(d != NULL);
assert(f->data[idx].d == NULL);
f->data[idx].d = d;
f->data[idx].flag = flag;
}
void fecDataAppend(fec* f, int idx, void* d)
{
fecAppendWithFlag(f, idx, d, FEC_FLAG_DATA | FEC_FLAG_USERMEM);
f->cnt += 1;
}
void fecBuffAppend(fec* f, int idx, void* d)
{
fecAppendWithFlag(f, idx, d, FEC_FLAG_USERMEM);
}
void* fecData(fec* f, int idx) {
assert(f != NULL);
assert(idx >= 0 && idx < f->n);
return f->data[idx].d;
}
static int prepare(fec* f)
{
// Alloc memeory
for (int i = 0; i < f->n; i++) {
if (!f->data[i].d) {
void* d = malloc(f->blockSize);
fecAppendWithFlag(f, i, d, 0);
}
}
_fecParaInit(&f->p, f->k);
_fecMatrixAInit(f);
// _fecMatrixPrint("Init A completed1", &f->m);
return _fecGetSymbols(f);
}
int fecEncode(fec* f)
{
assert(f->cnt >= f->k);
assert(f->cnt <= f->n);
if (prepare(f) != 0) {
return -1;
}
fec_decoder_t *dec = &f->d;
fec_para_t *p = &f->p;
for (int i = f->k; i < f->n; i++) {
int a,b,d;
_fecLtTriple(p, i, &d, &a, &b);
for (; b >= p->l; b = (a+b) % p->p) {}
memcpy(f->data[i].d, dec->data[b], f->blockSize);
if (d > p->l) {
d = p->l;
}
for (int j = 1; j < d; j++) {
for (b = (a+b) % p->p; b >= p->l; b = (a+b) % p->p) {}
for (int k = 0; k < f->blockSize; k++) {
f->data[i].d[k] ^= dec->data[b][k];
}
}
}
return 0;
}
int fecDecode(fec* f)
{
assert(f->cnt >= f->k);
assert(f->cnt <= f->n);
if(prepare(f) != 0) {
return -1;
}
fec_decoder_t *dec = &f->d;
fec_para_t *p = &f->p;
for (int i = 0; i < f->k; i++) {
if (f->data[i].flag & FEC_FLAG_DATA) {
continue;
}
int a,b,d;
_fecLtTriple(p, i, &d, &a, &b);
for (; b >= p->l; b = (a+b) % p->p) {}
memcpy(f->data[i].d, dec->data[b], f->blockSize);
if (d > p->l) {
d = p->l;
}
for (int j = 1; j < d; j++) {
for (b = (a+b) % p->p; b >= p->l; b = (a+b) % p->p) {}
for (int k = 0; k < f->blockSize; k++) {
f->data[i].d[k] ^= dec->data[b][k];
}
}
}
return 0;
}