base64.c 1.2 KB

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