tap_bio.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <string.h>
  11. #include "tu_local.h"
  12. static int tap_write_ex(BIO *b, const char *buf, size_t size, size_t *in_size);
  13. static int tap_read_ex(BIO *b, char *buf, size_t size, size_t *out_size);
  14. static int tap_puts(BIO *b, const char *str);
  15. static int tap_gets(BIO *b, char *str, int size);
  16. static long tap_ctrl(BIO *b, int cmd, long arg1, void *arg2);
  17. static int tap_new(BIO *b);
  18. static int tap_free(BIO *b);
  19. static long tap_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  20. const BIO_METHOD *BIO_f_tap(void)
  21. {
  22. static BIO_METHOD *tap = NULL;
  23. if (tap == NULL) {
  24. tap = BIO_meth_new(BIO_TYPE_START | BIO_TYPE_FILTER, "tap");
  25. if (tap != NULL) {
  26. BIO_meth_set_write_ex(tap, tap_write_ex);
  27. BIO_meth_set_read_ex(tap, tap_read_ex);
  28. BIO_meth_set_puts(tap, tap_puts);
  29. BIO_meth_set_gets(tap, tap_gets);
  30. BIO_meth_set_ctrl(tap, tap_ctrl);
  31. BIO_meth_set_create(tap, tap_new);
  32. BIO_meth_set_destroy(tap, tap_free);
  33. BIO_meth_set_callback_ctrl(tap, tap_callback_ctrl);
  34. }
  35. }
  36. return tap;
  37. }
  38. static int tap_new(BIO *b)
  39. {
  40. BIO_set_data(b, NULL);
  41. BIO_set_init(b, 1);
  42. return 1;
  43. }
  44. static int tap_free(BIO *b)
  45. {
  46. if (b == NULL)
  47. return 0;
  48. BIO_set_data(b, NULL);
  49. BIO_set_init(b, 0);
  50. return 1;
  51. }
  52. static int tap_read_ex(BIO *b, char *buf, size_t size, size_t *out_size)
  53. {
  54. BIO *next = BIO_next(b);
  55. int ret = 0;
  56. ret = BIO_read_ex(next, buf, size, out_size);
  57. BIO_clear_retry_flags(b);
  58. BIO_copy_next_retry(b);
  59. return ret;
  60. }
  61. /*
  62. * Output a string to the specified bio and return 1 if successful.
  63. */
  64. static int write_string(BIO *b, const char *buf, size_t n)
  65. {
  66. size_t m;
  67. return BIO_write_ex(b, buf, n, &m) != 0 && m == n;
  68. }
  69. /*
  70. * Write some data.
  71. *
  72. * This function implements a simple state machine that detects new lines.
  73. * It indents the output and prefixes it with a '#' character.
  74. *
  75. * It returns the number of input characters that were output in in_size.
  76. * More characters than this will likely have been output however any calling
  77. * code will be unable to correctly assess the actual number of characters
  78. * emitted and would be prone to failure if the actual number were returned.
  79. *
  80. * The BIO_data field is used as our state. If it is NULL, we've just
  81. * seen a new line. If it is not NULL, we're processing characters in a line.
  82. */
  83. static int tap_write_ex(BIO *b, const char *buf, size_t size, size_t *in_size)
  84. {
  85. static char empty[] = "";
  86. BIO *next = BIO_next(b);
  87. size_t i;
  88. int j;
  89. for (i = 0; i < size; i++) {
  90. if (BIO_get_data(b) == NULL) {
  91. BIO_set_data(b, empty);
  92. for (j = 0; j < subtest_level(); j++)
  93. if (!write_string(next, " ", 1))
  94. goto err;
  95. if (!write_string(next, "# ", 2))
  96. goto err;
  97. }
  98. if (!write_string(next, buf + i, 1))
  99. goto err;
  100. if (buf[i] == '\n')
  101. BIO_set_data(b, NULL);
  102. }
  103. *in_size = i;
  104. return 1;
  105. err:
  106. *in_size = i;
  107. return 0;
  108. }
  109. static long tap_ctrl(BIO *b, int cmd, long num, void *ptr)
  110. {
  111. BIO *next = BIO_next(b);
  112. switch (cmd) {
  113. case BIO_CTRL_RESET:
  114. BIO_set_data(b, NULL);
  115. break;
  116. default:
  117. break;
  118. }
  119. return BIO_ctrl(next, cmd, num, ptr);
  120. }
  121. static long tap_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  122. {
  123. return BIO_callback_ctrl(BIO_next(b), cmd, fp);
  124. }
  125. static int tap_gets(BIO *b, char *buf, int size)
  126. {
  127. return BIO_gets(BIO_next(b), buf, size);
  128. }
  129. static int tap_puts(BIO *b, const char *str)
  130. {
  131. size_t m;
  132. if (!tap_write_ex(b, str, strlen(str), &m))
  133. return 0;
  134. return m;
  135. }