> From: owner-openssl-us...@openssl.org On Behalf Of ml
> Sent: Saturday, 26 May, 2012 16:18

> I try to realize as base64 encoding 
> char *base64(char *input, int length)
> {
>   BIO *bmem, *b64;
>   BUF_MEM *bptr;
> 
>   b64 = BIO_new(BIO_f_base64());
>   bmem = BIO_new(BIO_s_mem());
>   b64 = BIO_push(b64, bmem);
>   BIO_write(b64, input, length);
>   BIO_flush(b64);
>   BIO_get_mem_ptr(b64, &bptr);
> 
>   char *buff = (char *)malloc(bptr->length+1);
>   memcpy(buff, bptr->data, bptr->length);
>   buff[bptr->length] = 0;
> 
>   BIO_free_all(b64);
> 
>   return buff;
> }
> but it does not work can you help me
> 
It works for me, with the needed #include's and call added.
What exactly is your problem? What did it do, what did you 
expect, and how do those differ? 

I note the cast of malloc to (char*) is suspicious.
If you #include'd <stdlib.h> as is required to correctly 
declare malloc, no cast is needed in C since about 1989.
A cast is needed in C++, ut in C++ you should normally use 
(typesafe) new and delete rather than malloc/free/etc.

And a call to malloc *without* #include'ing <stdlib.h> 
is not allowed in either current C (C99 or later) or C++, 
but the use of a declaration after executable statements 
within a block, as you have, is *only* allowed in those.

Also you should always check if malloc/calloc/realloc 
returned a null pointer (failed) before using it. In some 
applications you can actually handle the error usefully, 
and even when you can't a clear and specific assert() or 
similar suicide is better than an unexplained fault.
(Also fopen, tmpnam, getenv, etc, etc.)

If you are using some sort of nonstandard C (or C++, or even 
some other C-like something), give details; it may or may not 
be sufficiently compatible with C as used for OpenSSL.


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to