[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[minion-cvs] More wrapper work
Update of /home/minion/cvsroot/src/minion/src
In directory moria.mit.edu:/tmp/cvs-serv4063/src
Modified Files:
fec.c
Log Message:
More wrapper work
Index: fec.c
===================================================================
RCS file: /home/minion/cvsroot/src/minion/src/fec.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- fec.c 7 Jul 2003 20:03:07 -0000 1.1
+++ fec.c 7 Jul 2003 22:20:15 -0000 1.2
@@ -776,6 +776,12 @@
/* ======= PYTHON INTERFACE ====== */
+char mm_FECError__doc__[] =
+ "mixminion._minionlib.FECError\n\n"
+ "Exception raised for error in the FEC library.\n";
+
+PyObject *mm_FECError = NULL;
+
typedef struct mm_FEC {
PyObject_HEAD;
struct fec_parms *fec;
@@ -798,7 +804,7 @@
return NULL;
if (k < 1 || n < 1 || k > 255 || n > 255) {
- PyErr_SetString(mm_CryptoError, "K or N is out of range");
+ PyErr_SetString(mm_FECError, "K or N is out of range");
return NULL;
}
@@ -811,7 +817,7 @@
Py_END_ALLOW_THREADS
if (!output->fec) {
Py_DECREF(output);
- PyMem_Error();
+ PyErr_NoMemory();
return NULL;
}
@@ -826,13 +832,115 @@
PyObject_DEL(self);
}
+static PyObject*
+mm_FEC_getParameters(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ struct fec_parms *fec;
+ if (PyTuple_Size(args)) {
+ PyErr_SetString(PyExc_TypeError, "No arguments expected");
+ return NULL;
+ }
+ fec = ((mm_FEC)self)->fec;
+
+ return Py_BuildValue("ii", fec->k, fec->n);
+}
+
+
+
static PyObject *
mm_FEC_encode(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "idx", "blocks", NULL };
+ struct fec_parms *fec;
+ int idx;
+ PyObject *blocks;
+
+ int sz = -1;
+ int i;
+ PyObject *o;
+
+ PyObject *tup = NULL;
+ char **stringPtrs = NULL;
+ PyObject *result = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "iO:encode", kwlist,
+ &idx, &blocks))
+ return NULL;
+
+ fec = ((mm_FEC*)self)->fec;
+ /* Check whether the second arg is a sequence of N equally sized
+ * strings. */
+ if (!PySequence_Check(blocks)) {
+ PyErr_SetString(mm_FECError, "encode expects a list");
+ return NULL;
+ }
+ if (PySequence_Size(blocks) != fec->k) {
+ PyErr_SetString(mm_FECError,
+ "encode expects a list of length K");
+ return NULL;
+ }
+ if (idx < 0 || idx >= fec->n) {
+ PyErr_SetString(mm_FECError, "idx out of bounds");
+ return NULL;
+ }
+
+ /* We hold onto the sequence as a tuple to prevent it changing
+ * out from under us, and to temporarily incref all the strings.
+ */
+ if (!(tup = PySequence_Tuple(blocks))) {
+ return NULL;
+ }
+ if (!stringPtrs = malloc((sizeof gf*)*fec->n)) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ for (i = 0; i < fec->n; ++i) {
+ o = PyTuple_GET_ITEM(tup, i);
+ if (!PyString_Check(o)) {
+ PyErr_SetString(mm_FECError,
+ "encode expects a list of strings");
+ goto err;
+ }
+ if (sz<0)
+ sz = PyString_Length(o);
+ else if (sz != PyString_Length(o)) {
+ PyErr_SetString(mm_FECError,
+ "encode expects a list of equally long strings");
+ goto err;
+ }
+ stringPtrs[i] = PyString_AS_STRING(o);
+ }
+ /* We could pull this up, but it's good to do all the checking. */
+ if (idx < fec->k) {
+ result = PyTuple_GET_ITEM(tup, index);
+ Py_INCREF(result);
+ Py_DECREF(tup);
+ free(stringPtrs);
+ return result;
+ }
+ if (!(result = PyString_FromStringAndSize(NULL, sz))) {
+ PyErr_NoMemory(); goto err;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ fec_encode(fec, (gf**)stringPtrs, (gf*)PyString_AsString(result),
+ idx, sz);
+ Py_END_ALLOW_THREADS
+
+ Py_DECREF(tup);
+ free(stringPtrs);
+ return result;
+ err:
+ if (stringPtrs)
+ free(stringPtrs);
+ if (tup)
+ Py_DECREF(tup);
+ if (result)
+ Py_DECREF(result);
return NULL;
}
@@ -841,6 +949,8 @@
{
static char *kwlist[] = { "blocks", NULL };
+
+
return NULL;
@@ -875,3 +985,11 @@
0,0,
(char*)mm_FEC_Type__doc__
};
+
+/*
+ Local Variables:
+ mode:c
+ indent-tabs-mode:nil
+ c-basic-offset:8
+ End:
+*/