Skip to content

Commit

Permalink
[C] fix decode funtion (#5642)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
zhemant committed Mar 27, 2020
1 parent b49128f commit 2f9c20a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down

0 comments on commit 2f9c20a

Please sign in to comment.