1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. |
---|
5 | * |
---|
6 | * Licensed under the OpenSSL license (the "License"). You may not use |
---|
7 | * this file except in compliance with the License. You can obtain a copy |
---|
8 | * in the file LICENSE in the source distribution or at |
---|
9 | * https://www.openssl.org/source/license.html |
---|
10 | */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | #include <errno.h> |
---|
15 | #include <openssl/bio.h> |
---|
16 | #include "apps.h" |
---|
17 | |
---|
18 | static int prefix_write(BIO *b, const char *out, size_t outl, |
---|
19 | size_t *numwritten); |
---|
20 | static int prefix_read(BIO *b, char *buf, size_t size, size_t *numread); |
---|
21 | static int prefix_puts(BIO *b, const char *str); |
---|
22 | static int prefix_gets(BIO *b, char *str, int size); |
---|
23 | static long prefix_ctrl(BIO *b, int cmd, long arg1, void *arg2); |
---|
24 | static int prefix_create(BIO *b); |
---|
25 | static int prefix_destroy(BIO *b); |
---|
26 | static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp); |
---|
27 | |
---|
28 | static BIO_METHOD *prefix_meth = NULL; |
---|
29 | |
---|
30 | BIO_METHOD *apps_bf_prefix(void) |
---|
31 | { |
---|
32 | if (prefix_meth == NULL) { |
---|
33 | if ((prefix_meth = |
---|
34 | BIO_meth_new(BIO_TYPE_FILTER, "Prefix filter")) == NULL |
---|
35 | || !BIO_meth_set_create(prefix_meth, prefix_create) |
---|
36 | || !BIO_meth_set_destroy(prefix_meth, prefix_destroy) |
---|
37 | || !BIO_meth_set_write_ex(prefix_meth, prefix_write) |
---|
38 | || !BIO_meth_set_read_ex(prefix_meth, prefix_read) |
---|
39 | || !BIO_meth_set_puts(prefix_meth, prefix_puts) |
---|
40 | || !BIO_meth_set_gets(prefix_meth, prefix_gets) |
---|
41 | || !BIO_meth_set_ctrl(prefix_meth, prefix_ctrl) |
---|
42 | || !BIO_meth_set_callback_ctrl(prefix_meth, prefix_callback_ctrl)) { |
---|
43 | BIO_meth_free(prefix_meth); |
---|
44 | prefix_meth = NULL; |
---|
45 | } |
---|
46 | } |
---|
47 | return prefix_meth; |
---|
48 | } |
---|
49 | |
---|
50 | typedef struct prefix_ctx_st { |
---|
51 | char *prefix; |
---|
52 | int linestart; /* flag to indicate we're at the line start */ |
---|
53 | } PREFIX_CTX; |
---|
54 | |
---|
55 | static int prefix_create(BIO *b) |
---|
56 | { |
---|
57 | PREFIX_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
---|
58 | |
---|
59 | if (ctx == NULL) |
---|
60 | return 0; |
---|
61 | |
---|
62 | ctx->prefix = NULL; |
---|
63 | ctx->linestart = 1; |
---|
64 | BIO_set_data(b, ctx); |
---|
65 | BIO_set_init(b, 1); |
---|
66 | return 1; |
---|
67 | } |
---|
68 | |
---|
69 | static int prefix_destroy(BIO *b) |
---|
70 | { |
---|
71 | PREFIX_CTX *ctx = BIO_get_data(b); |
---|
72 | |
---|
73 | OPENSSL_free(ctx->prefix); |
---|
74 | OPENSSL_free(ctx); |
---|
75 | return 1; |
---|
76 | } |
---|
77 | |
---|
78 | static int prefix_read(BIO *b, char *in, size_t size, size_t *numread) |
---|
79 | { |
---|
80 | return BIO_read_ex(BIO_next(b), in, size, numread); |
---|
81 | } |
---|
82 | |
---|
83 | static int prefix_write(BIO *b, const char *out, size_t outl, |
---|
84 | size_t *numwritten) |
---|
85 | { |
---|
86 | PREFIX_CTX *ctx = BIO_get_data(b); |
---|
87 | |
---|
88 | if (ctx == NULL) |
---|
89 | return 0; |
---|
90 | |
---|
91 | /* If no prefix is set or if it's empty, we've got nothing to do here */ |
---|
92 | if (ctx->prefix == NULL || *ctx->prefix == '\0') { |
---|
93 | /* We do note if what comes next will be a new line, though */ |
---|
94 | if (outl > 0) |
---|
95 | ctx->linestart = (out[outl-1] == '\n'); |
---|
96 | return BIO_write_ex(BIO_next(b), out, outl, numwritten); |
---|
97 | } |
---|
98 | |
---|
99 | *numwritten = 0; |
---|
100 | |
---|
101 | while (outl > 0) { |
---|
102 | size_t i; |
---|
103 | char c; |
---|
104 | |
---|
105 | /* If we know that we're at the start of the line, output the prefix */ |
---|
106 | if (ctx->linestart) { |
---|
107 | size_t dontcare; |
---|
108 | |
---|
109 | if (!BIO_write_ex(BIO_next(b), ctx->prefix, strlen(ctx->prefix), |
---|
110 | &dontcare)) |
---|
111 | return 0; |
---|
112 | ctx->linestart = 0; |
---|
113 | } |
---|
114 | |
---|
115 | /* Now, go look for the next LF, or the end of the string */ |
---|
116 | for (i = 0, c = '\0'; i < outl && (c = out[i]) != '\n'; i++) |
---|
117 | continue; |
---|
118 | if (c == '\n') |
---|
119 | i++; |
---|
120 | |
---|
121 | /* Output what we found so far */ |
---|
122 | while (i > 0) { |
---|
123 | size_t num = 0; |
---|
124 | |
---|
125 | if (!BIO_write_ex(BIO_next(b), out, i, &num)) |
---|
126 | return 0; |
---|
127 | out += num; |
---|
128 | outl -= num; |
---|
129 | *numwritten += num; |
---|
130 | i -= num; |
---|
131 | } |
---|
132 | |
---|
133 | /* If we found a LF, what follows is a new line, so take note */ |
---|
134 | if (c == '\n') |
---|
135 | ctx->linestart = 1; |
---|
136 | } |
---|
137 | |
---|
138 | return 1; |
---|
139 | } |
---|
140 | |
---|
141 | static long prefix_ctrl(BIO *b, int cmd, long num, void *ptr) |
---|
142 | { |
---|
143 | long ret = 0; |
---|
144 | |
---|
145 | switch (cmd) { |
---|
146 | case PREFIX_CTRL_SET_PREFIX: |
---|
147 | { |
---|
148 | PREFIX_CTX *ctx = BIO_get_data(b); |
---|
149 | |
---|
150 | if (ctx == NULL) |
---|
151 | break; |
---|
152 | |
---|
153 | OPENSSL_free(ctx->prefix); |
---|
154 | ctx->prefix = OPENSSL_strdup((const char *)ptr); |
---|
155 | ret = ctx->prefix != NULL; |
---|
156 | } |
---|
157 | break; |
---|
158 | default: |
---|
159 | if (BIO_next(b) != NULL) |
---|
160 | ret = BIO_ctrl(BIO_next(b), cmd, num, ptr); |
---|
161 | break; |
---|
162 | } |
---|
163 | return ret; |
---|
164 | } |
---|
165 | |
---|
166 | static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
---|
167 | { |
---|
168 | return BIO_callback_ctrl(BIO_next(b), cmd, fp); |
---|
169 | } |
---|
170 | |
---|
171 | static int prefix_gets(BIO *b, char *buf, int size) |
---|
172 | { |
---|
173 | return BIO_gets(BIO_next(b), buf, size); |
---|
174 | } |
---|
175 | |
---|
176 | static int prefix_puts(BIO *b, const char *str) |
---|
177 | { |
---|
178 | return BIO_write(b, str, strlen(str)); |
---|
179 | } |
---|