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 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
36 #include <mLib/dstr.h>
40 /*----- Data structures ---------------------------------------------------*/
42 typedef struct key_flags
{
47 /*----- Flags table -------------------------------------------------------*/
49 typedef struct flagent
{
55 static const flagent flagtab
[] = {
57 /* --- Encoding types --- */
59 { "binary", KENC_BINARY
, KF_ENCMASK
},
60 { "integer", KENC_MP
, KF_ENCMASK
},
61 { "struct", KENC_STRUCT
, KF_ENCMASK
},
62 { "encrypt", KENC_ENCRYPT
, KF_ENCMASK
},
63 { "string", KENC_STRING
, KF_ENCMASK
},
64 { "ec", KENC_EC
, KF_ENCMASK
},
66 /* --- Classes of keys --- */
68 { "shared", KCAT_SHARE
, KF_CATMASK
},
69 { "public", KCAT_PUB
, KF_CATMASK
},
70 { "private", KCAT_PRIV
, KF_CATMASK
},
71 { "symmetric", KCAT_SYMM
, KF_CATMASK
},
72 { "secret", 0, KF_NONSECRET
},
73 { "-secret", KF_NONSECRET
, KF_NONSECRET
},
75 /* --- Other flags --- */
77 { "burn", KF_BURN
, KF_BURN
},
78 { "-burn", 0, KF_BURN
},
80 /* --- End marker --- */
85 /*----- Main code ---------------------------------------------------------*/
87 /* --- @key_readflags@ --- *
89 * Arguments: @const char *p@ = pointer to string to read
90 * @char **pp@ = where to store the end pointer
91 * @unsigned *ff@ = where to store the flags
92 * @unsigned *mm@ = where to store the mask
94 * Returns: Zero if all went well, nonzero if there was an error.
96 * Use: Reads a flag string.
99 int key_readflags(const char *p
, char **pp
, unsigned *ff
, unsigned *mm
)
101 unsigned f
= 0, m
= 0;
104 size_t sz
= strcspn(p
, ",:");
105 const flagent
*e
, *ee
= 0;
107 /* --- Look up the string in the flags table --- */
109 if (sz
== 4 && strncmp(p
, "none", 4) == 0)
111 for (e
= flagtab
; e
->name
; e
++) {
112 if (strncmp(e
->name
, p
, sz
) == 0) {
113 if (e
->name
[sz
] == 0) {
117 return (KERR_BADFLAGS
);
123 return (KERR_BADFLAGS
);
125 /* --- Adjust the flag words --- *
127 * Ensure that the flags set are disjoint.
131 return (KERR_BADFLAGS
);
136 if (*p
== 0 || *p
== ':')
141 /* --- Report the results --- */
145 if (pp
) *pp
= (char *)p
;
149 /* --- @key_writeflags@ --- *
151 * Arguments: @unsigned f@ = flags to write
152 * @dstr *d@ = pointer to destination string
156 * Use: Emits a flags word as a string representation.
159 void key_writeflags(unsigned f
, dstr
*d
)
165 for (e
= flagtab
; e
->name
; e
++) {
166 if (m
& e
->m
|| e
->name
[0] == '-' || (f
& e
->m
) != e
->f
)
176 /* --- @key_match@ --- *
178 * Arguments: @key_data *k@ = pointer to key data block
179 * @const key_filter *kf@ = pointer to filter block
181 * Returns: Nonzero if the key matches the filter.
183 * Use: Checks whether a key matches a filter.
186 int key_match(key_data
*k
, const key_filter
*kf
)
194 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
195 return ((k
->e
& kf
->m
) == kf
->f
);
197 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &kd
); ) {
198 if (key_match(kd
, kf
))
204 /*----- That's all, folks -------------------------------------------------*/