*.c: Publish algorithm and crypto-group tables as `mapping' objects.
[catacomb-python] / algorithms.c
index f792087..1b1545c 100644 (file)
@@ -3612,12 +3612,14 @@ void algorithms_pyinit(void)
   addmethods(methods);
 }
 
-GEN(gcciphers, cipher)
-GEN(gcaeads, aead)
-GEN(gchashes, hash)
-GEN(gcmacs, mac)
 #define gcprp prpinfo
-GEN(gcprps, prp)
+#define CLASS_TABLES(_) _(cipher) _(aead) _(hash) _(mac) _(prp)
+#define TABLE_FNS(pre)                                                 \
+  static const char *pre##_namefn(const void *p)                       \
+    { const gc##pre *const *cls = p; return (*cls ? (*cls)->name : 0); } \
+  static PyObject *pre##_valfn(const void *p)                          \
+    { gc##pre *const*cls = p; return (gc##pre##_pywrap(*cls)); }
+CLASS_TABLES(TABLE_FNS)
 
 void algorithms_pyinsert(PyObject *mod)
 {
@@ -3628,7 +3630,8 @@ void algorithms_pyinsert(PyObject *mod)
   INSERT("KeySZSet", keyszset_pytype);
   INSERT("GCCipher", gccipher_pytype);
   INSERT("GCipher", gcipher_pytype);
-  INSERT("gcciphers", gcciphers());
+  INSERT("gcciphers", make_algtab(gciphertab, sizeof(gccipher *),
+                                 cipher_namefn, cipher_valfn));
   INSERT("GCAEAD", gcaead_pytype);
   INSERT("GAEKey", gaeadkey_pytype);
   INSERT("GAEAADClass", gcaeadaad_pytype);
@@ -3637,16 +3640,19 @@ void algorithms_pyinsert(PyObject *mod)
   INSERT("GAEEnc", gaeadenc_pytype);
   INSERT("GAEDecClass", gcaeaddec_pytype);
   INSERT("GAEDec", gaeaddec_pytype);
-  INSERT("gcaeads", gcaeads());
+  INSERT("gcaeads", make_algtab(gaeadtab, sizeof(gcaead *),
+                               aead_namefn, aead_valfn));
   INSERT("GCHash", gchash_pytype);
   INSERT("GHash", ghash_pytype);
-  INSERT("gchashes", d = gchashes());
-  sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
-  has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
+  d = make_algtab(ghashtab, sizeof(gchash *), hash_namefn, hash_valfn);
+  INSERT("gchashes", d);
+  sha_pyobj = PyMapping_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
+  has160_pyobj = PyMapping_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
   INSERT("GCMAC", gcmac_pytype);
   INSERT("GMAC", gmac_pytype);
   INSERT("GMACHash", gmhash_pytype);
-  INSERT("gcmacs", gcmacs());
+  INSERT("gcmacs", make_algtab(gmactab, sizeof(gcmac *),
+                              mac_namefn, mac_valfn));
   INSERT("Poly1305Class", poly1305cls_pytype);
   INSERT("poly1305", poly1305key_pytype);
   INSERT("Poly1305Hash", poly1305hash_pytype);
@@ -3656,7 +3662,8 @@ void algorithms_pyinsert(PyObject *mod)
   INSERT("Shake256", shake256_pytype);
   INSERT("GCPRP", gcprp_pytype);
   INSERT("GPRP", gprp_pytype);
-  INSERT("gcprps", gcprps());
+  INSERT("gcprps", make_algtab(gprptab, sizeof(gcprp *),
+                              prp_namefn, prp_valfn));
   setconstants(mod, consts);
 }