]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blobdiff - zfec/zfec/_fecmodule.c
stick a .gitignore file
[tahoe-lafs/zfec.git] / zfec / zfec / _fecmodule.c
index 21e3e628320e2685ba73a22279aad1a1b8e92a46..a742051804f309231444e642c9302045c4f9d18f 100644 (file)
@@ -4,6 +4,8 @@
 
 #include <Python.h>
 #include <structmember.h>
+#include <stddef.h>
+
 
 #if (PY_VERSION_HEX < 0x02050000)
 typedef int Py_ssize_t;
@@ -29,8 +31,8 @@ typedef struct {
     PyObject_HEAD
 
     /* expose these */
-    short kk;
-    short mm;
+    unsigned short kk;
+    unsigned short mm;
 
     /* internal */
     fec_t* fec_matrix;
@@ -62,23 +64,23 @@ Encoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
         return -1;
 
     if (ink < 1) {
-        PyErr_Format(py_fec_error, "Precondition violation: first argument is required to be greater than or equal to 1, but it was %d", self->kk);
+        PyErr_Format(py_fec_error, "Precondition violation: first argument is required to be greater than or equal to 1, but it was %d", ink);
         return -1;
     }
     if (inm < 1) {
-        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", self->mm);
+        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", inm);
         return -1;
     }
     if (inm > 256) {
-        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be less than or equal to 256, but it was %d", self->mm);
+        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be less than or equal to 256, but it was %d", inm);
         return -1;
     }
     if (ink > inm) {
         PyErr_Format(py_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 = (short)ink;
-    self->mm = (short)inm;
+    self->kk = (unsigned short)ink;
+    self->mm = (unsigned short)inm;
     self->fec_matrix = fec_new(self->kk, self->mm);
 
     return 0;
@@ -167,7 +169,7 @@ Encoder_encode(Encoder *self, PyObject *args) {
         }
         oldsz = sz;
     }
-    
+
     /* Allocate space for all of the check blocks. */
 
     for (i=0; i<num_desired_blocks; i++) {
@@ -220,7 +222,8 @@ Encoder_encode(Encoder *self, PyObject *args) {
 
 static void
 Encoder_dealloc(Encoder * self) {
-    fec_free(self->fec_matrix);
+    if (self->fec_matrix)
+        fec_free(self->fec_matrix);
     self->ob_type->tp_free((PyObject*)self);
 }
 
@@ -287,8 +290,8 @@ typedef struct {
     PyObject_HEAD
 
     /* expose these */
-    short kk;
-    short mm;
+    unsigned short kk;
+    unsigned short mm;
 
     /* internal */
     fec_t* fec_matrix;
@@ -321,23 +324,23 @@ Decoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
         return -1;
 
     if (ink < 1) {
-        PyErr_Format(py_fec_error, "Precondition violation: first argument is required to be greater than or equal to 1, but it was %d", self->kk);
+        PyErr_Format(py_fec_error, "Precondition violation: first argument is required to be greater than or equal to 1, but it was %d", ink);
        return -1;
     }
     if (inm < 1) {
-        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", self->mm);
+        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", inm);
        return -1;
     }
     if (inm > 256) {
-        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be less than or equal to 256, but it was %d", self->mm);
+        PyErr_Format(py_fec_error, "Precondition violation: second argument is required to be less than or equal to 256, but it was %d", inm);
        return -1;
     }
     if (ink > inm) {
         PyErr_Format(py_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 = (short)ink;
-    self->mm = (short)inm;
+    self->kk = (unsigned short)ink;
+    self->mm = (unsigned short)inm;
     self->fec_matrix = fec_new(self->kk, self->mm);
 
     return 0;
@@ -386,11 +389,11 @@ Decoder_decode(Decoder *self, PyObject *args) {
         goto err;
 
     if (PySequence_Fast_GET_SIZE(fastblocks) != self->kk) {
-        PyErr_Format(py_fec_error, "Precondition violation: Wrong length -- first argument is required to contain exactly k blocks.  len(first): %Zu, k: %d", PySequence_Fast_GET_SIZE(fastblocks), self->kk); 
+        PyErr_Format(py_fec_error, "Precondition violation: Wrong length -- first argument is required to contain exactly k blocks.  len(first): %Zu, k: %d", PySequence_Fast_GET_SIZE(fastblocks), self->kk);
         goto err;
     }
     if (PySequence_Fast_GET_SIZE(fastblocknums) != self->kk) {
-        PyErr_Format(py_fec_error, "Precondition violation: Wrong length -- blocknums is required to contain exactly k blocks.  len(blocknums): %Zu, k: %d", PySequence_Fast_GET_SIZE(fastblocknums), self->kk); 
+        PyErr_Format(py_fec_error, "Precondition violation: Wrong length -- blocknums is required to contain exactly k blocks.  len(blocknums): %Zu, k: %d", PySequence_Fast_GET_SIZE(fastblocknums), self->kk);
         goto err;
     }
 
@@ -492,7 +495,8 @@ Decoder_decode(Decoder *self, PyObject *args) {
 
 static void
 Decoder_dealloc(Decoder * self) {
-    fec_free(self->fec_matrix);
+    if (self->fec_matrix)
+        fec_free(self->fec_matrix);
     self->ob_type->tp_free((PyObject*)self);
 }
 
@@ -549,34 +553,40 @@ static PyTypeObject Decoder_type = {
     Decoder_new,                 /* tp_new */
 };
 
+
 void
 _hexwrite(unsigned char*s, size_t l) {
-  for (size_t i = 0; i < l; i++)
+  size_t i;
+  for (i = 0; i < l; i++)
     printf("%.2x", s[i]);
 }
 
 
 PyObject*
-test_from_agl(); /* <- prototype */
-
-PyObject*
-test_from_agl() {
+test_from_agl(PyObject* self, PyObject* args) {
   unsigned char b0c[8], b1c[8];
   unsigned char b0[8], b1[8], b2[8], b3[8], b4[8];
-  memset(b0, 1, 8);
-  memset(b1, 2, 8);
-  memset(b2, 3, 8);
+
   const unsigned char *blocks[3] = {b0, b1, b2};
   unsigned char *outblocks[2] = {b3, b4};
   unsigned block_nums[] = {3, 4};
 
+  fec_t *const fec = fec_new(3, 5);
+
+  const unsigned char *inpkts[] = {b3, b4, b2};
+  unsigned char *outpkts[] = {b0, b1};
+  unsigned indexes[] = {3, 4, 2};
+
+  memset(b0, 1, 8);
+  memset(b1, 2, 8);
+  memset(b2, 3, 8);
+
   /*printf("_from_c before encoding:\n");
   printf("b0: "); _hexwrite(b0, 8); printf(", ");
   printf("b1: "); _hexwrite(b1, 8); printf(", ");
   printf("b2: "); _hexwrite(b2, 8); printf(", ");
   printf("\n");*/
 
-  fec_t *const fec = fec_new(3, 5);
   fec_encode(fec, blocks, outblocks, block_nums, 2, 8);
 
   /*printf("after encoding:\n");
@@ -586,10 +596,6 @@ test_from_agl() {
 
   memcpy(b0c, b0, 8); memcpy(b1c, b1, 8);
 
-  const unsigned char *inpkts[] = {b3, b4, b2};
-  unsigned char *outpkts[] = {b0, b1};
-  unsigned indexes[] = {3, 4, 2};
-
   fec_decode(fec, inpkts, outpkts, indexes, 8);
 
   /*printf("after decoding:\n");
@@ -603,9 +609,9 @@ test_from_agl() {
     Py_RETURN_FALSE;
 }
 
-static PyMethodDef fec_functions[] = { 
-  {"test_from_agl", test_from_agl, METH_NOARGS, NULL},
-    {NULL} 
+static PyMethodDef fec_functions[] = {
+    {"test_from_agl", test_from_agl, METH_NOARGS, NULL},
+    {NULL}
 };
 
 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */