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)
42 # zfec -- a fast C implementation of Reed-Solomon erasure coding with
43 # command-line, C, and Python interfaces
45 # Copyright (C) 2007 Allmydata, Inc.
46 # Author: Zooko Wilcox-O'Hearn
47 # mailto:zooko@zooko.com
49 # This file is part of zfec.
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.
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.
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.