# Encode Decode Simple¶

This example shows how to encode and decode a block of memory.

The complete example code is shown below.

  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 // Copyright Steinwurf ApS 2011. // Distributed under the "STEINWURF EVALUATION LICENSE 1.0". // See accompanying file LICENSE.rst or // http://www.steinwurf.com/licensing #include #include #include #include #include #include #include #include /// @example encode_decode_simple.cpp /// /// Simple example showing how to encode and decode a block of memory. int main() { // Seed the random number generator to get different random data srand(static_cast(time(0))); // Create an encoder & decoder factory that are used to build the actual // encoders/decoders. // If we don't specify any parameters for the factories, then they will // use the default values for the finite field, number of symbols and // symbol size parameters. See the "encode_decode_parameters" example for // more details on customizing these parameters kodo_rlnc::encoder::factory encoder_factory; auto encoder = encoder_factory.build(); kodo_rlnc::decoder::factory decoder_factory; auto decoder = decoder_factory.build(); // Allocate some storage for a "payload" the payload is what we would // eventually send over a network std::vector payload(encoder->payload_size()); // Allocate some data to encode. In this case we make a buffer // with the same size as the encoder's block size (the max. // amount a single encoder can encode) std::vector data_in(encoder->block_size()); // Just for fun - fill data_in with random data std::generate(data_in.begin(), data_in.end(), rand); // Assign the data buffer to the encoder so that we may start // to produce encoded symbols from it encoder->set_const_symbols(storage::storage(data_in)); // Define a data buffer where the symbols should be decoded std::vector data_out(decoder->block_size()); decoder->set_mutable_symbols(storage::storage(data_out)); while (!decoder->is_complete()) { // Encode a packet into the payload buffer encoder->write_payload(payload.data()); // Pass that packet to the decoder decoder->read_payload(payload.data()); } // Check if we properly decoded the data if (std::equal(data_out.begin(), data_out.end(), data_in.begin())) { std::cout << "Data decoded correctly" << std::endl; } else { std::cout << "Unexpected failure to decode " << "please file a bug report :)" << std::endl; } }