Skip to content

Commit

Permalink
Add vcpkg.json
Browse files Browse the repository at this point in the history
  • Loading branch information
imbillow committed Nov 20, 2023
1 parent d78d0ca commit f19371d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -771,14 +771,16 @@ endif()
if(CAPSTONE_BUILD_CSTEST)
find_package(PkgConfig REQUIRED)
pkg_check_modules(CMOCKA REQUIRED IMPORTED_TARGET cmocka)
find_path(DIRENT_INCLUDE_DIRS "dirent.h")

file(GLOB CSTEST_SRC suite/cstest/src/*.c)
file(GLOB CSTEST_SRC suite/cstest/src/*.c cstool/getopt.c)
add_executable(cstest ${CSTEST_SRC})
target_link_libraries(cstest PUBLIC capstone PkgConfig::CMOCKA)
target_include_directories(cstest PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
${PROJECT_SOURCE_DIR}/suite/cstest/include
${CMOCKA_INCLUDE_DIRS}
${DIRENT_INCLUDE_DIRS}
)

if(CAPSTONE_INSTALL)
Expand Down
12 changes: 9 additions & 3 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"version": 3,
"configurePresets": [
{
"name": "default",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}
},
{
"name": "locations-base",
"hidden": true,
Expand Down Expand Up @@ -39,17 +45,17 @@
},
{
"name": "linux-x64",
"inherits": [ "ninja", "x64", "locations-base", "warnings-base" ],
"inherits": [ "ninja", "x64", "locations-base", "warnings-base", "default" ],
"condition": {"type": "equals", "lhs": "${hostSystemName}", "rhs": "Linux"}
},
{
"name": "macos-x64",
"inherits": [ "ninja", "x64", "locations-base", "warnings-base" ],
"inherits": [ "ninja", "x64", "locations-base", "warnings-base", "default" ],
"condition": {"type": "equals", "lhs": "${hostSystemName}", "rhs": "Darwin"}
},
{
"name": "windows-x64",
"inherits": [ "ninja", "x64", "locations-base", "warnings-base" ],
"inherits": [ "ninja", "x64", "locations-base", "warnings-base", "default" ],
"condition": {"type": "equals", "lhs": "${hostSystemName}", "rhs": "Windows"}
}
],
Expand Down
85 changes: 60 additions & 25 deletions suite/cstest/src/helper.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
/* Capstone testing regression */
/* By Do Minh Tuan <[email protected]>, 02-2019 */


#include "helper.h"

#if defined(_WIN32) || defined(_WIN64)

#include <math.h>

// strndup() is not available on Windows
static char *strndup(const char *s1, size_t n)
{
char *copy = (char *)malloc(n + 1);
memcpy(copy, s1, n);
copy[n] = 0;
return copy;
};
#endif

// strndup() was only added in OSX lion
#if defined(__APPLE__)
static char *strndup(const char *s1, size_t n)
{
char *copy = calloc(n + 1, sizeof(char));
memcpy(copy, s1, n);
return copy;
};
#endif

char **split(char *str, char *delim, int *size)
{
char **result;
Expand All @@ -16,17 +39,18 @@ char **split(char *str, char *delim, int *size)

while ((token = strstr(src, delim)) != NULL) {
result = (char **)realloc(result, sizeof(char *) * (cnt + 1));
result[cnt] = (char *)calloc(1, sizeof(char) * (int)(token - src + 10));
result[cnt] = (char *)calloc(
1, sizeof(char) * (int)(token - src + 10));
memcpy(result[cnt], src, token - src);
result[cnt][token - src] = '\0';
src = token + strlen(delim);
cnt ++;
cnt++;
}

if (strlen(src) > 0) {
result = (char **)realloc(result, sizeof(char *) * (cnt + 1));
result[cnt] = strdup(src);
cnt ++;
cnt++;
}

*size = cnt;
Expand All @@ -39,7 +63,7 @@ void print_strs(char **list_str, int size)

printf("[+] Debug %d strings:\n", size);
for (i = 0; i < size; ++i)
printf("String %d'th: %s\n", i+1, list_str[i]);
printf("String %d'th: %s\n", i + 1, list_str[i]);
}

void free_strs(char **list_str, int size)
Expand Down Expand Up @@ -97,7 +121,7 @@ void add_str(char **src, const char *format, ...)
vsprintf(tmp, format, args);
va_end(args);

len1 = strlen(*src);
len1 = strlen(*src);
len2 = strlen(tmp);

*src = (char *)realloc(*src, sizeof(char) * (len1 + len2 + 10));
Expand Down Expand Up @@ -126,22 +150,25 @@ void replace_hex(char *src)
tmp_tmp = strndup(tmp, orig_found - tmp);
while (*found != '\0' && isxdigit(*found)) {
valid = 1;
if (*found >= 'a' && *found <='f')
value = value*0x10 + (*found - 'a' + 10);
if (*found >= 'a' && *found <= 'f')
value = value * 0x10 + (*found - 'a' + 10);
else
value = value*0x10 + (*found - '0');
value = value * 0x10 + (*found - '0');
found++;
}

if (valid == 1) add_str(&result, "%s%llu", tmp_tmp, value);
else add_str(&result, "%s0x", tmp_tmp);
if (valid == 1)
add_str(&result, "%s%llu", tmp_tmp, value);
else
add_str(&result, "%s0x", tmp_tmp);
tmp = found;
free(tmp_tmp);
}

add_str(&result, "%s", tmp);
if (strlen(result) >= MAXMEM) {
fprintf(stderr, "[ Error ] --- Buffer Overflow in replace_hex()\n");
fprintf(stderr,
"[ Error ] --- Buffer Overflow in replace_hex()\n");
free(result);
free(origin);
_fail(__FILE__, __LINE__);
Expand All @@ -168,9 +195,9 @@ void replace_negative(char *src, int mode)

while ((found = strstr(tmp, "-")) != NULL) {
orig_found = found;
found ++;
found++;
valid = 0;

value = strdup("-");
cnt = 2;

Expand All @@ -179,7 +206,7 @@ void replace_negative(char *src, int mode)
value = (char *)realloc(value, cnt + 1);
value[cnt - 1] = *found;
value[cnt] = '\0';
cnt ++;
cnt++;
found++;
}

Expand All @@ -196,8 +223,8 @@ void replace_negative(char *src, int mode)
sscanf(value, "%lu", &tmp_long);
add_str(&result, "%s%lu", tmp_tmp, tmp_long);
}
}
else add_str(&result, "%s-", tmp_tmp);
} else
add_str(&result, "%s-", tmp_tmp);

tmp = found;
free(value);
Expand All @@ -206,7 +233,8 @@ void replace_negative(char *src, int mode)

add_str(&result, "%s", tmp);
if (strlen(result) >= MAXMEM) {
fprintf(stderr, "[ Error ] --- Buffer Overflow in replace_negative()\n");
fprintf(stderr,
"[ Error ] --- Buffer Overflow in replace_negative()\n");
free(result);
free(origin);
_fail(__FILE__, __LINE__);
Expand All @@ -229,16 +257,21 @@ void listdir(const char *name, char ***files, int *num_files)
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
snprintf(path, sizeof(path), "%s/%s", name,
entry->d_name);
listdir(path, files, num_files);
} else {
cnt = *num_files;
*files = (char **)realloc(*files, sizeof(char *) * (cnt + 1));
(*files)[cnt] = (char *)malloc(sizeof(char) * ( strlen(name) + 1 + strlen(entry->d_name) + 10));
*files = (char **)realloc(*files,
sizeof(char *) * (cnt + 1));
(*files)[cnt] = (char *)malloc(
sizeof(char) * (strlen(name) + 1 +
strlen(entry->d_name) + 10));
sprintf((*files)[cnt], "%s/%s", name, entry->d_name);
cnt ++;
cnt++;
*num_files = cnt;
}
}
Expand All @@ -254,8 +287,10 @@ void trim_str(char *str)
start = 0;
end = strlen(str) - 1;
j = 0;
while (start < strlen(str) && isspace(str[start])) start++;
while (end >= 0 && isspace(str[end])) end--;
while (start < strlen(str) && isspace(str[start]))
start++;
while (end >= 0 && isspace(str[end]))
end--;

for (i = start; i <= end; ++i)
tmp[j++] = str[i];
Expand Down
2 changes: 1 addition & 1 deletion suite/cstest/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "helper.h"
#include "capstone_test.h"
#include <unistd.h>
#include "../../../cstool/getopt.h"

#define ARR_SIZE(a) (sizeof(a)/sizeof(a[0]))

Expand Down

0 comments on commit f19371d

Please sign in to comment.