]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/bf_prefix.c
2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/bio.h>
16 static int prefix_write(BIO
*b
, const char *out
, size_t outl
,
18 static int prefix_read(BIO
*b
, char *buf
, size_t size
, size_t *numread
);
19 static int prefix_puts(BIO
*b
, const char *str
);
20 static int prefix_gets(BIO
*b
, char *str
, int size
);
21 static long prefix_ctrl(BIO
*b
, int cmd
, long arg1
, void *arg2
);
22 static int prefix_create(BIO
*b
);
23 static int prefix_destroy(BIO
*b
);
24 static long prefix_callback_ctrl(BIO
*b
, int cmd
, BIO_info_cb
*fp
);
26 static BIO_METHOD
*prefix_meth
= NULL
;
28 BIO_METHOD
*apps_bf_prefix(void)
30 if (prefix_meth
== NULL
) {
32 BIO_meth_new(BIO_TYPE_FILTER
, "Prefix filter")) == NULL
33 || !BIO_meth_set_create(prefix_meth
, prefix_create
)
34 || !BIO_meth_set_destroy(prefix_meth
, prefix_destroy
)
35 || !BIO_meth_set_write_ex(prefix_meth
, prefix_write
)
36 || !BIO_meth_set_read_ex(prefix_meth
, prefix_read
)
37 || !BIO_meth_set_puts(prefix_meth
, prefix_puts
)
38 || !BIO_meth_set_gets(prefix_meth
, prefix_gets
)
39 || !BIO_meth_set_ctrl(prefix_meth
, prefix_ctrl
)
40 || !BIO_meth_set_callback_ctrl(prefix_meth
, prefix_callback_ctrl
)) {
41 BIO_meth_free(prefix_meth
);
48 typedef struct prefix_ctx_st
{
50 int linestart
; /* flag to indicate we're at the line start */
53 static int prefix_create(BIO
*b
)
55 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
);
89 /* If no prefix is set or if it's empty, we've got nothing to do here */
90 if (ctx
->prefix
== NULL
|| *ctx
->prefix
== '\0') {
91 /* We do note if what comes next will be a new line, though */
93 ctx
->linestart
= (out
[outl
-1] == '\n');
94 return BIO_write_ex(BIO_next(b
), out
, outl
, numwritten
);
103 /* If we know that we're at the start of the line, output the prefix */
104 if (ctx
->linestart
) {
107 if (!BIO_write_ex(BIO_next(b
), ctx
->prefix
, strlen(ctx
->prefix
),
113 /* Now, go look for the next LF, or the end of the string */
114 for (i
= 0, c
= '\0'; i
< outl
&& (c
= out
[i
]) != '\n'; i
++)
119 /* Output what we found so far */
123 if (!BIO_write_ex(BIO_next(b
), out
, i
, &num
))
131 /* If we found a LF, what follows is a new line, so take note */
139 static long prefix_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
144 case PREFIX_CTRL_SET_PREFIX
:
146 PREFIX_CTX
*ctx
= BIO_get_data(b
);
151 OPENSSL_free(ctx
->prefix
);
152 ctx
->prefix
= OPENSSL_strdup((const char *)ptr
);
153 ret
= ctx
->prefix
!= NULL
;
157 if (BIO_next(b
) != NULL
)
158 ret
= BIO_ctrl(BIO_next(b
), cmd
, num
, ptr
);
164 static long prefix_callback_ctrl(BIO
*b
, int cmd
, BIO_info_cb
*fp
)
166 return BIO_callback_ctrl(BIO_next(b
), cmd
, fp
);
169 static int prefix_gets(BIO
*b
, char *buf
, int size
)
171 return BIO_gets(BIO_next(b
), buf
, size
);
174 static int prefix_puts(BIO
*b
, const char *str
)
176 return BIO_write(b
, str
, strlen(str
));