Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
C client generator: better support for base64encode / base64decode
  • Loading branch information
michelealbano committed Apr 6, 2020
commit e05af82d3aeaf30a051b3d7618841d6f6cd9da8e
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ set(CMAKE_CXX_VISIBILITY_PRESET default)
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
set(CMAKE_BUILD_TYPE Debug)

find_package(OpenSSL)

if (OPENSSL_FOUND)
message (STATUS "OPENSSL found")
set (CMAKE_C_FLAGS "-DOPENSSL")
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIRS})
link_directories(${OPENSSL_LIBRARIES})
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
else()
message (STATUS "OpenSSL Not found.")
endif()

set(pkgName "{{projectName}}")

find_package(CURL 7.58.0 REQUIRED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
#include <string.h>
#include <stdio.h>
#include "../include/apiClient.h"
#ifdef OPENSSL
#include "openssl/pem.h"
#endif

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#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));
Expand All @@ -27,10 +30,8 @@ char *base64encode (const void *b64_encode_this, int encode_this_many_bytes){
(*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
char* ret = malloc(encode_this_many_bytes);
memcpy(ret, b64_encode_this, encode_this_many_bytes-1);
ret[encode_this_many_bytes] = 0;
return ret;
#warning Data will not be encoded. If you want to use function "base64encode", please define "-DOPENSSL" when building the library.
return NULL;
#endif // OPENSSL
}

Expand All @@ -51,10 +52,7 @@ char *base64decode (const void *b64_decode_this, int decode_this_many_bytes, int
*decoded_bytes = decoded_byte_index;
return base64_decoded; //Returns base-64 decoded data with trailing null terminator.
#else // OPENSSL
char* ret = malloc(decode_this_many_bytes);
memcpy(ret, b64_decode_this, decode_this_many_bytes-1);
ret[decode_this_many_bytes] = 0;
*decoded_bytes = decode_this_many_bytes;
return ret;
#warning Data will not be decoded. If you want to use function "base64decode", please define "-DOPENSSL" when building the library.
return NULL;
#endif // OPENSSL
}
3 changes: 0 additions & 3 deletions samples/client/petstore/c/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
#include <string.h>
#include <stdio.h>
#include "../include/apiClient.h"
#ifdef OPENSSL
#include "openssl/pem.h"
#endif

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp);

Expand Down
16 changes: 7 additions & 9 deletions samples/client/petstore/c/src/binary.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#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));
Expand All @@ -27,10 +30,8 @@ char *base64encode (const void *b64_encode_this, int encode_this_many_bytes){
(*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
char* ret = malloc(encode_this_many_bytes);
memcpy(ret, b64_encode_this, encode_this_many_bytes-1);
ret[encode_this_many_bytes] = 0;
return ret;
#warning Data will not be encoded. If you want to use function "base64encode", please define "-DOPENSSL" when building the library.
return NULL;
#endif // OPENSSL
}

Expand All @@ -51,10 +52,7 @@ char *base64decode (const void *b64_decode_this, int decode_this_many_bytes, int
*decoded_bytes = decoded_byte_index;
return base64_decoded; //Returns base-64 decoded data with trailing null terminator.
#else // OPENSSL
char* ret = malloc(decode_this_many_bytes);
memcpy(ret, b64_decode_this, decode_this_many_bytes-1);
ret[decode_this_many_bytes] = 0;
*decoded_bytes = decode_this_many_bytes;
return ret;
#warning Data will not be decoded. If you want to use function "base64decode", please define "-DOPENSSL" when building the library.
return NULL;
#endif // OPENSSL
}