Skip to content

Commit f879a98

Browse files
zhemantmichaelpro1
authored andcommitted
[C] fix decode funtion (OpenAPITools#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.
1 parent 3decbdb commit f879a98

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ char *strReplace(char *orig, char *rep, char *with) {
578578
return result;
579579
}
580580

581-
char *sbi_base64encode (const void *b64_encode_this, int encode_this_many_bytes){
581+
char *base64encode (const void *b64_encode_this, int encode_this_many_bytes){
582582
#ifdef OPENSSL
583583
BIO *b64_bio, *mem_bio; //Declares two OpenSSL BIOs: a base64 filter and a memory BIO.
584584
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)
597597
#endif
598598
}
599599
600-
char *sbi_base64decode (const void *b64_decode_this, int decode_this_many_bytes){
600+
char *base64decode (const void *b64_decode_this, int decode_this_many_bytes, int *decoded_bytes){
601601
#ifdef OPENSSL
602602
BIO *b64_bio, *mem_bio; //Declares two OpenSSL BIOs: a base64 filter and a memory BIO.
603603
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)
611611
decoded_byte_index++; //Increment the index until read of BIO decoded data is complete.
612612
} //Once we're done reading decoded data, BIO_read returns -1 even though there's no error.
613613
BIO_free_all(b64_bio); //Destroys all BIOs in chain, starting with b64 (i.e. the 1st one).
614+
*decoded_bytes = decoded_byte_index;
614615
return base64_decoded; //Returns base-64 decoded data with trailing null terminator.
615616
#endif
616617
}

modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,10 @@ fail:
540540
{
541541
goto end; //Binary
542542
}
543-
char* decoded = base64decode({{{name}}}->valuestring, strlen({{{name}}}->valuestring));
544-
decoded_str_{{{name}}}->data = malloc(strlen(decoded) - 1);
543+
decoded_str_{{{name}}}->data = base64decode({{{name}}}->valuestring, strlen({{{name}}}->valuestring), &decoded_str_{{{name}}}->len);
545544
if (!decoded_str_{{{name}}}->data) {
546545
goto end;
547546
}
548-
memcpy(decoded_str_{{{name}}}->data,decoded,(strlen(decoded)-1));
549-
decoded_str_{{{name}}}->len = strlen(decoded) - 1;
550547
{{/isBinary}}
551548
{{#isDate}}
552549
{{^required}}if ({{{name}}}) { {{/required}}

0 commit comments

Comments
 (0)