stricter typing -- using unsigned char for indexes into shares

This commit is contained in:
Zooko O'Whielacronx 2007-01-24 15:17:51 -07:00
parent c27564a3e2
commit 2b03427f19
4 changed files with 18 additions and 20 deletions

View File

@ -510,8 +510,8 @@ fec_free (fec_t *p) {
* the encoding matrix.
*/
fec_t *
fec_new (int k, int n) {
int row, col;
fec_new (unsigned char k, unsigned char n) {
unsigned char row, col;
gf *p, *tmp_m;
fec_t *retval;
@ -521,10 +521,6 @@ fec_new (int k, int n) {
if (fec_initialized == 0)
init_fec ();
if (k < 1 || k > 256 || n > 256 || k > n) {
ERR("Invalid parameters k %d n %d GF_SIZE %d", k, n, 255);
return NULL;
}
retval = (fec_t *) my_malloc (sizeof (fec_t), "new_code");
retval->k = k;
retval->n = n;
@ -605,7 +601,7 @@ fec_encode_all(const fec_t* code, const gf*restrict const*restrict const src, gf
* @param matrix a space allocated for a k by k matrix
*/
void
build_decode_matrix_into_space(const fec_t*restrict const code, const int*const restrict index, const int k, gf*restrict const matrix) {
build_decode_matrix_into_space(const fec_t*restrict const code, const unsigned char*const restrict index, const unsigned char k, gf*restrict const matrix) {
unsigned i;
gf* p;
for (i=0, p=matrix; i < k; i++, p += k) {
@ -620,7 +616,7 @@ build_decode_matrix_into_space(const fec_t*restrict const code, const int*const
}
void
fec_decode_all(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned*restrict const index, unsigned sz) {
fec_decode_all(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned char*restrict const index, size_t sz) {
gf m_dec[code->k * code->k];
build_decode_matrix_into_space(code, index, code->k, m_dec);

View File

@ -52,7 +52,7 @@ typedef struct {
} fec_t;
void fec_free (fec_t *p);
fec_t *fec_new (int k, int n);
fec_t *fec_new (unsigned char k, unsigned char n);
/**
* @param inpkts the "primary shares" i.e. the chunks of the input data
@ -68,6 +68,6 @@ void fec_encode_all(const fec_t* code, const gf*restrict const*restrict const sr
* @param index an array of the shareids of the packets in inpkts
* @param sz size of a packet in bytes
*/
void fec_decode_all(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned*restrict const index, unsigned sz);
void fec_decode_all(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned char*restrict const index, size_t sz);
/* end of file */

View File

@ -340,7 +340,7 @@ Decoder_decode(Decoder *self, PyObject *args) {
return NULL;
const gf*restrict cshares[self->kk];
unsigned csharenums[self->kk];
unsigned char csharenums[self->kk];
gf*restrict recoveredcstrs[self->kk]; /* self->kk is actually an upper bound -- we probably won't need all of this space. */
PyObject*restrict recoveredpystrs[self->kk]; /* self->kk is actually an upper bound -- we probably won't need all of this space. */
unsigned i;
@ -374,7 +374,12 @@ Decoder_decode(Decoder *self, PyObject *args) {
for (i=0; i<self->kk; i++) {
if (!PyInt_Check(fastsharenumsitems[i]))
goto err;
csharenums[i] = PyInt_AsLong(fastsharenumsitems[i]);
long tmpl = PyInt_AsLong(fastsharenumsitems[i]);
if (tmpl < 0 || tmpl >= UCHAR_MAX) {
py_raise_fec_error("Precondition violation: Share ids can't be less than zero or greater than 255. %ld\n", tmpl);
goto err;
}
csharenums[i] = (unsigned char)tmpl;
if (csharenums[i] >= self->kk)
needtorecover+=1;

View File

@ -33,7 +33,7 @@ def _h(k, m, ss):
nums = [ x[0] for x in nums_and_shares ]
# sys.stdout.write("about to construct Decoder.\n") ; sys.stdout.flush()
decer = fec.Decoder(k, m)
# sys.stdout.write("about to decode.\n") ; sys.stdout.flush()
# sys.stdout.write("about to decode from %s.\n"%nums) ; sys.stdout.flush()
decoded = decer.decode(shares, nums)
# sys.stdout.write("decoded.\n") ; sys.stdout.flush()
_assert(len(decoded) == len(ss), len(decoded), len(ss))
@ -64,17 +64,14 @@ def pad_size(n, k):
return 0
def _test_random():
# m = random.randrange(1, 255)
m = 99
# k = random.randrange(1, m+1)
k = 33
# l = random.randrange(0, 2**16)
l = 2**12
m = random.randrange(1, 255)
k = random.randrange(1, m+1)
l = random.randrange(0, 2**16)
ss = [ randstr(l/k) + '\x00' * pad_size(l/k, k) for x in range(k) ]
_h(k, m, ss)
def test_random():
for i in range(2**9):
for i in range(2**10):
sys.stdout.write(",")
_test_random()
sys.stdout.write(".")