In certain cases, no encoding has been set up for the b64 filter. In
[openssl.git] / crypto / evp / bio_b64.c
index 73172b9a07119bbc29b0e10c458d2e91fa1e76dd..f12eac1b55a06b37a49d1d579f1b65d7edb9fca0 100644 (file)
 #include <stdio.h>
 #include <errno.h>
 #include "cryptlib.h"
-#include "buffer.h"
-#include "evp.h"
-
-#ifndef NOPROTO
-static int b64_write(BIO *h,char *buf,int num);
-static int b64_read(BIO *h,char *buf,int size);
-/*static int b64_puts(BIO *h,char *str); */
-/*static int b64_gets(BIO *h,char *str,int size); */
-static long b64_ctrl(BIO *h,int cmd,long arg1,char *arg2);
+#include <openssl/buffer.h>
+#include <openssl/evp.h>
+
+static int b64_write(BIO *h, const char *buf, int num);
+static int b64_read(BIO *h, char *buf, int size);
+/*static int b64_puts(BIO *h, const char *str); */
+/*static int b64_gets(BIO *h, char *str, int size); */
+static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
 static int b64_new(BIO *h);
 static int b64_free(BIO *data);
-#else
-static int b64_write();
-static int b64_read();
-/*static int b64_puts(); */
-/*static int b64_gets(); */
-static long b64_ctrl();
-static int b64_new();
-static int b64_free();
-#endif
-
+static long b64_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
 #define B64_BLOCK_SIZE 1024
 #define B64_BLOCK_SIZE2        768
 #define B64_NONE       0
@@ -111,19 +101,19 @@ static BIO_METHOD methods_b64=
        b64_ctrl,
        b64_new,
        b64_free,
+       b64_callback_ctrl,
        };
 
-BIO_METHOD *BIO_f_base64()
+BIO_METHOD *BIO_f_base64(void)
        {
        return(&methods_b64);
        }
 
-static int b64_new(bi)
-BIO *bi;
+static int b64_new(BIO *bi)
        {
        BIO_B64_CTX *ctx;
 
-       ctx=(BIO_B64_CTX *)Malloc(sizeof(BIO_B64_CTX));
+       ctx=(BIO_B64_CTX *)OPENSSL_malloc(sizeof(BIO_B64_CTX));
        if (ctx == NULL) return(0);
 
        ctx->buf_len=0;
@@ -140,21 +130,17 @@ BIO *bi;
        return(1);
        }
 
-static int b64_free(a)
-BIO *a;
+static int b64_free(BIO *a)
        {
        if (a == NULL) return(0);
-       Free(a->ptr);
+       OPENSSL_free(a->ptr);
        a->ptr=NULL;
        a->init=0;
        a->flags=0;
        return(1);
        }
        
-static int b64_read(b,out,outl)
-BIO *b;
-char *out;
-int outl;
+static int b64_read(BIO *b, char *out, int outl)
        {
        int ret=0,i,ii,j,k,x,n,num,ret_code=0;
        BIO_B64_CTX *ctx;
@@ -253,8 +239,8 @@ int outl;
                                                        &(ctx->tmp[0]));
                                                for (x=0; x < i; x++)
                                                        ctx->tmp[x]=p[x];
-                                               EVP_DecodeInit(&ctx->base64);
                                                }
+                                       EVP_DecodeInit(&ctx->base64);
                                        ctx->start=0;
                                        break;
                                        }
@@ -354,10 +340,7 @@ int outl;
        return((ret == 0)?ret_code:ret);
        }
 
-static int b64_write(b,in,inl)
-BIO *b;
-char *in;
-int inl;
+static int b64_write(BIO *b, const char *in, int inl)
        {
        int ret=inl,n,i;
        BIO_B64_CTX *ctx;
@@ -387,10 +370,11 @@ int inl;
                n-=i;
                }
        /* at this point all pending data has been written */
+       ctx->buf_off=0;
+       ctx->buf_len=0;
 
        if ((in == NULL) || (inl <= 0)) return(0);
 
-       ctx->buf_off=0;
        while (inl > 0)
                {
                n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl;
@@ -400,14 +384,20 @@ int inl;
                        if (ctx->tmp_len > 0)
                                {
                                n=3-ctx->tmp_len;
+                               /* There's a teoretical possibility for this */
+                               if (n > inl) 
+                                       n=inl;
                                memcpy(&(ctx->tmp[ctx->tmp_len]),in,n);
                                ctx->tmp_len+=n;
-                               n=ctx->tmp_len;
-                               if (n < 3)
+                               if (ctx->tmp_len < 3)
                                        break;
                                ctx->buf_len=EVP_EncodeBlock(
                                        (unsigned char *)ctx->buf,
-                                       (unsigned char *)ctx->tmp,n);
+                                       (unsigned char *)ctx->tmp,
+                                       ctx->tmp_len);
+                               /* Since we're now done using the temporary
+                                  buffer, the length should be 0'd */
+                               ctx->tmp_len=0;
                                }
                        else
                                {
@@ -451,11 +441,7 @@ int inl;
        return(ret);
        }
 
-static long b64_ctrl(b,cmd,num,ptr)
-BIO *b;
-int cmd;
-long num;
-char *ptr;
+static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
        {
        BIO_B64_CTX *ctx;
        long ret=1;
@@ -479,7 +465,8 @@ char *ptr;
                break;
        case BIO_CTRL_WPENDING: /* More to write in buffer */
                ret=ctx->buf_len-ctx->buf_off;
-               if ((ret == 0) && (ctx->base64.num != 0))
+               if ((ret == 0) && (ctx->encode != B64_NONE)
+                       && (ctx->base64.num != 0))
                        ret=1;
                else if (ret <= 0)
                        ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
@@ -514,7 +501,7 @@ again:
                                goto again;
                                }
                        }
-               else if (ctx->base64.num != 0)
+               else if (ctx->encode != B64_NONE && ctx->base64.num != 0)
                        {
                        ctx->buf_off=0;
                        EVP_EncodeFinal(&(ctx->base64),
@@ -545,3 +532,17 @@ again:
        return(ret);
        }
 
+static long b64_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);
+       }
+