summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio/bf_lbuf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bio/bf_lbuf.c')
-rw-r--r--src/lib/libcrypto/bio/bf_lbuf.c378
1 files changed, 190 insertions, 188 deletions
diff --git a/src/lib/libcrypto/bio/bf_lbuf.c b/src/lib/libcrypto/bio/bf_lbuf.c
index ec0f7eb0b7..838839f1bf 100644
--- a/src/lib/libcrypto/bio/bf_lbuf.c
+++ b/src/lib/libcrypto/bio/bf_lbuf.c
@@ -62,7 +62,7 @@
62#include <openssl/bio.h> 62#include <openssl/bio.h>
63#include <openssl/evp.h> 63#include <openssl/evp.h>
64 64
65static int linebuffer_write(BIO *h, const char *buf,int num); 65static int linebuffer_write(BIO *h, const char *buf, int num);
66static int linebuffer_read(BIO *h, char *buf, int size); 66static int linebuffer_read(BIO *h, char *buf, int size);
67static int linebuffer_puts(BIO *h, const char *str); 67static int linebuffer_puts(BIO *h, const char *str);
68static int linebuffer_gets(BIO *h, char *str, int size); 68static int linebuffer_gets(BIO *h, char *str, int size);
@@ -76,8 +76,7 @@ static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
76 76
77/* #define DEBUG */ 77/* #define DEBUG */
78 78
79static BIO_METHOD methods_linebuffer= 79static BIO_METHOD methods_linebuffer = {
80 {
81 BIO_TYPE_LINEBUFFER, 80 BIO_TYPE_LINEBUFFER,
82 "linebuffer", 81 "linebuffer",
83 linebuffer_write, 82 linebuffer_write,
@@ -88,310 +87,313 @@ static BIO_METHOD methods_linebuffer=
88 linebuffer_new, 87 linebuffer_new,
89 linebuffer_free, 88 linebuffer_free,
90 linebuffer_callback_ctrl, 89 linebuffer_callback_ctrl,
91 }; 90};
92 91
93BIO_METHOD *BIO_f_linebuffer(void) 92BIO_METHOD
94 { 93*BIO_f_linebuffer(void)
95 return(&methods_linebuffer); 94{
96 } 95 return (&methods_linebuffer);
96}
97 97
98typedef struct bio_linebuffer_ctx_struct 98typedef struct bio_linebuffer_ctx_struct {
99 {
100 char *obuf; /* the output char array */ 99 char *obuf; /* the output char array */
101 int obuf_size; /* how big is the output buffer */ 100 int obuf_size; /* how big is the output buffer */
102 int obuf_len; /* how many bytes are in it */ 101 int obuf_len; /* how many bytes are in it */
103 } BIO_LINEBUFFER_CTX; 102} BIO_LINEBUFFER_CTX;
104 103
105static int linebuffer_new(BIO *bi) 104static int
106 { 105linebuffer_new(BIO *bi)
106{
107 BIO_LINEBUFFER_CTX *ctx; 107 BIO_LINEBUFFER_CTX *ctx;
108 108
109 ctx=(BIO_LINEBUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_LINEBUFFER_CTX)); 109 ctx = (BIO_LINEBUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_LINEBUFFER_CTX));
110 if (ctx == NULL) return(0); 110 if (ctx == NULL)
111 ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE); 111 return (0);
112 if (ctx->obuf == NULL) { OPENSSL_free(ctx); return(0); } 112 ctx->obuf = (char *)OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
113 ctx->obuf_size=DEFAULT_LINEBUFFER_SIZE; 113 if (ctx->obuf == NULL) {
114 ctx->obuf_len=0; 114 OPENSSL_free(ctx);
115 115 return (0);
116 bi->init=1;
117 bi->ptr=(char *)ctx;
118 bi->flags=0;
119 return(1);
120 } 116 }
117 ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
118 ctx->obuf_len = 0;
119
120 bi->init = 1;
121 bi->ptr = (char *)ctx;
122 bi->flags = 0;
123 return (1);
124}
121 125
122static int linebuffer_free(BIO *a) 126static int
123 { 127linebuffer_free(BIO *a)
128{
124 BIO_LINEBUFFER_CTX *b; 129 BIO_LINEBUFFER_CTX *b;
125 130
126 if (a == NULL) return(0); 131 if (a == NULL)
127 b=(BIO_LINEBUFFER_CTX *)a->ptr; 132 return (0);
128 if (b->obuf != NULL) OPENSSL_free(b->obuf); 133 b = (BIO_LINEBUFFER_CTX *)a->ptr;
134 if (b->obuf != NULL)
135 OPENSSL_free(b->obuf);
129 OPENSSL_free(a->ptr); 136 OPENSSL_free(a->ptr);
130 a->ptr=NULL; 137 a->ptr = NULL;
131 a->init=0; 138 a->init = 0;
132 a->flags=0; 139 a->flags = 0;
133 return(1); 140 return (1);
134 } 141}
135 142
136static int linebuffer_read(BIO *b, char *out, int outl) 143static int
137 { 144linebuffer_read(BIO *b, char *out, int outl)
138 int ret=0; 145{
139 146 int ret = 0;
140 if (out == NULL) return(0); 147
141 if (b->next_bio == NULL) return(0); 148 if (out == NULL)
142 ret=BIO_read(b->next_bio,out,outl); 149 return (0);
150 if (b->next_bio == NULL)
151 return (0);
152 ret = BIO_read(b->next_bio, out, outl);
143 BIO_clear_retry_flags(b); 153 BIO_clear_retry_flags(b);
144 BIO_copy_next_retry(b); 154 BIO_copy_next_retry(b);
145 return(ret); 155 return (ret);
146 } 156}
147 157
148static int linebuffer_write(BIO *b, const char *in, int inl) 158static int
149 { 159linebuffer_write(BIO *b, const char *in, int inl)
150 int i,num=0,foundnl; 160{
161 int i, num = 0, foundnl;
151 BIO_LINEBUFFER_CTX *ctx; 162 BIO_LINEBUFFER_CTX *ctx;
152 163
153 if ((in == NULL) || (inl <= 0)) return(0); 164 if ((in == NULL) || (inl <= 0))
154 ctx=(BIO_LINEBUFFER_CTX *)b->ptr; 165 return (0);
155 if ((ctx == NULL) || (b->next_bio == NULL)) return(0); 166 ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
167 if ((ctx == NULL) || (b->next_bio == NULL))
168 return (0);
156 169
157 BIO_clear_retry_flags(b); 170 BIO_clear_retry_flags(b);
158 171
159 do 172 do {
160 {
161 const char *p; 173 const char *p;
162 174
163 for(p = in; p < in + inl && *p != '\n'; p++) 175 for (p = in; p < in + inl && *p != '\n'; p++)
164 ; 176 ;
165 if (*p == '\n') 177 if (*p == '\n') {
166 {
167 p++; 178 p++;
168 foundnl = 1; 179 foundnl = 1;
169 } 180 } else
170 else
171 foundnl = 0; 181 foundnl = 0;
172 182
173 /* If a NL was found and we already have text in the save 183 /* If a NL was found and we already have text in the save
174 buffer, concatenate them and write */ 184 buffer, concatenate them and write */
175 while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len) 185 while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len) &&
176 && ctx->obuf_len > 0) 186 ctx->obuf_len > 0) {
177 {
178 int orig_olen = ctx->obuf_len; 187 int orig_olen = ctx->obuf_len;
179 188
180 i = ctx->obuf_size - ctx->obuf_len; 189 i = ctx->obuf_size - ctx->obuf_len;
181 if (p - in > 0) 190 if (p - in > 0) {
182 { 191 if (i >= p - in) {
183 if (i >= p - in)
184 {
185 memcpy(&(ctx->obuf[ctx->obuf_len]), 192 memcpy(&(ctx->obuf[ctx->obuf_len]),
186 in,p - in); 193 in, p - in);
187 ctx->obuf_len += p - in; 194 ctx->obuf_len += p - in;
188 inl -= p - in; 195 inl -= p - in;
189 num += p - in; 196 num += p - in;
190 in = p; 197 in = p;
191 } 198 } else {
192 else
193 {
194 memcpy(&(ctx->obuf[ctx->obuf_len]), 199 memcpy(&(ctx->obuf[ctx->obuf_len]),
195 in,i); 200 in, i);
196 ctx->obuf_len += i; 201 ctx->obuf_len += i;
197 inl -= i; 202 inl -= i;
198 in += i; 203 in += i;
199 num += i; 204 num += i;
200 }
201 } 205 }
206 }
202 207
203#if 0 208#if 0
204BIO_write(b->next_bio, "<*<", 3); 209 BIO_write(b->next_bio, "<*<", 3);
205#endif 210#endif
206 i=BIO_write(b->next_bio, 211 i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
207 ctx->obuf, ctx->obuf_len); 212 if (i <= 0) {
208 if (i <= 0)
209 {
210 ctx->obuf_len = orig_olen; 213 ctx->obuf_len = orig_olen;
211 BIO_copy_next_retry(b); 214 BIO_copy_next_retry(b);
212
213#if 0 215#if 0
214BIO_write(b->next_bio, ">*>", 3); 216 BIO_write(b->next_bio, ">*>", 3);
215#endif 217#endif
216 if (i < 0) return((num > 0)?num:i); 218 if (i < 0)
217 if (i == 0) return(num); 219 return ((num > 0) ? num : i);
218 } 220 if (i == 0)
221 return (num);
222 }
219#if 0 223#if 0
220BIO_write(b->next_bio, ">*>", 3); 224 BIO_write(b->next_bio, ">*>", 3);
221#endif 225#endif
222 if (i < ctx->obuf_len) 226 if (i < ctx->obuf_len)
223 memmove(ctx->obuf, ctx->obuf + i, 227 memmove(ctx->obuf, ctx->obuf + i,
224 ctx->obuf_len - i); 228 ctx->obuf_len - i);
225 ctx->obuf_len-=i; 229 ctx->obuf_len -= i;
226 } 230 }
227 231
228 /* Now that the save buffer is emptied, let's write the input 232 /* Now that the save buffer is emptied, let's write the input
229 buffer if a NL was found and there is anything to write. */ 233 buffer if a NL was found and there is anything to write. */
230 if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) 234 if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
231 {
232#if 0 235#if 0
233BIO_write(b->next_bio, "<*<", 3); 236 BIO_write(b->next_bio, "<*<", 3);
234#endif 237#endif
235 i=BIO_write(b->next_bio,in,p - in); 238 i = BIO_write(b->next_bio, in, p - in);
236 if (i <= 0) 239 if (i <= 0) {
237 {
238 BIO_copy_next_retry(b); 240 BIO_copy_next_retry(b);
239#if 0 241#if 0
240BIO_write(b->next_bio, ">*>", 3); 242 BIO_write(b->next_bio, ">*>", 3);
241#endif 243#endif
242 if (i < 0) return((num > 0)?num:i); 244 if (i < 0)
243 if (i == 0) return(num); 245 return ((num > 0) ? num : i);
244 } 246 if (i == 0)
247 return (num);
248 }
245#if 0 249#if 0
246BIO_write(b->next_bio, ">*>", 3); 250 BIO_write(b->next_bio, ">*>", 3);
247#endif 251#endif
248 num+=i; 252 num += i;
249 in+=i; 253 in += i;
250 inl-=i; 254 inl -= i;
251 }
252 } 255 }
253 while(foundnl && inl > 0); 256 } while (foundnl && inl > 0);
254 /* We've written as much as we can. The rest of the input buffer, if 257 /* We've written as much as we can. The rest of the input buffer, if
255 any, is text that doesn't and with a NL and therefore needs to be 258 any, is text that doesn't and with a NL and therefore needs to be
256 saved for the next trip. */ 259 saved for the next trip. */
257 if (inl > 0) 260 if (inl > 0) {
258 {
259 memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl); 261 memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
260 ctx->obuf_len += inl; 262 ctx->obuf_len += inl;
261 num += inl; 263 num += inl;
262 }
263 return num;
264 } 264 }
265 return num;
266}
265 267
266static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr) 268static long
267 { 269linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
270{
268 BIO *dbio; 271 BIO *dbio;
269 BIO_LINEBUFFER_CTX *ctx; 272 BIO_LINEBUFFER_CTX *ctx;
270 long ret=1; 273 long ret = 1;
271 char *p; 274 char *p;
272 int r; 275 int r;
273 int obs; 276 int obs;
274 277
275 ctx=(BIO_LINEBUFFER_CTX *)b->ptr; 278 ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
276 279
277 switch (cmd) 280 switch (cmd) {
278 {
279 case BIO_CTRL_RESET: 281 case BIO_CTRL_RESET:
280 ctx->obuf_len=0; 282 ctx->obuf_len = 0;
281 if (b->next_bio == NULL) return(0); 283 if (b->next_bio == NULL)
282 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 284 return (0);
285 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
283 break; 286 break;
284 case BIO_CTRL_INFO: 287 case BIO_CTRL_INFO:
285 ret=(long)ctx->obuf_len; 288 ret = (long)ctx->obuf_len;
286 break; 289 break;
287 case BIO_CTRL_WPENDING: 290 case BIO_CTRL_WPENDING:
288 ret=(long)ctx->obuf_len; 291 ret = (long)ctx->obuf_len;
289 if (ret == 0) 292 if (ret == 0) {
290 { 293 if (b->next_bio == NULL)
291 if (b->next_bio == NULL) return(0); 294 return (0);
292 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 295 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
293 } 296 }
294 break; 297 break;
295 case BIO_C_SET_BUFF_SIZE: 298 case BIO_C_SET_BUFF_SIZE:
296 obs=(int)num; 299 obs = (int)num;
297 p=ctx->obuf; 300 p = ctx->obuf;
298 if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) 301 if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
299 { 302 p = (char *)OPENSSL_malloc((int)num);
300 p=(char *)OPENSSL_malloc((int)num);
301 if (p == NULL) 303 if (p == NULL)
302 goto malloc_error; 304 goto malloc_error;
303 } 305 }
304 if (ctx->obuf != p) 306 if (ctx->obuf != p) {
305 { 307 if (ctx->obuf_len > obs) {
306 if (ctx->obuf_len > obs)
307 {
308 ctx->obuf_len = obs; 308 ctx->obuf_len = obs;
309 } 309 }
310 memcpy(p, ctx->obuf, ctx->obuf_len); 310 memcpy(p, ctx->obuf, ctx->obuf_len);
311 OPENSSL_free(ctx->obuf); 311 OPENSSL_free(ctx->obuf);
312 ctx->obuf=p; 312 ctx->obuf = p;
313 ctx->obuf_size=obs; 313 ctx->obuf_size = obs;
314 } 314 }
315 break; 315 break;
316 case BIO_C_DO_STATE_MACHINE: 316 case BIO_C_DO_STATE_MACHINE:
317 if (b->next_bio == NULL) return(0); 317 if (b->next_bio == NULL)
318 return (0);
318 BIO_clear_retry_flags(b); 319 BIO_clear_retry_flags(b);
319 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 320 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
320 BIO_copy_next_retry(b); 321 BIO_copy_next_retry(b);
321 break; 322 break;
322 323
323 case BIO_CTRL_FLUSH: 324 case BIO_CTRL_FLUSH:
324 if (b->next_bio == NULL) return(0); 325 if (b->next_bio == NULL)
325 if (ctx->obuf_len <= 0) 326 return (0);
326 { 327 if (ctx->obuf_len <= 0) {
327 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 328 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
328 break; 329 break;
329 } 330 }
330 331
331 for (;;) 332 for (;;) {
332 {
333 BIO_clear_retry_flags(b); 333 BIO_clear_retry_flags(b);
334 if (ctx->obuf_len > 0) 334 if (ctx->obuf_len > 0) {
335 { 335 r = BIO_write(b->next_bio,
336 r=BIO_write(b->next_bio, 336 ctx->obuf, ctx->obuf_len);
337 ctx->obuf, ctx->obuf_len);
338#if 0 337#if 0
339fprintf(stderr,"FLUSH %3d -> %3d\n",ctx->obuf_len,r); 338 fprintf(stderr, "FLUSH %3d -> %3d\n", ctx->obuf_len, r);
340#endif 339#endif
341 BIO_copy_next_retry(b); 340 BIO_copy_next_retry(b);
342 if (r <= 0) return((long)r); 341 if (r <= 0)
342 return ((long)r);
343 if (r < ctx->obuf_len) 343 if (r < ctx->obuf_len)
344 memmove(ctx->obuf, ctx->obuf + r, 344 memmove(ctx->obuf, ctx->obuf + r,
345 ctx->obuf_len - r); 345 ctx->obuf_len - r);
346 ctx->obuf_len-=r; 346 ctx->obuf_len -= r;
347 } 347 } else {
348 else 348 ctx->obuf_len = 0;
349 { 349 ret = 1;
350 ctx->obuf_len=0;
351 ret=1;
352 break; 350 break;
353 }
354 } 351 }
355 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 352 }
353 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
356 break; 354 break;
357 case BIO_CTRL_DUP: 355 case BIO_CTRL_DUP:
358 dbio=(BIO *)ptr; 356 dbio = (BIO *)ptr;
359 if ( !BIO_set_write_buffer_size(dbio,ctx->obuf_size)) 357 if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
360 ret=0; 358 ret = 0;
361 break; 359 break;
362 default: 360 default:
363 if (b->next_bio == NULL) return(0); 361 if (b->next_bio == NULL)
364 ret=BIO_ctrl(b->next_bio,cmd,num,ptr); 362 return (0);
363 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
365 break; 364 break;
366 }
367 return(ret);
368malloc_error:
369 BIOerr(BIO_F_LINEBUFFER_CTRL,ERR_R_MALLOC_FAILURE);
370 return(0);
371 } 365 }
366 return (ret);
367malloc_error:
368 BIOerr(BIO_F_LINEBUFFER_CTRL, ERR_R_MALLOC_FAILURE);
369 return (0);
370}
372 371
373static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 372static long
374 { 373linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
375 long ret=1; 374{
375 long ret = 1;
376 376
377 if (b->next_bio == NULL) return(0); 377 if (b->next_bio == NULL)
378 switch (cmd) 378 return (0);
379 { 379 switch (cmd) {
380 default: 380 default:
381 ret=BIO_callback_ctrl(b->next_bio,cmd,fp); 381 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
382 break; 382 break;
383 }
384 return(ret);
385 } 383 }
384 return (ret);
385}
386 386
387static int linebuffer_gets(BIO *b, char *buf, int size) 387static int
388 { 388linebuffer_gets(BIO *b, char *buf, int size)
389 if (b->next_bio == NULL) return(0); 389{
390 return(BIO_gets(b->next_bio,buf,size)); 390 if (b->next_bio == NULL)
391 } 391 return (0);
392 392 return (BIO_gets(b->next_bio, buf, size));
393static int linebuffer_puts(BIO *b, const char *str) 393}
394 {
395 return(linebuffer_write(b,str,strlen(str)));
396 }
397 394
395static int
396linebuffer_puts(BIO *b, const char *str)
397{
398 return (linebuffer_write(b, str, strlen(str)));
399}