[C] fix decode funtion (#5642)

* fix function names and add parameter to return decoded bytes length from base64decode function

* format base64decode function to avoid unnecessary malloc and fix wrong length assigning

* update the pointer assigning

for some reason var++ / *var++ cannot be done on int *var, hence making a local variable which is incremented and at the end it is assigned to the pointer.
This commit is contained in:
Hemant Zope 2020-03-27 16:16:20 +01:00 committed by GitHub
parent b49128f04b
commit 2f9c20a175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 6 deletions

View File

@ -578,7 +578,7 @@ char *strReplace(char *orig, char *rep, char *with) {
return result;
}
char *sbi_base64encode (const void *b64_encode_this, int encode_this_many_bytes){
char *base64encode (const void *b64_encode_this, int encode_this_many_bytes){
#ifdef OPENSSL
BIO *b64_bio, *mem_bio; //Declares two OpenSSL BIOs: a base64 filter and a memory BIO.
BUF_MEM *mem_bio_mem_ptr; //Pointer to a "memory BIO" structure holding our base64 data.
@ -597,7 +597,7 @@ char *sbi_base64encode (const void *b64_encode_this, int encode_this_many_bytes)
#endif
}
char *sbi_base64decode (const void *b64_decode_this, int decode_this_many_bytes){
char *base64decode (const void *b64_decode_this, int decode_this_many_bytes, int *decoded_bytes){
#ifdef OPENSSL
BIO *b64_bio, *mem_bio; //Declares two OpenSSL BIOs: a base64 filter and a memory BIO.
char *base64_decoded = calloc( (decode_this_many_bytes*3)/4+1, sizeof(char) ); //+1 = null.
@ -611,6 +611,7 @@ char *sbi_base64decode (const void *b64_decode_this, int decode_this_many_bytes)
decoded_byte_index++; //Increment the index until read of BIO decoded data is complete.
} //Once we're done reading decoded data, BIO_read returns -1 even though there's no error.
BIO_free_all(b64_bio); //Destroys all BIOs in chain, starting with b64 (i.e. the 1st one).
*decoded_bytes = decoded_byte_index;
return base64_decoded; //Returns base-64 decoded data with trailing null terminator.
#endif
}

View File

@ -540,13 +540,10 @@ fail:
{
goto end; //Binary
}
char* decoded = base64decode({{{name}}}->valuestring, strlen({{{name}}}->valuestring));
decoded_str_{{{name}}}->data = malloc(strlen(decoded) - 1);
decoded_str_{{{name}}}->data = base64decode({{{name}}}->valuestring, strlen({{{name}}}->valuestring), &decoded_str_{{{name}}}->len);
if (!decoded_str_{{{name}}}->data) {
goto end;
}
memcpy(decoded_str_{{{name}}}->data,decoded,(strlen(decoded)-1));
decoded_str_{{{name}}}->len = strlen(decoded) - 1;
{{/isBinary}}
{{#isDate}}
{{^required}}if ({{{name}}}) { {{/required}}