2 * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include "bio_local.h"
15 static int prefix_write(BIO *b, const char *out, size_t outl,
17 static int prefix_read(BIO *b, char *buf, size_t size, size_t *numread);
18 static int prefix_puts(BIO *b, const char *str);
19 static int prefix_gets(BIO *b, char *str, int size);
20 static long prefix_ctrl(BIO *b, int cmd, long arg1, void *arg2);
21 static int prefix_create(BIO *b);
22 static int prefix_destroy(BIO *b);
23 static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
25 static const BIO_METHOD prefix_meth = {
40 const BIO_METHOD *BIO_f_prefix(void)
45 typedef struct prefix_ctx_st {
46 char *prefix; /* Text prefix, given by user */
47 unsigned int indent; /* Indentation amount, given by user */
49 int linestart; /* flag to indicate we're at the line start */
52 static int prefix_create(BIO *b)
54 PREFIX_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
67 static int prefix_destroy(BIO *b)
69 PREFIX_CTX *ctx = BIO_get_data(b);
71 OPENSSL_free(ctx->prefix);
76 static int prefix_read(BIO *b, char *in, size_t size, size_t *numread)
78 return BIO_read_ex(BIO_next(b), in, size, numread);
81 static int prefix_write(BIO *b, const char *out, size_t outl,
84 PREFIX_CTX *ctx = BIO_get_data(b);
90 * If no prefix is set or if it's empty, and no indentation amount is set,
91 * we've got nothing to do here
93 if ((ctx->prefix == NULL || *ctx->prefix == '\0')
94 && ctx->indent == 0) {
96 * We do note if what comes next will be a new line, though, so we're
97 * prepared to handle prefix and indentation the next time around.
100 ctx->linestart = (out[outl-1] == '\n');
101 return BIO_write_ex(BIO_next(b), out, outl, numwritten);
111 * If we know that we're at the start of the line, output prefix and
114 if (ctx->linestart) {
117 if (ctx->prefix != NULL
118 && !BIO_write_ex(BIO_next(b), ctx->prefix, strlen(ctx->prefix),
121 BIO_printf(BIO_next(b), "%*s", ctx->indent, "");
125 /* Now, go look for the next LF, or the end of the string */
126 for (i = 0, c = '\0'; i < outl && (c = out[i]) != '\n'; i++)
131 /* Output what we found so far */
135 if (!BIO_write_ex(BIO_next(b), out, i, &num))
143 /* If we found a LF, what follows is a new line, so take note */
151 static long prefix_ctrl(BIO *b, int cmd, long num, void *ptr)
154 PREFIX_CTX *ctx = BIO_get_data(b);
160 case BIO_CTRL_SET_PREFIX:
161 OPENSSL_free(ctx->prefix);
166 ctx->prefix = OPENSSL_strdup((const char *)ptr);
167 ret = ctx->prefix != NULL;
170 case BIO_CTRL_SET_INDENT:
172 ctx->indent = (unsigned int)num;
176 case BIO_CTRL_GET_INDENT:
177 ret = (long)ctx->indent;
180 /* Commands that we intercept before passing them along */
182 case BIO_C_FILE_SEEK:
187 if (BIO_next(b) != NULL)
188 ret = BIO_ctrl(BIO_next(b), cmd, num, ptr);
194 static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
196 return BIO_callback_ctrl(BIO_next(b), cmd, fp);
199 static int prefix_gets(BIO *b, char *buf, int size)
201 return BIO_gets(BIO_next(b), buf, size);
204 static int prefix_puts(BIO *b, const char *str)
206 return BIO_write(b, str, strlen(str));