3 * Reading and writing key flag strings
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 #include <mLib/bits.h>
34 #include <mLib/dstr.h>
38 /*----- Data structures ---------------------------------------------------*/
40 typedef struct key_flags {
45 /*----- Flags table -------------------------------------------------------*/
47 typedef struct flagent {
53 static const flagent flagtab[] = {
55 /* --- Encoding types --- */
57 { "binary", KENC_BINARY, KF_ENCMASK },
58 { "integer", KENC_MP, KF_ENCMASK },
59 { "struct", KENC_STRUCT, KF_ENCMASK },
60 { "encrypt", KENC_ENCRYPT, KF_ENCMASK },
61 { "string", KENC_STRING, KF_ENCMASK },
62 { "ec", KENC_EC, KF_ENCMASK },
64 /* --- Classes of keys --- */
66 { "shared", KCAT_SHARE, KF_CATMASK },
67 { "public", KCAT_PUB, KF_CATMASK },
68 { "private", KCAT_PRIV, KF_CATMASK },
69 { "symmetric", KCAT_SYMM, KF_CATMASK },
70 { "secret", 0, KF_NONSECRET },
71 { "-secret", KF_NONSECRET, KF_NONSECRET },
73 /* --- Other flags --- */
75 { "burn", KF_BURN, KF_BURN },
76 { "-burn", 0, KF_BURN },
78 /* --- End marker --- */
83 /*----- Main code ---------------------------------------------------------*/
85 /* --- @key_readflags@ --- *
87 * Arguments: @const char *p@ = pointer to string to read
88 * @char **pp@ = where to store the end pointer
89 * @unsigned *ff@ = where to store the flags
90 * @unsigned *mm@ = where to store the mask
92 * Returns: Zero if all went well, nonzero if there was an error.
94 * Use: Reads a flag string.
97 int key_readflags(const char *p, char **pp, unsigned *ff, unsigned *mm)
99 unsigned f = 0, m = 0;
102 size_t sz = strcspn(p, ",:");
103 const flagent *e, *ee = 0;
105 /* --- Look up the string in the flags table --- */
107 if (sz == 4 && strncmp(p, "none", 4) == 0)
109 for (e = flagtab; e->name; e++) {
110 if (strncmp(e->name, p, sz) == 0) {
111 if (e->name[sz] == 0) {
115 return (KERR_BADFLAGS);
121 return (KERR_BADFLAGS);
123 /* --- Adjust the flag words --- *
125 * Ensure that the flags set are disjoint.
129 return (KERR_BADFLAGS);
134 if (*p == 0 || *p == ':')
139 /* --- Report the results --- */
143 if (pp) *pp = (char *)p;
147 /* --- @key_writeflags@ --- *
149 * Arguments: @unsigned f@ = flags to write
150 * @dstr *d@ = pointer to destination string
154 * Use: Emits a flags word as a string representation.
157 void key_writeflags(unsigned f, dstr *d)
163 for (e = flagtab; e->name; e++) {
164 if (m & e->m || e->name[0] == '-' || (f & e->m) != e->f)
174 /* --- @key_match@ --- *
176 * Arguments: @key_data *k@ = pointer to key data block
177 * @const key_filter *kf@ = pointer to filter block
179 * Returns: Nonzero if the key matches the filter.
181 * Use: Checks whether a key matches a filter.
184 int key_match(key_data *k, const key_filter *kf)
192 if ((k->e & KF_ENCMASK) != KENC_STRUCT)
193 return ((k->e & kf->m) == kf->f);
195 for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &kd); ) {
196 if (key_match(kd, kf))
202 /*----- That's all, folks -------------------------------------------------*/