From c8b1eec75ab6d90c0d3dc7624bceb8874e1191ee Mon Sep 17 00:00:00 2001
From: Zooko O'Whielacronx <zooko@zooko.com>
Date: Wed, 11 Apr 2007 10:34:27 -0700
Subject: [PATCH] merge changes and fix wrong type -- k and m need more than 8
 bits (because they are the count rather than the index, i.e. they are
 1-indexed)

---
 pyfec/fec/_fecmodule.c       | 24 ++++++++++++------------
 pyfec/fec/fec.c              | 10 +++++-----
 pyfec/fec/test/test_pyfec.py | 13 ++++++++++++-
 3 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/pyfec/fec/_fecmodule.c b/pyfec/fec/_fecmodule.c
index 1a69e00e..2c7fa1b2 100644
--- a/pyfec/fec/_fecmodule.c
+++ b/pyfec/fec/_fecmodule.c
@@ -71,8 +71,8 @@ typedef struct {
     PyObject_HEAD
 
     /* expose these */
-    int kk;
-    int mm;
+    unsigned short kk;
+    unsigned short mm;
 
     /* internal */
     fec_t* fec_matrix;
@@ -111,16 +111,16 @@ Encoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
         py_raise_fec_error("Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", self->mm);
 	return -1;
     }
-    if (self->mm > 255) {
-        py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 255, but it was %d", self->mm);
+    if (inm > 256) {
+        py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 256, but it was %d", self->mm);
 	return -1;
     }
     if (ink > inm) {
         py_raise_fec_error("Precondition violation: first argument is required to be less than or equal to the second argument, but they were %d and %d respectively", ink, inm);
 	return -1;
     }
-    self->kk = (unsigned char)ink;
-    self->mm = (unsigned char)inm;
+    self->kk = (unsigned short)ink;
+    self->mm = (unsigned short)inm;
     self->fec_matrix = fec_new(self->kk, self->mm);
 
     return 0;
@@ -326,8 +326,8 @@ typedef struct {
     PyObject_HEAD
 
     /* expose these */
-    int kk;
-    int mm;
+    unsigned short kk;
+    unsigned short mm;
 
     /* internal */
     fec_t* fec_matrix;
@@ -367,16 +367,16 @@ Decoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
         py_raise_fec_error("Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", self->mm);
 	return -1;
     }
-    if (self->mm > 255) {
-        py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 255, but it was %d", self->mm);
+    if (inm > 256) {
+        py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 256, but it was %d", self->mm);
 	return -1;
     }
     if (ink > inm) {
         py_raise_fec_error("Precondition violation: first argument is required to be less than or equal to the second argument, but they were %d and %d respectively", ink, inm);
 	return -1;
     }
-    self->kk = (unsigned char)ink;
-    self->mm = (unsigned char)inm;
+    self->kk = (unsigned short)ink;
+    self->mm = (unsigned short)inm;
     self->fec_matrix = fec_new(self->kk, self->mm);
 
     return 0;
diff --git a/pyfec/fec/fec.c b/pyfec/fec/fec.c
index a848d0f3..dce7706c 100644
--- a/pyfec/fec/fec.c
+++ b/pyfec/fec/fec.c
@@ -434,14 +434,14 @@ _invert_mat(gf* src, unsigned k) {
  * p = coefficients of the matrix (p_i)
  * q = values of the polynomial (known)
  */
-int
-_invert_vdm (gf * src, int k) {
-    int i, j, row, col;
+void
+_invert_vdm (gf* src, unsigned k) {
+    unsigned i, j, row, col;
     gf *b, *c, *p;
     gf t, xx;
 
     if (k == 1)                   /* degenerate case, matrix must be p^0 = 1 */
-        return 0;
+        return;
     /*
      * c holds the coefficient of P(x) = Prod (x - p_i), i=0..k-1
      * b holds the coefficient for the matrix inversion
@@ -486,7 +486,7 @@ _invert_vdm (gf * src, int k) {
     free (c);
     free (b);
     free (p);
-    return 0;
+    return;
 }
 
 static int fec_initialized = 0;
diff --git a/pyfec/fec/test/test_pyfec.py b/pyfec/fec/test/test_pyfec.py
index 013a3893..e536d3b1 100644
--- a/pyfec/fec/test/test_pyfec.py
+++ b/pyfec/fec/test/test_pyfec.py
@@ -30,6 +30,17 @@ import sys
 
 import fec
 
+from base64 import b32encode
+def ab(x): # debuggery
+    if len(x) >= 3:
+        return "%s:%s" % (len(x), b32encode(x[-3:]),)
+    elif len(x) == 2:
+        return "%s:%s" % (len(x), b32encode(x[-2:]),)
+    elif len(x) == 1:
+        return "%s:%s" % (len(x), b32encode(x[-1:]),)
+    elif len(x) == 0:
+        return "%s:%s" % (len(x), "--empty--",)
+
 def _h(k, m, ss):
     # sys.stdout.write("k: %s, m: %s,  len(ss): %r, len(ss[0]): %r" % (k, m, len(ss), len(ss[0]),)) ; sys.stdout.flush()
     encer = fec.Encoder(k, m)
@@ -47,7 +58,7 @@ def _h(k, m, ss):
     decoded = decer.decode(blocks, nums)
     # sys.stdout.write("decoded.\n") ; sys.stdout.flush()
     assert len(decoded) == len(ss), (len(decoded), len(ss),)
-    assert tuple([str(s) for s in decoded]) == tuple([str(s) for s in ss]), (tuple([str(s) for s in decoded]), tuple([str(s) for s in ss]),)
+    assert tuple([str(s) for s in decoded]) == tuple([str(s) for s in ss]), (tuple([ab(str(s)) for s in decoded]), tuple([ab(str(s)) for s in ss]),)
 
 def randstr(n):
     return ''.join(map(chr, map(random.randrange, [0]*n, [256]*n)))
-- 
2.45.2