| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
root |
1.3 |
Algorithm::FEC - Forward Error Correction using Vandermonde Matrices |
| 4 |
root |
1.1 |
|
| 5 |
|
|
=head1 SYNOPSIS |
| 6 |
|
|
|
| 7 |
root |
1.3 |
use Algorithm::FEC; |
| 8 |
root |
1.1 |
|
| 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 |
root |
1.7 |
This library implements a simple (C<encoded_blocks>,C<data_blocks>) |
| 15 |
root |
1.1 |
erasure code based on Vandermonde matrices. The encoder takes |
| 16 |
root |
1.7 |
C<data_blocks> blocks of size C<block_size> each, and is able to produce |
| 17 |
|
|
up to C<encoded_blocks> different encoded blocks, numbered from C<0> |
| 18 |
|
|
to C<encoded_blocks-1>, such that any subset of C<data_blocks> members |
| 19 |
root |
1.1 |
permits reconstruction of the original data. |
| 20 |
|
|
|
| 21 |
root |
1.7 |
Allowed values for C<data_blocks> and C<encoded_blocks> must obey the |
| 22 |
root |
1.1 |
following equation: |
| 23 |
|
|
|
| 24 |
root |
1.7 |
data_blocks <= encoded_blocks <= MAXBLOCKS |
| 25 |
root |
1.1 |
|
| 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 |
root |
1.3 |
package Algorithm::FEC; |
| 34 |
root |
1.1 |
|
| 35 |
|
|
require XSLoader; |
| 36 |
|
|
|
| 37 |
|
|
no warnings; |
| 38 |
|
|
|
| 39 |
root |
1.8 |
$VERSION = 0.5; |
| 40 |
root |
1.1 |
|
| 41 |
root |
1.3 |
XSLoader::load Algorithm::FEC, $VERSION; |
| 42 |
root |
1.1 |
|
| 43 |
root |
1.7 |
=item $fec = new data_blocks, encoded_blocks, blocksize |
| 44 |
root |
1.1 |
|
| 45 |
root |
1.6 |
=item $fec->set_encode_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 |
root |
1.8 |
bytes), then you must pad it (e.g. with zero bytes) on encoding (and you |
| 69 |
|
|
should truncate it after decoding). Otherwise, this library croaks. |
| 70 |
|
|
|
| 71 |
|
|
Future versions might instead load the short segment into memory or extend |
| 72 |
|
|
your scalar (this might enable nice tricks, like C<$fec->copy (..., my |
| 73 |
|
|
$x)> :). Mail me if you want this to happen. |
| 74 |
root |
1.1 |
|
| 75 |
|
|
If called without arguments, the internal storage associated with the |
| 76 |
|
|
blocks is freed again. |
| 77 |
|
|
|
| 78 |
|
|
=item $block = $fec->encode (block_index) |
| 79 |
|
|
|
| 80 |
root |
1.7 |
Creates a single encoded block of index C<block_index>, which must be |
| 81 |
|
|
between C<0> and C<encoded_blocks-1> (inclusive). The blocks from C<0> to |
| 82 |
|
|
C<data_blocks-1> are simply copies of the original data blocks. |
| 83 |
root |
1.1 |
|
| 84 |
|
|
The encoded block is returned as a perl scalar (so the blocks should fit |
| 85 |
|
|
into memory. If this is a problem for you mail me and I'll make it a file. |
| 86 |
|
|
|
| 87 |
root |
1.6 |
=item $fec->set_decode_blocks ([array_of_blocks], [array_of_indices]) |
| 88 |
root |
1.1 |
|
| 89 |
root |
1.7 |
Prepares to decode C<data_blocks> of blocks (see C<set_encode_blocks> for |
| 90 |
root |
1.6 |
the C<array_of_blocks> parameter). |
| 91 |
root |
1.1 |
|
| 92 |
|
|
Since these are not necessarily the original data blocks, an array of |
| 93 |
root |
1.7 |
indices (ranging from C<0> to C<encoded_blocks-1>) must be supplied as |
| 94 |
root |
1.1 |
the second arrayref. |
| 95 |
|
|
|
| 96 |
root |
1.7 |
Both arrays must have exactly C<data_blocks> entries. |
| 97 |
root |
1.1 |
|
| 98 |
root |
1.6 |
This method also reorders the blocks and index array in place (if |
| 99 |
|
|
necessary) to reflect the order the blocks will have in the decoded |
| 100 |
|
|
result. |
| 101 |
|
|
|
| 102 |
root |
1.7 |
Both arrays must have exactly C<data_blocks> entries. |
| 103 |
root |
1.6 |
|
| 104 |
|
|
The index array represents the decoded ordering, in that the n-th entry |
| 105 |
|
|
in the indices array corresponds to the n-th data block of the decoded |
| 106 |
|
|
result. The value stored in the n-th place in the array will contain the |
| 107 |
|
|
index of the encoded data block. |
| 108 |
|
|
|
| 109 |
root |
1.7 |
Input blocks with indices less than C<data_blocks> will be moved to their |
| 110 |
root |
1.6 |
final position (block k to position k), while the gaps between them will |
| 111 |
|
|
be filled with check blocks. The decoding process will not modify the |
| 112 |
|
|
already decoded data blocks, but will modify the check blocks. |
| 113 |
root |
1.1 |
|
| 114 |
|
|
That is, if you call this function with C<indices = [4,3,1]>, with |
| 115 |
root |
1.7 |
C<data_blocks = 3>, then this array will be returned: C<[0,2,1]>. This |
| 116 |
root |
1.4 |
means that input block C<0> corresponds to file block C<0>, input block |
| 117 |
|
|
C<1> to file block C<2> and input block C<2> to data block C<1>. |
| 118 |
root |
1.1 |
|
| 119 |
|
|
You can just iterate over this array and write out the corresponding data |
| 120 |
root |
1.4 |
block (although this is inefficient): |
| 121 |
|
|
|
| 122 |
root |
1.6 |
for my $i (0 .. $#idx) |
| 123 |
|
|
if ($idx[$i] != $i) # need we move this block? |
| 124 |
root |
1.7 |
copy encoded block $idx[$i] to position $i |
| 125 |
root |
1.6 |
} |
| 126 |
root |
1.4 |
} |
| 127 |
root |
1.1 |
|
| 128 |
root |
1.6 |
The C<copy> method can be helpful here. |
| 129 |
root |
1.1 |
|
| 130 |
root |
1.6 |
This method destroys the block array as set up by C<set_encode_blocks>. |
| 131 |
root |
1.7 |
|
| 132 |
|
|
=item $fec->shuffle ([array_of_blocks], [array_of_indices]) |
| 133 |
|
|
|
| 134 |
|
|
The same same as C<set_decode_blocks>, with the exception that the blocks |
| 135 |
|
|
are not actually set for decoding. |
| 136 |
|
|
|
| 137 |
|
|
This method is not normally used, but if you want to move blocks |
| 138 |
|
|
around after reodering and before decoding, then calling Cshuffle> |
| 139 |
|
|
followed by C<set_decode_blocks> incurs lower overhead than calling |
| 140 |
|
|
C<set_decode_blocks> twice, as files are not mmapped etc. |
| 141 |
root |
1.6 |
|
| 142 |
|
|
=item $fec->decode |
| 143 |
|
|
|
| 144 |
|
|
Decode the blocks set by a prior call to C<set_decode_blocks>. |
| 145 |
|
|
|
| 146 |
|
|
This method destroys the block array as set up by C<set_decode_blocks>. |
| 147 |
root |
1.1 |
|
| 148 |
root |
1.2 |
=item $fec->copy ($srcblock, $dstblock) |
| 149 |
root |
1.1 |
|
| 150 |
|
|
Utility function that simply copies one block (specified like in |
| 151 |
root |
1.6 |
C<set_encode_blocks>) into another. This, btw., destroys the blocks set by |
| 152 |
|
|
C<set_*_blocks>. |
| 153 |
root |
1.1 |
|
| 154 |
root |
1.8 |
=back |
| 155 |
|
|
|
| 156 |
|
|
=head1 COMPATIBILITY |
| 157 |
root |
1.1 |
|
| 158 |
|
|
The way this module works is compatible with the way freenet |
| 159 |
|
|
(L<http://freenet.sf.net>) encodes files. Comaptibility to other file |
| 160 |
|
|
formats or networks is not know, please tell me if you find more examples. |
| 161 |
|
|
|
| 162 |
|
|
=head1 SEE ALSO |
| 163 |
|
|
|
| 164 |
|
|
L<Net::FCP>. And the author, who might be happy to receive mail from any |
| 165 |
|
|
user, just to see that this rather rarely-used module is actually being |
| 166 |
|
|
used (except for freenet ;) |
| 167 |
|
|
|
| 168 |
|
|
=head1 BUGS |
| 169 |
|
|
|
| 170 |
root |
1.6 |
* too complicated. |
| 171 |
root |
1.1 |
* largely untested, please change this. |
| 172 |
|
|
* file descriptors are not supported, but should be. |
| 173 |
|
|
* utility functions for files should be provided. |
| 174 |
|
|
* 16 bit version not tested |
| 175 |
|
|
|
| 176 |
|
|
=head1 AUTHOR |
| 177 |
|
|
|
| 178 |
|
|
Marc Lehmann <pcg@goof.com> |
| 179 |
|
|
http://home.schmorp.de |
| 180 |
|
|
|
| 181 |
|
|
=cut |
| 182 |
|
|
|
| 183 |
|
|
1; |
| 184 |
|
|
|