General utilities cleanup. Add signature support to catcrypt. Throw in
[u/mdw/catacomb] / cc.h
diff --git a/cc.h b/cc.h
index 7c2c092..24fc205 100644 (file)
--- a/cc.h
+++ b/cc.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: cc.h,v 1.1 2004/04/17 09:58:37 mdw Exp $
+ * $Id$
  *
  * Catcrypt common stuff
  *
@@ -37,6 +37,7 @@
 /*----- Header files ------------------------------------------------------*/
 
 #include <stdio.h>
+#include <string.h>
 
 #include <mLib/dstr.h>
 
@@ -67,6 +68,14 @@ typedef struct kemops {
   void (*destroy)(kem */*k*/);
 } kemops;
 
+struct kemtab {
+  const char *name;
+  const kemops *encops;
+  const kemops *decops;
+};
+
+extern const struct kemtab kemtab[];
+
 /* --- Signing --- */
 
 typedef struct sig {
@@ -85,6 +94,15 @@ typedef struct sigops {
   void (*destroy)(sig */*s*/);
 } sigops;
 
+struct sigtab {
+  const char *name;
+  const sigops *signops;
+  const sigops *verifyops;
+  const gchash *ch;
+};
+    
+extern const struct sigtab sigtab[];
+
 /* --- Data encoding --- */
 
 typedef struct enc {
@@ -104,6 +122,8 @@ typedef struct encops {
   void (*destroy)(enc */*e*/);
 } encops;
 
+extern const encops enctab[];
+
 /*----- Functions provided ------------------------------------------------*/
 
 /* --- @getkem@ --- *
@@ -207,6 +227,112 @@ extern enc *initenc(const encops */*eo*/, FILE */*fp*/,
 
 extern void freeenc(enc */*e*/);
 
+/* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
+ *
+ * Produce list of things.  Requires @i@ and @w@ variables in scope.
+ * END-TEST and NAME-EXPR are in terms of @i@.
+ */
+
+#define LIST(what, fp, end, name) do {                                 \
+  fputs(what ":\n  ", fp);                                             \
+  w = 2;                                                               \
+  for (i = 0; end; i++) {                                              \
+    if (w == 2)                                                                \
+      w += strlen(name);                                               \
+    else {                                                             \
+      if (strlen(name) + w > 76) {                                     \
+       fputs("\n  ", fp);                                              \
+       w = 2 + strlen(name);                                           \
+      } else {                                                         \
+       fputc(' ', fp);                                                 \
+       w += strlen(name) + 1;                                          \
+      }                                                                        \
+    }                                                                  \
+    fputs(name, fp);                                                   \
+  }                                                                    \
+  fputc('\n', fp);                                                     \
+} while (0)
+
+#define STDLISTS(LI)                                                   \
+  LI("Hash functions", hash,                                           \
+     ghashtab[i], ghashtab[i]->name)                                   \
+  LI("Encryption schemes", enc,                                                \
+     gciphertab[i], gciphertab[i]->name)                               \
+  LI("Message authentication schemes", mac,                            \
+     gmactab[i], gmactab[i]->name)                                     \
+  LI("Elliptic curves", ec,                                            \
+     ectab[i].name, ectab[i].name)                                     \
+  LI("Diffie-Hellman groups", dh,                                      \
+     ptab[i].name, ptab[i].name)
+
+#define LIDECL(text, tag, test, name)                                  \
+  static void show_##tag(void);
+
+#define LIDEF(text, tag, test, name)                                   \
+  static void show_##tag(void)                                         \
+  {                                                                    \
+    unsigned i, w;                                                     \
+    LIST(text, stdout, test, name);                                    \
+  }
+
+#define LIENT(text, tag, test, name)                                   \
+  { #tag, show_##tag },
+
+struct listent {
+  const char *name;
+  void (*list)(void);
+};
+
+#define MAKELISTTAB(listtab, LISTS)                                    \
+  LISTS(LIDECL)                                                                \
+  static const struct listent listtab[] = {                            \
+    LISTS(LIENT)                                                       \
+    { 0, 0 }                                                           \
+  };                                                                   \
+  LISTS(LIDEF)
+
+extern int displaylists(const struct listent */*listtab*/,
+                       char *const /*argv*/[]);
+
+/*----- Subcommand dispatch -----------------------------------------------*/
+
+typedef struct cmd {
+  const char *name;
+  int (*cmd)(int /*argc*/, char */*argv*/[]);
+  const char *usage;
+  const char *help;
+} cmd;
+
+extern void version(FILE */*fp*/);
+extern void help_global(FILE */*fp*/);
+
+/* --- @findcmd@ --- *
+ *
+ * Arguments:  @const cmd *cmds@ = pointer to command table
+ *             @const char *name@ = a command name
+ *
+ * Returns:    Pointer to the command structure.
+ *
+ * Use:                Looks up a command by name.  If the command isn't found, an
+ *             error is reported and the program is terminated.
+ */
+
+const cmd *findcmd(const cmd */*cmds*/, const char */*name*/);
+
+/* --- @sc_help@ --- *
+ *
+ * Arguments:  @const cmd *cmds@ = pointer to command table
+ *             @FILE *fp@ = output file handle
+ *             @char *const *argv@ = remaining arguments
+ *
+ * Returns:    ---
+ *
+ * Use:                Prints a help message, maybe with help about subcommands.
+ */
+
+extern void sc_help(const cmd */*cmds*/, FILE */*fp*/,
+                   char *const */*argv*/);
+
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus