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