-
Notifications
You must be signed in to change notification settings - Fork 1
/
reedsolomon.cpp
371 lines (301 loc) · 9.03 KB
/
reedsolomon.cpp
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
// This file is part of par2cmdline (a PAR 2.0 compatible file verification and
// repair tool). See http://parchive.sourceforge.net for details of PAR 2.0.
//
// Copyright (c) 2003 Peter Brian Clements
//
// par2cmdline is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// par2cmdline is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "par2cmdline.h"
#ifdef _MSC_VER
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
u32 gcd(u32 a, u32 b)
{
if (a && b)
{
while (a && b)
{
if (a>b)
{
a = a%b;
}
else
{
b = b%a;
}
}
return a+b;
}
else
{
return 0;
}
}
template <> bool ReedSolomon<Galois8>::SetInput(const vector<bool> &present)
{
inputcount = (u32)present.size();
datapresentindex = new u32[inputcount];
datamissingindex = new u32[inputcount];
database = new G::ValueType[inputcount];
G::ValueType base = 1;
for (unsigned int index=0; index<inputcount; index++)
{
// Record the index of the file in the datapresentindex array
// or the datamissingindex array
if (present[index])
{
datapresentindex[datapresent++] = index;
}
else
{
datamissingindex[datamissing++] = index;
}
database[index] = base++;
}
return true;
}
template <> bool ReedSolomon<Galois8>::SetInput(u32 count)
{
inputcount = count;
datapresentindex = new u32[inputcount];
datamissingindex = new u32[inputcount];
database = new G::ValueType[inputcount];
G::ValueType base = 1;
for (unsigned int index=0; index<count; index++)
{
// Record that the file is present
datapresentindex[datapresent++] = index;
database[index] = base++;
}
return true;
}
template <> bool ReedSolomon<Galois8>::Process(size_t size, u32 inputindex, const void *inputbuffer, u32 outputindex, void *outputbuffer)
{
// Look up the appropriate element in the RS matrix
Galois8 factor = leftmatrix[outputindex * (datapresent + datamissing) + inputindex];
// Do nothing if the factor happens to be 0
if (factor == 0)
return eSuccess;
#ifdef LONGMULTIPLY
// The 8-bit long multiplication tables
Galois8 *table = glmt->tables;
// Split the factor into Low and High bytes
unsigned int fl = (factor >> 0) & 0xff;
// Get the four separate multiplication tables
Galois8 *LL = &table[(0*256 + fl) * 256 + 0]; // factor.low * source.low
// Combine the four multiplication tables into two
unsigned int L[256];
unsigned int *pL = &L[0];
for (unsigned int i=0; i<256; i++)
{
*pL = *LL;
pL++;
LL++;
}
// Treat the buffers as arrays of 32-bit unsigned ints.
u32 *src4 = (u32 *)inputbuffer;
u32 *end4 = (u32 *)&((u8*)inputbuffer)[size & ~3];
u32 *dst4 = (u32 *)outputbuffer;
// Process the data
while (src4 < end4)
{
u32 s = *src4++;
// Use the two lookup tables computed earlier
*dst4++ ^= (L[(s >> 0) & 0xff] )
^ (L[(s >> 8) & 0xff] << 8 )
^ (L[(s >> 16)& 0xff] << 16)
^ (L[(s >> 24)& 0xff] << 24);
}
// Process any left over bytes at the end of the buffer
if (size & 3)
{
u8 *src1 = &((u8*)inputbuffer)[size & ~3];
u8 *end1 = &((u8*)inputbuffer)[size];
u8 *dst1 = &((u8*)outputbuffer)[size & ~3];
// Process the data
while (src1 < end1)
{
u8 s = *src1++;
*dst1++ ^= L[s];
}
}
#else
// Treat the buffers as arrays of 16-bit Galois values.
Galois8 *src = (Galois8 *)inputbuffer;
Galois8 *end = (Galois8 *)&((u8*)inputbuffer)[size];
Galois8 *dst = (Galois8 *)outputbuffer;
// Process the data
while (src < end)
{
*dst++ += *src++ * factor;
}
#endif
return eSuccess;
}
////////////////////////////////////////////////////////////////////////////////////////////
// Set which of the source files are present and which are missing
// and compute the base values to use for the vandermonde matrix.
template <> bool ReedSolomon<Galois16>::SetInput(const vector<bool> &present)
{
inputcount = (u32)present.size();
datapresentindex = new u32[inputcount];
datamissingindex = new u32[inputcount];
database = new G::ValueType[inputcount];
unsigned int logbase = 0;
for (unsigned int index=0; index<inputcount; index++)
{
// Record the index of the file in the datapresentindex array
// or the datamissingindex array
if (present[index])
{
datapresentindex[datapresent++] = index;
}
else
{
datamissingindex[datamissing++] = index;
}
// Determine the next useable base value.
// Its log must must be relatively prime to 65535
while (gcd(G::Limit, logbase) != 1)
{
logbase++;
}
if (logbase >= G::Limit)
{
cerr << "Too many input blocks for Reed Solomon matrix." << endl;
return false;
}
G::ValueType base = G(logbase++).ALog();
database[index] = base;
}
return true;
}
// Record that the specified number of source files are all present
// and compute the base values to use for the vandermonde matrix.
template <> bool ReedSolomon<Galois16>::SetInput(u32 count)
{
inputcount = count;
datapresentindex = new u32[inputcount];
datamissingindex = new u32[inputcount];
database = new G::ValueType[inputcount];
unsigned int logbase = 0;
for (unsigned int index=0; index<count; index++)
{
// Record that the file is present
datapresentindex[datapresent++] = index;
// Determine the next useable base value.
// Its log must must be relatively prime to 65535
while (gcd(G::Limit, logbase) != 1)
{
logbase++;
}
if (logbase >= G::Limit)
{
cerr << "Too many input blocks for Reed Solomon matrix." << endl;
return false;
}
G::ValueType base = G(logbase++).ALog();
database[index] = base;
}
return true;
}
template <> bool ReedSolomon<Galois16>::Process(size_t size, u32 inputindex, const void *inputbuffer, u32 outputindex, void *outputbuffer)
{
// Look up the appropriate element in the RS matrix
Galois16 factor = leftmatrix[outputindex * (datapresent + datamissing) + inputindex];
// Do nothing if the factor happens to be 0
if (factor == 0)
return eSuccess;
#ifdef LONGMULTIPLY
// The 8-bit long multiplication tables
Galois16 *table = glmt->tables;
// Split the factor into Low and High bytes
unsigned int fl = (factor >> 0) & 0xff;
unsigned int fh = (factor >> 8) & 0xff;
// Get the four separate multiplication tables
Galois16 *LL = &table[(0*256 + fl) * 256 + 0]; // factor.low * source.low
Galois16 *LH = &table[(1*256 + fl) * 256 + 0]; // factor.low * source.high
Galois16 *HL = &table[(1*256 + 0) * 256 + fh]; // factor.high * source.low
Galois16 *HH = &table[(2*256 + fh) * 256 + 0]; // factor.high * source.high
// Combine the four multiplication tables into two
unsigned int L[256];
unsigned int H[256];
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int *pL = &L[0];
unsigned int *pH = &H[0];
#else
unsigned int *pL = &H[0];
unsigned int *pH = &L[0];
#endif
for (unsigned int i=0; i<256; i++)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
*pL = *LL + *HL;
#else
unsigned int temp = *LL + *HL;
*pL = (temp >> 8) & 0xff | (temp << 8) & 0xff00;
#endif
pL++;
LL++;
HL+=256;
#if __BYTE_ORDER == __LITTLE_ENDIAN
*pH = *LH + *HH;
#else
temp = *LH + *HH;
*pH = (temp >> 8) & 0xff | (temp << 8) & 0xff00;
#endif
pH++;
LH++;
HH++;
}
// Treat the buffers as arrays of 32-bit unsigned ints.
u32 *src = (u32 *)inputbuffer;
u32 *end = (u32 *)&((u8*)inputbuffer)[size];
u32 *dst = (u32 *)outputbuffer;
// Process the data
while (src < end)
{
u32 s = *src++;
// Use the two lookup tables computed earlier
//#if __BYTE_ORDER == __LITTLE_ENDIAN
u32 d = *dst ^ (L[(s >> 0) & 0xff] )
^ (H[(s >> 8) & 0xff] )
^ (L[(s >> 16)& 0xff] << 16)
^ (H[(s >> 24)& 0xff] << 16);
*dst++ = d;
//#else
// *dst++ ^= (L[(s >> 8) & 0xff] )
// ^ (H[(s >> 0) & 0xff] )
// ^ (L[(s >> 24)& 0xff] << 16)
// ^ (H[(s >> 16)& 0xff] << 16);
//#endif
}
#else
// Treat the buffers as arrays of 16-bit Galois values.
// BUG: This only works for __LITTLE_ENDIAN
Galois16 *src = (Galois16 *)inputbuffer;
Galois16 *end = (Galois16 *)&((u8*)inputbuffer)[size];
Galois16 *dst = (Galois16 *)outputbuffer;
// Process the data
while (src < end)
{
*dst++ += *src++ * factor;
}
#endif
return eSuccess;
}