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>
35 #include <mLib/macros.h>
39 /*----- Data structures ---------------------------------------------------*/
41 typedef struct key_flags
{
46 /*----- Flags table -------------------------------------------------------*/
48 typedef struct flagent
{
54 static const flagent flagtab
[] = {
56 /* --- Encoding types --- */
58 { "binary", KENC_BINARY
, KF_ENCMASK
},
59 { "integer", KENC_MP
, KF_ENCMASK
},
60 { "struct", KENC_STRUCT
, KF_ENCMASK
},
61 { "encrypt", KENC_ENCRYPT
, KF_ENCMASK
},
62 { "string", KENC_STRING
, KF_ENCMASK
},
63 { "ec", KENC_EC
, KF_ENCMASK
},
65 /* --- Classes of keys --- */
67 { "shared", KCAT_SHARE
, KF_CATMASK
},
68 { "public", KCAT_PUB
, KF_CATMASK
},
69 { "private", KCAT_PRIV
, KF_CATMASK
},
70 { "symmetric", KCAT_SYMM
, KF_CATMASK
},
71 { "secret", 0, KF_NONSECRET
},
72 { "-secret", KF_NONSECRET
, KF_NONSECRET
},
74 /* --- Other flags --- */
76 { "burn", KF_BURN
, KF_BURN
},
77 { "-burn", 0, KF_BURN
},
79 /* --- End marker --- */
84 /*----- Main code ---------------------------------------------------------*/
86 /* --- @key_readflags@ --- *
88 * Arguments: @const char *p@ = pointer to string to read
89 * @char **pp@ = where to store the end pointer
90 * @unsigned *ff@ = where to store the flags
91 * @unsigned *mm@ = where to store the mask
93 * Returns: Zero if all went well, nonzero if there was an error.
95 * Use: Reads a flag string.
98 int key_readflags(const char *p
, char **pp
, unsigned *ff
, unsigned *mm
)
100 unsigned f
= 0, m
= 0;
103 size_t sz
= strcspn(p
, ",:");
104 const flagent
*e
, *ee
= 0;
106 /* --- Look up the string in the flags table --- */
108 if (sz
== 4 && STRNCMP(p
, ==, "none", 4))
110 for (e
= flagtab
; e
->name
; e
++) {
111 if (STRNCMP(e
->name
, ==, p
, sz
)) {
112 if (e
->name
[sz
] == 0) {
116 return (KERR_BADFLAGS
);
122 return (KERR_BADFLAGS
);
124 /* --- Adjust the flag words --- *
126 * Ensure that the flags set are disjoint.
130 return (KERR_BADFLAGS
);
135 if (*p
== 0 || *p
== ':')
140 /* --- Report the results --- */
144 if (pp
) *pp
= (char *)p
;
148 /* --- @key_writeflags@ --- *
150 * Arguments: @unsigned f@ = flags to write
151 * @dstr *d@ = pointer to destination string
155 * Use: Emits a flags word as a string representation.
158 void key_writeflags(unsigned f
, dstr
*d
)
164 for (e
= flagtab
; e
->name
; e
++) {
165 if (m
& e
->m
|| e
->name
[0] == '-' || (f
& e
->m
) != e
->f
)
175 /* --- @key_match@ --- *
177 * Arguments: @key_data *k@ = pointer to key data block
178 * @const key_filter *kf@ = pointer to filter block
180 * Returns: Nonzero if the key matches the filter.
182 * Use: Checks whether a key matches a filter.
185 int key_match(key_data
*k
, const key_filter
*kf
)
193 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
194 return ((k
->e
& kf
->m
) == kf
->f
);
196 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &kd
); ) {
197 if (key_match(kd
, kf
))
203 /*----- That's all, folks -------------------------------------------------*/