]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blob - zfec/zfec/easyfec.py
zfec: yet another tweak to the licence
[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 # zfec -- fast forward error correction library with Python interface
42
43 # Copyright (C) 2007 Allmydata, Inc.
44 # Author: Zooko Wilcox-O'Hearn
45
46 # This file is part of zfec.
47 #
48 # This program is free software; you can redistribute it and/or modify it
49 # under the terms of the GNU General Public License as published by the Free
50 # Software Foundation; either version 2 of the License, or (at your option)
51 # any later version, with the added permission that, if you become obligated
52 # to release a derived work under this licence (as per section 2.b), you may
53 # delay the fulfillment of this obligation for up to 12 months.  See the file
54 # COPYING for details.
55 #
56 # If you would like to inquire about a commercial relationship with Allmydata,
57 # Inc., please contact partnerships@allmydata.com and visit
58 # http://allmydata.com/.
59
60 # This program is distributed in the hope that it will be useful, but WITHOUT
61 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
62 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
63 # more details.
64