2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the OpenSSL license (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
14 static int tap_write_ex(BIO *b, const char *buf, size_t size, size_t *in_size);
15 static int tap_read_ex(BIO *b, char *buf, size_t size, size_t *out_size);
16 static int tap_puts(BIO *b, const char *str);
17 static int tap_gets(BIO *b, char *str, int size);
18 static long tap_ctrl(BIO *b, int cmd, long arg1, void *arg2);
19 static int tap_new(BIO *b);
20 static int tap_free(BIO *b);
21 static long tap_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
23 const BIO_METHOD *BIO_f_tap(void)
25 static BIO_METHOD *tap = NULL;
28 tap = BIO_meth_new(BIO_TYPE_START | BIO_TYPE_FILTER, "tap");
30 BIO_meth_set_write_ex(tap, tap_write_ex);
31 BIO_meth_set_read_ex(tap, tap_read_ex);
32 BIO_meth_set_puts(tap, tap_puts);
33 BIO_meth_set_gets(tap, tap_gets);
34 BIO_meth_set_ctrl(tap, tap_ctrl);
35 BIO_meth_set_create(tap, tap_new);
36 BIO_meth_set_destroy(tap, tap_free);
37 BIO_meth_set_callback_ctrl(tap, tap_callback_ctrl);
43 static int tap_new(BIO *b)
45 BIO_set_data(b, NULL);
50 static int tap_free(BIO *b)
54 BIO_set_data(b, NULL);
59 static int tap_read_ex(BIO *b, char *buf, size_t size, size_t *out_size)
61 BIO *next = BIO_next(b);
64 ret = BIO_read_ex(next, buf, size, out_size);
65 BIO_clear_retry_flags(b);
66 BIO_copy_next_retry(b);
71 * Output a string to the specified bio and return 1 if successful.
73 static int write_string(BIO *b, const char *buf, size_t n)
77 return BIO_write_ex(b, buf, n, &m) != 0 && m == n;
83 * This function implements a simple state machine that detects new lines.
84 * It indents the output and prefixes it with a '#' character.
86 * It returns the number of input characters that were output in in_size.
87 * More characters than this will likely have been output however any calling
88 * code will be unable to correctly assess the actual number of characters
89 * emitted and would be prone to failure if the actual number were returned.
91 * The BIO_data field is used as our state. If it is NULL, we've just
92 * seen a new line. If it is not NULL, we're processing characters in a line.
94 static int tap_write_ex(BIO *b, const char *buf, size_t size, size_t *in_size)
96 BIO *next = BIO_next(b);
100 for (i = 0; i < size; i++) {
101 if (BIO_get_data(b) == NULL) {
103 for (j = 0; j < subtest_level(); j++)
104 if (!write_string(next, " ", 1))
106 if (!write_string(next, "# ", 2))
109 if (!write_string(next, buf + i, 1))
112 BIO_set_data(b, NULL);
122 static long tap_ctrl(BIO *b, int cmd, long num, void *ptr)
124 BIO *next = BIO_next(b);
128 BIO_set_data(b, NULL);
134 return BIO_ctrl(next, cmd, num, ptr);
137 static long tap_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
139 return BIO_callback_ctrl(BIO_next(b), cmd, fp);
142 static int tap_gets(BIO *b, char *buf, int size)
144 return BIO_gets(BIO_next(b), buf, size);
147 static int tap_puts(BIO *b, const char *str)
151 if (!tap_write_ex(b, str, strlen(str), &m))