[core,aad] refactor use of cJSON

* link cJSON public to FreeRDP
* export compatibility functions for older versions of cJSON
This commit is contained in:
akallabeth 2023-07-20 11:46:03 +02:00 committed by akallabeth
parent 0bc1a859e2
commit a0d38914d6
4 changed files with 63 additions and 60 deletions

View File

@ -97,10 +97,6 @@ endif()
list(APPEND LIBS freerdp winpr)
if(WITH_AAD)
list(APPEND LIBS ${CJSON_LIBRARIES})
endif()
target_link_libraries(${MODULE_NAME} PRIVATE ${FREERDP_CHANNELS_CLIENT_LIBS})
target_link_libraries(${MODULE_NAME} PUBLIC ${LIBS})

View File

@ -66,16 +66,6 @@
#include <freerdp/log.h>
#define TAG CLIENT_TAG("common")
#ifdef WITH_AAD
#if CJSON_VERSION_MAJOR == 1
#if CJSON_VERSION_MINOR <= 7
#if CJSON_VERSION_PATCH < 13
#define USE_CJSON_COMPAT
#endif
#endif
#endif
#endif
static BOOL freerdp_client_common_new(freerdp* instance, rdpContext* context)
{
RDP_CLIENT_ENTRY_POINTS* pEntryPoints;
@ -1105,19 +1095,6 @@ BOOL client_cli_get_access_token(freerdp* instance, AccessTokenType tokenType, c
}
}
static cJSON* compat_cJSON_ParseWithLength(const char* value, size_t buffer_length)
{
#if defined(USE_CJSON_COMPAT)
// Check for string '\0' termination.
const size_t slen = strnlen(value, buffer_length);
if (slen >= buffer_length)
return NULL;
return cJSON_Parse(value);
#else
return cJSON_ParseWithLength(value, buffer_length);
#endif
}
BOOL client_common_get_access_token(freerdp* instance, const char* request, char** token)
{
#ifdef WITH_AAD
@ -1146,7 +1123,7 @@ BOOL client_common_get_access_token(freerdp* instance, const char* request, char
goto cleanup;
}
json = compat_cJSON_ParseWithLength((const char*)response, response_length);
json = cJSON_ParseWithLength((const char*)response, response_length);
if (!json)
{
WLog_ERR(TAG, "Failed to parse access token response");

View File

@ -68,11 +68,18 @@ macro (freerdp_include_directory_add)
endif()
endmacro()
macro (freerdp_library_add_public)
foreach (_lib ${ARGN})
list (APPEND LIBFREERDP_PUB_LIBS "${_lib}")
endforeach()
set (LIBFREERDP_PUB_LIBS ${LIBFREERDP_PUB_LIBS} PARENT_SCOPE)
endmacro()
macro (freerdp_library_add)
foreach (_lib ${ARGN})
list (APPEND LIBFREERDP_LIBS "${_lib}")
endforeach()
set (LIBFREERDP_LIBS ${LIBFREERDP_LIBS} PARENT_SCOPE)
foreach (_lib ${ARGN})
list (APPEND LIBFREERDP_LIBS "${_lib}")
endforeach()
set (LIBFREERDP_LIBS ${LIBFREERDP_LIBS} PARENT_SCOPE)
endmacro()
macro (freerdp_definition_add)
@ -220,7 +227,7 @@ if(FAAC_FOUND)
endif()
if(WITH_AAD)
freerdp_library_add(${CJSON_LIBRARIES})
freerdp_library_add_public(${CJSON_LIBRARIES})
include_directories(${CJSON_INCLUDE_DIRS})
endif()
@ -382,6 +389,7 @@ endif()
list(REMOVE_DUPLICATES LIBFREERDP_DEFINITIONS)
list(REMOVE_DUPLICATES LIBFREERDP_LIBS)
list(REMOVE_DUPLICATES LIBFREERDP_PUB_LIBS)
list(REMOVE_DUPLICATES LIBFREERDP_INCLUDES)
include_directories(${LIBFREERDP_INCLUDES})
@ -411,8 +419,10 @@ if (WITH_LIBRARY_VERSIONING)
set_target_properties(${MODULE_NAME} PROPERTIES VERSION ${FREERDP_VERSION} SOVERSION ${FREERDP_API_VERSION})
endif()
target_link_libraries(${MODULE_NAME} PRIVATE ${LIBFREERDP_LIBS} winpr)
target_link_libraries(${MODULE_NAME} PRIVATE ${LIBFREERDP_LIBS})
if (LIBFREERDP_PUB_LIBS)
target_link_libraries(${MODULE_NAME} PUBLIC ${LIBFREERDP_PUB_LIBS} winpr)
endif()
install(TARGETS ${MODULE_NAME} COMPONENT libraries EXPORT FreeRDPTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

View File

@ -111,6 +111,43 @@ static BOOL json_get_object(wLog* wlog, cJSON* json, const char* key, cJSON** ob
return TRUE;
}
#if defined(USE_CJSON_COMPAT)
FREERDP_API double cJSON_GetNumberValue(const cJSON* const item);
double cJSON_GetNumberValue(const cJSON* const prop)
{
#ifndef NAN
#ifdef _WIN32
#define NAN sqrt(-1.0)
#define COMPAT_NAN_UNDEF
#else
#define NAN 0.0 / 0.0
#define COMPAT_NAN_UNDEF
#endif
#endif
if (!cJSON_IsNumber(prop))
return NAN;
char* val = cJSON_GetStringValue(prop);
if (!val)
return NAN;
errno = 0;
char* endptr = NULL;
double dval = strtod(val, &endptr);
if (val == endptr)
return NAN;
if (endptr != NULL)
return NAN;
if (errno != 0)
return NAN;
return dval;
#ifdef COMPAT_NAN_UNDEF
#undef NAN
#endif
}
#endif
static BOOL json_get_number(wLog* wlog, cJSON* json, const char* key, double* result)
{
BOOL rc = FALSE;
@ -123,26 +160,9 @@ static BOOL json_get_number(wLog* wlog, cJSON* json, const char* key, double* re
WLog_Print(wlog, WLOG_ERROR, "[json] object for key '%s' is NOT a NUMBER", key);
goto fail;
}
#if defined(USE_CJSON_COMPAT)
if (!cJSON_IsNumber(prop))
goto fail;
char* val = cJSON_GetStringValue(prop);
if (!val)
goto fail;
errno = 0;
char* endptr = NULL;
double dval = strtod(val, &endptr);
if (val == endptr)
goto fail;
if (endptr != NULL)
goto fail;
if (errno != 0)
goto fail;
*result = dval;
#else
*result = cJSON_GetNumberValue(prop);
#endif
rc = TRUE;
fail:
return rc;
@ -187,18 +207,18 @@ static BOOL json_get_string_alloc(wLog* wlog, cJSON* json, const char* key, char
return *result != NULL;
}
static cJSON* compat_cJSON_ParseWithLength(const char* value, size_t buffer_length)
{
#if defined(USE_CJSON_COMPAT)
FREERDP_API cJSON* cJSON_ParseWithLength(const char* value, size_t buffer_length);
cJSON* cJSON_ParseWithLength(const char* value, size_t buffer_length)
{
// Check for string '\0' termination.
const size_t slen = strnlen(value, buffer_length);
if (slen >= buffer_length)
return NULL;
return cJSON_Parse(value);
#else
return cJSON_ParseWithLength(value, buffer_length);
#endif
}
#endif
static BOOL aad_get_nonce(rdpAad* aad)
{
@ -507,7 +527,7 @@ static int aad_parse_state_initial(rdpAad* aad, wStream* s)
if (!Stream_SafeSeek(s, jlen))
goto fail;
json = compat_cJSON_ParseWithLength(jstr, jlen);
json = cJSON_ParseWithLength(jstr, jlen);
if (!json)
goto fail;
@ -531,7 +551,7 @@ static int aad_parse_state_auth(rdpAad* aad, wStream* s)
if (!Stream_SafeSeek(s, jlength))
goto fail;
json = compat_cJSON_ParseWithLength(jstr, jlength);
json = cJSON_ParseWithLength(jstr, jlength);
if (!json)
goto fail;