xref: /openbsd-src/sbin/isakmpd/constants.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: constants.c,v 1.6 1999/04/19 19:54:53 niklas Exp $	*/
2 /*	$EOM: constants.c,v 1.7 1999/04/02 00:57:31 niklas Exp $	*/
3 
4 /*
5  * Copyright (c) 1998, 1999 Niklas Hallqvist.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Ericsson Radio Systems.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * This code was written under funding by Ericsson Radio Systems.
35  */
36 
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include "sysdep.h"
41 
42 #include "constants.h"
43 
44 int
45 constant_value (struct constant_map *map, char *name)
46 {
47   struct constant_map *entry = map;
48 
49   for (entry = map; entry->name; entry++)
50     if (strcasecmp (entry->name, name) == 0)
51       return entry->value;
52   return 0;
53 }
54 
55 char *
56 constant_lookup (struct constant_map *map, int value)
57 {
58   struct constant_map *entry = map;
59 
60   for (entry = map; entry->name; entry++)
61     if (entry->value == value)
62       return entry->name;
63   return 0;
64 }
65 
66 struct constant_map *
67 constant_link_lookup (struct constant_map *map, int value)
68 {
69   struct constant_map *entry = map;
70 
71   for (entry = map; entry->name; entry++)
72     if (entry->value == value)
73       return entry->link;
74   return 0;
75 }
76 
77 char *
78 constant_name (struct constant_map *map, int value)
79 {
80   static char tmp[32];		/* XXX Ugly, I know.  */
81   char *retval = constant_lookup (map, value);
82 
83   if (!retval)
84     {
85       snprintf (tmp, 32, "<Unknown %d>", value);
86       return tmp;
87     }
88   return retval;
89 }
90 
91 char *
92 constant_name_maps (struct constant_map **maps, int value)
93 {
94   static char tmp[32];		/* XXX Ugly, I know.  */
95   char *retval;
96   struct constant_map **map;
97 
98   for (map = maps; *map; map++)
99     {
100       retval = constant_lookup (*map, value);
101       if (retval)
102 	return retval;
103     }
104   snprintf (tmp, 32, "<Unknown %d>", value);
105   return tmp;
106 }
107