Fix this
This commit is contained in:
1 file changed
+22
-16
+22
-16
@@ -1,10 +1,13 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <openssl/bio.h>
|
#include <openssl/bio.h>
|
||||||
|
#include <openssl/buffer.h>
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// This code is taken from https://gist.github.com/barrysteyn/4409525
|
// This code is taken from https://gist.github.com/barrysteyn/4409525
|
||||||
|
// Modified for Windows compatibility (no fmemopen)
|
||||||
|
|
||||||
int calcDecodeLength(const char *b64input) {
|
int calcDecodeLength(const char *b64input) {
|
||||||
int len = strlen(b64input);
|
int len = strlen(b64input);
|
||||||
@@ -18,40 +21,43 @@ int calcDecodeLength(const char *b64input) {
|
|||||||
return (int)len * 0.75 - padding;
|
return (int)len * 0.75 - padding;
|
||||||
}
|
}
|
||||||
|
|
||||||
int *base64_encode(const char *message, char **buffer) {
|
int base64_encode(const char *message, char **buffer) {
|
||||||
BIO *bio, *b64;
|
BIO *bio, *b64;
|
||||||
FILE *stream;
|
BUF_MEM *buffer_ptr;
|
||||||
int encodedSize = 4 * ceil((double)strlen(message) / 3);
|
int encodedSize = 4 * (int)ceil((double)strlen(message) / 3);
|
||||||
*buffer = (char *)malloc(encodedSize + 1);
|
|
||||||
|
|
||||||
stream = fmemopen(*buffer, encodedSize + 1, "w");
|
|
||||||
b64 = BIO_new(BIO_f_base64());
|
b64 = BIO_new(BIO_f_base64());
|
||||||
bio = BIO_new_fp(stream, BIO_NOCLOSE);
|
bio = BIO_new(BIO_s_mem());
|
||||||
bio = BIO_push(b64, bio);
|
bio = BIO_push(b64, bio);
|
||||||
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
|
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
|
||||||
BIO_write(bio, message, strlen(message));
|
BIO_write(bio, message, strlen(message));
|
||||||
BIO_flush(bio);
|
BIO_flush(bio);
|
||||||
BIO_free_all(bio);
|
BIO_get_mem_ptr(bio, &buffer_ptr);
|
||||||
fclose(stream);
|
|
||||||
|
|
||||||
return (0);
|
*buffer = (char *)malloc(buffer_ptr->length + 1);
|
||||||
|
memcpy(*buffer, buffer_ptr->data, buffer_ptr->length);
|
||||||
|
(*buffer)[buffer_ptr->length] = '\0';
|
||||||
|
|
||||||
|
BIO_free_all(bio);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int *base64_decode(char *b64message, char **buffer) {
|
int base64_decode(char *b64message, char **buffer) {
|
||||||
BIO *bio, *b64;
|
BIO *bio, *b64;
|
||||||
int decodeLen = calcDecodeLength(b64message), len = 0;
|
int decodeLen = calcDecodeLength(b64message);
|
||||||
*buffer = (char *)malloc(decodeLen + 1);
|
int len = 0;
|
||||||
FILE *stream = fmemopen(b64message, strlen(b64message), "r");
|
|
||||||
|
|
||||||
|
*buffer = (char *)malloc(decodeLen + 1);
|
||||||
|
|
||||||
|
bio = BIO_new_mem_buf(b64message, strlen(b64message));
|
||||||
b64 = BIO_new(BIO_f_base64());
|
b64 = BIO_new(BIO_f_base64());
|
||||||
bio = BIO_new_fp(stream, BIO_NOCLOSE);
|
|
||||||
bio = BIO_push(b64, bio);
|
bio = BIO_push(b64, bio);
|
||||||
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
|
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
|
||||||
len = BIO_read(bio, *buffer, strlen(b64message));
|
len = BIO_read(bio, *buffer, strlen(b64message));
|
||||||
(*buffer)[len] = '\0';
|
(*buffer)[len] = '\0';
|
||||||
|
|
||||||
BIO_free_all(bio);
|
BIO_free_all(bio);
|
||||||
fclose(stream);
|
|
||||||
|
|
||||||
return (0);
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user