3 * $Id: key-flags.c,v 1.2 2000/02/12 18:21:02 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.2 2000/02/12 18:21:02 mdw
34 * Overhaul of key management (again).
36 * Revision 1.1 1999/12/22 15:47:48 mdw
37 * Major key-management revision.
41 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/bits.h>
47 #include <mLib/dstr.h>
51 /*----- Data structures ---------------------------------------------------*/
53 typedef struct key_flags
{
58 /*----- Flags table -------------------------------------------------------*/
60 typedef struct flagent
{
66 static flagent flagtab
[] = {
68 /* --- Encoding types --- */
70 { "binary", KENC_BINARY
, KF_ENCMASK
},
71 { "integer", KENC_MP
, KF_ENCMASK
},
72 { "struct", KENC_STRUCT
, KF_ENCMASK
},
73 { "encrypt", KENC_ENCRYPT
, KF_ENCMASK
},
75 /* --- Classes of keys --- */
77 { "shared", KCAT_SHARE
, KF_CATMASK
},
78 { "public", KCAT_PUB
, KF_CATMASK
},
79 { "private", KCAT_PRIV
, KF_CATMASK
},
80 { "symmetric", KCAT_SYMM
, KF_CATMASK
},
81 { "secret", 0, KF_NONSECRET
},
82 { "-secret", KF_NONSECRET
, KF_NONSECRET
},
84 /* --- Other flags --- */
86 { "burn", KF_BURN
, KF_BURN
},
87 { "-burn", 0, KF_BURN
},
89 /* --- End marker --- */
94 /*----- Main code ---------------------------------------------------------*/
96 /* --- @key_readflags@ --- *
98 * Arguments: @const char *p@ = pointer to string to read
99 * @char **pp@ = where to store the end pointer
100 * @unsigned *ff@ = where to store the flags
101 * @unsigned *mm@ = where to store the mask
103 * Returns: Zero if all went well, nonzero if there was an error.
105 * Use: Reads a flag string.
108 int key_readflags(const char *p
, char **pp
, unsigned *ff
, unsigned *mm
)
110 unsigned f
= 0, m
= 0;
113 size_t sz
= strcspn(p
, ",:");
116 /* --- Look up the string in the flags table --- */
118 for (e
= flagtab
; e
->name
; e
++) {
119 if (strncmp(e
->name
, p
, sz
) == 0) {
120 if (e
->name
[sz
] == 0) {
124 return (KERR_BADFLAGS
);
130 return (KERR_BADFLAGS
);
132 /* --- Adjust the flag words --- *
134 * Ensure that the flags set are disjoint.
138 return (KERR_BADFLAGS
);
142 if (*p
== 0 || *p
== ':')
147 /* --- Report the results --- */
151 if (pp
) *pp
= (char *)p
;
155 /* --- @key_writeflags@ --- *
157 * Arguments: @unsigned f@ = flags to write
158 * @dstr *d@ = pointer to destination string
162 * Use: Emits a flags word as a string representation.
165 void key_writeflags(unsigned f
, dstr
*d
)
171 for (e
= flagtab
; e
->name
; e
++) {
172 if (m
& e
->m
|| e
->name
[0] == '-' || (f
& e
->m
) != e
->f
)
182 /* --- @key_match@ --- *
184 * Arguments: @key_data *k@ = pointer to key data block
185 * @const key_filter *kf@ = pointer to filter block
187 * Returns: Nonzero if the key matches the filter.
189 * Use: Checks whether a key matches a filter.
192 int key_match(key_data
*k
, const key_filter
*kf
)
199 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
200 return ((k
->e
& kf
->m
) == kf
->f
);
202 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
203 if (key_match(&ks
->k
, kf
))
209 /*----- That's all, folks -------------------------------------------------*/