Implement nd_vsnprintf_s and nd_memset if possible

pull/40/head
Ionel-Cristinel ANICHITEI 3 years ago
parent 9a30b907c7
commit e7803bdf72

@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.12)
option(BDD_INCLUDE_TOOL "Include the disasmtool executable" ON) option(BDD_INCLUDE_TOOL "Include the disasmtool executable" ON)
option(BDD_INCLUDE_ISAGENERATOR "Include the isagenerator target (if a python interpreter is found)" ON) option(BDD_INCLUDE_ISAGENERATOR "Include the isagenerator target (if a python interpreter is found)" ON)
option(BDD_USE_EXTERNAL_VSNPRINTF "Expect nd_vsnprintf_s implementation from the integrator" OFF)
option(BDD_USE_EXTERNAL_MEMSET "Expect nd_memset implementation from the integrator" OFF)
set(BDD_VER_FILE ${CMAKE_CURRENT_LIST_DIR}/inc/version.h) set(BDD_VER_FILE ${CMAKE_CURRENT_LIST_DIR}/inc/version.h)
@ -87,6 +89,9 @@ set(BDDISASM_INSTALL_INCLUDE_DIR
# -- bddisasm -- # -- bddisasm --
include(CheckFunctionExists)
include(CheckSymbolExists)
add_library( add_library(
bddisasm STATIC bddisasm STATIC
bddisasm/crt.c bddisasm/crt.c
@ -103,6 +108,26 @@ add_library(
bddisasm/include/tabledefs.h bddisasm/include/tabledefs.h
"${BDDISASM_PUBLIC_HEADERS}") "${BDDISASM_PUBLIC_HEADERS}")
if (NOT BDD_USE_EXTERNAL_VSNPRINTF)
check_function_exists(vsnprintf HAS_VSNPRINTF)
if (HAS_VSNPRINTF)
target_compile_definitions(bddisasm PUBLIC -DBDDISASM_HAS_VSNPRINTF)
else ()
# See https://cmake.org/Bug/view.php?id=15659
check_symbol_exists(vsnprintf stdio.h HAS_VSNPRINTF_SYMBOL)
if (HAS_VSNPRINTF_SYMBOL)
target_compile_definitions(bddisasm PUBLIC -DBDDISASM_HAS_VSNPRINTF)
endif ()
endif ()
endif ()
if (NOT BDD_USE_EXTERNAL_MEMSET)
check_function_exists(memset HAS_MEMSET)
if (HAS_MEMSET)
target_compile_definitions(bddisasm PUBLIC -DBDDISASM_HAS_MEMSET)
endif ()
endif ()
set_target_properties( set_target_properties(
bddisasm bddisasm
PROPERTIES POSITION_INDEPENDENT_CODE ON PROPERTIES POSITION_INDEPENDENT_CODE ON

@ -94,6 +94,10 @@ target_link_libraries(decoder-tool PRIVATE bddisasm::bddisasm)
target_link_libraries(emulator-tool PRIVATE bddisasm::bdshemu) target_link_libraries(emulator-tool PRIVATE bddisasm::bdshemu)
``` ```
### nd_vsnprintf_s and nd_memset
By default, if `vsnprintf` and `memset` functions are available, the `nd_vsnprintf_s` and `nd_memset` functions are implemented directly by `bddisasm`. To signal this, `BDDISASM_HAS_VSNPRINTF` and `BDDISASM_HAS_MEMSET` will be added to the public compile definitions of `bddisasm`. This can be disabled by configuring CMake with `BDD_USE_EXTERNAL_VSNPRINTF=ON` and `BDD_USE_EXTERNAL_MEMSET=ON`.
#### Using as a sub-project #### Using as a sub-project
The project can be consumed as a sub-project, either by adding it as a git submodule, or by using [CMake's FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html): The project can be consumed as a sub-project, either by adding it as a git submodule, or by using [CMake's FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html):
@ -127,6 +131,8 @@ To build the project run `make` in the root of the repository. This will build o
To install the project run `make install`. Depending on the install location you may need to run the command as root. To install the project run `make install`. Depending on the install location you may need to run the command as root.
[nd_vsnprintf_s and nd_memset](#nd_vsnprintf_s-and-nd_memset) will not be defined by `bddisasm`, integrators must provide these functions.
### Using MSBuild on Windows ### Using MSBuild on Windows
In order to build the projects on Windows you need: In order to build the projects on Windows you need:
@ -146,6 +152,8 @@ Building any of the projects is done directly from Visual Studio.
The results will be in the bin directory in the root of the repository. The results will be in the bin directory in the root of the repository.
[nd_vsnprintf_s and nd_memset](#nd_vsnprintf_s-and-nd_memset) will not be defined by `bddisasm`, integrators must provide these functions.
## Decoding instructions ## Decoding instructions
### Decoding API ### Decoding API

@ -4,7 +4,6 @@
*/ */
#include "include/nd_crt.h" #include "include/nd_crt.h"
// //
// nd_strcat_s // nd_strcat_s
// //
@ -42,3 +41,22 @@ nd_strcat_s(
return dst; return dst;
} }
#if !defined(BDDISASM_NO_FORMAT) && defined(BDDISASM_HAS_VSNPRINTF)
#include <stdio.h>
int nd_vsnprintf_s(char *buffer, size_t sizeOfBuffer, size_t count, const char *format, va_list argptr)
{
UNREFERENCED_PARAMETER(count);
return vsnprintf(buffer, sizeOfBuffer, format, argptr);
}
#endif // !defined(BDDISASM_NO_FORMAT) && defined(BDDISASM_HAS_VSNPRINTF)
#if !defined(BDDISASM_NO_FORMAT) && defined(BDDISASM_HAS_MEMSET)
#include <string.h>
void *nd_memset(void *s, int c, size_t n)
{
return memset(s, c, n);
}
#endif // !defined(BDDISASM_NO_FORMAT) && defined(BDDISASM_HAS_MEMSET)

@ -62,6 +62,7 @@ char *gSpaces[16] =
}; };
#if !defined(BDDISASM_HAS_VSNPRINTF)
// //
// nd_vsnprintf // nd_vsnprintf
// //
@ -75,12 +76,14 @@ int nd_vsnprintf_s(
{ {
return _vsnprintf_s(buffer, sizeOfBuffer, count, format, argptr); return _vsnprintf_s(buffer, sizeOfBuffer, count, format, argptr);
} }
#endif // !defined(BDDISASM_HAS_VSNPRINTF)
#if !defined(BDDISASM_HAS_MEMSET)
void* nd_memset(void *s, int c, size_t n) void* nd_memset(void *s, int c, size_t n)
{ {
return memset(s, c, n); return memset(s, c, n);
} }
#endif // !defined(BDDISASM_HAS_MEMSET)
// //
// set_to_string // set_to_string

@ -77,19 +77,22 @@ struct options {
extern "C" extern "C"
{ {
#if !defined(BDDISASM_HAS_VSNPRINTF)
int nd_vsnprintf_s(char *buffer, size_t sizeOfBuffer, [[maybe_unused]] size_t count, const char *format, va_list argptr) int nd_vsnprintf_s(char *buffer, size_t sizeOfBuffer, [[maybe_unused]] size_t count, const char *format, va_list argptr)
{ {
return vsnprintf(buffer, sizeOfBuffer, format, argptr); return vsnprintf(buffer, sizeOfBuffer, format, argptr);
} }
#endif // !defined(BDDISASM_HAS_VSNPRINTF)
#if !defined(BDDISASM_HAS_MEMSET)
void * void *
nd_memset(void *s, int c, size_t n) nd_memset(void *s, int c, size_t n)
{ {
return memset(s, c, n); return memset(s, c, n);
} }
#endif // !defined(BDDISASM_HAS_MEMSET)
} }
static bool _hexstring_to_bytes(options &opts) static bool _hexstring_to_bytes(options &opts)
{ {
if (!opts.hex_file.empty()) { if (!opts.hex_file.empty()) {

Loading…
Cancel
Save