Make sure to NUL-terminate the string on end-of-file (and error)
[openssl.git] / crypto / bio / bf_buff.c
index ac3ba14..c1fd75a 100644 (file)
@@ -1,5 +1,5 @@
 /* crypto/bio/bf_buff.c */
 /* crypto/bio/bf_buff.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * All rights reserved.
  *
  * This package is an SSL implementation written
 #include <stdio.h>
 #include <errno.h>
 #include "cryptlib.h"
 #include <stdio.h>
 #include <errno.h>
 #include "cryptlib.h"
-#include "bio.h"
-#include "evp.h"
-
-#ifndef NOPROTO
-static int buffer_write(BIO *h,char *buf,int num);
-static int buffer_read(BIO *h,char *buf,int size);
-static int buffer_puts(BIO *h,char *str);
-static int buffer_gets(BIO *h,char *str,int size);
-static long buffer_ctrl(BIO *h,int cmd,long arg1,char *arg2);
+#include <openssl/bio.h>
+
+static int buffer_write(BIO *h, const char *buf,int num);
+static int buffer_read(BIO *h, char *buf, int size);
+static int buffer_puts(BIO *h, const char *str);
+static int buffer_gets(BIO *h, char *str, int size);
+static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
 static int buffer_new(BIO *h);
 static int buffer_free(BIO *data);
 static int buffer_new(BIO *h);
 static int buffer_free(BIO *data);
-#else
-static int buffer_write();
-static int buffer_read();
-static int buffer_puts();
-static int buffer_gets();
-static long buffer_ctrl();
-static int buffer_new();
-static int buffer_free();
-#endif
-
-#define DEFAULT_BUFFER_SIZE    1024
+static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
+#define DEFAULT_BUFFER_SIZE    4096
 
 static BIO_METHOD methods_buffer=
        {
 
 static BIO_METHOD methods_buffer=
        {
-       BIO_TYPE_BUFFER,"buffer",
+       BIO_TYPE_BUFFER,
+       "buffer",
        buffer_write,
        buffer_read,
        buffer_puts,
        buffer_write,
        buffer_read,
        buffer_puts,
@@ -92,24 +82,24 @@ static BIO_METHOD methods_buffer=
        buffer_ctrl,
        buffer_new,
        buffer_free,
        buffer_ctrl,
        buffer_new,
        buffer_free,
+       buffer_callback_ctrl,
        };
 
        };
 
-BIO_METHOD *BIO_f_buffer()
+BIO_METHOD *BIO_f_buffer(void)
        {
        return(&methods_buffer);
        }
 
        {
        return(&methods_buffer);
        }
 
-static int buffer_new(bi)
-BIO *bi;
+static int buffer_new(BIO *bi)
        {
        BIO_F_BUFFER_CTX *ctx;
 
        {
        BIO_F_BUFFER_CTX *ctx;
 
-       ctx=(BIO_F_BUFFER_CTX *)Malloc(sizeof(BIO_F_BUFFER_CTX));
+       ctx=(BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
        if (ctx == NULL) return(0);
        if (ctx == NULL) return(0);
-       ctx->ibuf=(char *)Malloc(DEFAULT_BUFFER_SIZE);
-       if (ctx->ibuf == NULL) { Free(ctx); return(0); }
-       ctx->obuf=(char *)Malloc(DEFAULT_BUFFER_SIZE);
-       if (ctx->obuf == NULL) { Free(ctx->ibuf); Free(ctx); return(0); }
+       ctx->ibuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
+       if (ctx->ibuf == NULL) { OPENSSL_free(ctx); return(0); }
+       ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
+       if (ctx->obuf == NULL) { OPENSSL_free(ctx->ibuf); OPENSSL_free(ctx); return(0); }
        ctx->ibuf_size=DEFAULT_BUFFER_SIZE;
        ctx->obuf_size=DEFAULT_BUFFER_SIZE;
        ctx->ibuf_len=0;
        ctx->ibuf_size=DEFAULT_BUFFER_SIZE;
        ctx->obuf_size=DEFAULT_BUFFER_SIZE;
        ctx->ibuf_len=0;
@@ -123,26 +113,22 @@ BIO *bi;
        return(1);
        }
 
        return(1);
        }
 
-static int buffer_free(a)
-BIO *a;
+static int buffer_free(BIO *a)
        {
        BIO_F_BUFFER_CTX *b;
 
        if (a == NULL) return(0);
        b=(BIO_F_BUFFER_CTX *)a->ptr;
        {
        BIO_F_BUFFER_CTX *b;
 
        if (a == NULL) return(0);
        b=(BIO_F_BUFFER_CTX *)a->ptr;
-       if (b->ibuf != NULL) Free(b->ibuf);
-       if (b->obuf != NULL) Free(b->obuf);
-       Free(a->ptr);
+       if (b->ibuf != NULL) OPENSSL_free(b->ibuf);
+       if (b->obuf != NULL) OPENSSL_free(b->obuf);
+       OPENSSL_free(a->ptr);
        a->ptr=NULL;
        a->init=0;
        a->flags=0;
        return(1);
        }
        
        a->ptr=NULL;
        a->init=0;
        a->flags=0;
        return(1);
        }
        
-static int buffer_read(b,out,outl)
-BIO *b;
-char *out;
-int outl;
+static int buffer_read(BIO *b, char *out, int outl)
        {
        int i,num=0;
        BIO_F_BUFFER_CTX *ctx;
        {
        int i,num=0;
        BIO_F_BUFFER_CTX *ctx;
@@ -208,10 +194,7 @@ start:
        goto start;
        }
 
        goto start;
        }
 
-static int buffer_write(b,in,inl)
-BIO *b;
-char *in;
-int inl;
+static int buffer_write(BIO *b, const char *in, int inl)
        {
        int i,num=0;
        BIO_F_BUFFER_CTX *ctx;
        {
        int i,num=0;
        BIO_F_BUFFER_CTX *ctx;
@@ -284,11 +267,7 @@ start:
        goto start;
        }
 
        goto start;
        }
 
-static long buffer_ctrl(b,cmd,num,ptr)
-BIO *b;
-int cmd;
-long num;
-char *ptr;
+static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
        {
        BIO *dbio;
        BIO_F_BUFFER_CTX *ctx;
        {
        BIO *dbio;
        BIO_F_BUFFER_CTX *ctx;
@@ -306,6 +285,7 @@ char *ptr;
                ctx->ibuf_len=0;
                ctx->obuf_off=0;
                ctx->obuf_len=0;
                ctx->ibuf_len=0;
                ctx->obuf_off=0;
                ctx->obuf_len=0;
+               if (b->next_bio == NULL) return(0);
                ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                break;
        case BIO_CTRL_INFO:
                ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                break;
        case BIO_CTRL_INFO:
@@ -322,12 +302,31 @@ char *ptr;
        case BIO_CTRL_WPENDING:
                ret=(long)ctx->obuf_len;
                if (ret == 0)
        case BIO_CTRL_WPENDING:
                ret=(long)ctx->obuf_len;
                if (ret == 0)
+                       {
+                       if (b->next_bio == NULL) return(0);
                        ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                        ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+                       }
                break;
        case BIO_CTRL_PENDING:
                ret=(long)ctx->ibuf_len;
                if (ret == 0)
                break;
        case BIO_CTRL_PENDING:
                ret=(long)ctx->ibuf_len;
                if (ret == 0)
+                       {
+                       if (b->next_bio == NULL) return(0);
                        ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                        ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
+                       }
+               break;
+       case BIO_C_SET_BUFF_READ_DATA:
+               if (num > ctx->ibuf_size)
+                       {
+                       p1=OPENSSL_malloc((int)num);
+                       if (p1 == NULL) goto malloc_error;
+                       if (ctx->ibuf != NULL) OPENSSL_free(ctx->ibuf);
+                       ctx->ibuf=p1;
+                       }
+               ctx->ibuf_off=0;
+               ctx->ibuf_len=(int)num;
+               memcpy(ctx->ibuf,ptr,(int)num);
+               ret=1;
                break;
        case BIO_C_SET_BUFF_SIZE:
                if (ptr != NULL)
                break;
        case BIO_C_SET_BUFF_SIZE:
                if (ptr != NULL)
@@ -353,22 +352,21 @@ char *ptr;
                p2=ctx->obuf;
                if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size))
                        {
                p2=ctx->obuf;
                if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size))
                        {
-                       p1=(char *)Malloc((int)num);
-                       if (p1 == NULL) { ret=0; break; }
+                       p1=(char *)OPENSSL_malloc((int)num);
+                       if (p1 == NULL) goto malloc_error;
                        }
                if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size))
                        {
                        }
                if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size))
                        {
-                       p2=(char *)Malloc((int)num);
+                       p2=(char *)OPENSSL_malloc((int)num);
                        if (p2 == NULL)
                                {
                        if (p2 == NULL)
                                {
-                               ret=0;
-                               if (p1 != ctx->ibuf) Free(p1);
-                               break;
+                               if (p1 != ctx->ibuf) OPENSSL_free(p1);
+                               goto malloc_error;
                                }
                        }
                if (ctx->ibuf != p1)
                        {
                                }
                        }
                if (ctx->ibuf != p1)
                        {
-                       Free(ctx->ibuf);
+                       OPENSSL_free(ctx->ibuf);
                        ctx->ibuf=p1;
                        ctx->ibuf_off=0;
                        ctx->ibuf_len=0;
                        ctx->ibuf=p1;
                        ctx->ibuf_off=0;
                        ctx->ibuf_len=0;
@@ -376,7 +374,7 @@ char *ptr;
                        }
                if (ctx->obuf != p2)
                        {
                        }
                if (ctx->obuf != p2)
                        {
-                       Free(ctx->obuf);
+                       OPENSSL_free(ctx->obuf);
                        ctx->obuf=p2;
                        ctx->obuf_off=0;
                        ctx->obuf_len=0;
                        ctx->obuf=p2;
                        ctx->obuf_off=0;
                        ctx->obuf_len=0;
@@ -384,14 +382,19 @@ char *ptr;
                        }
                break;
        case BIO_C_DO_STATE_MACHINE:
                        }
                break;
        case BIO_C_DO_STATE_MACHINE:
+               if (b->next_bio == NULL) return(0);
                BIO_clear_retry_flags(b);
                ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                BIO_copy_next_retry(b);
                break;
 
        case BIO_CTRL_FLUSH:
                BIO_clear_retry_flags(b);
                ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                BIO_copy_next_retry(b);
                break;
 
        case BIO_CTRL_FLUSH:
+               if (b->next_bio == NULL) return(0);
                if (ctx->obuf_len <= 0)
                if (ctx->obuf_len <= 0)
+                       {
+                       ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                        break;
                        break;
+                       }
 
                for (;;)
                        {
 
                for (;;)
                        {
@@ -416,6 +419,7 @@ fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_
                                break;
                                }
                        }
                                break;
                                }
                        }
+               ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                break;
        case BIO_CTRL_DUP:
                dbio=(BIO *)ptr;
                break;
        case BIO_CTRL_DUP:
                dbio=(BIO *)ptr;
@@ -424,44 +428,63 @@ fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_
                        ret=0;
                break;
        default:
                        ret=0;
                break;
        default:
+               if (b->next_bio == NULL) return(0);
                ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                break;
                }
        return(ret);
                ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                break;
                }
        return(ret);
+malloc_error:
+       BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE);
+       return(0);
+       }
+
+static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+       {
+       long ret=1;
+
+       if (b->next_bio == NULL) return(0);
+       switch (cmd)
+               {
+       default:
+               ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
+               break;
+               }
+       return(ret);
        }
 
        }
 
-static int buffer_gets(b,buf,size)
-BIO *b;
-char *buf;
-int size;
+static int buffer_gets(BIO *b, char *buf, int size)
        {
        BIO_F_BUFFER_CTX *ctx;
        {
        BIO_F_BUFFER_CTX *ctx;
-       int num=0,i;
+       int num=0,i,flag;
        char *p;
 
        ctx=(BIO_F_BUFFER_CTX *)b->ptr;
        char *p;
 
        ctx=(BIO_F_BUFFER_CTX *)b->ptr;
-       size--;
+       size--; /* reserve space for a '\0' */
        BIO_clear_retry_flags(b);
 
        for (;;)
                {
        BIO_clear_retry_flags(b);
 
        for (;;)
                {
-               if (ctx->ibuf_len != 0)
+               if (ctx->ibuf_len > 0)
                        {
                        p= &(ctx->ibuf[ctx->ibuf_off]);
                        {
                        p= &(ctx->ibuf[ctx->ibuf_off]);
-                       for (i=0; (i<ctx->ibuf_len) && (i<(size-1)); i++)
+                       flag=0;
+                       for (i=0; (i<ctx->ibuf_len) && (i<size); i++)
                                {
                                *(buf++)=p[i];
                                {
                                *(buf++)=p[i];
-                               if (p[i] == '\n') break;
+                               if (p[i] == '\n')
+                                       {
+                                       flag=1;
+                                       i++;
+                                       break;
+                                       }
                                }
                        num+=i;
                        size-=i;
                        ctx->ibuf_len-=i;
                        ctx->ibuf_off+=i;
                                }
                        num+=i;
                        size-=i;
                        ctx->ibuf_len-=i;
                        ctx->ibuf_off+=i;
-                       if (p[i] == '\n')
+                       if (flag || size == 0)
                                {
                                {
-                               buf[i+1]='\0';
-                               ctx->ibuf_len--;
-                               ctx->ibuf_off++;
+                               *buf='\0';
                                return(num);
                                }
                        }
                                return(num);
                                }
                        }
@@ -471,6 +494,7 @@ int size;
                        if (i <= 0)
                                {
                                BIO_copy_next_retry(b);
                        if (i <= 0)
                                {
                                BIO_copy_next_retry(b);
+                               *buf='\0';
                                if (i < 0) return((num > 0)?num:i);
                                if (i == 0) return(num);
                                }
                                if (i < 0) return((num > 0)?num:i);
                                if (i == 0) return(num);
                                }
@@ -480,10 +504,8 @@ int size;
                }
        }
 
                }
        }
 
-static int buffer_puts(b,str)
-BIO *b;
-char *str;
+static int buffer_puts(BIO *b, const char *str)
        {
        {
-       return(BIO_write(b,str,strlen(str)));
+       return(buffer_write(b,str,strlen(str)));
        }
 
        }