int main (void)
{
 BIO *bmem, *b64;
 BUF_MEM *bptr;
 char message[] = "Hello World \n";
 int written = 0;
 
 b64 = BIO_new(BIO_f_base64());
 bmem = BIO_new(BIO_s_mem());
 b64 = BIO_push(b64, bmem);
 written = BIO_write(b64, message, strlen(message));
 cout << written << endl;
 BIO_get_mem_ptr(b64, &bptr);
 BIO_flush(b64);
 BIO_free_all(b64);
}
 
This should be straight forward, but it is not. It appears that base64
encoding to something other than stdout or a file does not work. I am using
vc++ 6.0, on windows platform. I can't get it to work with stdout either. I
get no compile or runtime errors. The &bptr gets a valid address, but the
address has nothing in it. Also the system tells me that 13 characters were
written, which is the correct number.Does anyone see why this is not working
correctly? I have even change the line BIO_get_mem_ptr(b64, &bptr); to
BIO_get_mem_ptr(bmem, &bptr); which does nothing.

Thanks in Advance....... 

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Joseph Oreste Bruni
Sent: Thursday, October 13, 2005 4:36 PM
To: openssl-users@openssl.org
Subject: Re: Base64 Help

Using VC++ is your first problem... :)

The call to BIO_push is your problem. You overwrite your reference to the
b64 BIO with another reference to the mem BIO. So now, b64 points to mem.

Do this:

int main()
{
     BIO *bmem, *b64;
     char message[] = "Hello World \n";
     int written = 0;

     b64 = BIO_new(BIO_f_base64());
     bmem = BIO_new_fp(stdout,0);
     b64 = BIO_push(b64, bmem);
     written = BIO_write(b64, message, strlen(message));
     cout << written << endl;
     BIO_flush(b64);
     BIO_free_all(b64);
}

This will output your Base64 to stdout. Then if you need to use a mem BIO
just change out the call to what you had, but you'll need to get pointers to
the data to display it (or whatever).




On Oct 13, 2005, at 1:55 PM, Adam Jones wrote:

> Visual C++ did not complain nor did it error out when it ran, but you 
> are correct it does take a BUF_MEM structure. I also added another BIO 
> method to the code. I also read that section in the book you 
> suggested. I also made the code simple, but it appears that it still 
> does not give me the
> base64
> encoding. Any suggestions...
>
> int main()
> {
>     BIO *bmem, *b64;
>     char message[] = "Hello World \n";
>     int written = 0;
>
>     b64 = BIO_new(BIO_f_base64());
>     bmem = BIO_new(BIO_s_mem());
>     bmem = BIO_push(b64, bmem);
>     written = BIO_write(b64, message, strlen(message));
>     cout << written << endl;
>     BIO_flush(b64);
>     BIO_free_all(b64);
> }
>
>
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Joseph Oreste 
> Bruni
> Sent: Thursday, October 13, 2005 2:46 PM
> To: openssl-users@openssl.org
> Subject: Re: Base64 Help
>
> "b64" is a filter BIO, it won't hold on to your data. You need to 
> append a "memory BIO" to the back end of the filter bio so that your 
> output can be accumulated.
>
> There are samples on how to do this in the OpenSSL book as well as a 
> rather lengthy discussion on BIO's in general.
>
> Also "BIO_get_mem_ptr()" gives you a pointer to BUF_MEM structure, not 
> a char*. Your compiler should have yelled at you for that.
>
>
> On Oct 13, 2005, at 12:41 PM, Adam Jones wrote:
>
>
>> Below is the code I am using to try and test the base64 encode in 
>> openssl. I am using rand to generate a binary and then encoding that 
>> to base64. Instead of using a file, I want to use memory to output 
>> the
>> base64 encoded buffer. This code compiles and runs, but my output 
>> buffer is all 0. Any help would be appreciated. What have I missed?
>>
>> The variable written does show 16 like it should......help!
>>
>> #include <iostream>
>> #include <memory.h>
>> #include "evp.h"
>> #include "rand.h"
>> #include "bio.h"
>>
>> using namespace std;
>>
>> int main()
>> {
>>  BIO *b64;
>>  unsigned char *pbuffer = new unsigned char [16];  unsigned char 
>> *pOutput = new unsigned char [100];  int written;
>>
>>  memset(pOutput, '0', 100);
>>  RAND_bytes(pbuffer, 16);
>>  b64 = BIO_new(BIO_f_base64());
>>  written = BIO_write(b64, pbuffer, 16);
>>
>>  cout << written << endl;
>>
>>  BIO_get_mem_ptr(b64, pOutput);
>>
>>  for ( int nLoop = 0; nLoop< 16; nLoop++)  {  cout << pOutput[nLoop]; 
>> }  cout << "\n" << endl;
>>
>>  BIO_free_all(b64);
>>
>
>
>
> Confidentiality Notice:
>
> This e-mail and any files transmitted with it are confidential and 
> intended solely for the use of the individual or entity to whom they 
> are addressed.  If you are not the named addressee you should not 
> disseminate, distribute or copy this e-mail. Please notify the sender 
> immediately by e-mail if you have received this e-mail by mistake and 
> delete this e-mail from your system.  If you are not the intended 
> recipient you are notified that disclosing, copying, distributing or 
> taking any action in reliance on the contents of this information is 
> strictly prohibited.
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    openssl-users@openssl.org
> Automated List Manager                           [EMAIL PROTECTED]
>



Confidentiality Notice:

This e-mail and any files transmitted with it are confidential and intended 
solely for the use of the individual or entity to whom they are addressed.  If 
you are not the named addressee you should not disseminate, distribute or copy 
this e-mail. Please notify the sender immediately by e-mail if you have 
received this e-mail by mistake and delete this e-mail from your system.  If 
you are not the intended recipient you are notified that disclosing, copying, 
distributing or taking any action in reliance on the contents of this 
information is strictly prohibited.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to