1 # zfec -- a fast C implementation of Reed-Solomon erasure coding with
2 # command-line, C, and Python interfaces
6 # div_ceil() was copied from the pyutil library.
9 The smallest integer k such that k*d >= n.
11 return (n/d) + (n%d != 0)
13 class Encoder(object):
14 def __init__(self, k, m):
15 self.fec = zfec.Encoder(k, m)
17 def encode(self, data):
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) ]
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)
30 class Decoder(object):
31 def __init__(self, k, m):
32 self.fec = zfec.Decoder(k, m)
34 def decode(self, blocks, sharenums, padlen=0):
35 blocks = self.fec.decode(blocks, sharenums)
36 data = ''.join(blocks)
41 # zfec -- fast forward error correction library with Python interface
43 # Copyright (C) 2007 Allmydata, Inc.
44 # Author: Zooko Wilcox-O'Hearn
46 # This file is part of zfec.
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.
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/.
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