@@@ py_buffer/freebin wip
[catacomb-python] / share.c
diff --git a/share.c b/share.c
index 16d376e..30bff92 100644 (file)
--- a/share.c
+++ b/share.c
@@ -61,8 +61,8 @@ static const PyGetSetDef gfshare_pygetset[]= {
   { 0 }
 };
 
-static PyTypeObject gfshare_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+static const PyTypeObject gfshare_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
   "GFShare",                           /* @tp_name@ */
   sizeof(gfshare_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -112,19 +112,19 @@ static 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);
@@ -137,7 +137,7 @@ static PyObject *gfsmeth_get(PyObject *me, PyObject *arg)
   if (!PyArg_ParseTuple(arg, "O&:get", convuint, &i)) goto end;
   if (i >= 255) VALERR("index must be < 255");
   rc = bytestring_pywrap(0, GFSHARE_S(me)->sz);
-  gfshare_get(GFSHARE_S(me), i, PyString_AS_STRING(rc));
+  gfshare_get(GFSHARE_S(me), i, BIN_PTR(rc));
 end:
   return (rc);
 }
@@ -149,8 +149,8 @@ static const PyMethodDef gfsharesplit_pymethods[] = {
   { 0 }
 };
 
-static PyTypeObject gfsharesplit_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+static const PyTypeObject gfsharesplit_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
   "GFShareSplit",                      /* @tp_name@ */
   sizeof(gfshare_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -228,26 +228,25 @@ 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);
 }
 
-static PyObject *gfsmeth_combine(PyObject *me, PyObject *arg)
+static PyObject *gfsmeth_combine(PyObject *me)
 {
   PyObject *rc = 0;
-  if (!PyArg_ParseTuple(arg, ":combine")) goto end;
   if (GFSHARE_S(me)->i < GFSHARE_S(me)->t) VALERR("not enough shares yet");
   rc = bytestring_pywrap(0, GFSHARE_S(me)->sz);
-  gfshare_combine(GFSHARE_S(me), PyString_AS_STRING(rc));
+  gfshare_combine(GFSHARE_S(me), BIN_PTR(rc));
 end:
   return (rc);
 }
@@ -256,7 +255,7 @@ static const PyMethodDef gfsharejoin_pymethods[] = {
 #define METHNAME(name) gfsmeth_##name
   METH (addedp,        "S.addedp(I) -> BOOL")
   METH (add,           "S.add(I, SHARE) -> REMAIN")
-  METH (combine,       "S.combine() -> SECRET")
+  NAMETH(combine,      "S.combine() -> SECRET")
 #undef METHNAME
   { 0 }
 };
@@ -271,8 +270,8 @@ static const PyGetSetDef gfsharejoin_pygetset[]= {
   { 0 }
 };
 
-static PyTypeObject gfsharejoin_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+static const PyTypeObject gfsharejoin_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
   "GFShareJoin",                       /* @tp_name@ */
   sizeof(gfshare_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -353,8 +352,8 @@ static const PyGetSetDef share_pygetset[]= {
   { 0 }
 };
 
-static PyTypeObject share_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+static const PyTypeObject share_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
   "Share",                             /* @tp_name@ */
   sizeof(share_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -446,8 +445,8 @@ static const PyMethodDef sharesplit_pymethods[] = {
   { 0 }
 };
 
-static PyTypeObject sharesplit_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+static const PyTypeObject sharesplit_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
   "ShareSplit",                                /* @tp_name@ */
   sizeof(share_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
@@ -541,10 +540,9 @@ end:
   return (rc);
 }
 
-static PyObject *smeth_combine(PyObject *me, PyObject *arg)
+static PyObject *smeth_combine(PyObject *me)
 {
   PyObject *rc = 0;
-  if (!PyArg_ParseTuple(arg, ":combine")) goto end;
   if (SHARE_S(me)->i < SHARE_S(me)->t) VALERR("not enough shares yet");
   rc = mp_pywrap(share_combine(SHARE_S(me)));
 end:
@@ -555,7 +553,7 @@ static const PyMethodDef sharejoin_pymethods[] = {
 #define METHNAME(name) smeth_##name
   METH (addedp,        "S.addedp(I) -> BOOL")
   METH (add,           "S.add(I, SHARE) -> REMAIN")
-  METH (combine,       "S.combine() -> SECRET")
+  NAMETH(combine,      "S.combine() -> SECRET")
 #undef METHNAME
   { 0 }
 };
@@ -570,8 +568,8 @@ static const PyGetSetDef sharejoin_pygetset[]= {
   { 0 }
 };
 
-static PyTypeObject sharejoin_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
+static const PyTypeObject sharejoin_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
   "ShareJoin",                         /* @tp_name@ */
   sizeof(share_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */