From 2f9c20a175d548b21d60f4d60466154241e1b982 Mon Sep 17 00:00:00 2001 From: Hemant Zope <42613258+zhemant@users.noreply.github.com> Date: Fri, 27 Mar 2020 16:16:20 +0100 Subject: [PATCH] [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. --- .../src/main/resources/C-libcurl/apiClient.c.mustache | 5 +++-- .../src/main/resources/C-libcurl/model-body.mustache | 5 +---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 69b2e74c331..a16f1ba2051 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -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 } diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache index 63599e4a8b0..7a911ec6796 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache @@ -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}}