+/*----- Exceptions --------------------------------------------------------*/
+
+static PyObject *keyexc;
+static PyObject *keyioexc;
+static PyObject *keyfilebrokenexc;
+
+static PyObject *kxmeth___init__(PyObject *me, PyObject *arg)
+{
+ long err;
+ PyObject *x = 0;
+ Py_ssize_t n;
+
+ n = PyTuple_GET_SIZE(arg);
+ if (n < 2) TYERR("__init__() takes at least two arguments");
+ me = PyTuple_GET_ITEM(arg, 0);
+ err = PyInt_AsLong(PyTuple_GET_ITEM(arg, 1));
+ if (err == -1 && PyErr_Occurred()) goto end;
+ if (INT_MIN > err || err > INT_MAX) OVFERR("error code out of range");
+
+ x = PyInt_FromLong(err); if (!x) goto end;
+ if (PyObject_SetAttrString(me, "err", x)) goto end;
+ Py_DECREF(x); x = 0;
+
+ x = TEXT_FROMSTR(key_strerror(err)); if (!x) goto end;
+ if (PyObject_SetAttrString(me, "errstring", x)) goto end;
+ Py_DECREF(x); x = 0;
+
+ x = PyTuple_GetSlice(arg, 1, n); if (!x) goto end;
+ if (PyObject_SetAttrString(me, "args", x)) goto end;
+ Py_DECREF(x); x = 0;
+ RETURN_NONE;
+
+end:
+ Py_XDECREF(x);
+ return (0);
+}
+
+static PyObject *kxmeth___str__(PyObject *me, PyObject *arg)
+{
+ long err;
+ const char *errstr, *errtag;
+ PyObject *x = 0;
+ PyObject *rc = 0;
+
+ static const char *const tab[] = {
+#define ENTRY(tag, num, str) "KERR_" #tag,
+ KEY_ERRORS(ENTRY)
+#undef ENTRY
+ };
+
+ if (!PyArg_ParseTuple(arg, "O:__str__", &me) ||
+ (x = PyObject_GetAttrString(me, "err")) == 0 ||
+ (err = PyInt_AsLong(x), PyErr_Occurred()))
+ goto done;
+ Py_DECREF(x); x = 0;
+ err = -err;
+ if (err >= 0 && err < N(tab)) errtag = tab[err];
+ else errtag = "<unknown>";
+ if ((x = PyObject_GetAttrString(me, "errstring")) == 0 ||
+ (errstr = TEXT_STR(x)) == 0)
+ goto done;
+ rc = TEXT_FORMAT("%s (%ld): %s", errtag, -err, errstr);
+
+done:
+ Py_XDECREF(x);
+ return (rc);
+}
+
+static const PyMethodDef keyexc_pymethods[] = {
+#define METHNAME(func) kxmeth_##func
+ METH (__init__, "KeyError(CODE)")
+ METH (__str__, "E.__str__() -> STRING")
+#undef METHNAME
+ { 0 }
+};
+
+static void keyexc_raise(int err)
+{
+ PyObject *arg = Py_BuildValue("(i)", err);
+ if (arg) PyErr_SetObject(keyexc, arg);
+ Py_XDECREF(arg);
+}
+#define KEYERR(err) do { keyexc_raise(err); goto end; } while (0)
+#define KEYIOERR(name) do { \
+ PyErr_SetFromErrnoWithFilename(keyioexc, name); \
+ goto end; \
+} while (0)
+#define KEYFILEBROKEN(name) do { \
+ PyErr_SetFromErrnoWithFilename(keyfilebrokenexc, name); \
+ goto end; \
+} while (0)
+
+/*----- Data structures ---------------------------------------------------*/
+
+typedef struct keydata_pyobj {
+ GMAP_PYOBJ_HEAD
+ key_data *kd;
+} keydata_pyobj;
+
+static PyTypeObject *keydata_pytype;
+static PyTypeObject *keydatabin_pytype;
+static PyTypeObject *keydataenc_pytype;
+static PyTypeObject *keydatamp_pytype;
+static PyTypeObject *keydatastruct_pytype;
+static PyTypeObject *keydatastr_pytype;
+static PyTypeObject *keydataec_pytype;
+#define KEYDATA_PYCHECK(o) PyObject_TypeCheck(o, keydata_pytype)
+#define KEYDATA_KD(o) (((keydata_pyobj *)(o))->kd)
+
+typedef struct keyfile_pyobj {
+ GMAP_PYOBJ_HEAD
+ key_file kf;
+} keyfile_pyobj;
+
+static PyTypeObject *keyfile_pytype;
+#define KEYFILE_PYCHECK(o) PyObject_TypeCheck(o, keyfile_pytype)
+#define KEYFILE_KF(o) (&((keyfile_pyobj *)(o))->kf)
+
+typedef struct key_pyobj {
+ PyObject_HEAD
+ key *k;
+ PyObject *kfobj;
+} key_pyobj;
+
+static PyTypeObject *key_pytype;
+#define KEY_PYCHECK(o) PyObject_TypeCheck(o, key_pytype)
+#define KEY_K(o) (((key_pyobj *)(o))->k)
+#define KEY_KFOBJ(o) (((key_pyobj *)(o))->kfobj)
+#define KEY_KF(o) KEYFILE_KF(KEY_KFOBJ(o))
+
+typedef struct keyattrs_pyobj {
+ GMAP_PYOBJ_HEAD
+ PyObject *kobj;
+} keyattrs_pyobj;
+
+static PyTypeObject *keyattrs_pytype;
+#define KEYATTRS_PYCHECK(o) PyObject_TypeCheck(o, keyattrs_pytype)
+#define KEYATTRS_KOBJ(o) (((keyattrs_pyobj *)(o))->kobj)
+#define KEYATTRS_KF(o) KEY_KF(KEYATTRS_KOBJ(o))
+#define KEYATTRS_K(o) KEY_K(KEYATTRS_KOBJ(o))
+
+/*----- Filters -----------------------------------------------------------*/
+
+static int convfilter(PyObject *x, void *p)
+{
+ key_filter *f = p;
+ const char *fs;
+ char *end;
+ int n;
+ PyObject *a = 0, *b = 0;
+ int err;
+ int rc = 0;
+
+ if ((fs = TEXT_STR(x)) != 0) {
+ if ((err = key_readflags(fs, &end, &f->f, &f->m)) != 0)
+ KEYERR(err);
+ if (*end)
+ KEYERR(KERR_BADFLAGS);
+ } else {
+ PyErr_Clear();
+ if (!PySequence_Check(x))
+ goto tyerr;
+ else if ((n = PySequence_Size(x)) < 0)
+ goto end;
+ else if (n != 2)
+ goto tyerr;
+ else if ((a = PySequence_GetItem(x, 0)) == 0 || !convuint(a, &f->f) ||
+ (b = PySequence_GetItem(x, 1)) == 0 || !convuint(b, &f->m))
+ goto end;
+ }
+ rc = 1;
+ goto end;
+tyerr:
+ TYERR("expected flag string or flag/mask pair");
+end:
+ Py_XDECREF(a);
+ Py_XDECREF(b);
+ return (rc);
+}
+
+static int convflags(PyObject *x, void *p)
+{
+ unsigned *f = p;
+ const char *fs;
+ char *end;
+ int err;
+ int rc = 0;
+
+ if (convuint(x, p))
+ return (1);
+ else {
+ PyErr_Clear();
+ if ((fs = TEXT_STR(x)) != 0) {
+ if ((err = key_readflags(fs, &end, f, 0)) != 0)
+ KEYERR(err);
+ if (*end)
+ KEYERR(KERR_BADFLAGS);
+ } else {
+ PyErr_Clear();
+ goto tyerr;
+ }
+ }
+ rc = 1;
+ goto end;
+tyerr:
+ TYERR("expected flag string or integer bitfield");
+end:
+ return (rc);
+}
+
+static PyObject *kdmeth_readflags(PyObject *me, PyObject *arg)
+{
+ const char *p;
+ char *end;
+ unsigned f, m;
+ PyObject *rc = 0;
+ int err;
+
+ if (!PyArg_ParseTuple(arg, "s:readflags", &p)) goto end;
+ if ((err = key_readflags(p, &end, &f, &m)) != 0) KEYERR(err);
+ rc = Py_BuildValue("(NNs)", getulong(f), getulong(m), end);
+end:
+ return (rc);
+}
+
+static PyObject *kdmeth_writeflags(PyObject *me, PyObject *arg)
+{
+ dstr d = DSTR_INIT;
+ PyObject *rc;
+ unsigned f;
+
+ if (!PyArg_ParseTuple(arg, "O&:key_writeflags", convuint, &f)) return (0);
+ key_writeflags(f, &d);
+ rc = TEXT_FROMSTRLEN(d.buf, d.len);
+ dstr_destroy(&d);
+ return (rc);
+}
+
+/*----- Key data ----------------------------------------------------------*/
+
+static const gmap_ops keydatastruct_gmops;
+
+static PyObject *keydata_pywrap(key_data *kd)
+{
+ PyTypeObject *ty;
+ keydata_pyobj *kdobj;
+ const gmap_ops *gmops = 0;
+
+ switch (kd->e & KF_ENCMASK) {
+ case KENC_BINARY: ty = keydatabin_pytype; break;
+ case KENC_ENCRYPT: ty = keydataenc_pytype; break;
+ case KENC_MP: ty = keydatamp_pytype; break;
+ case KENC_STRUCT:
+ ty = keydatastruct_pytype;
+ gmops = &keydatastruct_gmops;
+ break;
+ case KENC_STRING: ty = keydatastr_pytype; break;
+ case KENC_EC: ty = keydataec_pytype; break;
+ default: abort();
+ }
+ kdobj = PyObject_NEW(keydata_pyobj, ty);
+ kdobj->gmops = gmops;
+ kdobj->kd = kd;
+ return ((PyObject *)kdobj);
+}
+
+static void keydata_pydealloc(PyObject *me)
+{
+ key_drop(KEYDATA_KD(me));
+ FREEOBJ(me);
+}
+
+static PyObject *kdmeth_matchp(PyObject *me, PyObject *arg)
+{
+ key_filter f;
+
+ if (!PyArg_ParseTuple(arg, "O&:matchp", convfilter, &f))
+ return (0);
+ return (getbool(KEY_MATCH(KEYDATA_KD(me), &f)));
+}
+
+static PyObject *kdmeth_split(PyObject *me)
+ { key_split(&KEYDATA_KD(me)); RETURN_ME; }
+
+static PyObject *kdmeth_copy(PyObject *me, PyObject *arg, PyObject *kw)
+{
+ key_filter f = { 0, 0 };
+ static const char *const kwlist[] = { "filter", 0 };
+ key_data *kd;
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:copy", KWLIST,
+ convfilter, &f))
+ return (0);
+ if ((kd = key_copydata(KEYDATA_KD(me), &f)) == 0)
+ RETURN_NONE;
+ else
+ return (keydata_pywrap(kd));
+}
+
+static PyObject *kdmeth_write(PyObject *me, PyObject *arg, PyObject *kw)
+{
+ key_filter f = { 0, 0 };
+ dstr d = DSTR_INIT;
+ PyObject *rc = 0;
+ static const char *const kwlist[] = { "filter", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:write", KWLIST,
+ convfilter, &f))
+ return (0);
+ key_write(KEYDATA_KD(me), &d, &f);
+ rc = TEXT_FROMSTRLEN(d.buf, d.len);
+ dstr_destroy(&d);
+ return (rc);
+}
+
+static PyObject *kdmeth_encode(PyObject *me, PyObject *arg, PyObject *kw)
+{
+ key_filter f = { 0, 0 };
+ dstr d = DSTR_INIT;
+ PyObject *rc = 0;
+ static const char *const kwlist[] = { "filter", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:encode", KWLIST,
+ convfilter, &f))
+ return (0);
+ key_encode(KEYDATA_KD(me), &d, &f);
+ rc = bytestring_pywrap(d.buf, d.len);
+ dstr_destroy(&d);
+ return (rc);
+}
+
+static PyObject *kdmeth_plock(PyObject *me, PyObject *arg)
+{
+ char *tag;
+ int err;
+ PyObject *rc = 0;
+ key_data *kd;
+
+ if (!PyArg_ParseTuple(arg, "s:plock", &tag))
+ goto end;
+ if ((err = key_plock(&kd, KEYDATA_KD(me), tag)) != 0)
+ KEYERR(err);
+ rc = keydata_pywrap(kd);
+end:
+ return (rc);
+}
+
+static PyObject *kdmeth_lock(PyObject *me, PyObject *arg)
+{
+ struct bin pp;
+ PyObject *rc = 0;
+ key_data *kd;
+
+ if (!PyArg_ParseTuple(arg, "O&:lock", convbin, &pp))
+ goto end;
+ key_lock(&kd, KEYDATA_KD(me), pp.p, pp.sz);
+ rc = keydata_pywrap(kd);
+end:
+ return (rc);
+}
+
+static PyObject *kdmeth_read(PyObject *me, PyObject *arg)
+{
+ const char *p;
+ char *end;
+ key_data *kd;
+ PyObject *rc = 0;
+
+ if (!PyArg_ParseTuple(arg, "s:read", &p)) goto end;
+ if ((kd = key_read(p, &end)) == 0) KEYERR(KERR_MALFORMED);
+ rc = Py_BuildValue("(Ns)", keydata_pywrap(kd), end);
+end:
+ return (rc);
+}
+
+static PyObject *kdmeth_decode(PyObject *me, PyObject *arg)
+{
+ struct bin in;
+ key_data *kd;
+ PyObject *rc = 0;
+
+ if (!PyArg_ParseTuple(arg, "O&:decode", convbin, &in)) goto end;
+ if ((kd = key_decode(in.p, in.sz)) == 0) KEYERR(KERR_MALFORMED);
+ rc = keydata_pywrap(kd);
+end:
+ return (rc);
+}
+
+static PyObject *kdget_flags(PyObject *me, void *hunoz)
+ { return (getulong(KEYDATA_KD(me)->e)); }
+
+static const PyMethodDef keydata_pymethods[] = {
+#define METHNAME(func) kdmeth_##func
+ METH (matchp, "KD.matchp(FILTER) -> BOOL")
+ NAMETH(split, "KD.split()")
+ KWMETH(write, "KD.write([filter = <any>]) -> STRING")
+ KWMETH(encode, "KD.encode([filter = <any>]) -> BYTES")
+ KWMETH(copy, "KD.copy([filter = <any>]) -> KD")
+ METH (plock, "KD.plock(TAG) -> ENCRYPTED-KD")
+ METH (lock, "KD.lock(KEY) -> ENCRYPTED-KD")
+ SMTH (readflags, "readflags(STRING) -> (FLAGS, MASK, REST)")
+ SMTH (writeflags, "writeflags(FLAGS) -> STRING")
+ SMTH (read, "read(STRING) -> (KD, REST)")
+ SMTH (decode, "decode(BYTES) -> KD")
+#undef METHNAME
+ { 0 }
+};
+
+static const PyGetSetDef keydata_pygetset[] = {
+#define GETSETNAME(op, name) kd##op##_##name
+ GET (flags, "KD.flags -> FLAGS")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyTypeObject keydata_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KeyData", /* @tp_name@ */
+ sizeof(keydata_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ keydata_pydealloc, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "Key data base class.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(keydata), /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(keydata), /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ abstract_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+static PyObject *keydatabin_pynew(PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+{
+ struct bin in;
+ unsigned f = 0;
+ keydata_pyobj *me = 0;
+ static const char *const kwlist[] = { "key", "flags", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
+ convbin, &in, convflags, &f))
+ goto end;
+ me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
+ me->kd = key_newbinary(f & ~KF_ENCMASK, in.p, in.sz);
+end:
+ return ((PyObject *)me);
+}
+
+static PyObject *kdbget_bin(PyObject *me, void *hunoz)
+ { return (bytestring_pywrap(KEYDATA_KD(me)->u.k.k,
+ KEYDATA_KD(me)->u.k.sz)); }
+
+static const PyGetSetDef keydatabin_pygetset[] = {
+#define GETSETNAME(op, name) kdb##op##_##name
+ GET (bin, "KD.bin -> BYTES")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyTypeObject keydatabin_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KeyDataBinary", /* @tp_name@ */
+ sizeof(keydata_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ 0, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "KeyDataBinary(KEY, [flags = 0]): key data for binary keys.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ 0, /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(keydatabin), /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ keydatabin_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+static PyObject *keydataenc_pynew(PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+{
+ struct bin in;
+ unsigned f = 0;
+ keydata_pyobj *me = 0;
+ static const char *const kwlist[] = { "key", "flags", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
+ convbin, &in, convflags, &f))
+ goto end;
+ me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
+ me->kd = key_newencrypted(f & ~KF_ENCMASK, in.p, in.sz);
+end:
+ return ((PyObject *)me);
+}
+
+static PyObject *kdemeth_plock(PyObject *me, PyObject *arg)
+{
+ char *hunoz;
+ if (!PyArg_ParseTuple(arg, "s:plock", &hunoz)) goto end;
+ KEYERR(KERR_WRONGTYPE);
+end:
+ return (0);
+}
+
+static PyObject *kdemeth_lock(PyObject *me, PyObject *arg)
+{
+ struct bin hunoz;
+ if (!PyArg_ParseTuple(arg, "O&:lock", convbin, &hunoz)) goto end;
+ KEYERR(KERR_WRONGTYPE);
+end:
+ return (0);
+}
+
+static PyObject *kdemeth_punlock(PyObject *me, PyObject *arg)
+{
+ char *tag;
+ int err;
+ PyObject *rc = 0;
+ key_data *kd;
+
+ if (!PyArg_ParseTuple(arg, "s:punlock", &tag))
+ goto end;
+ if ((err = key_punlock(&kd, KEYDATA_KD(me), tag)) != 0)
+ KEYERR(err);
+ rc = keydata_pywrap(kd);
+end:
+ return (rc);
+}
+
+static PyObject *kdemeth_unlock(PyObject *me, PyObject *arg)
+{
+ struct bin pp;
+ int err;
+ PyObject *rc = 0;
+ key_data *kd;
+
+ if (!PyArg_ParseTuple(arg, "O&:unlock", convbin, &pp))
+ goto end;
+ if ((err = key_unlock(&kd, KEYDATA_KD(me), pp.p, pp.sz)) != 0)
+ KEYERR(err);
+ rc = keydata_pywrap(kd);
+end:
+ return (rc);
+}
+
+#define kdeget_ct kdbget_bin
+
+static const PyMethodDef keydataenc_pymethods[] = {
+#define METHNAME(func) kdemeth_##func
+ METH (plock, "KD.plock(TAG) -> ENCRYPTED-KD")
+ METH (lock, "KD.lock(KEY) -> ENCRYPTED-KD")
+ METH (punlock, "KD.punlock(TAG) -> KD")
+ METH (unlock, "KD.unlock(KEY) -> KD")
+#undef METHNAME
+ { 0 }
+};
+
+static const PyGetSetDef keydataenc_pygetset[] = {
+#define GETSETNAME(op, name) kde##op##_##name
+ GET (ct, "KD.ct -> BYTES")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyTypeObject keydataenc_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KeyDataEncrypted", /* @tp_name@ */
+ sizeof(keydata_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ 0, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "KeyDataEncrypted(KEY, [flags = 0]): key data for encrypted keys.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(keydataenc), /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(keydataenc), /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ keydataenc_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+static PyObject *keydatamp_pynew(PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+{
+ mp *x = 0;
+ unsigned f = 0;
+ keydata_pyobj *me = 0;
+ static const char *const kwlist[] = { "key", "flags", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
+ convmp, &x, convflags, &f))
+ goto end;
+ me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
+ me->kd = key_newmp(f & ~KF_ENCMASK, x);
+end:
+ mp_drop(x);
+ return ((PyObject *)me);
+}
+
+static PyObject *kdmget_mp(PyObject *me, void *hunoz)
+ { return (mp_pywrap(MP_COPY(KEYDATA_KD(me)->u.m))); }
+
+static const PyGetSetDef keydatamp_pygetset[] = {
+#define GETSETNAME(op, name) kdm##op##_##name
+ GET (mp, "KD.mp -> X")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyTypeObject keydatamp_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KeyDataMP", /* @tp_name@ */
+ sizeof(keydata_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ 0, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "KeyDataMP(KEY, [flags = 0]): key data for large-integer keys.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ 0, /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(keydatamp), /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ keydatamp_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+static PyObject *keydatastr_pynew(PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+{
+ char *p;
+ unsigned f = 0;
+ keydata_pyobj *me = 0;
+ static const char *const kwlist[] = { "key", "flags", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:new", KWLIST,
+ &p, convflags, &f))
+ goto end;
+ me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
+ me->kd = key_newstring(f & ~KF_ENCMASK, p);
+end:
+ return ((PyObject *)me);
+}
+
+static PyObject *kdsget_str(PyObject *me, void *hunoz)
+ { return (TEXT_FROMSTR(KEYDATA_KD(me)->u.p)); }
+
+static const PyGetSetDef keydatastr_pygetset[] = {
+#define GETSETNAME(op, name) kds##op##_##name
+ GET (str, "KD.str -> STRING")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyTypeObject keydatastr_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KeyDataString", /* @tp_name@ */
+ sizeof(keydata_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ 0, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "KeyDataString(KEY, [flags = 0]): key data for string keys.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ 0, /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(keydatastr), /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ keydatastr_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+static PyObject *keydataec_pynew(PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+{
+ ec x = EC_INIT;
+ unsigned f = 0;
+ keydata_pyobj *me = 0;
+ static const char *const kwlist[] = { "key", "flags", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
+ convecpt, &x, convflags, &f))
+ goto end;
+ me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
+ me->kd = key_newec(f & ~KF_ENCMASK, &x);
+end:
+ EC_DESTROY(&x);
+ return ((PyObject *)me);
+}
+
+static PyObject *kdeget_ecpt(PyObject *me, void *hunoz)
+{
+ ec pt = EC_INIT;
+ EC_COPY(&pt, &KEYDATA_KD(me)->u.e);
+ return (ecpt_pywrapout(ecpt_pytype, &pt));
+}
+
+static const PyGetSetDef keydataec_pygetset[] = {
+#define GETSETNAME(op, name) kde##op##_##name
+ GET (ecpt, "KD.ecpt -> ECPT")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyTypeObject keydataec_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KeyDataECPt", /* @tp_name@ */
+ sizeof(keydata_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ 0, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "KeyDataECPt(KEY, [flags = 0]): key data for elliptic-curve keys.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ 0, /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(keydataec), /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ keydataec_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+static void *keydatastruct_gmlookup(PyObject *me, PyObject *k, unsigned *f)
+{
+ key_data *kd = KEYDATA_KD(me);
+ const char *tag;
+ key_struct *ks;
+
+ assert((kd->e&KF_ENCMASK) == KENC_STRUCT);
+ if ((tag = TEXT_STR(k)) == 0) return (0);
+ if (f) { key_split(&kd); KEYDATA_KD(me) = kd; }
+ ks = sym_find(&kd->u.s, tag, -1, f ? sizeof(key_struct) : 0, f);
+ if (ks && f && !*f) ks->k = 0;
+ return (ks);
+}
+
+static void keydatastruct_gmiterinit(PyObject *me, void *i)
+{
+ key_data *kd = KEYDATA_KD(me);
+
+ assert((kd->e&KF_ENCMASK) == KENC_STRUCT);
+ sym_mkiter(i, &kd->u.s);
+}
+
+static void *keydatastruct_gmiternext(PyObject *me, void *i)
+ { return (sym_next(i)); }
+
+static PyObject *keydatastruct_gmentrykey(PyObject *me, void *e)
+ { key_struct *ks = e; return (TEXT_FROMSTR(SYM_NAME(ks))); }
+
+static PyObject *keydatastruct_gmentryvalue(PyObject *me, void *e)
+{
+ key_struct *ks = e;
+ key_data *kd = ks->k;
+
+ key_incref(kd);
+ return (keydata_pywrap(kd));
+}
+
+static key_struct *maybe_split_and_reprobe(PyObject *me, key_struct *ks)
+{
+ key_data *kd = KEYDATA_KD(me);
+
+ if (kd->ref == 1) return (ks);
+ key_split(&kd); KEYDATA_KD(me) = kd;
+ ks = sym_find(&kd->u.s, SYM_NAME(ks), SYM_LEN(ks), 0, 0); assert(ks);
+ return (ks);
+}
+
+static int keydatastruct_gmsetentry(PyObject *me, void *e, PyObject *val)
+{
+ key_struct *ks;
+ key_data *kd;
+ int rc = -1;
+
+ if (!KEYDATA_PYCHECK(val)) TYERR("expected KeyData value");
+ ks = maybe_split_and_reprobe(me, e);
+ if (ks->k) key_drop(ks->k);
+ ks->k = kd = KEYDATA_KD(val); key_incref(kd);
+ rc = 0;
+end:
+ return (rc);
+}
+
+static int keydatastruct_gmdelentry(PyObject *me, void *e)
+{
+ key_struct *ks;
+
+ ks = maybe_split_and_reprobe(me, e);
+ if (ks->k) key_drop(ks->k);
+ sym_remove(&KEYDATA_KD(me)->u.s, ks);
+ return (0);
+}
+
+static const gmap_ops keydatastruct_gmops = {
+ sizeof(sym_iter),
+ keydatastruct_gmlookup,
+ keydatastruct_gmiterinit,
+ keydatastruct_gmiternext,
+ keydatastruct_gmentrykey,
+ keydatastruct_gmentryvalue,
+ keydatastruct_gmsetentry,
+ keydatastruct_gmdelentry
+};
+
+static PyObject *keydatastruct_pynew(PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+{
+ PyObject *sub = 0;
+ PyObject *it = 0, *name = 0, *val = 0;
+ char *p;
+ keydata_pyobj *me = 0;
+ key_data *kd = 0;
+ static const char *const kwlist[] = { "subkeys", 0 };
+
+ Py_XINCREF(arg); Py_XINCREF(kw);
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:new", KWLIST, &sub))
+ goto end;
+ kd = key_newstruct();
+ if (sub) {
+ if (!PyMapping_Check(sub))
+ TYERR("subkeys must be an iterable mapping");
+ if ((it = PyObject_GetIter(sub)) == 0)
+ goto end;
+ while ((name = PyIter_Next(it)) != 0) {
+ if ((p = TEXT_STR(name)) == 0 ||
+ (val = PyObject_GetItem(sub, name)) == 0)
+ goto end;
+ if (!KEYDATA_PYCHECK(val))
+ TYERR("subkey objects must be subclasses of KeyData");
+ key_structset(kd, p, KEYDATA_KD(val));
+ Py_DECREF(name); name = 0;
+ Py_DECREF(val); val = 0;
+ }
+ if (PyErr_Occurred())
+ goto end;
+ Py_DECREF(it); it = 0;
+ }
+ me = (keydata_pyobj *)ty->tp_alloc(ty, 0);
+ me->gmops = &keydatastruct_gmops;
+ me->kd = kd;
+end:
+ if (kd && !me) key_drop(kd);
+ Py_XDECREF(name); Py_XDECREF(val); Py_XDECREF(it);
+ Py_XDECREF(arg); Py_XDECREF(kw);
+ return ((PyObject *)me);
+}
+
+static Py_ssize_t keydatastruct_pysize(PyObject *me)
+ { return gmap_pysize_from_sym(&KEYDATA_KD(me)->u.s); }
+
+static const PyMappingMethods keydatastruct_pymapping = {
+ keydatastruct_pysize,
+ gmap_pylookup,
+ gmap_pystore
+};
+
+static const PyTypeObject keydatastruct_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KeyDataStructured", /* @tp_name@ */
+ sizeof(keydata_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ 0, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ PYSEQUENCE(gmap), /* @tp_as_sequence@ */
+ PYMAPPING(keydatastruct), /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "KeyDataStructured([subkeys = []]): key data for structured keys.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ gmap_pyiter, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(gmap), /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ 0, /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ keydatastruct_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+/*----- Key attributes ----------------------------------------------------*/
+
+static void *keyattrs_gmlookup(PyObject *me, PyObject *k, unsigned *f)
+{
+ char *name = TEXT_STR(k);
+ key_attr *a = 0;
+
+ if (!name) goto end;
+ if (f && !(KEYATTRS_KF(me)->f&KF_WRITE)) KEYERR(KERR_READONLY);
+ a = sym_find(&KEYATTRS_K(me)->a, name, -1, f ? sizeof(key_attr) : 0, f);
+ if (f && !*f) a->p = 0;
+end:
+ return (a);
+}
+
+static void keyattrs_gmiterinit(PyObject *me, void *i)
+ { sym_mkiter(i, &KEYATTRS_K(me)->a); }
+
+static void *keyattrs_gmiternext(PyObject *me, void *i)
+ { return (sym_next(i)); }
+
+static PyObject *keyattrs_gmentrykey(PyObject *me, void *e)
+ { return (TEXT_FROMSTR(SYM_NAME(e))); }
+
+static PyObject *keyattrs_gmentryvalue(PyObject *me, void *e)
+ { return (TEXT_FROMSTR(((key_attr *)e)->p)); }
+
+static int keyattrs_gmsetentry(PyObject *me, void *e, PyObject *val)
+{
+ key_attr *a = e;
+ const char *p;
+ Py_ssize_t n;
+ int rc = -1;
+
+ if (!TEXT_CHECK(val)) TYERR("expected string");
+ TEXT_PTRLEN(val, p, n);
+ if (n > 255) VALERR("attribute too long");
+ if (memchr(p, 0, n)) VALERR("attribute must not contain nul");
+ if (a->p) xfree(a->p);
+ a->p = xmalloc(n + 1); memcpy(a->p, p, n + 1);
+ KEYATTRS_KF(me)->f |= KF_MODIFIED;
+ rc = 0;
+end:
+ return (rc);
+}
+
+static int keyattrs_gmdelentry(PyObject *me, void *e)
+{
+ key_attr *a = e;
+ int rc = -1;
+
+ if (!(KEYATTRS_KF(me)->f&KF_WRITE)) KEYERR(KERR_READONLY);
+ if (a->p) { KEYATTRS_KF(me)->f |= KF_MODIFIED; xfree(a->p); }
+ sym_remove(&KEYATTRS_K(me)->a, a);
+ rc = 0;
+end:
+ return (rc);
+}
+
+static const gmap_ops keyattrs_gmops = {
+ sizeof(sym_iter),
+ keyattrs_gmlookup,
+ keyattrs_gmiterinit,
+ keyattrs_gmiternext,
+ keyattrs_gmentrykey,
+ keyattrs_gmentryvalue,
+ keyattrs_gmsetentry,
+ keyattrs_gmdelentry
+};
+
+static PyObject *keyattrs_make(PyObject *kobj)
+{
+ keyattrs_pyobj *me = PyObject_NEW(keyattrs_pyobj, keyattrs_pytype);
+ me->gmops = &keyattrs_gmops;
+ me->kobj = kobj;
+ Py_INCREF(kobj);
+ return ((PyObject *)me);
+}
+
+static void keyattrs_pydealloc(PyObject *me)
+{
+ Py_DECREF(KEYATTRS_KOBJ(me));
+ FREEOBJ(me);
+}
+
+static Py_ssize_t keyattrs_pysize(PyObject *me)
+ { return gmap_pysize_from_sym(&KEYATTRS_K(me)->a); }
+
+static const PyMappingMethods keyattrs_pymapping = {
+ keyattrs_pysize,
+ gmap_pylookup,
+ gmap_pystore
+};
+
+static const PyTypeObject keyattrs_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KeyAttributes", /* @tp_name@ */
+ sizeof(keyattrs_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ keyattrs_pydealloc, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ PYSEQUENCE(gmap), /* @tp_as_sequence@ */
+ PYMAPPING(keyattrs), /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "Proxy thing for talking about key attributes.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ gmap_pyiter, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(gmap), /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ 0, /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ abstract_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+/*----- Key objects -------------------------------------------------------*/
+
+static PyObject *key_dowrap(PyTypeObject *ty, PyObject *kfobj, key *k)
+{
+ key_pyobj *kobj = (key_pyobj *)ty->tp_alloc(ty, 0);
+ kobj->kfobj = kfobj;
+ Py_INCREF(kfobj);
+ kobj->k = k;
+ return ((PyObject *)kobj);
+}
+
+static PyObject *key_pywrap(PyObject *kfobj, key *k)
+ { return (key_dowrap(key_pytype, kfobj, k)); }
+
+static PyObject *key_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
+{
+ PyObject *kfobj;
+ uint32 id;
+ char *type;
+ unsigned long exptime = KEXP_FOREVER;
+ static const char *const kwlist[] =
+ { "keyfile", "id", "type", "exptime", 0 };
+ key *k;
+ int err;
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&s|O&:new", KWLIST,
+ keyfile_pytype, &kfobj, convu32, &id,
+ &type, convulong, &exptime))
+ goto end;
+ if ((err = key_new(KEYFILE_KF(kfobj), id, type, exptime, &k)) != 0)
+ KEYERR(err);
+ return (key_dowrap(ty, kfobj, k));
+end:
+ return (0);
+}
+
+static void key_pydealloc(PyObject *me)
+{
+ Py_DECREF(KEY_KFOBJ(me));
+ FREEOBJ(me);
+}
+
+static Py_hash_t key_pyhash(PyObject *me)
+ { return ((Py_hash_t)KEY_K(me)); }
+
+static PyObject *key_pyrichcompare(PyObject *me, PyObject *you, int op)
+{
+ if (!KEY_PYCHECK(you)) RETURN_NOTIMPL;
+ switch (op) {
+ case Py_EQ: return (getbool(KEY_K(me) == KEY_K(you)));
+ case Py_NE: return (getbool(KEY_K(me) == KEY_K(you)));
+ default: TYERR("ordering makes no sense");
+ }
+end:
+ return (0);
+}
+
+static PyObject *kmeth_delete(PyObject *me)
+{
+ int err;
+
+ if ((err = key_delete(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
+ RETURN_ME;
+end:
+ return (0);
+}
+
+static PyObject *kmeth_expire(PyObject *me)
+{
+ int err;
+
+ if ((err = key_expire(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
+ RETURN_ME;
+end:
+ return (0);
+}
+
+static PyObject *kmeth_used(PyObject *me, PyObject *arg)
+{
+ long t;
+ int err;
+
+ if (!PyArg_ParseTuple(arg, "l:used", &t)) goto end;
+ if ((err = key_used(KEY_KF(me), KEY_K(me), t)) != 0) KEYERR(err);
+ RETURN_ME;
+end:
+ return (0);
+}
+
+static PyObject *kmeth_extractline(PyObject *me, PyObject *arg, PyObject *kw)
+{
+ key_filter f = { 0, 0 };
+ dstr d = DSTR_INIT;
+ PyObject *rc = 0;
+ static const char *const kwlist[] = { "filter", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:extract", KWLIST,
+ convfilter, &f))
+ goto end;
+ key_extractline(KEY_KF(me), KEY_K(me), &d, &f);
+ rc = TEXT_FROMSTRLEN(d.buf, d.len);
+end:
+ dstr_destroy(&d);
+ return (rc);
+}
+
+static PyObject *kmeth_fingerprint(PyObject *me,
+ PyObject *arg, PyObject *kw)
+{
+ ghash *h;
+ key_filter f = { KF_NONSECRET, KF_NONSECRET };
+ static const char *const kwlist[] = { "hash", "filter", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:fingerprint", KWLIST,
+ convghash, &h, convfilter, &f))
+ return (0);
+ return (getbool(key_fingerprint(KEY_K(me), h, &f)));
+}
+
+static PyObject *kget_id(PyObject *me, void *hunoz)
+ { return (getulong(KEY_K(me)->id)); }
+static PyObject *kget_file(PyObject *me, void *hunoz)
+ { RETURN_OBJ(KEY_KFOBJ(me)); }
+static PyObject *kget_type(PyObject *me, void *hunoz)
+ { return (TEXT_FROMSTR(KEY_K(me)->type)); }
+static PyObject *kget_exptime(PyObject *me, void *hunoz)
+ { return (getulong(KEY_K(me)->exp)); }
+static PyObject *kget_deltime(PyObject *me, void *hunoz)
+ { return (getulong(KEY_K(me)->del)); }
+static PyObject *kget_expiredp(PyObject *me, void *hunoz)
+ { return (getbool(key_expired(KEY_K(me)))); }
+static PyObject *kget_attr(PyObject *me, void *hunoz)
+ { return (keyattrs_make(me)); }
+
+static int kset_exptime(PyObject *me, PyObject *x, void *hunoz)
+{
+ key *k = KEY_K(me);
+ unsigned long et;
+
+ if (!x) NIERR("__del__");
+ if (!convulong(x, &et))
+ goto end;
+ if (!(KEY_KF(me)->f & KF_WRITE))
+ KEYERR(KERR_READONLY);
+ k->exp = et;
+ KEY_KF(me)->f |= KF_MODIFIED;
+ return (0);
+end:
+ return (-1);
+}
+
+static int kset_deltime(PyObject *me, PyObject *x, void *hunoz)
+{
+ key *k = KEY_K(me);
+ unsigned long dt;
+
+ if (!x) NIERR("__del__");
+ if (!convulong(x, &dt))
+ goto end;
+ if (dt == KEXP_FOREVER && k->exp != KEXP_FOREVER)
+ VALERR("key will eventually expire");
+ if (!(KEY_KF(me)->f & KF_WRITE))
+ KEYERR(KERR_READONLY);
+ k->del = dt;
+ KEY_KF(me)->f |= KF_MODIFIED;
+ return (0);
+end:
+ return (-1);
+}
+
+static PyObject *kget_data(PyObject *me, void *hunoz)
+{
+ key_data *kd = KEY_K(me)->k;
+ key_incref(kd);
+ return (keydata_pywrap(kd));
+}
+static int kset_data(PyObject *me, PyObject *x, void *hunoz)
+{
+ int err;
+
+ if (!x) NIERR("__del__");
+ if (!KEYDATA_PYCHECK(x)) TYERR("expected KeyData object");
+ if ((err = key_setkeydata(KEY_KF(me), KEY_K(me), KEYDATA_KD(x))) != 0)
+ KEYERR(err);
+ return (0);
+end:
+ return (-1);
+}
+
+static PyObject *kget_fulltag(PyObject *me, void *hunoz)
+{
+ dstr d = DSTR_INIT;
+ PyObject *rc;
+
+ key_fulltag(KEY_K(me), &d);
+ rc = TEXT_FROMSTRLEN(d.buf, d.len);
+ dstr_destroy(&d);
+ return (rc);
+}
+
+static PyObject *kget_tag(PyObject *me, void *hunoz)
+{
+ if (!KEY_K(me)->tag) RETURN_NONE;
+ return (TEXT_FROMSTR(KEY_K(me)->tag));
+}
+static int kset_tag(PyObject *me, PyObject *x, void *hunoz)
+{
+ int err;
+ char *tag;
+
+ if (!x || x == Py_None) tag = 0;
+ else if ((tag = TEXT_STR(x)) == 0) goto end;
+ if ((err = key_settag(KEY_KF(me), KEY_K(me), tag)) != 0) KEYERR(err);
+ return (0);
+end:
+ return (-1);
+}
+
+static PyObject *kget_comment(PyObject *me, void *hunoz)
+{
+ if (!KEY_K(me)->c) RETURN_NONE;
+ return (TEXT_FROMSTR(KEY_K(me)->c));
+}
+static int kset_comment(PyObject *me, PyObject *x, void *hunoz)
+{
+ int err;
+ char *c;
+
+ if (!x || x == Py_None) c = 0;
+ else if ((c = TEXT_STR(x)) == 0) goto end;
+ if ((err = key_setcomment(KEY_KF(me), KEY_K(me), c)) != 0) KEYERR(err);
+ return (0);
+end:
+ return (-1);
+}
+
+static const PyMethodDef key_pymethods[] = {
+#define METHNAME(func) kmeth_##func
+ NAMETH(delete, "KEY.delete()")
+ NAMETH(expire, "KEY.expire()")
+ METH (used, "KEY.used(TIME)")
+ KWMETH(extractline, "KEY.extractline([filter = <any>])")
+ KWMETH(fingerprint, "KEY.fingerprint(HASH, [filter = '-secret'])")
+#undef METHNAME
+ { 0 }
+};
+
+static const PyGetSetDef key_pygetset[] = {
+#define GETSETNAME(op, name) k##op##_##name
+ GET (file, "KEY.file -> KF")
+ GET (id, "KEY.id -> ID")
+ GETSET(tag, "KEY.tag -> TAG")
+ GET (type, "KEY.type -> TYPE")
+ GETSET(exptime, "KEY.exptime -> TIME")
+ GETSET(deltime, "KEY.deltime -> TIME")
+ GET (expiredp, "KEY.expiredp -> BOOL")
+ GET (attr, "KEY.attr -> ATTRIBUTES")
+ GETSET(data, "KEY.data -> KD")
+ GETSET(comment, "KEY.comment -> COMMENT")
+ GET (fulltag, "KEY.fulltag -> FULLTAG")
+#undef GETSETNAME
+ { 0 }
+};
+
+static const PyTypeObject key_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "Key", /* @tp_name@ */
+ sizeof(key_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ key_pydealloc, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ key_pyhash, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "Key(KF, ID, TYPE, [exptime = KEXP_FOREVER]): key object.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ key_pyrichcompare, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(key), /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ PYGETSET(key), /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ key_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+