X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/bfe5ccdcf939280dfbea28c2d94cb8928da33998..4e191783f8701425b598c57aad6860497ed2d3bc:/key.c diff --git a/key.c b/key.c index dff59ec..634eb29 100644 --- a/key.c +++ b/key.c @@ -36,29 +36,31 @@ static PyObject *keyfilebrokenexc; static PyObject *kxmeth___init__(PyObject *me, PyObject *arg) { - int err; + long err; PyObject *x = 0; + Py_ssize_t n; - if (!PyArg_ParseTuple(arg, "Oi:__init__", &me, &err) || - (x = PyInt_FromLong(err)) == 0 || - PyObject_SetAttrString(me, "err", x)) - goto fail; - Py_DECREF(x); x = 0; - if ((x = PyString_FromString(key_strerror(err))) == 0 || - PyObject_SetAttrString(me, "errstring", x)) - goto fail; + 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; - if ((x = PyString_FromString(key_strerror(err))) == 0 || - PyObject_SetAttrString(me, "errstring", x)) - goto fail; + + x = PyString_FromString(key_strerror(err)); if (!x) goto end; + if (PyObject_SetAttrString(me, "errstring", x)) goto end; Py_DECREF(x); x = 0; - if ((x = PySequence_GetSlice(arg, 1, PySequence_Size(arg))) == 0 || - PyObject_SetAttrString(me, "args", x)) - goto fail; + + 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; -fail: +end: Py_XDECREF(x); return (0); } @@ -96,15 +98,15 @@ done: static PyMethodDef keyexc_pymethods[] = { #define METHNAME(func) kxmeth_##func - METH (__init__, "KeyError(CODE)") - METH (__str__, "E.__str__() -> STRING") + METH (__init__, "KeyError(CODE)") + METH (__str__, "E.__str__() -> STRING") #undef METHNAME { 0 } }; static void keyexc_raise(int err) { - PyObject *arg = Py_BuildValue("(is)", err, key_strerror(err)); + PyObject *arg = Py_BuildValue("(i)", err); if (arg) PyErr_SetObject(keyexc, arg); Py_XDECREF(arg); } @@ -351,10 +353,10 @@ static PyObject *kdmeth_split(PyObject *me, PyObject *arg) static PyObject *kdmeth_copy(PyObject *me, PyObject *arg, PyObject *kw) { key_filter f = { 0, 0 }; - static char *kwlist[] = { "filter", 0 }; + static const char *const kwlist[] = { "filter", 0 }; key_data *kd; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:copy", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:copy", KWLIST, convfilter, &f)) return (0); if ((kd = key_copydata(KEYDATA_KD(me), &f)) == 0) @@ -368,9 +370,9 @@ static PyObject *kdmeth_write(PyObject *me, PyObject *arg, PyObject *kw) key_filter f = { 0, 0 }; dstr d = DSTR_INIT; PyObject *rc = 0; - static char *kwlist[] = { "filter", 0 }; + static const char *const kwlist[] = { "filter", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:write", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:write", KWLIST, convfilter, &f)) return (0); key_write(KEYDATA_KD(me), &d, &f); @@ -384,9 +386,9 @@ static PyObject *kdmeth_encode(PyObject *me, PyObject *arg, PyObject *kw) key_filter f = { 0, 0 }; dstr d = DSTR_INIT; PyObject *rc = 0; - static char *kwlist[] = { "filter", 0 }; + static const char *const kwlist[] = { "filter", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:encode", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:encode", KWLIST, convfilter, &f)) return (0); key_encode(KEYDATA_KD(me), &d, &f); @@ -463,20 +465,20 @@ static PyObject *kdget_flags(PyObject *me, void *hunoz) static PyMethodDef keydata_pymethods[] = { #define METHNAME(func) kdmeth_##func - METH (matchp, "KD.matchp(FILTER) -> BOOL") - METH (split, "KD.split()") - KWMETH(write, "KD.write([filter = ]) -> STRING") - KWMETH(encode, "KD.encode([filter = ]) -> BYTES") - KWMETH(copy, "KD.copy([filter = ]) -> KD") - METH (plock, "KD.plock(TAG) -> ENCRYPTED-KD") - METH (lock, "KD.lock(KEY) -> ENCRYPTED-KD") + METH (matchp, "KD.matchp(FILTER) -> BOOL") + METH (split, "KD.split()") + KWMETH(write, "KD.write([filter = ]) -> STRING") + KWMETH(encode, "KD.encode([filter = ]) -> BYTES") + KWMETH(copy, "KD.copy([filter = ]) -> KD") + METH (plock, "KD.plock(TAG) -> ENCRYPTED-KD") + METH (lock, "KD.lock(KEY) -> ENCRYPTED-KD") #undef METHNAME { 0 } }; static PyGetSetDef keydata_pygetset[] = { #define GETSETNAME(op, name) kd##op##_##name - GET (flags, "KD.flags -> FLAGS") + GET (flags, "KD.flags -> FLAGS") #undef GETSETNAME { 0 } }; @@ -506,7 +508,7 @@ static PyTypeObject keydata_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key data base class.", + "Key data base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -536,9 +538,9 @@ static PyObject *keydatabin_pynew(PyTypeObject *ty, Py_ssize_t n; unsigned f = 0; keydata_pyobj *me = 0; - static char *kwlist[] = { "key", "flags", 0 }; + static const char *const kwlist[] = { "key", "flags", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", KWLIST, &p, &n, convflags, &f)) goto end; me = (keydata_pyobj *)ty->tp_alloc(ty, 0); @@ -553,7 +555,7 @@ static PyObject *kdbget_bin(PyObject *me, void *hunoz) static PyGetSetDef keydatabin_pygetset[] = { #define GETSETNAME(op, name) kdb##op##_##name - GET (bin, "KD.bin -> BYTES") + GET (bin, "KD.bin -> BYTES") #undef GETSETNAME { 0 } }; @@ -583,7 +585,7 @@ static PyTypeObject keydatabin_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key data for binary keys.", + "KeyDataBinary(KEY, [flags = 0]): key data for binary keys.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -613,9 +615,9 @@ static PyObject *keydataenc_pynew(PyTypeObject *ty, Py_ssize_t n; unsigned f = 0; keydata_pyobj *me = 0; - static char *kwlist[] = { "key", "flags", 0 }; + static const char *const kwlist[] = { "key", "flags", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:new", KWLIST, &p, &n, convflags, &f)) goto end; me = (keydata_pyobj *)ty->tp_alloc(ty, 0); @@ -680,17 +682,17 @@ end: static 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") + 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 PyGetSetDef keydataenc_pygetset[] = { #define GETSETNAME(op, name) kde##op##_##name - GET (ct, "KD.ct -> BYTES") + GET (ct, "KD.ct -> BYTES") #undef GETSETNAME { 0 } }; @@ -720,7 +722,7 @@ static PyTypeObject keydataenc_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key data for encrypted keys.", + "KeyDataEncrypted(KEY, [flags = 0]): key data for encrypted keys.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -749,9 +751,9 @@ static PyObject *keydatamp_pynew(PyTypeObject *ty, mp *x = 0; unsigned f = 0; keydata_pyobj *me = 0; - static char *kwlist[] = { "key", "flags", 0 }; + static const char *const kwlist[] = { "key", "flags", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST, convmp, &x, convflags, &f)) goto end; me = (keydata_pyobj *)ty->tp_alloc(ty, 0); @@ -766,7 +768,7 @@ static PyObject *kdmget_mp(PyObject *me, void *hunoz) static PyGetSetDef keydatamp_pygetset[] = { #define GETSETNAME(op, name) kdm##op##_##name - GET (mp, "KD.mp -> X") + GET (mp, "KD.mp -> X") #undef GETSETNAME { 0 } }; @@ -796,7 +798,7 @@ static PyTypeObject keydatamp_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key data for large-integer keys.", + "KeyDataMP(KEY, [flags = 0]): key data for large-integer keys.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -825,9 +827,9 @@ static PyObject *keydatastr_pynew(PyTypeObject *ty, char *p; unsigned f = 0; keydata_pyobj *me = 0; - static char *kwlist[] = { "key", "flags", 0 }; + static const char *const kwlist[] = { "key", "flags", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O&:new", KWLIST, &p, convflags, &f)) goto end; me = (keydata_pyobj *)ty->tp_alloc(ty, 0); @@ -841,7 +843,7 @@ static PyObject *kdsget_str(PyObject *me, void *hunoz) static PyGetSetDef keydatastr_pygetset[] = { #define GETSETNAME(op, name) kds##op##_##name - GET (str, "KD.str -> STRING") + GET (str, "KD.str -> STRING") #undef GETSETNAME { 0 } }; @@ -871,7 +873,7 @@ static PyTypeObject keydatastr_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key data for string keys.", + "KeyDataString(KEY, [flags = 0]): key data for string keys.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -900,9 +902,9 @@ static PyObject *keydataec_pynew(PyTypeObject *ty, ec x = EC_INIT; unsigned f = 0; keydata_pyobj *me = 0; - static char *kwlist[] = { "key", "flags", 0 }; + static const char *const kwlist[] = { "key", "flags", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST, convecpt, &x, convflags, &f)) goto end; me = (keydata_pyobj *)ty->tp_alloc(ty, 0); @@ -921,7 +923,7 @@ static PyObject *kdeget_ecpt(PyObject *me, void *hunoz) static PyGetSetDef keydataec_pygetset[] = { #define GETSETNAME(op, name) kde##op##_##name - GET (ecpt, "KD.ecpt -> ECPT") + GET (ecpt, "KD.ecpt -> ECPT") #undef GETSETNAME { 0 } }; @@ -951,7 +953,7 @@ static PyTypeObject keydataec_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key data for elliptic-curve keys.", + "KeyDataECPt(KEY, [flags = 0]): key data for elliptic-curve keys.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1022,7 +1024,7 @@ static PyTypeObject subkeyiter_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Iterator for structured keys.", + "Iterator for structured keys.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1053,10 +1055,10 @@ static PyObject *keydatastruct_pynew(PyTypeObject *ty, char *p; keydata_pyobj *me = 0; key_data *kd = 0; - static char *kwlist[] = { "subkeys", 0 }; + static const char *const kwlist[] = { "subkeys", 0 }; Py_XINCREF(arg); Py_XINCREF(kw); - if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:new", kwlist, &sub)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:new", KWLIST, &sub)) goto end; kd = key_newstruct(); if (sub) { @@ -1157,7 +1159,7 @@ static PyTypeObject keydatastruct_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key data for structured keys.", + "KeyDataStructured([subkeys = []]): key data for structured keys.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1233,7 +1235,7 @@ static PyTypeObject keyattriter_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Iterator for key attributes.", + "Iterator for key attributes.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1343,7 +1345,7 @@ static PyTypeObject keyattrs_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Proxy thing for talking about key attributes.", + "Proxy thing for talking about key attributes.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1386,11 +1388,12 @@ static PyObject *key_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) uint32 id; char *type; unsigned long exptime = KEXP_FOREVER; - static char *kwlist[] = { "keyfile", "id", "type", "exptime", 0 }; + 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, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&s|O&:new", KWLIST, keyfile_pytype, &kfobj, convu32, &id, &type, convulong, &exptime)) goto end; @@ -1448,9 +1451,9 @@ static PyObject *kmeth_extract(PyObject *me, PyObject *arg, PyObject *kw) PyObject *nameobj; char *name; FILE *fp; - static char *kwlist[] = { "file", "filter", 0 }; + static const char *const kwlist[] = { "file", "filter", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O&:extract", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O&:extract", KWLIST, &PyFile_Type, &file, convfilter, &f) || (fp = PyFile_AsFile(file)) == 0 || @@ -1469,9 +1472,9 @@ static PyObject *kmeth_fingerprint(PyObject *me, { ghash *h; key_filter f = { KF_NONSECRET, KF_NONSECRET }; - static char *kwlist[] = { "hash", "filter", 0 }; + static const char *const kwlist[] = { "hash", "filter", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:fingerprint", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:fingerprint", KWLIST, convghash, &h, convfilter, &f)) return (0); return (getbool(key_fingerprint(KEY_K(me), h, &f))); @@ -1647,7 +1650,7 @@ static PyTypeObject key_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key object.", + "Key(KF, ID, TYPE, [exptime = KEXP_FOREVER]): key object.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1721,7 +1724,7 @@ static PyTypeObject keyiter_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Keyring iterator.", + "Keyring iterator.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1775,10 +1778,10 @@ static PyObject *keyfile_pynew(PyTypeObject *ty, char *file = 0; unsigned how = KOPEN_READ; keyfile_pyobj *rc = 0; - static char *kwlist[] = { "file", "how", "report", 0 }; + static const char *const kwlist[] = { "file", "how", "report", 0 }; Py_XINCREF(arg); Py_XINCREF(kw); - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|iO:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|iO:new", KWLIST, &file, &how, &ri.func)) goto end; if (ri.func && !PyCallable_Check(ri.func)) @@ -1834,10 +1837,10 @@ static PyObject *kfmeth_merge(PyObject *me, PyObject *arg, PyObject *kw) PyObject *x = 0; FILE *fp = 0; int rc; - static char *kwlist[] = { "file", "report", 0 }; + static const char *const kwlist[] = { "file", "report", 0 }; Py_XINCREF(arg); Py_XINCREF(kw); - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O:merge", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O:merge", KWLIST, &PyFile_Type, &x, &ri.func)) goto end; if (ri.func && !PyCallable_Check(ri.func)) @@ -1933,11 +1936,11 @@ static PyObject *kfmeth_newkey(PyObject *me, PyObject *arg, PyObject *kw) uint32 id; char *type; long exptime = KEXP_FOREVER; - static char *kwlist[] = { "id", "type", "exptime", 0 }; + static const char *const kwlist[] = { "id", "type", "exptime", 0 }; key *k; int err; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&s|l:newkey", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&s|l:newkey", KWLIST, convu32, &id, &type, &exptime)) goto end; if ((err = key_new(KEYFILE_KF(me), id, type, exptime, &k)) != 0) @@ -1955,9 +1958,9 @@ static PyObject *kfmeth_qtag(PyObject *me, PyObject *arg, PyObject *kw) char *tag; dstr d = DSTR_INIT; PyObject *rc = 0; - static char *kwlist[] = { "tag", "new", 0 }; + static const char *const kwlist[] = { "tag", "new", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O!:qtag", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s|O!:qtag", KWLIST, &tag, keydata_pytype, &newkdobj)) goto end; if (key_qtag(KEYFILE_KF(me), tag, &d, &k, &kd)) @@ -1991,8 +1994,8 @@ static PyMethodDef keyfile_pymethods[] = { #define METHNAME(func) kfmeth_##func METH (save, "KF.save()") KWMETH(merge, "KF.merge(FILE, [report = ])") - KWMETH(newkey, "KF.newkey(ID, TYPE, " - "[exptime = KEXP_FOREVER]) -> KEY") + KWMETH(newkey, "KF.newkey(ID, TYPE, [exptime = KEXP_FOREVER]) " + "-> KEY") METH (byid, "KF.byid(KEYID) -> KEY|None") METH (bytype, "KF.bytype(TYPE) -> KEY|None") METH (bytag, "KF.bytag(TAG) -> KEY|None") @@ -2043,7 +2046,8 @@ static PyTypeObject keyfile_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Keyring file.", + "KeyFile(FILE, [how = KOPEN_READ], [report = ?]): Keyring file.\n" + " calls REPORT(FILE, LINE, MSG) on problems", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -2068,24 +2072,13 @@ static PyTypeObject keyfile_pytype_skel = { /*----- Global stuff ------------------------------------------------------*/ -static PyObject *meth_barf(PyObject *me, PyObject *arg) -{ - int err; - - if (PyArg_ParseTuple(arg, "i:barf", &err)) - KEYERR(err); -end: - return (0); -} - static PyMethodDef methods[] = { #define METHNAME(func) meth_##func - METH (_KeyData_readflags, - "KeyData.readflags(STRING) -> (FLAGS, MASK, REST)") - METH (_KeyData_writeflags, "KeyData.writeflags(FLAGS) -> STRING") - METH (_KeyData_read, "KeyData.read(STRING) -> (KD, REST)") - METH (_KeyData_decode, "KeyData.decode(BYTES) -> KD") - METH (barf, "barf(ERR)") + METH (_KeyData_readflags, "KeyData.readflags(STRING) " + "-> (FLAGS, MASK, REST)") + METH (_KeyData_writeflags, "KeyData.writeflags(FLAGS) -> STRING") + METH (_KeyData_read, "KeyData.read(STRING) -> (KD, REST)") + METH (_KeyData_decode, "KeyData.decode(BYTES) -> KD") #undef METHNAME { 0 } };