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 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 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 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 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 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 while ((o = opt_next()) != OPT_EOF) { 162 switch (o) { 163 case OPT_AMOUNT: 164 arg = opt_arg(); 165 amount = strtoul(arg, &endptr, 10); 166 if (endptr[0] != '\0') { 167 BIO_printf(bio_err, 168 "%s: -n argument isn't a decimal number: %s", 169 progname, arg); 170 return 0; 171 } 172 if (amount < 1) { 173 BIO_printf(bio_err, "%s: must set up at least one filter", 174 progname); 175 return 0; 176 } 177 if (!setup_bio_chain(progname)) { 178 BIO_printf(bio_err, "%s: failed setting up filter chain", 179 progname); 180 return 0; 181 } 182 break; 183 case OPT_INDENT: 184 if (chain == NULL) { 185 BIO_printf(bio_err, "%s: -i given before -n", progname); 186 return 0; 187 } 188 arg = opt_arg(); 189 colon = strchr(arg, ':'); 190 idx = 0; 191 if (colon != NULL) { 192 idx = strtoul(arg, &endptr, 10); 193 if (endptr[0] != ':') { 194 BIO_printf(bio_err, 195 "%s: -i index isn't a decimal number: %s", 196 progname, arg); 197 return 0; 198 } 199 colon++; 200 } else { 201 colon = arg; 202 } 203 indent = strtoul(colon, &endptr, 10); 204 if (endptr[0] != '\0') { 205 BIO_printf(bio_err, 206 "%s: -i value isn't a decimal number: %s", 207 progname, arg); 208 return 0; 209 } 210 if (idx >= amount) { 211 BIO_printf(bio_err, "%s: index (%zu) not within range 0..%zu", 212 progname, idx, amount - 1); 213 return 0; 214 } 215 if (BIO_set_indent(chain[idx], (long)indent) <= 0) { 216 BIO_printf(bio_err, "%s: failed setting indentation: %s", 217 progname, arg); 218 return 0; 219 } 220 break; 221 case OPT_PREFIX: 222 if (chain == NULL) { 223 BIO_printf(bio_err, "%s: -p given before -n", progname); 224 return 0; 225 } 226 arg = opt_arg(); 227 colon = strchr(arg, ':'); 228 idx = 0; 229 if (colon != NULL) { 230 idx = strtoul(arg, &endptr, 10); 231 if (endptr[0] != ':') { 232 BIO_printf(bio_err, 233 "%s: -p index isn't a decimal number: %s", 234 progname, arg); 235 return 0; 236 } 237 colon++; 238 } else { 239 colon = arg; 240 } 241 if (idx >= amount) { 242 BIO_printf(bio_err, "%s: index (%zu) not within range 0..%zu", 243 progname, idx, amount - 1); 244 return 0; 245 } 246 if (BIO_set_prefix(chain[idx], colon) <= 0) { 247 BIO_printf(bio_err, "%s: failed setting prefix: %s", 248 progname, arg); 249 return 0; 250 } 251 break; 252 default: 253 case OPT_ERR: 254 return 0; 255 } 256 } 257 return 1; 258 } 259 260 int main(int argc, char **argv) 261 { 262 int rv = EXIT_SUCCESS; 263 264 opt_init(argc, argv, options); 265 rv = (setup() && run_pipe()) ? EXIT_SUCCESS : EXIT_FAILURE; 266 cleanup(); 267 return rv; 268 } 269