| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
|
|
Convert::FEC - Forward Error Correction using Vandermonde Matrices |
| 4 |
|
|
|
| 5 |
|
|
=head1 SYNOPSIS |
| 6 |
|
|
|
| 7 |
|
|
use Convert::FEC; |
| 8 |
|
|
|
| 9 |
|
|
=head1 DESCRIPTION |
| 10 |
|
|
|
| 11 |
|
|
This module is an interface to the fec library by Luigi Rizzo et al., see |
| 12 |
|
|
the file README.fec in the distribution for more details. |
| 13 |
|
|
|
| 14 |
|
|
This library implements a simple (C<encoded_packets>,C<data_packets>) |
| 15 |
|
|
erasure code based on Vandermonde matrices. The encoder takes |
| 16 |
|
|
C<data_packets> packets of size C<block_size> each, and is able to produce |
| 17 |
|
|
up to C<encoded_packets> different encoded packets, numbered from C<0> |
| 18 |
|
|
to C<encoded_packets-1>, such that any subset of C<data_packets> members |
| 19 |
|
|
permits reconstruction of the original data. |
| 20 |
|
|
|
| 21 |
|
|
Allowed values for C<data_packets> and C<encoded_packets> must obey the |
| 22 |
|
|
following equation: |
| 23 |
|
|
|
| 24 |
|
|
data_packets <= encoded_packets <= MAXBLOCKS |
| 25 |
|
|
|
| 26 |
|
|
Where C<MAXBLOCKS=256> for the fast implementation and C<MAXBLOCKS=65536> |
| 27 |
|
|
for the slow implementation (the implementation is chosen automatically). |
| 28 |
|
|
|
| 29 |
|
|
=over 4 |
| 30 |
|
|
|
| 31 |
|
|
=cut |
| 32 |
|
|
|
| 33 |
|
|
package Convert::FEC; |
| 34 |
|
|
|
| 35 |
|
|
require XSLoader; |
| 36 |
|
|
|
| 37 |
|
|
no warnings; |
| 38 |
|
|
|
| 39 |
|
|
$VERSION = 0.01; |
| 40 |
|
|
|
| 41 |
|
|
XSLoader::load Convert::FEC, $VERSION; |
| 42 |
|
|
|
| 43 |
|
|
=item $fec = new data_packets, encoded_packets, blocksize |
| 44 |
|
|
|
| 45 |
root |
1.2 |
=item $fec->set_blocks ([array_of_blocks]) |
| 46 |
root |
1.1 |
|
| 47 |
|
|
Sets the data blocks used for the encoding. Each member of the array can either be: |
| 48 |
|
|
|
| 49 |
|
|
=over 4 |
| 50 |
|
|
|
| 51 |
|
|
=item * a string of size C<blocksize> C<exactly>. |
| 52 |
|
|
|
| 53 |
|
|
This is useful for small files (encoding entirely in memory). |
| 54 |
|
|
|
| 55 |
|
|
=item * a filehandle of a file of size C<blocksize> C<exactly>. |
| 56 |
|
|
|
| 57 |
|
|
This is useful when the amount of data is large and resides in single files. |
| 58 |
|
|
|
| 59 |
|
|
=item * a reference to an array containing a filehandle and, optionally, an offset into that file. |
| 60 |
|
|
|
| 61 |
|
|
This is useful if the amount of data is large and resides in a single |
| 62 |
|
|
file. Needless to say, all parts must not overlap and must fit into the |
| 63 |
|
|
file. |
| 64 |
|
|
|
| 65 |
|
|
=back |
| 66 |
|
|
|
| 67 |
|
|
If your data is not of the required size (i.e. a multiple of C<blocksize> |
| 68 |
|
|
bytes), then you must pad it (e.g. with zero bytes) on encoding, and |
| 69 |
|
|
truncate it after decoding. |
| 70 |
|
|
|
| 71 |
|
|
If called without arguments, the internal storage associated with the |
| 72 |
|
|
blocks is freed again. |
| 73 |
|
|
|
| 74 |
|
|
=item $block = $fec->encode (block_index) |
| 75 |
|
|
|
| 76 |
|
|
Creates a single encoded packet of index C<block_index>, which must be |
| 77 |
|
|
between C<0> and C<encoded_packets-1> (inclusive). The blocks from C<0> to |
| 78 |
|
|
C<data_packets-1> are simply copies of the original data blocks. |
| 79 |
|
|
|
| 80 |
|
|
The encoded block is returned as a perl scalar (so the blocks should fit |
| 81 |
|
|
into memory. If this is a problem for you mail me and I'll make it a file. |
| 82 |
|
|
|
| 83 |
|
|
=item $fec->decode ([array_of_blocks], [array_of_indices]) |
| 84 |
|
|
|
| 85 |
root |
1.2 |
Decode C<data_packets> of blocks (see C<set_blocks> for the |
| 86 |
root |
1.1 |
C<array_of_blocks> parameter). |
| 87 |
|
|
|
| 88 |
|
|
Since these are not necessarily the original data blocks, an array of |
| 89 |
|
|
indices (ranging from C<0> to C<encoded_packets-1>) must be supplied as |
| 90 |
|
|
the second arrayref. |
| 91 |
|
|
|
| 92 |
|
|
Both arrays must have exactly C<data_packets> entries. |
| 93 |
|
|
|
| 94 |
|
|
After decoding, the blocks will be modified in place (if necessary), and |
| 95 |
|
|
the array of indices will be updates to reflect the changes: The n-th |
| 96 |
|
|
entry in the indices array is the index of the n-th data block of the |
| 97 |
|
|
file. |
| 98 |
|
|
|
| 99 |
|
|
That is, if you call this function with C<indices = [4,3,1]>, with |
| 100 |
|
|
C<data_packets = 3>, then this array will be returned: C<[0,2,1]>. This |
| 101 |
|
|
means that input block C<0> corresponds file block C<0>, input block C<1> |
| 102 |
|
|
to file block C<2> and input block C<2> to data block C<1>. |
| 103 |
|
|
|
| 104 |
|
|
You can just iterate over this array and write out the corresponding data |
| 105 |
|
|
block (although this is inefficient). |
| 106 |
|
|
|
| 107 |
|
|
Only input blocks with indices >= C<data_packets> will be modified, blocks |
| 108 |
|
|
that already contain the original data will just be reordered. |
| 109 |
|
|
|
| 110 |
root |
1.2 |
This method destroys the block array as set up by C<set_blocks>. |
| 111 |
root |
1.1 |
|
| 112 |
root |
1.2 |
=item $fec->copy ($srcblock, $dstblock) |
| 113 |
root |
1.1 |
|
| 114 |
|
|
Utility function that simply copies one block (specified like in |
| 115 |
root |
1.2 |
C<set_blocks>) into another. This, btw., destroys the blocks set by |
| 116 |
|
|
C<set_blocks>. |
| 117 |
root |
1.1 |
|
| 118 |
|
|
If you don't understand why this helps, feel free to ignore it :) |
| 119 |
|
|
|
| 120 |
|
|
=item COMPATIBILITY |
| 121 |
|
|
|
| 122 |
|
|
The way this module works is compatible with the way freenet |
| 123 |
|
|
(L<http://freenet.sf.net>) encodes files. Comaptibility to other file |
| 124 |
|
|
formats or networks is not know, please tell me if you find more examples. |
| 125 |
|
|
|
| 126 |
|
|
=head1 SEE ALSO |
| 127 |
|
|
|
| 128 |
|
|
L<Net::FCP>. And the author, who might be happy to receive mail from any |
| 129 |
|
|
user, just to see that this rather rarely-used module is actually being |
| 130 |
|
|
used (except for freenet ;) |
| 131 |
|
|
|
| 132 |
|
|
=head1 BUGS |
| 133 |
|
|
|
| 134 |
|
|
* largely untested, please change this. |
| 135 |
|
|
* file descriptors are not supported, but should be. |
| 136 |
|
|
* utility functions for files should be provided. |
| 137 |
|
|
* 16 bit version not tested |
| 138 |
|
|
|
| 139 |
|
|
=head1 AUTHOR |
| 140 |
|
|
|
| 141 |
|
|
Marc Lehmann <pcg@goof.com> |
| 142 |
|
|
http://home.schmorp.de |
| 143 |
|
|
|
| 144 |
|
|
=cut |
| 145 |
|
|
|
| 146 |
|
|
1; |
| 147 |
|
|
|