-
Notifications
You must be signed in to change notification settings - Fork 31
/
schifra_reed_solomon_example02.cpp
132 lines (105 loc) · 5.47 KB
/
schifra_reed_solomon_example02.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
/*
(**************************************************************************)
(* *)
(* Schifra *)
(* Reed-Solomon Error Correcting Code Library *)
(* *)
(* Release Version 0.0.1 *)
(* http://www.schifra.com *)
(* Copyright (c) 2000-2020 Arash Partow, All Rights Reserved. *)
(* *)
(* The Schifra Reed-Solomon error correcting code library and all its *)
(* components are supplied under the terms of the General Schifra License *)
(* agreement. The contents of the Schifra Reed-Solomon error correcting *)
(* code library and all its components may not be copied or disclosed *)
(* except in accordance with the terms of that agreement. *)
(* *)
(* URL: http://www.schifra.com/license.html *)
(* *)
(**************************************************************************)
*/
/*
Description: This example will demonstrate how to instantiate a Reed-Solomon
encoder and decoder, add the full amount of possible erasures,
correct the erasures, and output the various pieces of relevant
information.
*/
#include <cstddef>
#include <iostream>
#include <string>
#include "schifra_galois_field.hpp"
#include "schifra_galois_field_polynomial.hpp"
#include "schifra_sequential_root_generator_polynomial_creator.hpp"
#include "schifra_reed_solomon_encoder.hpp"
#include "schifra_reed_solomon_decoder.hpp"
#include "schifra_reed_solomon_block.hpp"
#include "schifra_error_processes.hpp"
int main()
{
/* Finite Field Parameters */
const std::size_t field_descriptor = 8;
const std::size_t generator_polynomial_index = 120;
const std::size_t generator_polynomial_root_count = 32;
/* Reed Solomon Code Parameters */
const std::size_t code_length = 255;
const std::size_t fec_length = 32;
const std::size_t data_length = code_length - fec_length;
/* Instantiate Finite Field and Generator Polynomials */
const schifra::galois::field field(field_descriptor,
schifra::galois::primitive_polynomial_size06,
schifra::galois::primitive_polynomial06);
schifra::galois::field_polynomial generator_polynomial(field);
if (
!schifra::make_sequential_root_generator_polynomial(field,
generator_polynomial_index,
generator_polynomial_root_count,
generator_polynomial)
)
{
std::cout << "Error - Failed to create sequential root generator!" << std::endl;
return 1;
}
/* Instantiate Encoder and Decoder (Codec) */
typedef schifra::reed_solomon::encoder<code_length,fec_length,data_length> encoder_t;
typedef schifra::reed_solomon::decoder<code_length,fec_length,data_length> decoder_t;
const encoder_t encoder(field, generator_polynomial);
const decoder_t decoder(field, generator_polynomial_index);
std::string message = "An expert is someone who knows more and more about less and "
"less until they know absolutely everything about nothing";
/* Pad message with nulls up until the code-word length */
message.resize(code_length, 0x00);
std::cout << "Original Message: [" << message << "]" << std::endl;
/* Instantiate RS Block For Codec */
schifra::reed_solomon::block<code_length,fec_length> block;
/* Transform message into Reed-Solomon encoded codeword */
if (!encoder.encode(message, block))
{
std::cout << "Error - Critical encoding failure! "
<< "Msg: " << block.error_as_string() << std::endl;
return 1;
}
schifra::reed_solomon::erasure_locations_t erasure_location_list;
/* Add erasures at every 2nd location starting at position zero */
schifra::corrupt_message_all_erasures00(block, erasure_location_list, 0, 2);
std::cout << "Corrupted Codeword: [" << block << "]" << std::endl;
if (!decoder.decode(block, erasure_location_list))
{
std::cout << "Error - Critical decoding failure! "
<< "Msg: " << block.error_as_string() << std::endl;
return 1;
}
else if (!schifra::is_block_equivelent(block, message))
{
std::cout << "Error - Error correction failed!" << std::endl;
return 1;
}
block.data_to_string(message);
std::cout << "Corrected Message: [" << message << "]" << std::endl;
std::cout << "Encoder Parameters [" << encoder_t::trait::code_length << ","
<< encoder_t::trait::data_length << ","
<< encoder_t::trait::fec_length << "]" << std::endl;
std::cout << "Decoder Parameters [" << decoder_t::trait::code_length << ","
<< decoder_t::trait::data_length << ","
<< decoder_t::trait::fec_length << "]" << std::endl;
return 0;
}