]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blobdiff - zfec/zfec/_fecmodule.c
narrow types of k and m from unsigned to unsigned short
[tahoe-lafs/zfec.git] / zfec / zfec / _fecmodule.c
index abe21ec15e08d55dd5b37c60f28186605a02283e..e9449c9f373eb502655190f2aec171f087e97d91 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <Python.h>
 #include <structmember.h>
+#include <stddef.h>
 
 #if (PY_VERSION_HEX < 0x02050000)
 typedef int Py_ssize_t;
@@ -13,22 +14,6 @@ typedef int Py_ssize_t;
 
 #include "stdarg.h"
 
-#if defined(_MSC_VER)
-// actually, some of the flavors (i.e. Enterprise) do support restrict
-//#define restrict __restrict
-#define restrict
-#define inline __inline
-#define alloca _alloca
-#else
-#ifdef __GNUC__
-#define alloca(x) __builtin_alloca(x)
-#else
-#include <alloca.h>
-#endif
-#endif
-
-
-
 static PyObject *py_fec_error;
 
 static char fec__doc__[] = "\
@@ -45,8 +30,8 @@ typedef struct {
     PyObject_HEAD
 
     /* expose these */
-    short kk;
-    short mm;
+    unsigned short kk;
+    unsigned short mm;
 
     /* internal */
     fec_t* fec_matrix;
@@ -74,27 +59,27 @@ Encoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
         NULL
     };
     int ink, inm;
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii", kwlist, &ink, &inm))
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii:Encoder.__init__", kwlist, &ink, &inm))
         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;
@@ -129,7 +114,7 @@ Encoder_encode(Encoder *self, PyObject *args) {
     Py_ssize_t sz, oldsz = 0;
     unsigned char check_block_index = 0; /* index into the check_blocks_produced and (parallel) pystrs_produced arrays */
 
-    if (!PyArg_ParseTuple(args, "O|O", &inblocks, &desired_blocks_nums))
+    if (!PyArg_ParseTuple(args, "O|O:Encoder.encode", &inblocks, &desired_blocks_nums))
         return NULL;
 
     for (i=0; i<self->mm - self->kk; i++)
@@ -161,7 +146,7 @@ Encoder_encode(Encoder *self, PyObject *args) {
         goto err;
 
     if (PySequence_Fast_GET_SIZE(fastinblocks) != self->kk) {
-        PyErr_Format(py_fec_error, "Precondition violation: Wrong length -- first argument (the sequence of input blocks) is required to contain exactly k blocks.  len(first): %d, k: %d", PySequence_Fast_GET_SIZE(fastinblocks), self->kk);
+        PyErr_Format(py_fec_error, "Precondition violation: Wrong length -- first argument (the sequence of input blocks) is required to contain exactly k blocks.  len(first): %Zu, k: %d", PySequence_Fast_GET_SIZE(fastinblocks), self->kk);
         goto err;
     }
 
@@ -236,7 +221,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);
 }
 
@@ -303,8 +289,8 @@ typedef struct {
     PyObject_HEAD
 
     /* expose these */
-    short kk;
-    short mm;
+    unsigned short kk;
+    unsigned short mm;
 
     /* internal */
     fec_t* fec_matrix;
@@ -333,27 +319,27 @@ Decoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
     };
 
     int ink, inm;
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii", kwlist, &ink, &inm))
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii:Decoder.__init__", kwlist, &ink, &inm))
         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;
@@ -389,7 +375,7 @@ Decoder_decode(Decoder *self, PyObject *args) {
     long tmpl;
     unsigned nextrecoveredix=0;
 
-    if (!PyArg_ParseTuple(args, "OO", &blocks, &blocknums))
+    if (!PyArg_ParseTuple(args, "OO:Decoder.decode", &blocks, &blocknums))
         return NULL;
 
     for (i=0; i<self->kk; i++)
@@ -402,11 +388,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): %d, 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): %d, 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;
     }
 
@@ -445,7 +431,9 @@ Decoder_decode(Decoder *self, PyObject *args) {
         oldsz = sz;
     }
 
-    /* move src packets into position */
+    /* Move src packets into position.  At the end of this loop we want the i'th
+       element of the arrays to be the block with block number i, if that block
+       is among our inputs. */
     for (i=0; i<self->kk;) {
         if (cblocknums[i] >= self->kk || cblocknums[i] == i)
             i++;
@@ -506,7 +494,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);
 }
 
@@ -563,7 +552,59 @@ static PyTypeObject Decoder_type = {
     Decoder_new,                 /* tp_new */
 };
 
-static PyMethodDef fec_methods[] = { 
+void
+_hexwrite(unsigned char*s, size_t l) {
+  for (size_t i = 0; i < l; i++)
+    printf("%.2x", s[i]);
+}
+
+
+PyObject*
+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};
+
+  /*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");
+  printf("b3: "); _hexwrite(b3, 8); printf(", ");
+  printf("b4: "); _hexwrite(b4, 8); printf(", ");
+  printf("\n");*/
+
+  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");
+  printf("b0: "); _hexwrite(b0, 8); printf(", ");
+  printf("b1: "); _hexwrite(b1, 8);
+  printf("\n");*/
+
+  if ((memcmp(b0, b0c,8) == 0) && (memcmp(b1, b1c,8) == 0))
+    Py_RETURN_TRUE;
+  else
+    Py_RETURN_FALSE;
+}
+
+static PyMethodDef fec_functions[] = { 
+    {"test_from_agl", test_from_agl, METH_NOARGS, NULL},
     {NULL} 
 };
 
@@ -580,7 +621,7 @@ init_fec(void) {
     if (PyType_Ready(&Decoder_type) < 0)
         return;
 
-    module = Py_InitModule3("_fec", fec_methods, fec__doc__);
+    module = Py_InitModule3("_fec", fec_functions, fec__doc__);
     if (module == NULL)
       return;
 
@@ -596,28 +637,6 @@ init_fec(void) {
 }
 
 /**
- * zfec -- fast forward error correction library with Python interface
- * 
- * Copyright (C) 2007 Allmydata, Inc.
- * Author: Zooko Wilcox-O'Hearn
- * 
- * This file is part of zfec.
- * 
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version, with the added permission that, if you become obligated
- * to release a derived work under this licence (as per section 2.b), you may
- * delay the fulfillment of this obligation for up to 12 months.  See the file
- * COPYING for details.
- *
- * If you would like to inquire about a commercial relationship with Allmydata,
- * Inc., please contact partnerships@allmydata.com and visit
- * http://allmydata.com/.
+ * originally inspired by fecmodule.c by the Mnet Project, especially Myers
+ * Carpenter and Hauke Johannknecht
  */
-
-/**
- * based on fecmodule.c by the Mnet Project, especially Myers Carpenter and
- * Hauke Johannknecht
- */
-