1/* This is a simple example of using the base64 BIO to a memory BIO and then 2 * getting the data. 3 */ 4#include <stdio.h> 5#include <openssl/bio.h> 6#include <openssl/evp.h> 7 8main() 9 { 10 int i; 11 BIO *mbio,*b64bio,*bio; 12 char buf[512]; 13 char *p; 14 15 mbio=BIO_new(BIO_s_mem()); 16 b64bio=BIO_new(BIO_f_base64()); 17 18 bio=BIO_push(b64bio,mbio); 19 /* We now have bio pointing at b64->mem, the base64 bio encodes on 20 * write and decodes on read */ 21 22 for (;;) 23 { 24 i=fread(buf,1,512,stdin); 25 if (i <= 0) break; 26 BIO_write(bio,buf,i); 27 } 28 /* We need to 'flush' things to push out the encoding of the 29 * last few bytes. There is special encoding if it is not a 30 * multiple of 3 31 */ 32 BIO_flush(bio); 33 34 printf("We have %d bytes available\n",BIO_pending(mbio)); 35 36 /* We will now get a pointer to the data and the number of elements. */ 37 /* hmm... this one was not defined by a macro in bio.h, it will be for 38 * 0.9.1. The other option is too just read from the memory bio. 39 */ 40 i=(int)BIO_ctrl(mbio,BIO_CTRL_INFO,0,(char *)&p); 41 42 printf("%d\n",i); 43 fwrite("---\n",1,4,stdout); 44 fwrite(p,1,i,stdout); 45 fwrite("---\n",1,4,stdout); 46 47 /* This call will walk the chain freeing all the BIOs */ 48 BIO_free_all(bio); 49 } 50