ENGINE uses a very opaque design, so we can predeclare the structure type
[openssl.git] / crypto / bio / bss_mem.c
index d9a2280d8f0994a87608eec2c74e477f1eec8859..28ff7582bff87a52da73390879543aa57b8ebd1d 100644 (file)
 #include "cryptlib.h"
 #include <openssl/bio.h>
 
-static int mem_write(BIO *h,char *buf,int num);
-static int mem_read(BIO *h,char *buf,int size);
-static int mem_puts(BIO *h,char *str);
-static int mem_gets(BIO *h,char *str,int size);
-static long mem_ctrl(BIO *h,int cmd,long arg1,char *arg2);
+static int mem_write(BIO *h, const char *buf, int num);
+static int mem_read(BIO *h, char *buf, int size);
+static int mem_puts(BIO *h, const char *str);
+static int mem_gets(BIO *h, char *str, int size);
+static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
 static int mem_new(BIO *h);
 static int mem_free(BIO *data);
 static BIO_METHOD mem_method=
@@ -79,6 +79,7 @@ static BIO_METHOD mem_method=
        mem_ctrl,
        mem_new,
        mem_free,
+       NULL,
        };
 
 /* bio->num is used to hold the value to return on 'empty', if it is
@@ -162,14 +163,14 @@ static int mem_read(BIO *b, char *out, int outl)
                }
        } else if (bm->length == 0)
                {
-               if (b->num != 0)
+               ret = b->num;
+               if (ret != 0)
                        BIO_set_retry_read(b);
-               ret= b->num;
                }
        return(ret);
        }
 
-static int mem_write(BIO *b, char *in, int inl)
+static int mem_write(BIO *b, const char *in, int inl)
        {
        int ret= -1;
        int blen;
@@ -197,7 +198,7 @@ end:
        return(ret);
        }
 
-static long mem_ctrl(BIO *b, int cmd, long num, char *ptr)
+static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
        {
        long ret=1;
        char **pptr;
@@ -207,15 +208,20 @@ static long mem_ctrl(BIO *b, int cmd, long num, char *ptr)
        switch (cmd)
                {
        case BIO_CTRL_RESET:
-               if (bm->data != NULL) {
+               if (bm->data != NULL)
+                       {
                        /* For read only case reset to the start again */
                        if(b->flags & BIO_FLAGS_MEM_RDONLY) 
-                                       bm->data -= bm->max - bm->length;
-                       else {
+                               {
+                               bm->data -= bm->max - bm->length;
+                               bm->length = bm->max;
+                               }
+                       else
+                               {
                                memset(bm->data,0,bm->max);
                                bm->length=0;
+                               }
                        }
-               }
                break;
        case BIO_CTRL_EOF:
                ret=(long)(bm->length == 0);
@@ -299,7 +305,7 @@ static int mem_gets(BIO *bp, char *buf, int size)
        return(ret);
        }
 
-static int mem_puts(BIO *bp, char *str)
+static int mem_puts(BIO *bp, const char *str)
        {
        int n,ret;