C 我是否正确地使用OpenSSL对缓冲区进行base64编码?

C 我是否正确地使用OpenSSL对缓冲区进行base64编码?,c,openssl,C,Openssl,我有一个缓冲区: unsigned char *buffer; int buffer_length; 这就是我当前将其转换为base64编码缓冲区的方式: BIO *mem = BIO_new(BIO_s_mem()); BIO *b64 = BIO_new(BIO_f_base64()); mem = BIO_push(b64, mem); int write_length = BIO_write(mem, buffer, buffer_length); if (write_l

我有一个缓冲区:

unsigned char *buffer; 
int buffer_length; 
这就是我当前将其转换为base64编码缓冲区的方式:

BIO *mem = BIO_new(BIO_s_mem()); 
BIO *b64 = BIO_new(BIO_f_base64()); 
mem = BIO_push(b64, mem); 

int write_length = BIO_write(mem, buffer, buffer_length); 
if (write_length != buffer_length) //* 
  return -1; 

int flush_result = BIO_flush(mem); 
if (flush_result != 1) 
  return -1; 

unsigned char *result; //** 
int result_length = BIO_get_mem_data(mem, &result); 

//use the base64-encoded result to do whatever I need to do 

BIO_free_all(mem); 
return 0;
到目前为止,这似乎是可行的。然而,这是好的、健壮的代码吗?我对上面标有星号的代码有一些特别的问题:

  • /*
    )假设
    BIO_write()
    总是一次写出整个base64编码字符串是正确的,还是我必须在这里创建一个循环
  • /**
    )使用类型
    无符号字符*
    是正确的,还是必须改用
    字符*

//*您应该将您的
BIO_write()
放入一个循环中。手册页对此非常清楚(它尝试写入请求的字节数),这与C中的其他写入内容一致

//**您应该使用
char*
,因为这是手册页指定的,尽管我不确定这有什么大不了的