added base64_decode fn
All checks were successful
Codacy Scan / Codacy Security Scan (push) Successful in 22s
Build WHY2-chat / test-why2 (./out/why2-chat-client, ./configure.sh, ubuntu-latest, ./out/why2-chat-server) (push) Successful in 1m56s
Test WHY2-core / test-why2 (why2, ./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./out/why2-core-test, valgrind --leak-check=full --show-leak-kinds=reachable --track-origins=yes -s) (push) Successful in 2m17s
Test Project / test-project (./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./test) (push) Successful in 2m16s
Test WHY2-logger / test-why2 (why2-logger, ./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./out/why2-logger-test, valgrind --leak-check=full --show-leak-kinds=reachable --track-origins=yes -s) (push) Successful in 2m29s

and tweaked the encode fn a bit
This commit is contained in:
Václav Šmejkal 2025-01-29 22:20:11 +01:00
parent 685b8e446e
commit d764002546
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -35,33 +35,55 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
EVP_PKEY *keypair = NULL; //KEYPAIR
//LOCAL
char *base64_encode(char *data)
char* base64_encode(char *message)
{
//VARIABLES
BIO *bio;
BIO *b64;
BUF_MEM *buffer_ptr;
char* encoded_message;
//CREATE BIO
//INIT BIOs
b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //NO NEWLINES
BIO_write(bio, data, strlen(data)); //ENCODE
//ENCODE
BIO_write(bio, message, strlen(message));
BIO_flush(bio);
BIO_get_mem_ptr(bio, &buffer_ptr); //GET PTR
char *b64text = why2_malloc(buffer_ptr -> length + 1); //ALLOCATE
BIO_get_mem_ptr(bio, &buffer_ptr);
//COPY
memcpy(b64text, buffer_ptr -> data, buffer_ptr -> length);
b64text[buffer_ptr -> length] = '\0'; //NULL-TERM
encoded_message = why2_malloc(buffer_ptr -> length + 1);
memcpy(encoded_message, buffer_ptr -> data, buffer_ptr -> length);
encoded_message[buffer_ptr -> length] = '\0';
//DEALLOCATION
BIO_free_all(bio);
return b64text;
return encoded_message;
}
char* base64_decode(char *encoded_message)
{
//VARIABLES
BIO *bio;
BIO *b64;
size_t length = strlen(encoded_message);
char* decoded_message = why2_malloc(length);
//INIT BIOs
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf(encoded_message, length);
bio = BIO_push(b64, bio);
//NULL-TERM
decoded_message[BIO_read(bio, decoded_message, length)] = '\0';
//DEALLOCATION
BIO_free_all(bio);
return decoded_message;
}
//GLOBAL