]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
2d905f67 | 2 | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. |
3ac82faa | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
3ac82faa BM |
8 | */ |
9 | ||
198c42f5 | 10 | #include <internal/cryptlib.h> |
d02b48c6 RE |
11 | #include <stdio.h> |
12 | #include <string.h> | |
13 | #include <stdlib.h> | |
ec577822 BM |
14 | #include <openssl/bio.h> |
15 | #include <openssl/crypto.h> | |
682b444f | 16 | #include <openssl/trace.h> |
ec577822 BM |
17 | #include <openssl/lhash.h> |
18 | #include <openssl/conf.h> | |
19 | #include <openssl/x509.h> | |
20 | #include <openssl/pem.h> | |
21 | #include <openssl/ssl.h> | |
0b13e9f0 | 22 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 23 | # include <openssl/engine.h> |
0b13e9f0 | 24 | #endif |
ec577822 | 25 | #include <openssl/err.h> |
3b061a00 RS |
26 | /* Needed to get the other O_xxx flags. */ |
27 | #ifdef OPENSSL_SYS_VMS | |
28 | # include <unixio.h> | |
29 | #endif | |
7e1b7485 | 30 | #include "apps.h" |
dab2cd68 | 31 | #include "progs.h" |
7e1b7485 | 32 | |
7e1b7485 RS |
33 | /* Special sentinel to exit the program. */ |
34 | #define EXIT_THE_PROGRAM (-1) | |
d02b48c6 | 35 | |
0f113f3e MC |
36 | /* |
37 | * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with | |
38 | * the base prototypes (we cast each variable inside the function to the | |
39 | * required type of "FUNCTION*"). This removes the necessity for | |
40 | * macro-generated wrapper functions. | |
41 | */ | |
0f113f3e MC |
42 | static LHASH_OF(FUNCTION) *prog_init(void); |
43 | static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]); | |
0f113f3e | 44 | char *default_config_file = NULL; |
d02b48c6 | 45 | |
7e1b7485 RS |
46 | BIO *bio_in = NULL; |
47 | BIO *bio_out = NULL; | |
0f113f3e | 48 | BIO *bio_err = NULL; |
7e1b7485 | 49 | |
3cb7c5cf | 50 | static int apps_startup(void) |
7e1b7485 RS |
51 | { |
52 | #ifdef SIGPIPE | |
53 | signal(SIGPIPE, SIG_IGN); | |
54 | #endif | |
a0a82324 | 55 | |
b9f75707 | 56 | /* Set non-default library initialisation settings */ |
0488c0bb RL |
57 | if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN |
58 | | OPENSSL_INIT_LOAD_CONFIG, NULL)) | |
0fc32b07 | 59 | return 0; |
a0a82324 | 60 | |
7e1b7485 | 61 | setup_ui_method(); |
b9f75707 | 62 | |
a0a82324 | 63 | return 1; |
7e1b7485 RS |
64 | } |
65 | ||
3cb7c5cf | 66 | static void apps_shutdown(void) |
7e1b7485 | 67 | { |
7e1b7485 | 68 | destroy_ui_method(); |
7e1b7485 RS |
69 | } |
70 | ||
3cb7c5cf | 71 | static char *make_config_name(void) |
7e1b7485 | 72 | { |
cc01d217 | 73 | const char *t; |
7e1b7485 RS |
74 | size_t len; |
75 | char *p; | |
76 | ||
b0700d2c | 77 | if ((t = getenv("OPENSSL_CONF")) != NULL) |
7644a9ae | 78 | return OPENSSL_strdup(t); |
cc01d217 RS |
79 | |
80 | t = X509_get_default_cert_area(); | |
81 | len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1; | |
68dc6824 | 82 | p = app_malloc(len, "config filename buffer"); |
cc01d217 | 83 | strcpy(p, t); |
7e1b7485 | 84 | #ifndef OPENSSL_SYS_VMS |
cc01d217 | 85 | strcat(p, "/"); |
d02b48c6 | 86 | #endif |
cc01d217 | 87 | strcat(p, OPENSSL_CONF); |
7e1b7485 RS |
88 | |
89 | return p; | |
90 | } | |
91 | ||
0fda9f7c DMSP |
92 | |
93 | #ifndef OPENSSL_NO_TRACE | |
682b444f RL |
94 | typedef struct tracedata_st { |
95 | BIO *bio; | |
96 | unsigned int ingroup:1; | |
97 | } tracedata; | |
98 | ||
99 | static size_t internal_trace_cb(const char *buf, size_t cnt, | |
100 | int category, int cmd, void *vdata) | |
101 | { | |
13d06925 | 102 | int ret = 0; |
682b444f | 103 | tracedata *trace_data = vdata; |
2d905f67 P |
104 | char buffer[256], *hex; |
105 | CRYPTO_THREAD_ID tid; | |
682b444f RL |
106 | |
107 | switch (cmd) { | |
108 | case OSSL_TRACE_CTRL_BEGIN: | |
fe50e115 DMSP |
109 | if (!ossl_assert(!trace_data->ingroup)) |
110 | return 0; | |
682b444f | 111 | trace_data->ingroup = 1; |
682b444f | 112 | |
2d905f67 P |
113 | tid = CRYPTO_THREAD_get_current_id(); |
114 | hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid)); | |
115 | BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ", | |
116 | hex, OSSL_trace_get_category_name(category)); | |
117 | OPENSSL_free(hex); | |
682b444f RL |
118 | BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX, |
119 | strlen(buffer), buffer); | |
13d06925 DMSP |
120 | break; |
121 | case OSSL_TRACE_CTRL_WRITE: | |
fe50e115 DMSP |
122 | if (!ossl_assert(trace_data->ingroup)) |
123 | return 0; | |
124 | ||
13d06925 DMSP |
125 | ret = BIO_write(trace_data->bio, buf, cnt); |
126 | break; | |
127 | case OSSL_TRACE_CTRL_END: | |
fe50e115 DMSP |
128 | if (!ossl_assert(trace_data->ingroup)) |
129 | return 0; | |
13d06925 DMSP |
130 | trace_data->ingroup = 0; |
131 | ||
132 | BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX, 0, NULL); | |
133 | ||
134 | break; | |
682b444f | 135 | } |
682b444f RL |
136 | |
137 | return ret < 0 ? 0 : ret; | |
138 | } | |
139 | ||
18e1e302 RL |
140 | DEFINE_STACK_OF(tracedata) |
141 | static STACK_OF(tracedata) *trace_data_stack; | |
142 | ||
143 | static void tracedata_free(tracedata *data) | |
144 | { | |
145 | BIO_free_all(data->bio); | |
146 | OPENSSL_free(data); | |
147 | } | |
148 | ||
149 | static STACK_OF(tracedata) *trace_data_stack; | |
150 | ||
151 | static void cleanup_trace(void) | |
152 | { | |
153 | sk_tracedata_pop_free(trace_data_stack, tracedata_free); | |
154 | } | |
155 | ||
02bd2d7f DMSP |
156 | static void setup_trace_category(int category) |
157 | { | |
158 | BIO *channel; | |
159 | tracedata *trace_data; | |
160 | ||
161 | if (OSSL_trace_enabled(category)) | |
162 | return; | |
163 | ||
164 | channel = BIO_push(BIO_new(apps_bf_prefix()), | |
165 | dup_bio_err(FORMAT_TEXT)); | |
166 | trace_data = OPENSSL_zalloc(sizeof(*trace_data)); | |
167 | ||
168 | if (trace_data == NULL | |
169 | || (trace_data->bio = channel) == NULL | |
170 | || OSSL_trace_set_callback(category, internal_trace_cb, | |
171 | trace_data) == 0 | |
172 | || sk_tracedata_push(trace_data_stack, trace_data) == 0) { | |
173 | ||
174 | fprintf(stderr, | |
175 | "warning: unable to setup trace callback for category '%s'.\n", | |
176 | OSSL_trace_get_category_name(category)); | |
177 | ||
178 | OSSL_trace_set_callback(category, NULL, NULL); | |
179 | BIO_free_all(channel); | |
180 | } | |
181 | } | |
182 | ||
682b444f RL |
183 | static void setup_trace(const char *str) |
184 | { | |
185 | char *val; | |
186 | ||
ba434131 RL |
187 | /* |
188 | * We add this handler as early as possible to ensure it's executed | |
189 | * as late as possible, i.e. after the TRACE code has done its cleanup | |
190 | * (which happens last in OPENSSL_cleanup). | |
191 | */ | |
192 | atexit(cleanup_trace); | |
193 | ||
18e1e302 | 194 | trace_data_stack = sk_tracedata_new_null(); |
682b444f RL |
195 | val = OPENSSL_strdup(str); |
196 | ||
197 | if (val != NULL) { | |
198 | char *valp = val; | |
199 | char *item; | |
200 | ||
201 | for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) { | |
202 | int category = OSSL_trace_get_category_num(item); | |
203 | ||
3a8269b3 | 204 | if (category == OSSL_TRACE_CATEGORY_ALL) { |
02bd2d7f DMSP |
205 | while (++category < OSSL_TRACE_CATEGORY_NUM) |
206 | setup_trace_category(category); | |
207 | break; | |
208 | } else if (category > 0) { | |
209 | setup_trace_category(category); | |
682b444f RL |
210 | } else { |
211 | fprintf(stderr, | |
02bd2d7f | 212 | "warning: unknown trace category: '%s'.\n", item); |
682b444f RL |
213 | } |
214 | } | |
215 | } | |
216 | ||
217 | OPENSSL_free(val); | |
218 | } | |
0fda9f7c | 219 | #endif /* OPENSSL_NO_TRACE */ |
682b444f | 220 | |
7e1b7485 | 221 | int main(int argc, char *argv[]) |
0f113f3e | 222 | { |
0f113f3e | 223 | FUNCTION f, *fp; |
0f113f3e | 224 | LHASH_OF(FUNCTION) *prog = NULL; |
cc01d217 | 225 | char *p, *pname; |
7e1b7485 RS |
226 | char buf[1024]; |
227 | const char *prompt; | |
228 | ARGS arg; | |
229 | int first, n, i, ret = 0; | |
8ecef24a | 230 | |
7e1b7485 RS |
231 | arg.argv = NULL; |
232 | arg.size = 0; | |
233 | ||
7768e116 RS |
234 | /* Set up some of the environment. */ |
235 | default_config_file = make_config_name(); | |
a60994df RL |
236 | bio_in = dup_bio_in(FORMAT_TEXT); |
237 | bio_out = dup_bio_out(FORMAT_TEXT); | |
149bd5d6 | 238 | bio_err = dup_bio_err(FORMAT_TEXT); |
7768e116 | 239 | |
368058d0 | 240 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
9b542d72 | 241 | argv = copy_argv(&argc, argv); |
4e155ec4 AP |
242 | #elif defined(_WIN32) |
243 | /* | |
244 | * Replace argv[] with UTF-8 encoded strings. | |
245 | */ | |
246 | win32_utf8argv(&argc, &argv); | |
7e1b7485 RS |
247 | #endif |
248 | ||
682b444f RL |
249 | /* |
250 | * We use the prefix method to get the trace output we want. Since some | |
251 | * trace outputs happen with OPENSSL_cleanup(), which is run automatically | |
252 | * after exit(), we need to destroy the prefix method as late as possible. | |
253 | */ | |
254 | atexit(destroy_prefix_method); | |
255 | ||
0fda9f7c | 256 | #ifndef OPENSSL_NO_TRACE |
682b444f | 257 | setup_trace(getenv("OPENSSL_TRACE")); |
0fda9f7c | 258 | #endif |
682b444f | 259 | |
7e1b7485 | 260 | p = getenv("OPENSSL_DEBUG_MEMORY"); |
bbd86bf5 RS |
261 | if (p != NULL && strcmp(p, "on") == 0) |
262 | CRYPTO_set_mem_debug(1); | |
0f113f3e | 263 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); |
3ac82faa | 264 | |
0f113f3e | 265 | if (getenv("OPENSSL_FIPS")) { |
7768e116 RS |
266 | BIO_printf(bio_err, "FIPS mode not supported.\n"); |
267 | return 1; | |
0f113f3e MC |
268 | } |
269 | ||
f2da4a49 RL |
270 | if (!apps_startup()) { |
271 | BIO_printf(bio_err, | |
272 | "FATAL: Startup failure (dev note: apps_startup() failed)\n"); | |
273 | ERR_print_errors(bio_err); | |
274 | ret = 1; | |
a0a82324 | 275 | goto end; |
f2da4a49 | 276 | } |
a0a82324 | 277 | |
7e1b7485 | 278 | prog = prog_init(); |
368058d0 | 279 | pname = opt_progname(argv[0]); |
0f113f3e | 280 | |
0f113f3e | 281 | /* first check the program name */ |
0f113f3e MC |
282 | f.name = pname; |
283 | fp = lh_FUNCTION_retrieve(prog, &f); | |
284 | if (fp != NULL) { | |
368058d0 RL |
285 | argv[0] = pname; |
286 | ret = fp->func(argc, argv); | |
0f113f3e MC |
287 | goto end; |
288 | } | |
289 | ||
7e1b7485 RS |
290 | /* If there is stuff on the command line, run with that. */ |
291 | if (argc != 1) { | |
292 | argc--; | |
368058d0 RL |
293 | argv++; |
294 | ret = do_cmd(prog, argc, argv); | |
0f113f3e MC |
295 | if (ret < 0) |
296 | ret = 0; | |
297 | goto end; | |
298 | } | |
299 | ||
7e1b7485 | 300 | /* ok, lets enter interactive mode */ |
0f113f3e MC |
301 | for (;;) { |
302 | ret = 0; | |
57d5edad | 303 | /* Read a line, continue reading if line ends with \ */ |
cbe29648 | 304 | for (p = buf, n = sizeof(buf), i = 0, first = 1; n > 0; first = 0) { |
1c9c2435 | 305 | prompt = first ? "OpenSSL> " : "> "; |
0f113f3e | 306 | p[0] = '\0'; |
57d5edad | 307 | #ifndef READLINE |
0f113f3e MC |
308 | fputs(prompt, stdout); |
309 | fflush(stdout); | |
310 | if (!fgets(p, n, stdin)) | |
311 | goto end; | |
312 | if (p[0] == '\0') | |
313 | goto end; | |
314 | i = strlen(p); | |
315 | if (i <= 1) | |
316 | break; | |
317 | if (p[i - 2] != '\\') | |
318 | break; | |
319 | i -= 2; | |
320 | p += i; | |
321 | n -= i; | |
57d5edad RS |
322 | #else |
323 | { | |
324 | extern char *readline(const char *); | |
325 | extern void add_history(const char *cp); | |
326 | char *text; | |
327 | ||
72106aaa | 328 | text = readline(prompt); |
57d5edad RS |
329 | if (text == NULL) |
330 | goto end; | |
331 | i = strlen(text); | |
332 | if (i == 0 || i > n) | |
333 | break; | |
334 | if (text[i - 1] != '\\') { | |
335 | p += strlen(strcpy(p, text)); | |
336 | free(text); | |
337 | add_history(buf); | |
338 | break; | |
339 | } | |
340 | ||
341 | text[i - 1] = '\0'; | |
342 | p += strlen(strcpy(p, text)); | |
343 | free(text); | |
344 | n -= i; | |
345 | } | |
346 | #endif | |
0f113f3e | 347 | } |
57d5edad | 348 | |
7e1b7485 RS |
349 | if (!chopup_args(&arg, buf)) { |
350 | BIO_printf(bio_err, "Can't parse (no memory?)\n"); | |
0f113f3e | 351 | break; |
7e1b7485 | 352 | } |
0f113f3e | 353 | |
7e1b7485 RS |
354 | ret = do_cmd(prog, arg.argc, arg.argv); |
355 | if (ret == EXIT_THE_PROGRAM) { | |
0f113f3e MC |
356 | ret = 0; |
357 | goto end; | |
358 | } | |
359 | if (ret != 0) | |
7e1b7485 RS |
360 | BIO_printf(bio_err, "error in %s\n", arg.argv[0]); |
361 | (void)BIO_flush(bio_out); | |
0f113f3e MC |
362 | (void)BIO_flush(bio_err); |
363 | } | |
0f113f3e MC |
364 | ret = 1; |
365 | end: | |
cc01d217 | 366 | OPENSSL_free(default_config_file); |
25aaa98a | 367 | lh_FUNCTION_free(prog); |
b548a1f1 | 368 | OPENSSL_free(arg.argv); |
3ee1eac2 | 369 | app_RAND_write(); |
0f113f3e | 370 | |
7e1b7485 RS |
371 | BIO_free(bio_in); |
372 | BIO_free_all(bio_out); | |
0f113f3e | 373 | apps_shutdown(); |
c2e27310 | 374 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
541e9565 DSH |
375 | if (CRYPTO_mem_leaks(bio_err) <= 0) |
376 | ret = 1; | |
bbd86bf5 | 377 | #endif |
ca3a82c3 | 378 | BIO_free(bio_err); |
aa147792 | 379 | EXIT(ret); |
7e1b7485 RS |
380 | } |
381 | ||
f3b3d7f0 RS |
382 | typedef enum HELP_CHOICE { |
383 | OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP | |
384 | } HELP_CHOICE; | |
385 | ||
44c83ebd | 386 | const OPTIONS help_options[] = { |
da6be198 RL |
387 | {OPT_HELP_STR, 1, '-', "Usage: help [options]\n"}, |
388 | {OPT_HELP_STR, 1, '-', " help [command]\n"}, | |
f3b3d7f0 | 389 | {"help", OPT_hHELP, '-', "Display this summary"}, |
7e1b7485 RS |
390 | {NULL} |
391 | }; | |
392 | ||
f3b3d7f0 | 393 | |
7e1b7485 RS |
394 | int help_main(int argc, char **argv) |
395 | { | |
396 | FUNCTION *fp; | |
397 | int i, nl; | |
398 | FUNC_TYPE tp; | |
399 | char *prog; | |
f3b3d7f0 | 400 | HELP_CHOICE o; |
296cbb57 | 401 | DISPLAY_COLUMNS dc; |
7e1b7485 RS |
402 | |
403 | prog = opt_init(argc, argv, help_options); | |
f3b3d7f0 | 404 | while ((o = opt_next()) != OPT_hEOF) { |
7e1b7485 | 405 | switch (o) { |
f3b3d7f0 RS |
406 | case OPT_hERR: |
407 | case OPT_hEOF: | |
7e1b7485 RS |
408 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
409 | return 1; | |
f3b3d7f0 | 410 | case OPT_hHELP: |
7e1b7485 RS |
411 | opt_help(help_options); |
412 | return 0; | |
413 | } | |
414 | } | |
7e1b7485 | 415 | |
da6be198 RL |
416 | if (opt_num_rest() == 1) { |
417 | char *new_argv[3]; | |
418 | ||
419 | new_argv[0] = opt_rest()[0]; | |
420 | new_argv[1] = "--help"; | |
421 | new_argv[2] = NULL; | |
422 | return do_cmd(prog_init(), 2, new_argv); | |
423 | } | |
5d94e5b6 | 424 | if (opt_num_rest() != 0) { |
7e1b7485 RS |
425 | BIO_printf(bio_err, "Usage: %s\n", prog); |
426 | return 1; | |
427 | } | |
428 | ||
753149d9 | 429 | calculate_columns(functions, &dc); |
296cbb57 | 430 | BIO_printf(bio_err, "Standard commands"); |
7e1b7485 RS |
431 | i = 0; |
432 | tp = FT_none; | |
433 | for (fp = functions; fp->name != NULL; fp++) { | |
434 | nl = 0; | |
296cbb57 | 435 | if (i++ % dc.columns == 0) { |
7e1b7485 RS |
436 | BIO_printf(bio_err, "\n"); |
437 | nl = 1; | |
438 | } | |
439 | if (fp->type != tp) { | |
440 | tp = fp->type; | |
441 | if (!nl) | |
442 | BIO_printf(bio_err, "\n"); | |
443 | if (tp == FT_md) { | |
444 | i = 1; | |
445 | BIO_printf(bio_err, | |
446 | "\nMessage Digest commands (see the `dgst' command for more details)\n"); | |
447 | } else if (tp == FT_cipher) { | |
448 | i = 1; | |
449 | BIO_printf(bio_err, | |
450 | "\nCipher commands (see the `enc' command for more details)\n"); | |
451 | } | |
452 | } | |
296cbb57 | 453 | BIO_printf(bio_err, "%-*s", dc.width, fp->name); |
7e1b7485 RS |
454 | } |
455 | BIO_printf(bio_err, "\n\n"); | |
456 | return 0; | |
457 | } | |
8c00f4cf | 458 | |
3c1d6bbc | 459 | static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]) |
0f113f3e MC |
460 | { |
461 | FUNCTION f, *fp; | |
0f113f3e | 462 | |
7e1b7485 | 463 | if (argc <= 0 || argv[0] == NULL) |
26a7d938 | 464 | return 0; |
0f113f3e MC |
465 | f.name = argv[0]; |
466 | fp = lh_FUNCTION_retrieve(prog, &f); | |
467 | if (fp == NULL) { | |
468 | if (EVP_get_digestbyname(argv[0])) { | |
7e1b7485 | 469 | f.type = FT_md; |
0f113f3e MC |
470 | f.func = dgst_main; |
471 | fp = &f; | |
472 | } else if (EVP_get_cipherbyname(argv[0])) { | |
7e1b7485 | 473 | f.type = FT_cipher; |
0f113f3e MC |
474 | f.func = enc_main; |
475 | fp = &f; | |
476 | } | |
477 | } | |
478 | if (fp != NULL) { | |
26a7d938 | 479 | return fp->func(argc, argv); |
7e1b7485 RS |
480 | } |
481 | if ((strncmp(argv[0], "no-", 3)) == 0) { | |
482 | /* | |
483 | * User is asking if foo is unsupported, by trying to "run" the | |
484 | * no-foo command. Strange. | |
485 | */ | |
0f113f3e | 486 | f.name = argv[0] + 3; |
7e1b7485 RS |
487 | if (lh_FUNCTION_retrieve(prog, &f) == NULL) { |
488 | BIO_printf(bio_out, "%s\n", argv[0]); | |
26a7d938 | 489 | return 0; |
0f113f3e | 490 | } |
7e1b7485 RS |
491 | BIO_printf(bio_out, "%s\n", argv[0] + 3); |
492 | return 1; | |
50acf46b | 493 | } |
7e1b7485 RS |
494 | if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 || |
495 | strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0) | |
496 | /* Special value to mean "exit the program. */ | |
497 | return EXIT_THE_PROGRAM; | |
0f113f3e | 498 | |
7e1b7485 RS |
499 | BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n", |
500 | argv[0]); | |
208fb891 | 501 | return 1; |
0f113f3e | 502 | } |
50acf46b | 503 | |
0f113f3e MC |
504 | static int function_cmp(const FUNCTION * a, const FUNCTION * b) |
505 | { | |
506 | return strncmp(a->name, b->name, 8); | |
507 | } | |
50acf46b | 508 | |
0f113f3e MC |
509 | static unsigned long function_hash(const FUNCTION * a) |
510 | { | |
739a1eb1 | 511 | return OPENSSL_LH_strhash(a->name); |
0f113f3e | 512 | } |
d02b48c6 | 513 | |
7e1b7485 RS |
514 | static int SortFnByName(const void *_f1, const void *_f2) |
515 | { | |
516 | const FUNCTION *f1 = _f1; | |
517 | const FUNCTION *f2 = _f2; | |
518 | ||
519 | if (f1->type != f2->type) | |
520 | return f1->type - f2->type; | |
521 | return strcmp(f1->name, f2->name); | |
522 | } | |
523 | ||
0f113f3e MC |
524 | static LHASH_OF(FUNCTION) *prog_init(void) |
525 | { | |
391e987e RL |
526 | static LHASH_OF(FUNCTION) *ret = NULL; |
527 | static int prog_inited = 0; | |
0f113f3e MC |
528 | FUNCTION *f; |
529 | size_t i; | |
530 | ||
391e987e RL |
531 | if (prog_inited) |
532 | return ret; | |
533 | ||
534 | prog_inited = 1; | |
535 | ||
7e1b7485 | 536 | /* Sort alphabetically within category. For nicer help displays. */ |
391e987e RL |
537 | for (i = 0, f = functions; f->name != NULL; ++f, ++i) |
538 | ; | |
b4faea50 | 539 | qsort(functions, i, sizeof(*functions), SortFnByName); |
0f113f3e | 540 | |
62d0577e | 541 | if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL) |
26a7d938 | 542 | return NULL; |
0f113f3e MC |
543 | |
544 | for (f = functions; f->name != NULL; f++) | |
545 | (void)lh_FUNCTION_insert(ret, f); | |
296cbb57 | 546 | return ret; |
0f113f3e | 547 | } |