-
Notifications
You must be signed in to change notification settings - Fork 21
/
rssimple.C
72 lines (63 loc) · 2.58 KB
/
rssimple.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
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>
#include <sys/time.h>
#include <array>
#include <vector>
#include <ezpwd/rs>
#include <ezpwd/timeofday>
#include <ezpwd/output>
int main()
{
int failures= 0;
ezpwd::RS<255,253> rs; // 255 symbol codeword, up to 253 data == 2 symbols parity
std::string orig = "Hello, world!";
// Most basic use of API to correct an error, in a dynamic container
std::cout << std::endl << std::endl << "Simple std::string container:" << std::endl;
{
std::string copy = orig; // working copy, copy of orig
std::cout << "Original: " << ezpwd::hexstr( copy ) << std::endl;
rs.encode( copy ); // 13 symbols copy + 2 symbols R-S parity added
std::cout << "Encoded: " << ezpwd::hexstr( copy ) << std::endl;
copy[3] = 'x'; // Corrupt one symbol
std::cout << "Corrupted: " << ezpwd::hexstr( copy ) << std::endl;
int count = rs.decode( copy ); // Correct any symbols possible
std::cout << "Corrected: " << ezpwd::hexstr( copy ) << " : " << count << " errors fixed" << std::endl;
copy.resize( copy.size() - rs.nroots() ); // Discard added R-S parity symbols
std::cout << "Restored: " << ezpwd::hexstr( copy ) << std::endl;
if ( copy != orig ) { // Ensure original copy is recovered
failures += 1;
std::cout << "Failed to restore origin data." << std::endl;
}
}
// Iterate through orig, corrupting or erasing the i'th character, and fixing it.
std::cout << std::endl << std::endl << "Iterate over std::vector container:" << std::endl;
for ( size_t i = 0; i < orig.size(); ++i ) {
std::vector<uint8_t> data( orig.begin(), orig.end() );
rs.encode( data );
std::vector<int> erasure;
if ( i & 1 ) { // every other loop...
erasure.push_back( i );
data[i] = ' '; // erasure
std::cout << "Erasure: " << data << std::endl;
} else {
data[i] ^= 1 << i % 8; // error
std::cout << "Corrupted: " << data << std::endl;
}
std::vector<int> position;
int count = rs.decode( data, erasure, &position );
std::string fixes( data.size() * 2, ' ' );
for ( int i : position )
fixes[i*2+0] = fixes[i*2+1] = '^';
std::cout << "Fixed: " << fixes << "(count: " << count << ")" << std::endl;
std::cout << "Decoded: " << data << std::endl << std::endl;
// Remove R-S parity symbols, ensure original copy is recovered
data.resize( data.size() - rs.nroots() );
if ( std::string( data.begin(), data.end() ) != orig ) {
failures += 1;
std::cout << "Failed to restore origin data." << std::endl;
}
}
return failures ? 1 : 0;
}