2
0

bio_prefix_text.c 7.4 KB

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