OpenDNSSEC-enforcer  1.3.15
ksm_keyword.c
Go to the documentation of this file.
1 /*
2  * $Id: ksm_keyword.c 3082 2010-03-24 17:15:20Z sion $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * ksm_keyword - Keyword/Value Conversions
31  *
32  * Description:
33  * Some values in the database are numeric but need to be translated to
34  * and from strings. This module does that.
35  *
36  * Although the translations are held in tables, this nmodule hard-codes
37  * the strings in the code.
38 -*/
39 
40 #include <assert.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "ksm/ksm.h"
46 #include "ksm/string_util.h"
47 #include "ksm/string_util2.h"
48 
49 /* Mapping of keywords to values */
50 
51 static STR_KEYWORD_ELEMENT m_algorithm_keywords[] = {
63  {NULL, -1}
64 };
65 
66 static STR_KEYWORD_ELEMENT m_format_keywords[] = {
70  {NULL, -1}
71 };
72 
73 static STR_KEYWORD_ELEMENT m_state_keywords[] = {
84  {NULL, -1}
85 };
86 
87 static STR_KEYWORD_ELEMENT m_type_keywords[] = {
90  {NULL, -1}
91 };
92 
93 /*
94  * Parameters do not have an associated number; instead, the numeric field
95  * is the default value used if the parameter is not set.
96  */
97 
98 static STR_KEYWORD_ELEMENT m_parameter_keywords[] = {
110  {NULL, -1}
111 };
112 
113 static STR_KEYWORD_ELEMENT m_serial_keywords[] = {
118  {NULL, -1}
119 };
120 
121 static STR_KEYWORD_ELEMENT m_roll_keywords[] = {
124 /* {KSM_ROLL_RRSET_STRING, KSM_ROLL_RRSET}, */
125  {NULL, -1}
126 };
127 
128 /*+
129  * KsmKeywordNameToValue - Convert Name to Value
130  * KsmKeywordValueToName - Convert Value to Name
131  *
132  * Description:
133  * Converts between keywords and associated values for the specific
134  * element.
135  *
136  * When searching for a keyword, the given string need only be an
137  * unambiguous abbreviation of one of the keywords in the list. For
138  * example, given the keywords
139  *
140  * taiwan, tanzania, uganda
141  *
142  * ... then "t" or "ta" are ambiguous but "tai" matches taiwan. "u" (a
143  * single letter) will match uganda.
144  *
145  * Arguments:
146  * STR_KEYWORD_ELEMENT* elements
147  * Element list to search.
148  *
149  * const char* name -or- int value
150  * Name or value to convert.
151  *
152  * Returns:
153  * int -or- const char*
154  * Converted value. The return value is NULL or 0 if no conversion is
155  * found. (This implies that no keyword should have a value of 0.)
156  *
157  * Note that the returned string pointer is a pointer to a static
158  * string in this module. It should not be freed by the caller.
159 -*/
160 
161 static int KsmKeywordNameToValue(STR_KEYWORD_ELEMENT* elements, const char* name)
162 {
163  int status = 1; /* Status return - assume error */
164  int value; /* Return value */
165 
166  if (name) {
167  status = StrKeywordSearch(name, elements, &value);
168  }
169  return (status == 0) ? value : 0;
170 }
171 
172 static const char* KsmKeywordValueToName(STR_KEYWORD_ELEMENT* elements, int value)
173 {
174  int i; /* Loop counter */
175  const char* string = NULL; /* Return value */
176 
177  if (elements == NULL) {
178  return NULL;
179  }
180 
181  for (i = 0; elements[i].string; ++i) {
182  if (value == elements[i].value) {
183  string = elements[i].string;
184  break;
185  }
186  }
187 
188  return string;
189 }
190 
191 /*+
192  * KsmKeyword<type>NameToValue - Convert Name to Value
193  * KsmKeyword<type>ValueToName - Convert Value to Name
194  *
195  * Description:
196  * Converts between keywords and associated values for the specific
197  * element.
198  *
199  * Arguments:
200  * const char* name -or- int value
201  * Name of ID to convert.
202  *
203  * Returns:
204  * int -or- const char*
205  * Converted value. The return value is NULL or 0 if no conversion is
206  * found.
207 -*/
208 
209 int KsmKeywordAlgorithmNameToValue(const char* name)
210 {
211  return KsmKeywordNameToValue(m_algorithm_keywords, name);
212 }
213 
214 int KsmKeywordFormatNameToValue(const char* name)
215 {
216  return KsmKeywordNameToValue(m_format_keywords, name);
217 }
218 
219 int KsmKeywordParameterNameToValue(const char* name)
220 {
221  return KsmKeywordNameToValue(m_parameter_keywords, name);
222 }
223 
224 int KsmKeywordStateNameToValue(const char* name)
225 {
226  return KsmKeywordNameToValue(m_state_keywords, name);
227 }
228 
229 int KsmKeywordTypeNameToValue(const char* name)
230 {
231  return KsmKeywordNameToValue(m_type_keywords, name);
232 }
233 
234 const char* KsmKeywordAlgorithmValueToName(int value)
235 {
236  return KsmKeywordValueToName(m_algorithm_keywords, value);
237 }
238 
239 const char* KsmKeywordFormatValueToName(int value)
240 {
241  return KsmKeywordValueToName(m_format_keywords, value);
242 }
243 
244 const char* KsmKeywordStateValueToName(int value)
245 {
246  return KsmKeywordValueToName(m_state_keywords, value);
247 }
248 
249 const char* KsmKeywordTypeValueToName(int value)
250 {
251  return KsmKeywordValueToName(m_type_keywords, value);
252 }
253 
254 const char* KsmKeywordSerialValueToName(int value)
255 {
256  return KsmKeywordValueToName(m_serial_keywords, value);
257 }
258 
259 int KsmKeywordRollNameToValue(const char* name)
260 {
261  return KsmKeywordNameToValue(m_roll_keywords, name);
262 }
263 
264 const char* KsmKeywordRollValueToName(int value)
265 {
266  return KsmKeywordValueToName(m_roll_keywords, value);
267 }
268 
269 /*+
270  * KsmKeywordParameterExists - Check if Keyword Exists
271  *
272  * Description:
273  * Checks if the keyword is the name of a parameter, returning true (1) if
274  * it is and false (0) if it isn't.
275  *
276  * Unlike the other keyword checks, the match must be exact.
277  *
278  * Arguments:
279  * const char* name
280  * Name of the keyword to check.
281  *
282  * Returns:
283  * int
284  * 1 Keyword exists
285  * 0 Keyword does not exist
286 -*/
287 
288 int KsmKeywordParameterExists(const char* name)
289 {
290  int exists = 0;
291  int i;
292 
293  if (name) {
294  for (i = 0; m_parameter_keywords[i].string; ++i) {
295  if (strcmp(name, m_parameter_keywords[i].string) == 0) {
296  exists = 1;
297  break;
298  }
299  }
300  }
301 
302  return exists;
303 }