/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- hx509_env_add
- hx509_env_add_binding
- hx509_env_lfind
- hx509_env_find
- hx509_env_find_binding
- env_free
- hx509_env_free
1 /*
2 * Copyright (c) 2007 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * 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 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "hx_locl.h"
35 RCSID("$Id$");
36
37 /**
38 * @page page_env Hx509 enviroment functions
39 *
40 * See the library functions here: @ref hx509_env
41 */
42
43 /**
44 * Add a new key/value pair to the hx509_env.
45 *
46 * @param context A hx509 context.
47 * @param env enviroment to add the enviroment variable too.
48 * @param key key to add
49 * @param value value to add
50 *
51 * @return An hx509 error code, see hx509_get_error_string().
52 *
53 * @ingroup hx509_env
54 */
55
56 int
57 hx509_env_add(hx509_context context, hx509_env *env,
/* [<][>][^][v][top][bottom][index][help] */
58 const char *key, const char *value)
59 {
60 hx509_env n;
61
62 n = malloc(sizeof(*n));
63 if (n == NULL) {
64 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
65 return ENOMEM;
66 }
67
68 n->type = env_string;
69 n->next = NULL;
70 n->name = strdup(key);
71 if (n->name == NULL) {
72 free(n);
73 return ENOMEM;
74 }
75 n->u.string = strdup(value);
76 if (n->u.string == NULL) {
77 free(n->name);
78 free(n);
79 return ENOMEM;
80 }
81
82 /* add to tail */
83 if (*env) {
84 hx509_env e = *env;
85 while (e->next)
86 e = e->next;
87 e->next = n;
88 } else
89 *env = n;
90
91 return 0;
92 }
93
94 /**
95 * Add a new key/binding pair to the hx509_env.
96 *
97 * @param context A hx509 context.
98 * @param env enviroment to add the enviroment variable too.
99 * @param key key to add
100 * @param list binding list to add
101 *
102 * @return An hx509 error code, see hx509_get_error_string().
103 *
104 * @ingroup hx509_env
105 */
106
107 int
108 hx509_env_add_binding(hx509_context context, hx509_env *env,
/* [<][>][^][v][top][bottom][index][help] */
109 const char *key, hx509_env list)
110 {
111 hx509_env n;
112
113 n = malloc(sizeof(*n));
114 if (n == NULL) {
115 hx509_set_error_string(context, 0, ENOMEM, "out of memory");
116 return ENOMEM;
117 }
118
119 n->type = env_list;
120 n->next = NULL;
121 n->name = strdup(key);
122 if (n->name == NULL) {
123 free(n);
124 return ENOMEM;
125 }
126 n->u.list = list;
127
128 /* add to tail */
129 if (*env) {
130 hx509_env e = *env;
131 while (e->next)
132 e = e->next;
133 e->next = n;
134 } else
135 *env = n;
136
137 return 0;
138 }
139
140
141 /**
142 * Search the hx509_env for a length based key.
143 *
144 * @param context A hx509 context.
145 * @param env enviroment to add the enviroment variable too.
146 * @param key key to search for.
147 * @param len length of key.
148 *
149 * @return the value if the key is found, NULL otherwise.
150 *
151 * @ingroup hx509_env
152 */
153
154 const char *
155 hx509_env_lfind(hx509_context context, hx509_env env,
/* [<][>][^][v][top][bottom][index][help] */
156 const char *key, size_t len)
157 {
158 while(env) {
159 if (strncmp(key, env->name ,len) == 0
160 && env->name[len] == '\0' && env->type == env_string)
161 return env->u.string;
162 env = env->next;
163 }
164 return NULL;
165 }
166
167 /**
168 * Search the hx509_env for a key.
169 *
170 * @param context A hx509 context.
171 * @param env enviroment to add the enviroment variable too.
172 * @param key key to search for.
173 *
174 * @return the value if the key is found, NULL otherwise.
175 *
176 * @ingroup hx509_env
177 */
178
179 const char *
180 hx509_env_find(hx509_context context, hx509_env env, const char *key)
/* [<][>][^][v][top][bottom][index][help] */
181 {
182 while(env) {
183 if (strcmp(key, env->name) == 0 && env->type == env_string)
184 return env->u.string;
185 env = env->next;
186 }
187 return NULL;
188 }
189
190 /**
191 * Search the hx509_env for a binding.
192 *
193 * @param context A hx509 context.
194 * @param env enviroment to add the enviroment variable too.
195 * @param key key to search for.
196 *
197 * @return the binding if the key is found, NULL if not found.
198 *
199 * @ingroup hx509_env
200 */
201
202 hx509_env
203 hx509_env_find_binding(hx509_context context,
/* [<][>][^][v][top][bottom][index][help] */
204 hx509_env env,
205 const char *key)
206 {
207 while(env) {
208 if (strcmp(key, env->name) == 0 && env->type == env_list)
209 return env->u.list;
210 env = env->next;
211 }
212 return NULL;
213 }
214
215 static void
216 env_free(hx509_env b)
/* [<][>][^][v][top][bottom][index][help] */
217 {
218 while(b) {
219 hx509_env next = b->next;
220
221 if (b->type == env_string)
222 free(b->u.string);
223 else if (b->type == env_list)
224 env_free(b->u.list);
225
226 free(b->name);
227 free(b);
228 b = next;
229 }
230 }
231
232 /**
233 * Free an hx509_env enviroment context.
234 *
235 * @param env the enviroment to free.
236 *
237 * @ingroup hx509_env
238 */
239
240 void
241 hx509_env_free(hx509_env *env)
/* [<][>][^][v][top][bottom][index][help] */
242 {
243 if (*env)
244 env_free(*env);
245 *env = NULL;
246 }