Catcrypt tools: Roll out progress indicator stuff from hashsum.
[u/mdw/catacomb] / cc.h
diff --git a/cc.h b/cc.h
index 7c2c092..6f92978 100644 (file)
--- a/cc.h
+++ b/cc.h
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: cc.h,v 1.1 2004/04/17 09:58:37 mdw Exp $
+ * $Id$
  *
  * Catcrypt common stuff
  *
  * (c) 2004 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
 /*----- Header files ------------------------------------------------------*/
 
+#if _FILE_OFFSET_BITS != 64
+#  error "Must set _FILE_OFFSET_BITS to 64."
+#endif
+
 #include <stdio.h>
+#include <string.h>
+#include <time.h>
 
 #include <mLib/dstr.h>
 
 
 /*----- Data structures ---------------------------------------------------*/
 
+/* --- Progress indicators --- */
+
+typedef struct fprogress {
+  const char *bp;
+  off_t o, sz, olast;
+  time_t start, last;
+  char name[24];
+} fprogress;
+
 /* --- Key encapsulation --- */
 
 typedef struct kem {
@@ -67,12 +82,21 @@ 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 {
   const struct sigops *ops;
   key_packdef *kp;
   void *kd;
+  const gchash *ch;
   ghash *h;
 } sig;
 
@@ -85,6 +109,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 {
@@ -95,8 +128,10 @@ typedef struct enc {
 typedef struct encops {
   const char *name;
   const char *rmode, *wmode;
+  int nraw, ncook;
   enc *(*initenc)(FILE */*fp*/, const char */*msg*/);
-  enc *(*initdec)(FILE */*fp*/, const char */*msg*/);
+  enc *(*initdec)(FILE */*fp*/,
+                 int (*/*func*/)(const char *, void *), void */*p*/);
   int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/);
   int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/);
   int (*encdone)(enc */*e*/);
@@ -104,6 +139,8 @@ typedef struct encops {
   void (*destroy)(enc */*e*/);
 } encops;
 
+extern const encops enctab[];
+
 /*----- Functions provided ------------------------------------------------*/
 
 /* --- @getkem@ --- *
@@ -181,20 +218,45 @@ extern void freesig(sig */*s*/);
 
 extern const encops *getenc(const char */*enc*/);
 
+/* --- @checkbdry@ --- *
+ *
+ * Arguments:  @const char *b@ = boundary string found
+ *             @void *p@ = boundary string wanted
+ *
+ * Returns:    Nonzero if the boundary string is the one we wanted.
+ *
+ * Use:                Pass as @func@ to @initdec@ if you just want a simple life.
+ */
+
+extern int checkbdry(const char */*b*/, void */*p*/);
+
 /* --- @initenc@ --- *
  *
  * Arguments:  @const encops *eo@ = operations (from @getenc@)
  *             @FILE *fp@ = file handle to attach
  *             @const char *msg@ = banner message
- *             @int wantenc@ = nonzero if we want to encode
  *
  * Returns:    The encoder object.
  *
  * Use:                Initializes an encoder.
  */
 
-extern enc *initenc(const encops */*eo*/, FILE */*fp*/,
-                   const char */*msg*/, int /*wantenc*/);
+extern enc *initenc(const encops */*eo*/, FILE */*fp*/, const char */*msg*/);
+
+/* --- @initdec@ --- *
+ *
+ * Arguments:  @const encops *eo@ = operations (from @getenc@)
+ *             @FILE *fp@ = file handle to attach
+ *             @int (*func)(const char *, void *)@ = banner check function
+ *             @void *p@ = argument for @func@
+ *
+ * Returns:    The encoder object.
+ *
+ * Use:                Initializes an encoder.
+ */
+
+extern enc *initdec(const encops */*eo*/, FILE */*fp*/,
+                   int (*/*func*/)(const char *, void *), void */*p*/);
 
 /* --- @freeenc@ --- *
  *
@@ -207,6 +269,193 @@ extern enc *initenc(const encops */*eo*/, FILE */*fp*/,
 
 extern void freeenc(enc */*e*/);
 
+/* --- @cmd_encode@, @cmd_decode@ --- */
+
+#define CMD_ENCODE {                                                   \
+  "encode", cmd_encode,                                                        \
+    "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]",           \
+    "\
+Options:\n\
+\n\
+-f, --format=FORMAT    Encode to FORMAT.\n\
+-b, --boundary=LABEL   PEM boundary is LABEL.\n\
+-o, --output=FILE      Write output to FILE.\n\
+-p, --progress         Show progress on large files.\n\
+" }
+
+#define CMD_DECODE {                                                   \
+  "decode", cmd_decode,                                                        \
+    "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]",           \
+    "\
+Options:\n\
+\n\
+-f, --format=FORMAT    Decode from FORMAT.\n\
+-b, --boundary=LABEL   PEM boundary is LABEL.\n\
+-o, --output=FILE      Write output to FILE.\n\
+-p, --progress         Show progress on large files.\n\
+" }
+
+extern int cmd_encode(int /*argc*/, char */*argv*/[]);
+extern int cmd_decode(int /*argc*/, char */*argv*/[]);
+
+/* --- @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*/);
+
+/*----- Progress indicators -----------------------------------------------*/
+
+/* --- @fprogress_init@ --- *
+ *
+ * Arguments:  @fprogress *f@ = progress context to be initialized
+ *             @const char *name@ = file name string to show
+ *             @FILE *fp@ = file we're reading from
+ *
+ * Returns:    Zero on success, nonzero if the file's state is now broken.
+ *
+ * Use:                Initializes a progress context.  Nothing is actually
+ *             displayed yet.
+ */
+
+extern int fprogress_init(fprogress */*f*/,
+                         const char */*name*/, FILE */*fp*/);
+
+/* --- @fprogress_update@ --- *
+ *
+ * Arguments:  @fprogress *f@ = progress context
+ *             @size_t n@ = how much progress has been made
+ *
+ * Returns:    ---
+ *
+ * Use:                Maybe updates the display to show that some progress has been
+ *             made.
+ */
+
+extern void fprogress_update(fprogress */*f*/, size_t /*n*/);
+
+/* --- @fprogress_clear@ --- *
+ *
+ * Arguments:  @fprogress *f@ = progress context
+ *
+ * Returns:    ---
+ *
+ * Use:                Clears the progress display from the screen.
+ */
+
+extern void fprogress_clear(fprogress */*f*/);
+
+/* --- @fprogress_done@ --- *
+ *
+ * Arguments:  @fprogress *f@ = progress context
+ *
+ * Returns:    ---
+ *
+ * Use:                Clear up the progress context and removes any display.
+ */
+
+extern void fprogress_done(fprogress */*f*/);
+
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus