bf_prefix.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 2018-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 <stdio.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include "bio_local.h"
  13. static int prefix_write(BIO *b, const char *out, size_t outl,
  14. size_t *numwritten);
  15. static int prefix_read(BIO *b, char *buf, size_t size, size_t *numread);
  16. static int prefix_puts(BIO *b, const char *str);
  17. static int prefix_gets(BIO *b, char *str, int size);
  18. static long prefix_ctrl(BIO *b, int cmd, long arg1, void *arg2);
  19. static int prefix_create(BIO *b);
  20. static int prefix_destroy(BIO *b);
  21. static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
  22. static const BIO_METHOD prefix_meth = {
  23. BIO_TYPE_BUFFER,
  24. "prefix",
  25. prefix_write,
  26. NULL,
  27. prefix_read,
  28. NULL,
  29. prefix_puts,
  30. prefix_gets,
  31. prefix_ctrl,
  32. prefix_create,
  33. prefix_destroy,
  34. prefix_callback_ctrl,
  35. };
  36. const BIO_METHOD *BIO_f_prefix(void)
  37. {
  38. return &prefix_meth;
  39. }
  40. typedef struct prefix_ctx_st {
  41. char *prefix; /* Text prefix, given by user */
  42. unsigned int indent; /* Indentation amount, given by user */
  43. int linestart; /* flag to indicate we're at the line start */
  44. } PREFIX_CTX;
  45. static int prefix_create(BIO *b)
  46. {
  47. PREFIX_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  48. if (ctx == NULL)
  49. return 0;
  50. ctx->prefix = NULL;
  51. ctx->indent = 0;
  52. ctx->linestart = 1;
  53. BIO_set_data(b, ctx);
  54. BIO_set_init(b, 1);
  55. return 1;
  56. }
  57. static int prefix_destroy(BIO *b)
  58. {
  59. PREFIX_CTX *ctx = BIO_get_data(b);
  60. OPENSSL_free(ctx->prefix);
  61. OPENSSL_free(ctx);
  62. return 1;
  63. }
  64. static int prefix_read(BIO *b, char *in, size_t size, size_t *numread)
  65. {
  66. return BIO_read_ex(BIO_next(b), in, size, numread);
  67. }
  68. static int prefix_write(BIO *b, const char *out, size_t outl,
  69. size_t *numwritten)
  70. {
  71. PREFIX_CTX *ctx = BIO_get_data(b);
  72. if (ctx == NULL)
  73. return 0;
  74. /*
  75. * If no prefix is set or if it's empty, and no indentation amount is set,
  76. * we've got nothing to do here
  77. */
  78. if ((ctx->prefix == NULL || *ctx->prefix == '\0')
  79. && ctx->indent == 0) {
  80. /*
  81. * We do note if what comes next will be a new line, though, so we're
  82. * prepared to handle prefix and indentation the next time around.
  83. */
  84. if (outl > 0)
  85. ctx->linestart = (out[outl-1] == '\n');
  86. return BIO_write_ex(BIO_next(b), out, outl, numwritten);
  87. }
  88. *numwritten = 0;
  89. while (outl > 0) {
  90. size_t i;
  91. char c;
  92. /*
  93. * If we know that we're at the start of the line, output prefix and
  94. * indentation.
  95. */
  96. if (ctx->linestart) {
  97. size_t dontcare;
  98. if (ctx->prefix != NULL
  99. && !BIO_write_ex(BIO_next(b), ctx->prefix, strlen(ctx->prefix),
  100. &dontcare))
  101. return 0;
  102. BIO_printf(BIO_next(b), "%*s", ctx->indent, "");
  103. ctx->linestart = 0;
  104. }
  105. /* Now, go look for the next LF, or the end of the string */
  106. for (i = 0, c = '\0'; i < outl && (c = out[i]) != '\n'; i++)
  107. continue;
  108. if (c == '\n')
  109. i++;
  110. /* Output what we found so far */
  111. while (i > 0) {
  112. size_t num = 0;
  113. if (!BIO_write_ex(BIO_next(b), out, i, &num))
  114. return 0;
  115. out += num;
  116. outl -= num;
  117. *numwritten += num;
  118. i -= num;
  119. }
  120. /* If we found a LF, what follows is a new line, so take note */
  121. if (c == '\n')
  122. ctx->linestart = 1;
  123. }
  124. return 1;
  125. }
  126. static long prefix_ctrl(BIO *b, int cmd, long num, void *ptr)
  127. {
  128. long ret = 0;
  129. PREFIX_CTX *ctx = BIO_get_data(b);
  130. if (ctx == NULL)
  131. return -1;
  132. switch (cmd) {
  133. case BIO_CTRL_SET_PREFIX:
  134. OPENSSL_free(ctx->prefix);
  135. if (ptr == NULL) {
  136. ctx->prefix = NULL;
  137. ret = 1;
  138. } else {
  139. ctx->prefix = OPENSSL_strdup((const char *)ptr);
  140. ret = ctx->prefix != NULL;
  141. }
  142. break;
  143. case BIO_CTRL_SET_INDENT:
  144. if (num >= 0) {
  145. ctx->indent = (unsigned int)num;
  146. ret = 1;
  147. }
  148. break;
  149. case BIO_CTRL_GET_INDENT:
  150. ret = (long)ctx->indent;
  151. break;
  152. default:
  153. /* Commands that we intercept before passing them along */
  154. switch (cmd) {
  155. case BIO_C_FILE_SEEK:
  156. case BIO_CTRL_RESET:
  157. ctx->linestart = 1;
  158. break;
  159. }
  160. if (BIO_next(b) != NULL)
  161. ret = BIO_ctrl(BIO_next(b), cmd, num, ptr);
  162. break;
  163. }
  164. return ret;
  165. }
  166. static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  167. {
  168. return BIO_callback_ctrl(BIO_next(b), cmd, fp);
  169. }
  170. static int prefix_gets(BIO *b, char *buf, int size)
  171. {
  172. return BIO_gets(BIO_next(b), buf, size);
  173. }
  174. static int prefix_puts(BIO *b, const char *str)
  175. {
  176. return BIO_write(b, str, strlen(str));
  177. }