1 | unit lbc_reedsolomon;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | procedure rs_init_gf(poly: Integer);
|
---|
8 | procedure rs_init_code(nsym: Integer; index: Integer);
|
---|
9 | procedure rs_encode(len: Integer; data: PBYTE; res: PBYTE);
|
---|
10 | procedure rs_encode_long(len: Integer; data: PCardinal; res: PCardinal);
|
---|
11 | procedure rs_free();
|
---|
12 |
|
---|
13 | implementation
|
---|
14 |
|
---|
15 | var
|
---|
16 | // gfpoly: integer;
|
---|
17 | // symsize: integer; // in bits
|
---|
18 | logmod: integer; // 2**symsize - 1
|
---|
19 | rlen: integer;
|
---|
20 |
|
---|
21 | logt: PInteger = nil;
|
---|
22 | alog: PInteger = nil;
|
---|
23 | rspoly: PInteger = nil;
|
---|
24 |
|
---|
25 | procedure rs_init_gf(poly: Integer);
|
---|
26 | var
|
---|
27 | b: Integer;
|
---|
28 | p: Integer;
|
---|
29 | v: Integer;
|
---|
30 | m: Integer;
|
---|
31 | begin
|
---|
32 | b := 1;
|
---|
33 | m := 0;
|
---|
34 | while b <= poly do
|
---|
35 | begin
|
---|
36 | Inc (m);
|
---|
37 | b := b shl 1;
|
---|
38 | end;
|
---|
39 | b := b shr 1;
|
---|
40 | Dec (m);
|
---|
41 | // gfpoly := poly;
|
---|
42 | // symsize := m;
|
---|
43 | logmod := (1 shl m) - 1;
|
---|
44 | logt := PInteger (GetMem (SizeOf (Integer) * (logmod + 1)));
|
---|
45 | alog := PInteger (GetMem (SizeOf (Integer) * logmod));
|
---|
46 | p := 1;
|
---|
47 | v := 0;
|
---|
48 | while v < logmod do
|
---|
49 | begin
|
---|
50 | alog[v] := p;
|
---|
51 | logt[p] := v;
|
---|
52 | p := p shl 1;
|
---|
53 | if (p and b)<>0 then
|
---|
54 | begin
|
---|
55 | p:=p xor poly;
|
---|
56 | end;
|
---|
57 | Inc (v);
|
---|
58 | end;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | procedure rs_init_code(nsym: Integer; index: Integer);
|
---|
62 | var
|
---|
63 | k: Integer;
|
---|
64 | i: Integer;
|
---|
65 | begin
|
---|
66 | rspoly := PInteger (GetMem (SizeOf (Integer) * (nsym + 1)));
|
---|
67 | rlen := nsym;
|
---|
68 | rspoly[0] := 1;
|
---|
69 | i := 1;
|
---|
70 | while i <= nsym do
|
---|
71 | begin
|
---|
72 | rspoly[i] := 1;
|
---|
73 | k := i - 1;
|
---|
74 | while k > 0 do
|
---|
75 | begin
|
---|
76 | if rspoly[k]<>0 then
|
---|
77 | begin
|
---|
78 | rspoly[k] := alog[(logt[rspoly[k]] + index) mod logmod];
|
---|
79 | end;
|
---|
80 | rspoly[k] := rspoly[k] xor rspoly[k - 1];
|
---|
81 | Dec (k);
|
---|
82 | end;
|
---|
83 | rspoly[0] := alog[(logt[rspoly[0]] + index) mod logmod];
|
---|
84 | Inc (index);
|
---|
85 | Inc (i);
|
---|
86 | end;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | procedure rs_encode(len: Integer; data: PBYTE; res: PBYTE);
|
---|
90 | var
|
---|
91 | k: Integer;
|
---|
92 | m: Integer;
|
---|
93 | i: Integer;
|
---|
94 | begin
|
---|
95 | i := 0;
|
---|
96 | while i < rlen do
|
---|
97 | begin
|
---|
98 | res[i] := 0;
|
---|
99 | Inc (i);
|
---|
100 | end;
|
---|
101 | i := 0;
|
---|
102 | while i < len do
|
---|
103 | begin
|
---|
104 | m := res[rlen - 1] xor data[i];
|
---|
105 | k := rlen - 1;
|
---|
106 | while k > 0 do
|
---|
107 | begin
|
---|
108 | if (m<>0) and (rspoly[k]<>0) then
|
---|
109 | begin
|
---|
110 | res[k] := integer(res[k - 1]) xor alog[(logt[m] + logt[rspoly[k]]) mod logmod];
|
---|
111 | end else begin
|
---|
112 | res[k] := integer(res[k - 1]);
|
---|
113 | end;
|
---|
114 | Dec (k);
|
---|
115 | end;
|
---|
116 | if (m<>0) and (rspoly[0]<>0) then
|
---|
117 | begin
|
---|
118 | res[0] := alog[(logt[m] + logt[rspoly[0]]) mod logmod];
|
---|
119 | end else begin
|
---|
120 | res[0] := 0;
|
---|
121 | end;
|
---|
122 | Inc (i);
|
---|
123 | end;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | procedure rs_encode_long(len: Integer; data: PCardinal; res: PCardinal);
|
---|
127 | var
|
---|
128 | k: Integer;
|
---|
129 | m: Integer;
|
---|
130 | i: Integer;
|
---|
131 | begin
|
---|
132 | i := 0;
|
---|
133 | while i < rlen do
|
---|
134 | begin
|
---|
135 | res[i] := 0;
|
---|
136 | Inc (i);
|
---|
137 | end;
|
---|
138 | i := 0;
|
---|
139 | while i < len do
|
---|
140 | begin
|
---|
141 | m := res[rlen - 1] xor data[i];
|
---|
142 | k := rlen - 1;
|
---|
143 | while k > 0 do
|
---|
144 | begin
|
---|
145 | if (m<>0) and (rspoly[k]<>0) then
|
---|
146 | begin
|
---|
147 | res[k] := Integer(res[k - 1]) xor alog[(logt[m] + logt[rspoly[k]]) mod logmod];
|
---|
148 | end else begin
|
---|
149 | res[k] := res[k - 1];
|
---|
150 | end;
|
---|
151 | Dec (k);
|
---|
152 | end;
|
---|
153 | if (m<>0) and (rspoly[0]<>0) then
|
---|
154 | begin
|
---|
155 | res[0] := alog[(logt[m] + logt[rspoly[0]]) mod logmod];
|
---|
156 | end else begin
|
---|
157 | res[0] := 0;
|
---|
158 | end;
|
---|
159 | Inc (i);
|
---|
160 | end;
|
---|
161 | end;
|
---|
162 |
|
---|
163 | procedure rs_free();
|
---|
164 | begin
|
---|
165 | freeMem (logt);
|
---|
166 | freeMem (alog);
|
---|
167 | freeMem (rspoly);
|
---|
168 | rspoly := nil;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | end.
|
---|
172 |
|
---|