bio/b_print.c: recognize even 'j' format modifier.
[openssl.git] / crypto / bio / bf_lbuf.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include "bio_lcl.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/evp.h>
15
16 static int linebuffer_write(BIO *h, const char *buf, int num);
17 static int linebuffer_read(BIO *h, char *buf, int size);
18 static int linebuffer_puts(BIO *h, const char *str);
19 static int linebuffer_gets(BIO *h, char *str, int size);
20 static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
21 static int linebuffer_new(BIO *h);
22 static int linebuffer_free(BIO *data);
23 static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
24
25 /* A 10k maximum should be enough for most purposes */
26 #define DEFAULT_LINEBUFFER_SIZE 1024*10
27
28 /* #define DEBUG */
29
30 static const BIO_METHOD methods_linebuffer = {
31     BIO_TYPE_LINEBUFFER,
32     "linebuffer",
33     /* TODO: Convert to new style write function */
34     bwrite_conv,
35     linebuffer_write,
36     /* TODO: Convert to new style read function */
37     bread_conv,
38     linebuffer_read,
39     linebuffer_puts,
40     linebuffer_gets,
41     linebuffer_ctrl,
42     linebuffer_new,
43     linebuffer_free,
44     linebuffer_callback_ctrl,
45 };
46
47 const BIO_METHOD *BIO_f_linebuffer(void)
48 {
49     return (&methods_linebuffer);
50 }
51
52 typedef struct bio_linebuffer_ctx_struct {
53     char *obuf;                 /* the output char array */
54     int obuf_size;              /* how big is the output buffer */
55     int obuf_len;               /* how many bytes are in it */
56 } BIO_LINEBUFFER_CTX;
57
58 static int linebuffer_new(BIO *bi)
59 {
60     BIO_LINEBUFFER_CTX *ctx;
61
62     ctx = OPENSSL_malloc(sizeof(*ctx));
63     if (ctx == NULL)
64         return (0);
65     ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
66     if (ctx->obuf == NULL) {
67         OPENSSL_free(ctx);
68         return (0);
69     }
70     ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
71     ctx->obuf_len = 0;
72
73     bi->init = 1;
74     bi->ptr = (char *)ctx;
75     bi->flags = 0;
76     return (1);
77 }
78
79 static int linebuffer_free(BIO *a)
80 {
81     BIO_LINEBUFFER_CTX *b;
82
83     if (a == NULL)
84         return (0);
85     b = (BIO_LINEBUFFER_CTX *)a->ptr;
86     OPENSSL_free(b->obuf);
87     OPENSSL_free(a->ptr);
88     a->ptr = NULL;
89     a->init = 0;
90     a->flags = 0;
91     return (1);
92 }
93
94 static int linebuffer_read(BIO *b, char *out, int outl)
95 {
96     int ret = 0;
97
98     if (out == NULL)
99         return (0);
100     if (b->next_bio == NULL)
101         return (0);
102     ret = BIO_read(b->next_bio, out, outl);
103     BIO_clear_retry_flags(b);
104     BIO_copy_next_retry(b);
105     return (ret);
106 }
107
108 static int linebuffer_write(BIO *b, const char *in, int inl)
109 {
110     int i, num = 0, foundnl;
111     BIO_LINEBUFFER_CTX *ctx;
112
113     if ((in == NULL) || (inl <= 0))
114         return (0);
115     ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
116     if ((ctx == NULL) || (b->next_bio == NULL))
117         return (0);
118
119     BIO_clear_retry_flags(b);
120
121     do {
122         const char *p;
123
124         for (p = in; p < in + inl && *p != '\n'; p++) ;
125         if (*p == '\n') {
126             p++;
127             foundnl = 1;
128         } else
129             foundnl = 0;
130
131         /*
132          * If a NL was found and we already have text in the save buffer,
133          * concatenate them and write
134          */
135         while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
136                && ctx->obuf_len > 0) {
137             int orig_olen = ctx->obuf_len;
138
139             i = ctx->obuf_size - ctx->obuf_len;
140             if (p - in > 0) {
141                 if (i >= p - in) {
142                     memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
143                     ctx->obuf_len += p - in;
144                     inl -= p - in;
145                     num += p - in;
146                     in = p;
147                 } else {
148                     memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
149                     ctx->obuf_len += i;
150                     inl -= i;
151                     in += i;
152                     num += i;
153                 }
154             }
155             i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
156             if (i <= 0) {
157                 ctx->obuf_len = orig_olen;
158                 BIO_copy_next_retry(b);
159
160                 if (i < 0)
161                     return ((num > 0) ? num : i);
162                 if (i == 0)
163                     return (num);
164             }
165             if (i < ctx->obuf_len)
166                 memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
167             ctx->obuf_len -= i;
168         }
169
170         /*
171          * Now that the save buffer is emptied, let's write the input buffer
172          * if a NL was found and there is anything to write.
173          */
174         if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
175             i = BIO_write(b->next_bio, in, p - in);
176             if (i <= 0) {
177                 BIO_copy_next_retry(b);
178                 if (i < 0)
179                     return ((num > 0) ? num : i);
180                 if (i == 0)
181                     return (num);
182             }
183             num += i;
184             in += i;
185             inl -= i;
186         }
187     }
188     while (foundnl && inl > 0);
189     /*
190      * We've written as much as we can.  The rest of the input buffer, if
191      * any, is text that doesn't and with a NL and therefore needs to be
192      * saved for the next trip.
193      */
194     if (inl > 0) {
195         memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
196         ctx->obuf_len += inl;
197         num += inl;
198     }
199     return num;
200 }
201
202 static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
203 {
204     BIO *dbio;
205     BIO_LINEBUFFER_CTX *ctx;
206     long ret = 1;
207     char *p;
208     int r;
209     int obs;
210
211     ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
212
213     switch (cmd) {
214     case BIO_CTRL_RESET:
215         ctx->obuf_len = 0;
216         if (b->next_bio == NULL)
217             return (0);
218         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
219         break;
220     case BIO_CTRL_INFO:
221         ret = (long)ctx->obuf_len;
222         break;
223     case BIO_CTRL_WPENDING:
224         ret = (long)ctx->obuf_len;
225         if (ret == 0) {
226             if (b->next_bio == NULL)
227                 return (0);
228             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
229         }
230         break;
231     case BIO_C_SET_BUFF_SIZE:
232         obs = (int)num;
233         p = ctx->obuf;
234         if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
235             p = OPENSSL_malloc((int)num);
236             if (p == NULL)
237                 goto malloc_error;
238         }
239         if (ctx->obuf != p) {
240             if (ctx->obuf_len > obs) {
241                 ctx->obuf_len = obs;
242             }
243             memcpy(p, ctx->obuf, ctx->obuf_len);
244             OPENSSL_free(ctx->obuf);
245             ctx->obuf = p;
246             ctx->obuf_size = obs;
247         }
248         break;
249     case BIO_C_DO_STATE_MACHINE:
250         if (b->next_bio == NULL)
251             return (0);
252         BIO_clear_retry_flags(b);
253         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
254         BIO_copy_next_retry(b);
255         break;
256
257     case BIO_CTRL_FLUSH:
258         if (b->next_bio == NULL)
259             return (0);
260         if (ctx->obuf_len <= 0) {
261             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
262             break;
263         }
264
265         for (;;) {
266             BIO_clear_retry_flags(b);
267             if (ctx->obuf_len > 0) {
268                 r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
269                 BIO_copy_next_retry(b);
270                 if (r <= 0)
271                     return ((long)r);
272                 if (r < ctx->obuf_len)
273                     memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
274                 ctx->obuf_len -= r;
275             } else {
276                 ctx->obuf_len = 0;
277                 break;
278             }
279         }
280         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
281         break;
282     case BIO_CTRL_DUP:
283         dbio = (BIO *)ptr;
284         if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
285             ret = 0;
286         break;
287     default:
288         if (b->next_bio == NULL)
289             return (0);
290         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
291         break;
292     }
293     return (ret);
294  malloc_error:
295     BIOerr(BIO_F_LINEBUFFER_CTRL, ERR_R_MALLOC_FAILURE);
296     return (0);
297 }
298
299 static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
300 {
301     long ret = 1;
302
303     if (b->next_bio == NULL)
304         return (0);
305     switch (cmd) {
306     default:
307         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
308         break;
309     }
310     return (ret);
311 }
312
313 static int linebuffer_gets(BIO *b, char *buf, int size)
314 {
315     if (b->next_bio == NULL)
316         return (0);
317     return (BIO_gets(b->next_bio, buf, size));
318 }
319
320 static int linebuffer_puts(BIO *b, const char *str)
321 {
322     return (linebuffer_write(b, str, strlen(str)));
323 }