improve readability of #if conditions (ELF, if defined, supersedes BSDI
[openssl.git] / crypto / bio / bf_buff.c
index ac3ba14fe064669989353262d840cd9bb24f34dc..acd81481389247a8790e628effc9a9f55e7eebc2 100644 (file)
@@ -1,5 +1,5 @@
 /* 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
 #include <stdio.h>
 #include <errno.h>
 #include "cryptlib.h"
-#include "bio.h"
-#include "evp.h"
+#include <openssl/bio.h>
+#include <openssl/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);
@@ -70,21 +69,12 @@ static int buffer_gets(BIO *h,char *str,int size);
 static long buffer_ctrl(BIO *h,int cmd,long arg1,char *arg2);
 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 BIO_METHOD methods_buffer=
        {
-       BIO_TYPE_BUFFER,"buffer",
+       BIO_TYPE_BUFFER,
+       "buffer",
        buffer_write,
        buffer_read,
        buffer_puts,
@@ -94,13 +84,12 @@ static BIO_METHOD methods_buffer=
        buffer_free,
        };
 
-BIO_METHOD *BIO_f_buffer()
+BIO_METHOD *BIO_f_buffer(void)
        {
        return(&methods_buffer);
        }
 
-static int buffer_new(bi)
-BIO *bi;
+static int buffer_new(BIO *bi)
        {
        BIO_F_BUFFER_CTX *ctx;
 
@@ -123,8 +112,7 @@ BIO *bi;
        return(1);
        }
 
-static int buffer_free(a)
-BIO *a;
+static int buffer_free(BIO *a)
        {
        BIO_F_BUFFER_CTX *b;
 
@@ -139,10 +127,7 @@ BIO *a;
        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;
@@ -208,10 +193,7 @@ start:
        goto start;
        }
 
-static int buffer_write(b,in,inl)
-BIO *b;
-char *in;
-int inl;
+static int buffer_write(BIO *b, char *in, int inl)
        {
        int i,num=0;
        BIO_F_BUFFER_CTX *ctx;
@@ -284,11 +266,7 @@ 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, char *ptr)
        {
        BIO *dbio;
        BIO_F_BUFFER_CTX *ctx;
@@ -329,6 +307,19 @@ char *ptr;
                if (ret == 0)
                        ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                break;
+       case BIO_C_SET_BUFF_READ_DATA:
+               if (num > ctx->ibuf_size)
+                       {
+                       p1=Malloc((int)num);
+                       if (p1 == NULL) goto malloc_error;
+                       if (ctx->ibuf != NULL) 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)
                        {
@@ -354,16 +345,15 @@ char *ptr;
                if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size))
                        {
                        p1=(char *)Malloc((int)num);
-                       if (p1 == NULL) { ret=0; break; }
+                       if (p1 == NULL) goto malloc_error;
                        }
                if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size))
                        {
                        p2=(char *)Malloc((int)num);
                        if (p2 == NULL)
                                {
-                               ret=0;
                                if (p1 != ctx->ibuf) Free(p1);
-                               break;
+                               goto malloc_error;
                                }
                        }
                if (ctx->ibuf != p1)
@@ -391,7 +381,10 @@ char *ptr;
 
        case BIO_CTRL_FLUSH:
                if (ctx->obuf_len <= 0)
+                       {
+                       ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                        break;
+                       }
 
                for (;;)
                        {
@@ -416,6 +409,7 @@ fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_
                                break;
                                }
                        }
+               ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
                break;
        case BIO_CTRL_DUP:
                dbio=(BIO *)ptr;
@@ -428,40 +422,44 @@ fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_
                break;
                }
        return(ret);
+malloc_error:
+       BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE);
+       return(0);
        }
 
-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;
-       int num=0,i;
+       int num=0,i,flag;
        char *p;
 
        ctx=(BIO_F_BUFFER_CTX *)b->ptr;
-       size--;
+       size--; /* reserve space for a '\0' */
        BIO_clear_retry_flags(b);
 
        for (;;)
                {
-               if (ctx->ibuf_len != 0)
+               if (ctx->ibuf_len > 0)
                        {
                        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];
-                               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;
-                       if (p[i] == '\n')
+                       if ((flag) || (i == size))
                                {
-                               buf[i+1]='\0';
-                               ctx->ibuf_len--;
-                               ctx->ibuf_off++;
+                               *buf='\0';
                                return(num);
                                }
                        }
@@ -480,9 +478,7 @@ int size;
                }
        }
 
-static int buffer_puts(b,str)
-BIO *b;
-char *str;
+static int buffer_puts(BIO *b, char *str)
        {
        return(BIO_write(b,str,strlen(str)));
        }