bf_prefix.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <openssl/bio.h>
  13. #include "apps.h"
  14. static int prefix_write(BIO *b, const char *out, size_t outl,
  15. size_t *numwritten);
  16. static int prefix_read(BIO *b, char *buf, size_t size, size_t *numread);
  17. static int prefix_puts(BIO *b, const char *str);
  18. static int prefix_gets(BIO *b, char *str, int size);
  19. static long prefix_ctrl(BIO *b, int cmd, long arg1, void *arg2);
  20. static int prefix_create(BIO *b);
  21. static int prefix_destroy(BIO *b);
  22. static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
  23. static BIO_METHOD *prefix_meth = NULL;
  24. BIO_METHOD *apps_bf_prefix(void)
  25. {
  26. if (prefix_meth == NULL) {
  27. if ((prefix_meth =
  28. BIO_meth_new(BIO_TYPE_FILTER, "Prefix filter")) == NULL
  29. || !BIO_meth_set_create(prefix_meth, prefix_create)
  30. || !BIO_meth_set_destroy(prefix_meth, prefix_destroy)
  31. || !BIO_meth_set_write_ex(prefix_meth, prefix_write)
  32. || !BIO_meth_set_read_ex(prefix_meth, prefix_read)
  33. || !BIO_meth_set_puts(prefix_meth, prefix_puts)
  34. || !BIO_meth_set_gets(prefix_meth, prefix_gets)
  35. || !BIO_meth_set_ctrl(prefix_meth, prefix_ctrl)
  36. || !BIO_meth_set_callback_ctrl(prefix_meth, prefix_callback_ctrl)) {
  37. BIO_meth_free(prefix_meth);
  38. prefix_meth = NULL;
  39. }
  40. }
  41. return prefix_meth;
  42. }
  43. typedef struct prefix_ctx_st {
  44. char *prefix;
  45. int linestart; /* flag to indicate we're at the line start */
  46. } PREFIX_CTX;
  47. static int prefix_create(BIO *b)
  48. {
  49. PREFIX_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  50. if (ctx == NULL)
  51. return 0;
  52. ctx->prefix = NULL;
  53. ctx->linestart = 1;
  54. BIO_set_data(b, ctx);
  55. BIO_set_init(b, 1);
  56. return 1;
  57. }
  58. static int prefix_destroy(BIO *b)
  59. {
  60. PREFIX_CTX *ctx = BIO_get_data(b);
  61. OPENSSL_free(ctx->prefix);
  62. OPENSSL_free(ctx);
  63. return 1;
  64. }
  65. static int prefix_read(BIO *b, char *in, size_t size, size_t *numread)
  66. {
  67. return BIO_read_ex(BIO_next(b), in, size, numread);
  68. }
  69. static int prefix_write(BIO *b, const char *out, size_t outl,
  70. size_t *numwritten)
  71. {
  72. PREFIX_CTX *ctx = BIO_get_data(b);
  73. if (ctx == NULL)
  74. return 0;
  75. /* If no prefix is set or if it's empty, we've got nothing to do here */
  76. if (ctx->prefix == NULL || *ctx->prefix == '\0') {
  77. /* We do note if what comes next will be a new line, though */
  78. if (outl > 0)
  79. ctx->linestart = (out[outl-1] == '\n');
  80. return BIO_write_ex(BIO_next(b), out, outl, numwritten);
  81. }
  82. *numwritten = 0;
  83. while (outl > 0) {
  84. size_t i;
  85. char c;
  86. /* If we know that we're at the start of the line, output the prefix */
  87. if (ctx->linestart) {
  88. size_t dontcare;
  89. if (!BIO_write_ex(BIO_next(b), ctx->prefix, strlen(ctx->prefix),
  90. &dontcare))
  91. return 0;
  92. ctx->linestart = 0;
  93. }
  94. /* Now, go look for the next LF, or the end of the string */
  95. for (i = 0, c = '\0'; i < outl && (c = out[i]) != '\n'; i++)
  96. continue;
  97. if (c == '\n')
  98. i++;
  99. /* Output what we found so far */
  100. while (i > 0) {
  101. size_t num = 0;
  102. if (!BIO_write_ex(BIO_next(b), out, i, &num))
  103. return 0;
  104. out += num;
  105. outl -= num;
  106. *numwritten += num;
  107. i -= num;
  108. }
  109. /* If we found a LF, what follows is a new line, so take note */
  110. if (c == '\n')
  111. ctx->linestart = 1;
  112. }
  113. return 1;
  114. }
  115. static long prefix_ctrl(BIO *b, int cmd, long num, void *ptr)
  116. {
  117. long ret = 0;
  118. switch (cmd) {
  119. case PREFIX_CTRL_SET_PREFIX:
  120. {
  121. PREFIX_CTX *ctx = BIO_get_data(b);
  122. if (ctx == NULL)
  123. break;
  124. OPENSSL_free(ctx->prefix);
  125. ctx->prefix = OPENSSL_strdup((const char *)ptr);
  126. ret = ctx->prefix != NULL;
  127. }
  128. break;
  129. default:
  130. if (BIO_next(b) != NULL)
  131. ret = BIO_ctrl(BIO_next(b), cmd, num, ptr);
  132. break;
  133. }
  134. return ret;
  135. }
  136. static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  137. {
  138. return BIO_callback_ctrl(BIO_next(b), cmd, fp);
  139. }
  140. static int prefix_gets(BIO *b, char *buf, int size)
  141. {
  142. return BIO_gets(BIO_next(b), buf, size);
  143. }
  144. static int prefix_puts(BIO *b, const char *str)
  145. {
  146. return BIO_write(b, str, strlen(str));
  147. }