-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
) * C client generator improvement to support: openapi-generator/modules/openapi-generator/src/test/resources/3_0/petstore.yaml * Improvements to the C client generator: - moved base64* from apiClient.c to binary.h/binary.c - changed CR/LF to LF in binary.h/binary.c * C client generator: better support for base64encode / base64decode
- Loading branch information
1 parent
ff0c730
commit 5fd724f
Showing
25 changed files
with
216 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
modules/openapi-generator/src/main/resources/C-libcurl/binary.c.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "../include/binary.h" | ||
#ifdef OPENSSL | ||
#include "openssl/pem.h" | ||
#endif | ||
|
||
binary_t* instantiate_binary_t(char* data, int len) { | ||
binary_t* ret = malloc(sizeof(struct binary_t)); | ||
ret->len=len; | ||
ret->data = malloc(len); | ||
memcpy(ret->data, data, len); | ||
return ret; | ||
} | ||
|
||
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. | ||
b64_bio = BIO_new(BIO_f_base64()); //Initialize our base64 filter BIO. | ||
mem_bio = BIO_new(BIO_s_mem()); //Initialize our memory sink BIO. | ||
BIO_push(b64_bio, mem_bio); //Link the BIOs by creating a filter-sink BIO chain. | ||
BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL); //No newlines every 64 characters or less. | ||
BIO_write(b64_bio, b64_encode_this, encode_this_many_bytes); //Records base64 encoded data. | ||
BIO_flush(b64_bio); //Flush data. Necessary for b64 encoding, because of pad characters. | ||
BIO_get_mem_ptr(mem_bio, &mem_bio_mem_ptr); //Store address of mem_bio's memory structure. | ||
BIO_set_close(mem_bio, BIO_NOCLOSE); //Permit access to mem_ptr after BIOs are destroyed. | ||
BIO_free_all(b64_bio); //Destroys all BIOs in chain, starting with b64 (i.e. the 1st one). | ||
BUF_MEM_grow(mem_bio_mem_ptr, (*mem_bio_mem_ptr).length + 1); //Makes space for end null. | ||
(*mem_bio_mem_ptr).data[(*mem_bio_mem_ptr).length] = '\0'; //Adds null-terminator to tail. | ||
return (*mem_bio_mem_ptr).data; //Returns base-64 encoded data. (See: "buf_mem_st" struct). | ||
#else // OPENSSL | ||
#warning Data will not be encoded. If you want to use function "base64encode", please define "-DOPENSSL" when building the library. | ||
return NULL; | ||
#endif // OPENSSL | ||
} | ||
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. | ||
b64_bio = BIO_new(BIO_f_base64()); //Initialize our base64 filter BIO. | ||
mem_bio = BIO_new(BIO_s_mem()); //Initialize our memory source BIO. | ||
BIO_write(mem_bio, b64_decode_this, decode_this_many_bytes); //Base64 data saved in source. | ||
BIO_push(b64_bio, mem_bio); //Link the BIOs by creating a filter-source BIO chain. | ||
BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL); //Don't require trailing newlines. | ||
int decoded_byte_index = 0; //Index where the next base64_decoded byte should be written. | ||
while ( 0 < BIO_read(b64_bio, base64_decoded+decoded_byte_index, 1) ){ //Read byte-by-byte. | ||
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. | ||
#else // OPENSSL | ||
#warning Data will not be decoded. If you want to use function "base64decode", please define "-DOPENSSL" when building the library. | ||
return NULL; | ||
#endif // OPENSSL | ||
} |
18 changes: 18 additions & 0 deletions
18
modules/openapi-generator/src/main/resources/C-libcurl/binary.h.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef INCLUDE_BINARY_H | ||
#define INCLUDE_BINARY_H | ||
|
||
#include <stdint.h> | ||
|
||
typedef struct binary_t | ||
{ | ||
uint8_t* data; | ||
unsigned int len; | ||
} binary_t; | ||
|
||
binary_t* instantiate_binary_t(char* data, int len); | ||
|
||
char *base64encode(const void *b64_encode_this, int encode_this_many_bytes); | ||
|
||
char *base64decode(const void *b64_decode_this, int decode_this_many_bytes, int *decoded_bytes); | ||
|
||
#endif // INCLUDE_BINARY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
4.3.0-SNAPSHOT | ||
4.3.1-SNAPSHOT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.