primeiter: New functions for iterating over small primes.
[u/mdw/catacomb] / key.h
diff --git a/key.h b/key.h
index d779489..dffd0eb 100644 (file)
--- a/key.h
+++ b/key.h
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: key.h,v 1.4 2000/02/12 18:21:02 mdw Exp $
+ * $Id$
  *
  * Simple key management
  *
  * (c) 1999 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,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: key.h,v $
- * Revision 1.4  2000/02/12 18:21:02  mdw
- * Overhaul of key management (again).
- *
- * Revision 1.3  1999/12/22 15:47:48  mdw
- * Major key-management revision.
- *
- * Revision 1.2  1999/12/10 23:29:48  mdw
- * Change header file guard names.
- *
- * Revision 1.1  1999/09/03 08:41:12  mdw
- * Initial import.
- *
- */
-
 #ifndef CATACOMB_KEY_H
 #define CATACOMB_KEY_H
 
 #include <mLib/hash.h>
 #include <mLib/sym.h>
 
+#ifndef CATACOMB_KEY_ERROR_H
+#  include "key-error.h"
+#endif
+
 #ifndef CATACOMB_KEY_DATA_H
 #  include "key-data.h"
 #endif
 
+#ifndef CATACOMB_GHASH_H
+#  include "ghash.h"
+#endif
+
 #ifndef CATACOMB_MP_H
 #  include "mp.h"
 #endif
@@ -93,7 +84,7 @@ typedef struct key_attr {
 typedef struct key {
 
   /* --- Hashtable management --- */
-  
+
   hash_base _b;                                /* Symbol table data */
   struct key *next;                    /* Next key of the same type */
 
@@ -106,7 +97,7 @@ typedef struct key {
 
   /* --- The key data itself --- */
 
-  key_data k;                          /* The actual key data */
+  key_data *k;                         /* The actual key data */
 
   /* --- Other attributes and commentary --- */
 
@@ -135,10 +126,8 @@ typedef struct key_file {
 
 /* --- Key file flags --- */
 
-enum {
-  KF_WRITE = 1,                                /* File opened for writing */
-  KF_MODIFIED = 2                      /* File has been modified */
-};
+#define KF_WRITE 1u                    /* File opened for writing */
+#define KF_MODIFIED 2u                 /* File has been modified */
 
 /* --- Iterating over keys --- *
  *
@@ -149,37 +138,27 @@ enum {
 typedef struct { hash_iter i; time_t t; } key_iter;
 typedef struct { sym_iter i; } key_attriter;
 
+/* --- Key fetching --- */
+
+typedef struct key_fetchdef {
+  char *name;                          /* Name of item */
+  size_t off;                          /* Offset into target structure */
+  unsigned e;                          /* Flags for the item */
+  const struct key_fetchdef *kf;       /* Substructure pointer */
+} key_fetchdef;
+
 /* --- File opening options --- */
 
-enum {
-  KOPEN_READ,
-  KOPEN_WRITE
-};
+#define KOPEN_READ 0u
+#define KOPEN_WRITE 1u
+#define KOPEN_MASK 0xff
+#define KOPEN_NOFILE 0x100
 
 /* --- Various other magic numbers --- */
 
 #define KEXP_FOREVER ((time_t)-1)      /* Never expire this key */
 #define KEXP_EXPIRE ((time_t)-2)       /* Expire this key when unused */
 
-/* --- Key error codes --- */
-
-enum {
-  KERR_OK = 0,                         /* No error */
-  KERR_BADTAG = -1,                    /* Malformed tag string */
-  KERR_BADTYPE = -2,                   /* Malformed type string */
-  KERR_BADCOMMENT = -3,                        /* Malformed comment string */
-  KERR_DUPID = -4,                     /* Duplicate keyid */
-  KERR_DUPTAG = -5,                    /* Duplicate key tag string */
-  KERR_READONLY = -6,                  /* Key file is read-only */
-  KERR_WILLEXPIRE = -7,                        /* Key will eventually expire */
-  KERR_EXPIRED = -8,                   /* Key has already expired */
-  KERR_BADFLAGS = -9,                  /* Error in flags string */
-  KERR_BADPASS = -10,                  /* Error decrypting locked key */
-  KERR_BADTYPE = -11,                  /* Key has incorrect type */
-  KERR_NOTFOUND = -12,                 /* Key couldn't be found */
-  KERR_MAX                             /* Largest possible error */
-};
-
 /* --- Write error codes --- */
 
 enum {
@@ -235,7 +214,7 @@ extern int key_extract(key_file */*f*/, key */*k*/, FILE */*fp*/,
  *
  * Arguments:  @key_file *f@ = pointer to file structure to initialize
  *             @const char *file@ = pointer to the file name
- *             @int how@ = opening options (@KOPEN_*@).
+ *             @unsigned how@ = opening options (@KOPEN_*@).
  *             @key_reporter *rep@ = error reporting function
  *             @void *arg@ = argument for function
  *
@@ -249,9 +228,20 @@ extern int key_extract(key_file */*f*/, key */*k*/, FILE */*fp*/,
  *             owner only.
  */
 
-extern int key_open(key_file */*f*/, const char */*file*/, int /*how*/,
+extern int key_open(key_file */*f*/, const char */*file*/, unsigned /*how*/,
                    key_reporter */*rep*/, void */*arg*/);
 
+/* --- @key_discard@ --- *
+ *
+ * Arguments:  @key_file *f@ = pointer to key file block
+ *
+ * Returns:    ---
+ *
+ * Use:                Frees all the key data, without writing changes.
+ */
+
+extern void key_discard(key_file */*f*/);
+
 /* --- @key_close@ --- *
  *
  * Arguments:  @key_file *f@ = pointer to key file block
@@ -286,7 +276,7 @@ extern int key_save(key_file */*f*/);
  *
  * Arguments:  @key_file *f@ = pointer to file structure to initialize
  *             @const char *file@ = pointer to the file name
- *             @int how@ = opening options (@KOPEN_*@).
+ *             @unsigned how@ = opening options (@KOPEN_*@).
  *
  * Returns:    Zero if it worked, nonzero otherwise.
  *
@@ -301,7 +291,8 @@ extern int key_save(key_file */*f*/);
  *             for the private use of @key_open@.
  */
 
-extern int key_lockfile(key_file */*f*/, const char */*file*/, int /*how*/);
+extern int key_lockfile(key_file */*f*/, const char */*file*/,
+                       unsigned /*how*/);
 
 /*----- Creating and manipulating keys ------------------------------------*/
 
@@ -311,10 +302,9 @@ extern int key_lockfile(key_file */*f*/, const char */*file*/, int /*how*/);
  *             @uint32 id@ = keyid to set
  *             @const char *type@ = the type of this key
  *             @time_t exp@ = when the key expires
- *             @int *err@ = where to store the error condition
+ *             @key *kk@ = where to put the key pointer
  *
- * Returns:    Key block containing new data, or null if it couldn't be
- *             done.
+ * Returns:    Error code (one of the @KERR@ constants).
  *
  * Use:                Attaches a new key to a key file.  You must have a writable
  *             key file for this to work.
@@ -333,8 +323,8 @@ extern int key_lockfile(key_file */*f*/, const char */*file*/, int /*how*/);
  *             You have to set the actual key yourself.
  */
 
-extern key *key_new(key_file */*f*/, uint32 /*id*/, const char */*type*/,
-                   time_t /*exp*/, int */*err*/);
+extern int key_new(key_file */*f*/, uint32 /*id*/, const char */*type*/,
+                  time_t /*exp*/, key **/*kk*/);
 
 /* --- @key_delete@ --- *
  *
@@ -353,6 +343,15 @@ extern key *key_new(key_file */*f*/, uint32 /*id*/, const char */*type*/,
 
 extern int key_delete(key_file */*f*/, key */*k*/);
 
+/* --- @key_expired@ --- *
+ *
+ * Arguments:  @key *k@ = pointer to key block
+ *
+ * Returns:    Zero if the key is OK, nonzero if it's expired.
+ */
+
+int key_expired(key */*k*/);
+
 /* --- @key_expire@ --- *
  *
  * Arguments:  @key_file *f@ = pointer to file block
@@ -387,6 +386,20 @@ extern int key_expire(key_file */*f*/, key */*k*/);
 
 extern int key_used(key_file */*f*/, key */*k*/, time_t /*t*/);
 
+/* --- @key_fingerprint@ --- *
+ *
+ * Arguments:  @key *k@ = the key to fingerprint
+ *             @ghash *h@ = the hash to use
+ *             @const key_filter *kf@ = filter to apply
+ *
+ * Returns:    Nonzero if the key slightly matched the filter.
+ *
+ * Use:                Updates the hash context with the key contents.
+ */
+
+extern int key_fingerprint(key */*k*/, ghash */*h*/,
+                          const key_filter */*kf*/);
+
 /*----- Setting and reading attributes ------------------------------------*/
 
 /* --- @key_chkident@ --- *
@@ -437,6 +450,19 @@ extern int key_setcomment(key_file */*f*/, key */*k*/, const char */*c*/);
 
 extern int key_settag(key_file */*f*/, key */*k*/, const char */*tag*/);
 
+/* --- @key_setkeydata@ --- *
+ *
+ * Arguments:  @key_file *kf@ = pointer to key file
+ *             @key *k@ = pointer to key
+ *             @key_data *kd@ = new key data
+ *
+ * Returns:    Zero on success, or a @KERR_@ error code on failure.
+ *
+ * Use:                Sets the key data for a key.
+ */
+
+extern int key_setkeydata(key_file */*kf*/, key */*k*/, key_data */*kd*/);
+
 /* --- @key_fulltag@ --- *
  *
  * Arguments:  @key *k@ = pointer to key
@@ -457,7 +483,7 @@ extern void key_fulltag(key */*k*/, dstr */*d*/);
  *             @const char *tag@ = pointer to tag string
  *             @dstr *d@ = pointer to string for full tag name
  *             @key **k@ = where to store the key pointer
- *             @key_data **kd@ = where to store the key data pointer
+ *             @key_data ***kd@ = where to store the key data pointer
  *
  * Returns:    Zero if OK, nonzero if it failed.
  *
@@ -465,11 +491,11 @@ extern void key_fulltag(key */*k*/, dstr */*d*/);
  *             qualified by the names of subkeys, separated by dots.  Hence,
  *             a qualified tag is ID|TAG[.TAG...].  The various result
  *             pointers can be null to indicate that the result isn't
- *             interesting. 
+ *             interesting.
  */
 
 extern int key_qtag(key_file */*f*/, const char */*tag*/,
-                   dstr */*d*/, key **/*k*/, key_data **/*kd*/);
+                   dstr */*d*/, key **/*k*/, key_data ***/*kd*/);
 
 /* --- @key_getattr@ --- *
  *
@@ -593,6 +619,63 @@ extern void key_mkiter(key_iter */*i*/, key_file */*f*/);
 
 extern key *key_next(key_iter */*i*/);
 
+/*----- Fetching key data conveniently ------------------------------------*/
+
+/* --- @key_fetchinit@ --- *
+ *
+ * Arguments:  @const key_fetchdef *kf@ = pointer to base definition
+ *             @key_packstruct *kps@ = pointer to destination packing def
+ *             @void *p@ = pointer to destination block
+ *
+ * Returns:    Pointer to packing definition.
+ *
+ * Use:                Initializes a packing definition (@key_packdef@ structure).
+ *             If @kps@ is null on entry, an appropriately sized block is
+ *             allocated automatically.  Otherwise it must be large enough.
+ */
+
+extern key_packdef *key_fetchinit(const key_fetchdef */*kf*/,
+                                 key_packstruct */*kp*/, void */*p*/);
+
+/* --- @key_fetch@ --- *
+ *
+ * Arguments:  @key_packdef *kp@ = pointer to packing structure
+ *             @key *k@ = key file containing desired key
+ *
+ * Returns:    Error code, or zero.
+ *
+ * Use:                Fetches an unpacked key from a packed one.
+ */
+
+extern int key_fetch(key_packdef */*kp*/, key */*k*/);
+
+/* --- @key_fetchbyname@ --- *
+ *
+ * Arguments:  @key_packdef *kp@ = pointer to packing structure
+ *             @key_file *kf@ = key file containing desired key
+ *             @const char *tag@ = user's tag describing the key
+ *
+ * Returns:    Error code, or zero.
+ *
+ * Use:                Fetches a named key from a key file and unpacks it
+ *             conveniently.
+ */
+
+extern int key_fetchbyname(key_packdef */*kp*/,
+                          key_file */*kf*/, const char */*tag*/);
+
+/* --- @key_fetchdone@ --- *
+ *
+ * Arguments:  @key_packdef *kp@ = pointer to packing structure
+ *
+ * Returns:    ---
+ *
+ * Use:                Frees a packing structure.  If the structure was allocated by
+ *             @key_fetchinit@ then it is freed.
+ */
+
+extern void key_fetchdone(key_packdef */*kp*/);
+
 /*----- Other functions ---------------------------------------------------*/
 
 /* --- @key_moan@ --- *
@@ -610,17 +693,6 @@ extern key *key_next(key_iter */*i*/);
 extern void key_moan(const char */*file*/, int /*line*/,
                     const char */*msg*/, void */*p*/);
 
-/* --- @key_strerror@ --- *
- *
- * Arguments:  @int err@ = error code from @key_new@
- *
- * Returns:    Pointer to error string.
- *
- * Use:                Translates a @KERR@ error code into a human-readable string.
- */
-
-extern const char *key_strerror(int /*err*/);
-
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus