*.c: Introduce a new input conversion for binary strings.
[catacomb-python] / share.c
diff --git a/share.c b/share.c
index 3248f78..eadaa48 100644 (file)
--- a/share.c
+++ b/share.c
@@ -112,19 +112,19 @@ static const PyTypeObject gfshare_pytype_skel = {
 static PyObject *gfsharesplit_pynew(PyTypeObject *ty,
                                    PyObject *arg, PyObject *kw)
 {
-  char *p;
-  Py_ssize_t n;
+  struct bin in;
   unsigned t;
   grand *r = &rand_global;
   gfshare_pyobj *s;
   static const char *const kwlist[] = { "threshold", "secret", "rng", 0 };
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&s#|O&:new", KWLIST,
-                                  convuint, &t, &p, &n, convgrand, &r))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&:new", KWLIST,
+                                  convuint, &t, convbin, &in,
+                                  convgrand, &r))
     goto end;
   if (!t || t > 255) VALERR("threshold must be nonzero and < 256");
   s = (gfshare_pyobj *)ty->tp_alloc(ty, 0);
-  gfshare_create(&s->s, t, n);
-  gfshare_mkshares(&s->s, r, p);
+  gfshare_create(&s->s, t, in.sz);
+  gfshare_mkshares(&s->s, r, in.p);
   return ((PyObject *)s);
 end:
   return (0);
@@ -228,14 +228,14 @@ end:
 static PyObject *gfsmeth_add(PyObject *me, PyObject *arg)
 {
   unsigned i;
-  char *p;
-  Py_ssize_t n;
-  if (!PyArg_ParseTuple(arg, "O&s#:add", convuint, &i, &p, &n)) goto end;
+  struct bin s;
+  if (!PyArg_ParseTuple(arg, "O&O&:add", convuint, &i, convbin, &s))
+    goto end;
   if (i > 254) VALERR("index must be < 255");
-  if (n != GFSHARE_S(me)->sz) VALERR("bad share size");
+  if (s.sz != GFSHARE_S(me)->sz) VALERR("bad share size");
   if (gfshare_addedp(GFSHARE_S(me), i)) VALERR("this share already added");
   if (GFSHARE_S(me)->i >= GFSHARE_S(me)->t) VALERR("enough shares already");
-  gfshare_add(GFSHARE_S(me), i, p);
+  gfshare_add(GFSHARE_S(me), i, s.p);
   return (PyInt_FromLong(GFSHARE_S(me)->t - GFSHARE_S(me)->i));
 end:
   return (0);