yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 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 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 |
| 9 | */ |
| 10 | |
| 11 | #include <string.h> |
| 12 | #include "tu_local.h" |
| 13 | |
| 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); |
| 22 | |
| 23 | const BIO_METHOD *BIO_f_tap(void) |
| 24 | { |
| 25 | static BIO_METHOD *tap = NULL; |
| 26 | |
| 27 | if (tap == NULL) { |
| 28 | tap = BIO_meth_new(BIO_TYPE_START | BIO_TYPE_FILTER, "tap"); |
| 29 | if (tap != NULL) { |
| 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); |
| 38 | } |
| 39 | } |
| 40 | return tap; |
| 41 | } |
| 42 | |
| 43 | static int tap_new(BIO *b) |
| 44 | { |
| 45 | BIO_set_data(b, NULL); |
| 46 | BIO_set_init(b, 1); |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | static int tap_free(BIO *b) |
| 51 | { |
| 52 | if (b == NULL) |
| 53 | return 0; |
| 54 | BIO_set_data(b, NULL); |
| 55 | BIO_set_init(b, 0); |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | static int tap_read_ex(BIO *b, char *buf, size_t size, size_t *out_size) |
| 60 | { |
| 61 | BIO *next = BIO_next(b); |
| 62 | int ret = 0; |
| 63 | |
| 64 | ret = BIO_read_ex(next, buf, size, out_size); |
| 65 | BIO_clear_retry_flags(b); |
| 66 | BIO_copy_next_retry(b); |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Output a string to the specified bio and return 1 if successful. |
| 72 | */ |
| 73 | static int write_string(BIO *b, const char *buf, size_t n) |
| 74 | { |
| 75 | size_t m; |
| 76 | |
| 77 | return BIO_write_ex(b, buf, n, &m) != 0 && m == n; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Write some data. |
| 82 | * |
| 83 | * This function implements a simple state machine that detects new lines. |
| 84 | * It indents the output and prefixes it with a '#' character. |
| 85 | * |
| 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. |
| 90 | * |
| 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. |
| 93 | */ |
| 94 | static int tap_write_ex(BIO *b, const char *buf, size_t size, size_t *in_size) |
| 95 | { |
| 96 | BIO *next = BIO_next(b); |
| 97 | size_t i; |
| 98 | int j; |
| 99 | |
| 100 | for (i = 0; i < size; i++) { |
| 101 | if (BIO_get_data(b) == NULL) { |
| 102 | BIO_set_data(b, ""); |
| 103 | for (j = 0; j < subtest_level(); j++) |
| 104 | if (!write_string(next, " ", 1)) |
| 105 | goto err; |
| 106 | if (!write_string(next, "# ", 2)) |
| 107 | goto err; |
| 108 | } |
| 109 | if (!write_string(next, buf + i, 1)) |
| 110 | goto err; |
| 111 | if (buf[i] == '\n') |
| 112 | BIO_set_data(b, NULL); |
| 113 | } |
| 114 | *in_size = i; |
| 115 | return 1; |
| 116 | |
| 117 | err: |
| 118 | *in_size = i; |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static long tap_ctrl(BIO *b, int cmd, long num, void *ptr) |
| 123 | { |
| 124 | BIO *next = BIO_next(b); |
| 125 | |
| 126 | switch (cmd) { |
| 127 | case BIO_CTRL_RESET: |
| 128 | BIO_set_data(b, NULL); |
| 129 | break; |
| 130 | |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | return BIO_ctrl(next, cmd, num, ptr); |
| 135 | } |
| 136 | |
| 137 | static long tap_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
| 138 | { |
| 139 | return BIO_callback_ctrl(BIO_next(b), cmd, fp); |
| 140 | } |
| 141 | |
| 142 | static int tap_gets(BIO *b, char *buf, int size) |
| 143 | { |
| 144 | return BIO_gets(BIO_next(b), buf, size); |
| 145 | } |
| 146 | |
| 147 | static int tap_puts(BIO *b, const char *str) |
| 148 | { |
| 149 | size_t m; |
| 150 | |
| 151 | if (!tap_write_ex(b, str, strlen(str), &m)) |
| 152 | return 0; |
| 153 | return m; |
| 154 | } |