1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <string.h>
#include <stdarg.h>
#include <openssl/bio.h>
#include <openssl/safestack.h>
#include "opt.h"
static BIO *bio_in = NULL;
static BIO *bio_out = NULL;
static BIO *bio_err = NULL;
/*-
* This program sets up a chain of BIO_f_filter() on top of bio_out, how
* many is governed by the user through -n. It allows the user to set the
* indentation for each individual filter using -i and -p. Then it reads
* text from bio_in and prints it out through the BIO chain.
*
* The filter index is counted from the source/sink, i.e. index 0 is closest
* to it.
*
* Example:
*
* $ echo foo | ./bio_prefix_text -n 2 -i 1:32 -p 1:FOO -i 0:3
* FOO foo
* ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* | |
* | +------ 32 spaces from filter 1
* +-------------------------- 3 spaces from filter 0
*/
static size_t amount = 0;
static BIO **chain = NULL;
typedef enum OPTION_choice {
OPT_ERR = -1,
OPT_EOF = 0,
OPT_AMOUNT,
OPT_INDENT,
OPT_PREFIX
} OPTION_CHOICE;
static const OPTIONS options[] = {
{ "n", OPT_AMOUNT, 'p', "Amount of BIO_f_prefix() filters" },
/*
* idx is the index to the BIO_f_filter chain(), where 0 is closest
* to the source/sink BIO. If idx isn't given, 0 is assumed
*/
{ "i", OPT_INDENT, 's', "Indentation in form '[idx:]indent'" },
{ "p", OPT_PREFIX, 's', "Prefix in form '[idx:]prefix'" },
{ NULL }
};
int opt_printf_stderr(const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = BIO_vprintf(bio_err, fmt, ap);
va_end(ap);
return ret;
}
static int run_pipe(void)
{
char buf[4096];
while (!BIO_eof(bio_in)) {
size_t bytes_in;
size_t bytes_out;
if (!BIO_read_ex(bio_in, buf, sizeof(buf), &bytes_in))
return 0;
bytes_out = 0;
while (bytes_out < bytes_in) {
size_t bytes;
if (!BIO_write_ex(chain[amount - 1], buf, bytes_in, &bytes))
return 0;
bytes_out += bytes;
}
}
return 1;
}
static int setup_bio_chain(const char *progname)
{
BIO *next = NULL;
size_t n = amount;
chain = OPENSSL_zalloc(sizeof(*chain) * n);
if (chain != NULL) {
size_t i;
next = bio_out;
BIO_up_ref(next); /* Protection against freeing */
for (i = 0; n > 0; i++, n--) {
BIO *curr = BIO_new(BIO_f_prefix());
if (curr == NULL)
goto err;
chain[i] = BIO_push(curr, next);
if (chain[i] == NULL)
goto err;
next = chain[i];
}
}
return chain != NULL;
err:
/* Free the chain we built up */
BIO_free_all(next);
OPENSSL_free(chain);
return 0;
}
static void cleanup(void)
{
if (chain != NULL) {
BIO_free_all(chain[amount - 1]);
OPENSSL_free(chain);
}
BIO_free_all(bio_in);
BIO_free_all(bio_out);
BIO_free_all(bio_err);
}
static int setup(void)
{
OPTION_CHOICE o;
char *arg;
char *colon;
char *endptr;
size_t idx, indent;
const char *progname = opt_getprog();
bio_in = BIO_new_fp(stdin, BIO_NOCLOSE | BIO_FP_TEXT);
bio_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
#ifdef __VMS
bio_out = BIO_push(BIO_new(BIO_f_linebuffer()), bio_out);
bio_err = BIO_push(BIO_new(BIO_f_linebuffer()), bio_err);
#endif
OPENSSL_assert(bio_in != NULL);
OPENSSL_assert(bio_out != NULL);
OPENSSL_assert(bio_err != NULL);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_AMOUNT:
arg = opt_arg();
amount = strtoul(arg, &endptr, 10);
if (endptr[0] != '\0') {
BIO_printf(bio_err,
"%s: -n argument isn't a decimal number: %s",
progname, arg);
return 0;
}
if (amount < 1) {
BIO_printf(bio_err, "%s: must set up at least one filter",
progname);
return 0;
}
if (!setup_bio_chain(progname)) {
BIO_printf(bio_err, "%s: failed setting up filter chain",
progname);
return 0;
}
break;
case OPT_INDENT:
if (chain == NULL) {
BIO_printf(bio_err, "%s: -i given before -n", progname);
return 0;
}
arg = opt_arg();
colon = strchr(arg, ':');
idx = 0;
if (colon != NULL) {
idx = strtoul(arg, &endptr, 10);
if (endptr[0] != ':') {
BIO_printf(bio_err,
"%s: -i index isn't a decimal number: %s",
progname, arg);
return 0;
}
colon++;
} else {
colon = arg;
}
indent = strtoul(colon, &endptr, 10);
if (endptr[0] != '\0') {
BIO_printf(bio_err,
"%s: -i value isn't a decimal number: %s",
progname, arg);
return 0;
}
if (idx >= amount) {
BIO_printf(bio_err, "%s: index (%zu) not within range 0..%zu",
progname, idx, amount - 1);
return 0;
}
if (!BIO_set_indent(chain[idx], (long)indent)) {
BIO_printf(bio_err, "%s: failed setting indentation: %s",
progname, arg);
return 0;
}
break;
case OPT_PREFIX:
if (chain == NULL) {
BIO_printf(bio_err, "%s: -p given before -n", progname);
return 0;
}
arg = opt_arg();
colon = strchr(arg, ':');
idx = 0;
if (colon != NULL) {
idx = strtoul(arg, &endptr, 10);
if (endptr[0] != ':') {
BIO_printf(bio_err,
"%s: -p index isn't a decimal number: %s",
progname, arg);
return 0;
}
colon++;
} else {
colon = arg;
}
if (idx >= amount) {
BIO_printf(bio_err, "%s: index (%zu) not within range 0..%zu",
progname, idx, amount - 1);
return 0;
}
if (!BIO_set_prefix(chain[idx], colon)) {
BIO_printf(bio_err, "%s: failed setting prefix: %s",
progname, arg);
return 0;
}
break;
default:
case OPT_ERR:
return 0;
}
}
return 1;
}
int main(int argc, char **argv)
{
int rv = EXIT_SUCCESS;
opt_init(argc, argv, options);
rv = (setup() && run_pipe()) ? EXIT_SUCCESS : EXIT_FAILURE;
cleanup();
return rv;
}
|