3 * $Id: key-flags.c,v 1.1 1999/12/22 15:47:48 mdw Exp $
5 * Reading and writing key flag strings
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: key-flags.c,v $
33 * Revision 1.1 1999/12/22 15:47:48 mdw
34 * Major key-management revision.
38 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/bits.h>
44 #include <mLib/dstr.h>
48 /*----- Data structures ---------------------------------------------------*/
50 typedef struct key_flags
{
55 /*----- Flags table -------------------------------------------------------*/
57 typedef struct flagent
{
63 static flagent flagtab
[] = {
65 /* --- Encoding types --- */
67 { "binary", KENC_BINARY
, KF_ENCMASK
},
68 { "integer", KENC_MP
, KF_ENCMASK
},
69 { "struct", KENC_STRUCT
, KF_ENCMASK
},
70 { "encrypt", KENC_ENCRYPT
, KF_ENCMASK
},
72 /* --- Classes of keys --- */
74 { "shared", KCAT_SHARE
, KF_CATMASK
},
75 { "public", KCAT_PUB
, KF_CATMASK
},
76 { "private", KCAT_PRIV
, KF_CATMASK
},
77 { "symmetric", KCAT_SYMM
, KF_CATMASK
},
78 { "secret", 0, KF_NONSECRET
},
79 { "-secret", KF_NONSECRET
, KF_NONSECRET
},
81 /* --- Other flags --- */
83 { "burn", KF_BURN
, KF_BURN
},
84 { "-burn", 0, KF_BURN
},
86 /* --- End marker --- */
91 /*----- Main code ---------------------------------------------------------*/
93 /* --- @key_readflags@ --- *
95 * Arguments: @const char *p@ = pointer to string to read
96 * @char **pp@ = where to store the end pointer
97 * @unsigned *ff@ = where to store the flags
98 * @unsigned *mm@ = where to store the mask
100 * Returns: Zero if all went well, nonzero if there was an error.
102 * Use: Reads a flag string.
105 int key_readflags(const char *p
, char **pp
, unsigned *ff
, unsigned *mm
)
107 unsigned f
= 0, m
= 0;
110 size_t sz
= strcspn(p
, ",:");
113 /* --- Look up the string in the flags table --- */
115 for (e
= flagtab
; e
->name
; e
++) {
116 if (strncmp(e
->name
, p
, sz
) == 0) {
117 if (e
->name
[sz
] == 0) {
121 return (KERR_BADFLAGS
);
127 return (KERR_BADFLAGS
);
129 /* --- Adjust the flag words --- *
131 * Ensure that the flags set are disjoint.
135 return (KERR_BADFLAGS
);
139 if (*p
== 0 || *p
== ':')
144 /* --- Report the results --- */
148 if (pp
) *pp
= (char *)p
;
152 /* --- @key_writeflags@ --- *
154 * Arguments: @unsigned f@ = flags to write
155 * @dstr *d@ = pointer to destination string
159 * Use: Emits a flags word as a string representation.
162 void key_writeflags(unsigned f
, dstr
*d
)
168 for (e
= flagtab
; e
->name
; e
++) {
169 if (m
& e
->m
|| e
->name
[0] == '-' || (f
& e
->m
) != e
->f
)
179 /*----- That's all, folks -------------------------------------------------*/