]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blob - zfec/zfec/easyfec.py
zfec: change formatting of copyright timestamps, licence etc.
[tahoe-lafs/zfec.git] / zfec / zfec / easyfec.py
1 # zfec -- a fast C implementation of Reed-Solomon erasure coding with
2 # command-line, C, and Python interfaces
3
4 import zfec
5
6 # div_ceil() was copied from the pyutil library.
7 def div_ceil(n, d):
8     """
9     The smallest integer k such that k*d >= n.
10     """
11     return (n/d) + (n%d != 0)
12
13 class Encoder(object):
14     def __init__(self, k, m):
15         self.fec = zfec.Encoder(k, m)
16
17     def encode(self, data):
18         """
19         @param data: string
20         """
21         chunksize = div_ceil(len(data), self.fec.k)
22         numchunks = div_ceil(len(data), chunksize)
23         l = [ data[i:i+chunksize] for i in range(0, len(data), chunksize) ]
24         # padding
25         if len(l[-1]) != len(l[0]):
26             l[-1] = l[-1] + ('\x00'*(len(l[0])-len(l[-1])))
27         res = self.fec.encode(l)
28         return res
29         
30 class Decoder(object):
31     def __init__(self, k, m):
32         self.fec = zfec.Decoder(k, m)
33
34     def decode(self, blocks, sharenums, padlen=0):
35         blocks = self.fec.decode(blocks, sharenums)
36         data = ''.join(blocks)
37         if padlen:
38             data = data[:-padlen]
39         return data
40
41
42 # zfec -- a fast C implementation of Reed-Solomon erasure coding with
43 # command-line, C, and Python interfaces
44
45 # Copyright (C) 2007 Allmydata, Inc.
46 # Author: Zooko Wilcox-O'Hearn
47 # mailto:zooko@zooko.com
48
49 # This file is part of zfec.
50
51 # This program is free software; you can redistribute it and/or modify it
52 # under the terms of the GNU General Public License as published by the Free
53 # Software Foundation; either version 2 of the License, or (at your option)
54 # any later version.  This package also comes with the added permission that,
55 # in the case that you are obligated to release a derived work under this
56 # licence (as per section 2.b of the GPL), you may delay the fulfillment of
57 # this obligation for up to 12 months.
58
59 # This program is distributed in the hope that it will be useful,
60 # but WITHOUT ANY WARRANTY; without even the implied warranty of
61 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62 # GNU General Public License for more details.
63
64 # You should have received a copy of the GNU General Public License
65 # along with this program; if not, write to the Free Software
66 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
67