xref: /freebsd/crypto/openssl/test/bio_prefix_text.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright 2019-2025 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 <string.h>
11 #include <stdarg.h>
12 #include <openssl/bio.h>
13 #include <openssl/safestack.h>
14 #include "opt.h"
15 
16 static BIO *bio_in = NULL;
17 static BIO *bio_out = NULL;
18 static BIO *bio_err = NULL;
19 
20 /*-
21  * This program sets up a chain of BIO_f_filter() on top of bio_out, how
22  * many is governed by the user through -n.  It allows the user to set the
23  * indentation for each individual filter using -i and -p.  Then it reads
24  * text from bio_in and prints it out through the BIO chain.
25  *
26  * The filter index is counted from the source/sink, i.e. index 0 is closest
27  * to it.
28  *
29  * Example:
30  *
31  * $ echo foo | ./bio_prefix_text -n 2 -i 1:32 -p 1:FOO -i 0:3
32  *    FOO                                foo
33  * ^^^   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34  *  |                   |
35  *  |                   +------ 32 spaces from filter 1
36  *  +-------------------------- 3 spaces from filter 0
37  */
38 
39 static size_t amount = 0;
40 static BIO **chain = NULL;
41 
42 typedef enum OPTION_choice {
43     OPT_ERR = -1,
44     OPT_EOF = 0,
45     OPT_AMOUNT,
46     OPT_INDENT,
47     OPT_PREFIX
48 } OPTION_CHOICE;
49 
50 static const OPTIONS options[] = {
51     { "n", OPT_AMOUNT, 'p', "Amount of BIO_f_prefix() filters" },
52     /*
53      * idx is the index to the BIO_f_filter chain(), where 0 is closest
54      * to the source/sink BIO.  If idx isn't given, 0 is assumed
55      */
56     { "i", OPT_INDENT, 's', "Indentation in form '[idx:]indent'" },
57     { "p", OPT_PREFIX, 's', "Prefix in form '[idx:]prefix'" },
58     { NULL }
59 };
60 
opt_printf_stderr(const char * fmt,...)61 int opt_printf_stderr(const char *fmt, ...)
62 {
63     va_list ap;
64     int ret;
65 
66     va_start(ap, fmt);
67     ret = BIO_vprintf(bio_err, fmt, ap);
68     va_end(ap);
69     return ret;
70 }
71 
run_pipe(void)72 static int run_pipe(void)
73 {
74     char buf[4096];
75 
76     while (!BIO_eof(bio_in)) {
77         size_t bytes_in;
78         size_t bytes_out;
79 
80         if (!BIO_read_ex(bio_in, buf, sizeof(buf), &bytes_in))
81             return 0;
82         bytes_out = 0;
83         while (bytes_out < bytes_in) {
84             size_t bytes;
85 
86             if (!BIO_write_ex(chain[amount - 1], buf, bytes_in, &bytes))
87                 return 0;
88             bytes_out += bytes;
89         }
90     }
91     return 1;
92 }
93 
setup_bio_chain(const char * progname)94 static int setup_bio_chain(const char *progname)
95 {
96     BIO *next = NULL;
97     size_t n = amount;
98 
99     chain = OPENSSL_zalloc(sizeof(*chain) * n);
100 
101     if (chain != NULL) {
102         size_t i;
103 
104         if (!BIO_up_ref(bio_out)) /* Protection against freeing */
105             goto err;
106 
107         next = bio_out;
108 
109         for (i = 0; n > 0; i++, n--) {
110             BIO *curr = BIO_new(BIO_f_prefix());
111 
112             if (curr == NULL)
113                 goto err;
114             chain[i] = BIO_push(curr, next);
115             if (chain[i] == NULL)
116                 goto err;
117             next = chain[i];
118         }
119     }
120     return chain != NULL;
121  err:
122     /* Free the chain we built up */
123     BIO_free_all(next);
124     OPENSSL_free(chain);
125     return 0;
126 }
127 
cleanup(void)128 static void cleanup(void)
129 {
130     if (chain != NULL) {
131         BIO_free_all(chain[amount - 1]);
132         OPENSSL_free(chain);
133     }
134 
135     BIO_free_all(bio_in);
136     BIO_free_all(bio_out);
137     BIO_free_all(bio_err);
138 }
139 
setup(void)140 static int setup(void)
141 {
142     OPTION_CHOICE o;
143     char *arg;
144     char *colon;
145     char *endptr;
146     size_t idx, indent;
147     const char *progname = opt_getprog();
148 
149     bio_in = BIO_new_fp(stdin, BIO_NOCLOSE | BIO_FP_TEXT);
150     bio_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
151     bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
152 #ifdef __VMS
153     bio_out = BIO_push(BIO_new(BIO_f_linebuffer()), bio_out);
154     bio_err = BIO_push(BIO_new(BIO_f_linebuffer()), bio_err);
155 #endif
156 
157     OPENSSL_assert(bio_in != NULL);
158     OPENSSL_assert(bio_out != NULL);
159     OPENSSL_assert(bio_err != NULL);
160 
161 
162     while ((o = opt_next()) != OPT_EOF) {
163         switch (o) {
164         case OPT_AMOUNT:
165             arg = opt_arg();
166             amount = strtoul(arg, &endptr, 10);
167             if (endptr[0] != '\0') {
168                 BIO_printf(bio_err,
169                            "%s: -n argument isn't a decimal number: %s",
170                            progname, arg);
171                 return 0;
172             }
173             if (amount < 1) {
174                 BIO_printf(bio_err, "%s: must set up at least one filter",
175                            progname);
176                 return 0;
177             }
178             if (!setup_bio_chain(progname)) {
179                 BIO_printf(bio_err, "%s: failed setting up filter chain",
180                            progname);
181                 return 0;
182             }
183             break;
184         case OPT_INDENT:
185             if (chain == NULL) {
186                 BIO_printf(bio_err, "%s: -i given before -n", progname);
187                 return 0;
188             }
189             arg = opt_arg();
190             colon = strchr(arg, ':');
191             idx = 0;
192             if (colon != NULL) {
193                 idx = strtoul(arg, &endptr, 10);
194                 if (endptr[0] != ':') {
195                     BIO_printf(bio_err,
196                                "%s: -i index isn't a decimal number: %s",
197                                progname, arg);
198                     return 0;
199                 }
200                 colon++;
201             } else {
202                 colon = arg;
203             }
204             indent = strtoul(colon, &endptr, 10);
205             if (endptr[0] != '\0') {
206                 BIO_printf(bio_err,
207                            "%s: -i value isn't a decimal number: %s",
208                            progname, arg);
209                 return 0;
210             }
211             if (idx >= amount) {
212                 BIO_printf(bio_err, "%s: index (%zu) not within range 0..%zu",
213                            progname, idx, amount - 1);
214                 return 0;
215             }
216             if (BIO_set_indent(chain[idx], (long)indent) <= 0) {
217                 BIO_printf(bio_err, "%s: failed setting indentation: %s",
218                            progname, arg);
219                 return 0;
220             }
221             break;
222         case OPT_PREFIX:
223             if (chain == NULL) {
224                 BIO_printf(bio_err, "%s: -p given before -n", progname);
225                 return 0;
226             }
227             arg = opt_arg();
228             colon = strchr(arg, ':');
229             idx = 0;
230             if (colon != NULL) {
231                 idx = strtoul(arg, &endptr, 10);
232                 if (endptr[0] != ':') {
233                     BIO_printf(bio_err,
234                                "%s: -p index isn't a decimal number: %s",
235                                progname, arg);
236                     return 0;
237                 }
238                 colon++;
239             } else {
240                 colon = arg;
241             }
242             if (idx >= amount) {
243                 BIO_printf(bio_err, "%s: index (%zu) not within range 0..%zu",
244                            progname, idx, amount - 1);
245                 return 0;
246             }
247             if (BIO_set_prefix(chain[idx], colon) <= 0) {
248                 BIO_printf(bio_err, "%s: failed setting prefix: %s",
249                            progname, arg);
250                 return 0;
251             }
252             break;
253         default:
254         case OPT_ERR:
255             return 0;
256         }
257     }
258     return 1;
259 }
260 
main(int argc,char ** argv)261 int main(int argc, char **argv)
262 {
263     int rv = EXIT_SUCCESS;
264 
265     opt_init(argc, argv, options);
266     rv = (setup() && run_pipe()) ? EXIT_SUCCESS : EXIT_FAILURE;
267     cleanup();
268     return rv;
269 }
270