0

To calculate base64 I'm using the following code:

#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>

char* encode(const char* input) {
  BIO* b64 = BIO_new(BIO_f_base64());
  BIO* b_mem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, b_mem);
  BIO_write(b64, input, sizeof(input));
  BIO_flush(b64);

  BUF_MEM* b_ptr;
  BIO_get_mem_ptr(b64, &b_ptr);

  char* buff = (char*) malloc(b_ptr->length);
  memcpy(buff, b_ptr->data, b_ptr->length - 1);
  buff[b_ptr->length - 1] = 0;
  BIO_free_all(b64);

  return buff;
}

But not only it returns warnings that most of the functions are deprecated such as 'BIO_ctrl' is deprecated: first deprecated in OS X 10.7, but also doesn't compile because of Undefined symbols for architecture x86_64: "_BIO_ctrl".

How else can I encode a string in base64?

No working code How do I base64 encode (decode) in C?

Community
  • 1
  • 1
Incerteza
  • 32,326
  • 47
  • 154
  • 261
  • 1
    possible duplicate of [How do I base64 encode (decode) in C?](http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c) – CrApHeR May 07 '15 at 11:42
  • @CrApHeR, any working code? – Incerteza May 10 '15 at 11:15
  • The code in the marked-as-duplicate does not work? Anyway, the algorithm is well defined, and there are lots of implementations around. – Jongware May 10 '15 at 11:20
  • @Jongware, first of all I asked the question, the link doesn't duplicate my question. Secondly, I'm not interested in the implementations out there, I want to make my code work, that's it. – Incerteza May 10 '15 at 11:31
  • Then use the `base64_encode` function in [this answer](http://stackoverflow.com/a/6782480/2564301). The question is the same - a string in C is just any other data. – Jongware May 10 '15 at 11:35
  • @Jongware, that's an implementation from scratch instead of using a library, that doesn't make sense. – Incerteza May 10 '15 at 11:56
  • Besides, it doesn't compile. – Incerteza May 10 '15 at 11:57

0 Answers0