]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/lib/app_params.c
Allow an output indentation of zero in apps.
[thirdparty/openssl.git] / apps / lib / app_params.c
CommitLineData
16485a3a
RL
1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
8 */
9
10#include "apps.h"
11#include "app_params.h"
12
13static int describe_param_type(char *buf, size_t bufsz, const OSSL_PARAM *param)
14{
15 const char *type_mod = "";
16 const char *type = NULL;
17 int show_type_number = 0;
18 int printed_len;
19
20 switch (param->data_type) {
21 case OSSL_PARAM_UNSIGNED_INTEGER:
22 type_mod = "unsigned ";
23 /* FALLTHRU */
24 case OSSL_PARAM_INTEGER:
25 type = "integer";
26 break;
27 case OSSL_PARAM_UTF8_PTR:
28 type_mod = "pointer to a ";
29 /* FALLTHRU */
30 case OSSL_PARAM_UTF8_STRING:
31 type = "UTF8 encoded string";
32 break;
33 case OSSL_PARAM_OCTET_PTR:
34 type_mod = "pointer to an ";
35 /* FALLTHRU */
36 case OSSL_PARAM_OCTET_STRING:
37 type = "octet string";
38 break;
39 default:
40 type = "unknown type";
41 show_type_number = 1;
42 break;
43 }
44
45 printed_len = BIO_snprintf(buf, bufsz, "%s: ", param->key);
46 if (printed_len > 0) {
47 buf += printed_len;
48 bufsz -= printed_len;
49 }
50 printed_len = BIO_snprintf(buf, bufsz, "%s%s", type_mod, type);
51 if (printed_len > 0) {
52 buf += printed_len;
53 bufsz -= printed_len;
54 }
55 if (show_type_number) {
56 printed_len = BIO_snprintf(buf, bufsz, " [%d]", param->data_type);
57 if (printed_len > 0) {
58 buf += printed_len;
59 bufsz -= printed_len;
60 }
61 }
62 if (param->data_size == 0)
63 printed_len = BIO_snprintf(buf, bufsz, " (arbitrary size)");
64 else
65 printed_len = BIO_snprintf(buf, bufsz, " (max %zu bytes large)",
66 param->data_size);
67 if (printed_len > 0) {
68 buf += printed_len;
69 bufsz -= printed_len;
70 }
71 *buf = '\0';
72 return 1;
73}
74
75int print_param_types(const char *thing, const OSSL_PARAM *pdefs, int indent)
76{
77 if (pdefs == NULL) {
c92d0c5c 78 BIO_printf(bio_out, "%*sNo declared %s\n", indent, "", thing);
16485a3a
RL
79 } else if (pdefs->key == NULL) {
80 /*
81 * An empty list? This shouldn't happen, but let's just make sure to
82 * say something if there's a badly written provider...
83 */
c92d0c5c 84 BIO_printf(bio_out, "%*sEmpty list of %s (!!!)\n", indent, "", thing);
16485a3a 85 } else {
c92d0c5c 86 BIO_printf(bio_out, "%*s%s:\n", indent, "", thing);
16485a3a
RL
87 for (; pdefs->key != NULL; pdefs++) {
88 char buf[200]; /* This should be ample space */
89
90 describe_param_type(buf, sizeof(buf), pdefs);
c92d0c5c 91 BIO_printf(bio_out, "%*s %s\n", indent, "", buf);
16485a3a
RL
92 }
93 }
94 return 1;
95}
96