diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..70b7b6b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.sh eol=lf +Testing/example_data/*.txt binary diff --git a/.travis.yml b/.travis.yml index 98d1df8..f51dbeb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,11 @@ language: cpp -compiler: - - gcc +matrix: + include: + - os: linux + compiler: gcc + - os: osx + compiler: clang + - os: windows -script: - - ./make.sh +script: ./make.sh || cat CMakeOutput.log diff --git a/CMakeLists.txt b/CMakeLists.txt index 16c0743..b97c517 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,7 +51,7 @@ # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED # OF THE POSSIBILITY OF SUCH DAMAGE. -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 2.8) project(TLSH) @@ -59,65 +59,71 @@ set(VERSION_MAJOR 3) set(VERSION_MINOR 13) set(VERSION_PATCH 1) +# Standard shared library flag is supported, enable to build a shared library +#set(BUILD_SHARED_LIBS OFF) + +# Edit those values or call cmake with options defined below +set(TLSH_BUCKETS_DEFAULT "TLSH_BUCKETS_128") +set(TLSH_CHECKSUM_DEFAULT "TLSH_CHECKSUM_1B") + # TLSH uses only half the counting buckets. # It can use all the buckets now. -set(TLSH_BUCKETS_128 1) -if(TLSH_BUCKETS_48 EQUAL 1) - set(TLSH_HASH "min hash") - add_definitions(-DBUCKETS_48) +option(TLSH_BUCKETS_48 "Use 48 bits buckets" OFF) +option(TLSH_BUCKETS_128 "Use 128 bits buckets" OFF) +option(TLSH_BUCKETS_256 "Use 256 bits buckets" OFF) + +if(NOT TLSH_BUCKETS_48 AND NOT TLSH_BUCKETS_128 AND NOT TLSH_BUCKETS_256) + set(${TLSH_BUCKETS_DEFAULT} ON) endif() -if(TLSH_BUCKETS_128 EQUAL 1) + +if(TLSH_BUCKETS_48) + set(TLSH_HASH "min hash") + set(TLSH_BUCKETS_SIZE "BUCKETS_48") +elseif(TLSH_BUCKETS_128) set(TLSH_HASH "compact hash") - add_definitions(-DBUCKETS_128) -endif() -if(TLSH_BUCKETS_256 EQUAL 1) + set(TLSH_BUCKETS_SIZE "BUCKETS_128") +elseif(TLSH_BUCKETS_256) set(TLSH_HASH "full hash") - add_definitions(-DBUCKETS_256) + set(TLSH_BUCKETS_SIZE "BUCKETS_256") endif() -# TLSH uses 1 byte checksum. The collision rate is 1 in 24. -# It can use 3 bytes checksum now. That collision rate in 1 in 5800. -set(TLSH_CHECKSUM_1B 1) +message(STATUS "Hash type: ${TLSH_HASH}") -if(TLSH_CHECKSUM_0B EQUAL 1) - set(TLSH_CHECKSUM "no checksum") - add_definitions(-DCHECKSUM_0B) +option(TLSH_CHECKSUM_0B "Do not use checksum" OFF) +option(TLSH_CHECKSUM_1B "Use 1 byte checksum (collision rate: 1/24)" OFF) +option(TLSH_CHECKSUM_3B "Use 3 bytes checksum (collision rate: 1/5000)" OFF) + +if(NOT TLSH_CHECKSUM_0B AND NOT TLSH_CHECKSUM_1B AND NOT TLSH_CHECKSUM_3B) + set(${TLSH_CHECKSUM_DEFAULT} ON) endif() -if(TLSH_CHECKSUM_1B EQUAL 1) + +if(TLSH_CHECKSUM_0B) + set(TLSH_CHECKSUM "no checksum") + set(TLSH_CHECKSUM_TYPE "CHECKSUM_0B") +elseif(TLSH_CHECKSUM_1B) set(TLSH_CHECKSUM "1 byte checksum") -endif() -if(TLSH_CHECKSUM_3B EQUAL 1) + set(TLSH_CHECKSUM_TYPE "CHECKSUM_1B") +elseif(TLSH_CHECKSUM_3B) set(TLSH_CHECKSUM "3 bytes checksum") - add_definitions(-DCHECKSUM_3B) + set(TLSH_CHECKSUM_TYPE "CHECKSUM_3B") endif() -# setting TLSH_DISTANCE_PARAMETERS to 1 allows you to set command line arguments -# to set - and hence experiment with the distance parameters -# by default this is zero -set(TLSH_DISTANCE_PARAMETERS 0) - -# write a file with the VERSION information -file(REMOVE VERSION) -file(WRITE VERSION - "// This file is generated by cmake. Modify\n" - "// CMakeLists.txt to change the VERSION numbers\n" - "TLSH version: ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} ${TLSH_HASH}, ${TLSH_CHECKSUM}\n") - -file(REMOVE include/version.h) -file(WRITE include/version.h - "/****************************************************\n" - " * This file is generated by cmake. Modify the top\n" - " * level CMakeLists.txt to change the VERSION numbers\n" - " ****************************************************/\n\n" - "#define VERSION_MAJOR ${VERSION_MAJOR}\n" - "#define VERSION_MINOR ${VERSION_MINOR}\n" - "#define VERSION_PATCH ${VERSION_PATCH}\n" - "#define TLSH_HASH \"${TLSH_HASH}\"\n" - "#define TLSH_CHECKSUM \"${TLSH_CHECKSUM}\"\n") -if(TLSH_DISTANCE_PARAMETERS EQUAL 1) -file(APPEND include/version.h - "#define TLSH_DISTANCE_PARAMETERS ${TLSH_DISTANCE_PARAMETERS}\n") -endif() +message(STATUS "Checksum: ${TLSH_CHECKSUM}") + +# setting TLSH_DISTANCE_PARAMETERS to 'ON' allows you to set command line +# arguments to set - and hence experiment with the distance parameters +option(TLSH_DISTANCE_PARAMETERS "Enable cli control of the distance parameters" OFF) + +configure_file(cmake/VERSION.in ${CMAKE_CURRENT_SOURCE_DIR}/VERSION) +configure_file(cmake/version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/include/version.h) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${TLSH_SOURCE_DIR}/bin) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${TLSH_SOURCE_DIR}/bin) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${TLSH_SOURCE_DIR}/bin) + +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${TLSH_SOURCE_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${TLSH_SOURCE_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${TLSH_SOURCE_DIR}/lib) if (CMAKE_BUILD_TYPE STREQUAL Debug) if(CMAKE_COMPILER_IS_GNUCXX) @@ -132,11 +138,6 @@ else(CMAKE_BUILD_TYPE STREQUAL Debug) endif() endif(CMAKE_BUILD_TYPE STREQUAL Debug) -if(MSVC) - add_definitions(-DWINDOWS -DTLSH_LIB) - include_directories(Windows) -endif() - # user can override CXX; make sure tests link and load properly regardless of LD_LIBRARY_PATH if(CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc") @@ -145,7 +146,6 @@ endif() enable_testing() -include_directories(include) add_subdirectory(src) add_subdirectory(test) add_subdirectory(utils) diff --git a/README.md b/README.md index c45505b..204663d 100644 --- a/README.md +++ b/README.md @@ -69,11 +69,13 @@ git checkout master Edit [CMakeLists.txt](CMakeLists.txt) to build TLSH with different options. -- TLSH_BUCKETS: determines using 128 or 256 buckets +- TLSH_BUCKETS_DEFAULT: determines using 128 or 256 buckets use the default 128 buckets unless you are an expert and know you need 256 buckets -- TLSH_CHECKSUM_1B: determines checksum length, longer means less collision +- TLSH_CHECKSUM_DEFAULT: determines checksum length, longer means less collision use the default 1 byte unless you are an expert and know you need a larger checksum +Alternatively, those values can be set from usual cmake options if you are not using the provided build script (see TLSH_BUCKETS_128 ) + ## Linux Execute: @@ -86,14 +88,29 @@ make.sh `make` the project, so the build will fail if `cmake` is not installed.* -## Windows (Visual Studio) +## Windows +### Visual Studio versioned .sln Use the version-specific tlsh solution files ([tlsh.VC2005.sln](Windows/tlsh.VC2005.sln), [tlsh.VC2008.sln](Windows/tlsh.VC2008.sln), ...) under the Windows directory. See [tlsh.h](include/tlsh.h) for the tlsh library interface and [tlsh_unittest.cpp](test/tlsh_unittest.cpp) and [simple_unittest.cpp](test/simple_unittest.cpp) under the `test` directory for example code. +**Note:** *It is also possible to generate .sln files using cmake* +``` +mkdir build +cd build +cmake .. +``` + +### Visual Studio and WSL or Git Bash +Within WSL's bash shell or Git Bash's execute: + +```bash +./make.sh +``` + ## Python Extension There is a README.python with notes about the python version diff --git a/Testing/CMakeLists.txt b/Testing/CMakeLists.txt index ce85448..154a930 100644 --- a/Testing/CMakeLists.txt +++ b/Testing/CMakeLists.txt @@ -1,10 +1,10 @@ # set(CTEST_OUTPUT_ON_FAILURE true) # cmake bug - the above does not work, call "make test --output-on-failure" # instead -add_test(tlsh_unittest_len "${CMAKE_SOURCE_DIR}/Testing/test.sh") -add_test(tlsh_unittest_xlen "${CMAKE_SOURCE_DIR}/Testing/test.sh" "-xlen") +add_test(tlsh_unittest_len "bash" "${TLSH_SOURCE_DIR}/Testing/test.sh") +add_test(tlsh_unittest_xlen "bash" "${TLSH_SOURCE_DIR}/Testing/test.sh" "-xlen") if(CMAKE_HOST_UNIX) - if(CMAKE_SHARED_LIBRARY EQUAL 1) + if(BUILD_SHARED_LIBS) set_tests_properties(tlsh_unittest_len tlsh_unittest_xlen PROPERTIES ENVIRONMENT "LD_PRELOAD=${CMAKE_SOURCE_DIR}/lib/libtlsh.so.0") endif() endif() diff --git a/Testing/test.sh b/Testing/test.sh index 147e718..1fee198 100755 --- a/Testing/test.sh +++ b/Testing/test.sh @@ -7,20 +7,25 @@ export LD_LIBRARY_PATH=../lib:$LD_LIBRARY_PATH BASEDIR=$(dirname $0) pushd $BASEDIR > /dev/null +# Check Windows typical env variable to add extension for git bash +if [ ! -z "${WINDIR}" ]; then + EXT=".exe" +fi + TMP="tmp" -HASH=`../bin/tlsh_version | head -1 | cut -f1` -CHKSUM=`../bin/tlsh_version | tail -1 | cut -f1` +HASH=`../bin/tlsh_version${EXT} | head -1 | cut -f1` +CHKSUM=`../bin/tlsh_version${EXT} | tail -1 | cut -f1` echo "HASH is $HASH" echo "CHKSUM is $CHKSUM" -if test ! -f ../bin/tlsh +if test ! -f ../bin/tlsh${EXT} then echoerr "error: (127), you must compile tlsh" popd > /dev/null exit 127 fi -if test ! -f ../bin/simple_unittest +if test ! -f ../bin/simple_unittest${EXT} then echoerr "error: (127), you must compile ../bin/simple_unittest" popd > /dev/null @@ -51,10 +56,10 @@ runit() { fi if test "$1" = "-tlsh_c" then - TLSH_PROG="tlsh_c" + TLSH_PROG="tlsh_c${EXT}" echo "Scenario: tlsh_c (c standalone version)..." else - TLSH_PROG="tlsh" + TLSH_PROG="tlsh${EXT}" echo "Scenario: tlsh (c++ standard version)..." fi @@ -274,7 +279,7 @@ echo "passed" echo echo "Running simple_unittest" -../bin/simple_unittest > $TMP/simple_unittest.out +../bin/simple_unittest${EXT} > $TMP/simple_unittest.out diff --ignore-all-space $TMP/simple_unittest.out exp/simple_unittest_EXP > /dev/null 2>/dev/null if [ $? -ne 0 ]; then echoerr "error: diff $TMP/simple_unittest.out exp/simple_unittest_EXP" diff --git a/Testing/testlen.sh b/Testing/testlen.sh index 4c98a29..7dc84cf 100755 --- a/Testing/testlen.sh +++ b/Testing/testlen.sh @@ -8,6 +8,11 @@ fi rm -f testlen.txt +# Check Windows typical env variable to add extension for git bash +if [ ! -z "${WINDIR}" ]; then + EXT=".exe" +fi + echo "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()" > testlen.txt echo "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()" >> testlen.txt cat testlen.txt testlen.txt > testlen2.txt @@ -15,7 +20,7 @@ cat testlen.txt testlen.txt > testlen2.txt for x in `seq 1 $HIGH_SEQ` ; do echo "iter $x" wc -c testlen2.txt - ../bin/tlsh -f testlen2.txt + ../bin/tlsh${EXT} -f testlen2.txt # # grow the size of the file according to the Fibonacci sequence # diff --git a/Windows/CMakeLists.txt b/Windows/CMakeLists.txt new file mode 100644 index 0000000..0a3cc22 --- /dev/null +++ b/Windows/CMakeLists.txt @@ -0,0 +1,94 @@ +# TLSH is provided for use under two licenses: Apache OR BSD. +# Users may opt to use either license depending on the license +# restictions of the systems with which they plan to integrate +# the TLSH code. +# +# ============== +# Apache License +# ============== +# Copyright 2013 Trend Micro Incorporated +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# =========== +# BSD License +# =========== +# Copyright (c) 2013, Trend Micro Incorporated +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. + +# 3. Neither the name of the copyright holder nor the names of its contributors +# may be used to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED +# OF THE POSSIBILITY OF SUCH DAMAGE. + +add_library(winfunc STATIC + WinFunctions.h + WinFunctions.cpp +) + +target_compile_definitions(winfunc + PUBLIC + -DWINDOWS + -D${TLSH_BUCKETS_SIZE} + -D${TLSH_CHECKSUM_TYPE} +) + +if(BUILD_SHARED_LIBS) + target_compile_definitions(winfunc PRIVATE -DTLSH_EXPORTS) +else() + target_compile_definitions(winfunc PUBLIC -DTLSH_LIB) +endif() + +# Disable unwanted microsoft warnings +target_compile_options(winfunc + PUBLIC + /wd4477 + /wd4267 + /wd4996 + /EHsc +) + +target_include_directories(winfunc + PUBLIC + $ +) + +if(NOT BUILD_SHARED_LIBS) + install( + TARGETS winfunc + EXPORT tlsh-export + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + ) +endif() diff --git a/Windows/WinFunctions.cpp b/Windows/WinFunctions.cpp index 5817561..3552d89 100644 --- a/Windows/WinFunctions.cpp +++ b/Windows/WinFunctions.cpp @@ -4,6 +4,29 @@ #include +#include // struct timeval +#include + +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +int gettimeofday(struct timeval *tv, struct timezone *tzp) { + FILETIME file_time; + GetSystemTimeAsFileTime(&file_time); + + uint64_t time = ((uint64_t)file_time.dwLowDateTime); + time += ((uint64_t)file_time.dwHighDateTime) << 32; + + time /= 10; // convert into microseconds + time -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(time / 1000000UL); + tv->tv_usec = (long)(time % 1000000UL); + return 0; +} + DIR *opendir(const char *dirname) { if (strlen(dirname) >= NAME_LENGTH) { diff --git a/Windows/WinFunctions.h b/Windows/WinFunctions.h index 31930d3..5ef6d1e 100644 --- a/Windows/WinFunctions.h +++ b/Windows/WinFunctions.h @@ -6,16 +6,6 @@ #include #include -#ifndef TLSH_LIB -# ifdef TLSH_EXPORTS -# define TLSH_API __declspec(dllexport) -# else -# define TLSH_API __declspec(dllimport) -# endif -#else -# define TLSH_API -#endif - #define strdup _strdup #define NAME_LENGTH MAX_PATH #define snprintf _snprintf @@ -40,6 +30,7 @@ extern struct dirent *readdir(DIR *dir); extern int closedir(DIR *dir); extern struct tm *localtime_r(const time_t *timep, struct tm *results); extern bool read_file_win(const char *fname, int sizefile, unsigned char* data); +extern int gettimeofday(struct timeval *tv, struct timezone *tz); #endif // #ifndef WINFUNCTIONS_H diff --git a/cmake/VERSION.in b/cmake/VERSION.in new file mode 100644 index 0000000..535f498 --- /dev/null +++ b/cmake/VERSION.in @@ -0,0 +1,3 @@ +// This file is generated by cmake. Modify +// CMakeLists.txt to change the VERSION numbers +TLSH version: @VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@ @TLSH_HASH@, @TLSH_CHECKSUM@ diff --git a/cmake/version.h.in b/cmake/version.h.in new file mode 100644 index 0000000..cf139c9 --- /dev/null +++ b/cmake/version.h.in @@ -0,0 +1,12 @@ +/****************************************************" + * This file is generated by cmake. Modify the top + * level CMakeLists.txt to change the VERSION numbers + ****************************************************/ + +#define VERSION_MAJOR @VERSION_MAJOR@ +#define VERSION_MINOR @VERSION_MINOR@ +#define VERSION_PATCH @VERSION_PATCH@ +#define TLSH_HASH "@TLSH_HASH@" +#define TLSH_CHECKSUM "@TLSH_CHECKSUM@" + +#cmakedefine TLSH_DISTANCE_PARAMETERS diff --git a/include/tlsh.h b/include/tlsh.h index 5b8eeaa..b3e982d 100644 --- a/include/tlsh.h +++ b/include/tlsh.h @@ -60,11 +60,7 @@ #ifndef HEADER_TLSH_H #define HEADER_TLSH_H -#ifdef WINDOWS -#include "win_version.h" -#else #include "version.h" -#endif #ifndef NULL #define NULL 0 @@ -112,7 +108,15 @@ class TlshImpl; #define TLSH_STRING_BUFFER_LEN (TLSH_STRING_LEN+1) #ifdef WINDOWS -#include + #ifndef TLSH_LIB + #ifdef TLSH_EXPORTS + #define TLSH_API __declspec(dllexport) + #else + #define TLSH_API __declspec(dllimport) + #endif + #else + #define TLSH_API + #endif #else #if defined(__SPARC) || defined(_AS_MK_OS_RH73) #define TLSH_API diff --git a/include/tlsh_impl.h b/include/tlsh_impl.h index 4309674..ea7922e 100644 --- a/include/tlsh_impl.h +++ b/include/tlsh_impl.h @@ -55,11 +55,7 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifdef WINDOWS -#include "win_version.h" -#else #include "version.h" -#endif #ifndef HEADER_TLSH_IMPL_H #define HEADER_TLSH_IMPL_H diff --git a/include/win_version.h b/include/win_version.h deleted file mode 100644 index 29856df..0000000 --- a/include/win_version.h +++ /dev/null @@ -1,20 +0,0 @@ -/**************************************************** - * The file "version.h" is generated by cmake. - * But there was trouble creating this file on Windows - * so this is the default calues required - * Beware - do not change these values unless you really understand the software. - ****************************************************/ - -#define VERSION_MAJOR 3 -#define VERSION_MINOR 16 -#define VERSION_PATCH 1 -#define TLSH_HASH "compact hash" -#define TLSH_CHECKSUM "1 byte checksum" - -#define buckets 128 -#define TLSH_CHECKSUM_LEN 1 -#define EFF_BUCKETS 128 -#define CODE_SIZE 32 -#define MIN_DATA_LENGTH 256 -#define MIN_FORCE_DATA_LENGTH 50 -#define TLSH_STRING_LEN 70 diff --git a/make.sh b/make.sh index fb0187f..a1cf13c 100755 --- a/make.sh +++ b/make.sh @@ -1,5 +1,7 @@ #!/bin/sh +set -e + #################################################### # process command line options #################################################### @@ -27,9 +29,14 @@ if [ $# -eq 1 -a "$1" = "-c" ]; then makecversion=1 fi -make +cmake --build . + cd ../../bin -cmake -E create_symlink tlsh_unittest tlsh +if [ -z "${WINDIR}" ]; then + cmake -E create_symlink tlsh_unittest tlsh +else + cmake -E copy tlsh_unittest.exe tlsh.exe +fi cd - if test $notest = 0 @@ -38,7 +45,7 @@ then echo "===========" echo "Cmake Tests" echo "===========" - make test + ctest fi cd ../.. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5ab9ad2..01f07df 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -51,33 +51,63 @@ # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED # OF THE POSSIBILITY OF SUCH DAMAGE. -if(MSVC) - add_definitions(-DTLSH_EXPORTS -DTLSH_LIB) -endif() +set(TLSH_SRCS + tlsh.cpp + tlsh_impl.cpp + tlsh_util.cpp +) -set(TLSH_SRCS tlsh.cpp tlsh_impl.cpp tlsh_util.cpp) -add_library(tlsh STATIC ${TLSH_SRCS}) -set_target_properties(tlsh PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) -set_target_properties(tlsh PROPERTIES OUTPUT_NAME tlsh${BUILD_POSTFIX}) +add_library(tlsh ${TLSH_SRCS}) -########################## -# the tlsh library is small - there is no reason to have a shared library -# it was causing problems when compiling / testing tools on Linux -########################## +target_include_directories(tlsh + PUBLIC + $ + INTERFACE + $ +) -set(TLSH_SHARED_LIBRARY 0) -if(TLSH_SHARED_LIBRARY EQUAL 1) - add_library(tlsh_shared SHARED ${TLSH_SRCS}) - set_target_properties(tlsh_shared PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) - set_target_properties(tlsh_shared PROPERTIES OUTPUT_NAME tlsh${BUILD_POSTFIX}) - set_target_properties(tlsh_shared PROPERTIES VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}" - SOVERSION "0") -endif() +target_compile_definitions(tlsh + PUBLIC + -D${TLSH_BUCKETS_SIZE} + -D${TLSH_CHECKSUM_TYPE} + -D$<$:WINDOWS> +) -if(TLSH_SHARED_LIBRARY EQUAL 1) - install(TARGETS tlsh tlsh_shared DESTINATION lib) +if(BUILD_SHARED_LIBS) + target_compile_definitions(tlsh PRIVATE -DTLSH_EXPORTS) + + set_target_properties(tlsh + PROPERTIES + VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}" + SOVERSION "0" + ) else() - install(TARGETS tlsh DESTINATION lib) + target_compile_definitions(tlsh PUBLIC -DTLSH_LIB) endif() -install(FILES ../include/tlsh.h DESTINATION include) +target_link_libraries(tlsh PRIVATE $<$:winfunc>) + +if(NOT TLSH_NO_INSTALL_HEADERS) + set(PUBLIC_HEADERS + "${TLSH_SOURCE_DIR}/include/tlsh.h" + "${TLSH_SOURCE_DIR}/include/version.h" + ) + set_target_properties(tlsh PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}") +endif() + +install( + TARGETS tlsh + EXPORT tlsh-export + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + PUBLIC_HEADER DESTINATION include/tlsh +) + +install( + EXPORT tlsh-export + DESTINATION share/tlsh + FILE tlshConfig.cmake + NAMESPACE tlsh:: + CONFIGURATIONS Release +) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 074e8b4..cba442b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -51,36 +51,35 @@ # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED # OF THE POSSIBILITY OF SUCH DAMAGE. +if(MSVC) + set(EXTRALIBS winfunc) + add_compile_options(/EHsc) +endif() + add_executable(tlsh_version tlsh_version.cpp) -target_link_libraries(tlsh_version tlsh) -set_target_properties(tlsh_version PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set_target_properties(tlsh_version PROPERTIES OUTPUT_NAME tlsh_version${BUILD_POSTFIX}) +target_link_libraries(tlsh_version PRIVATE tlsh ${EXTRALIBS}) add_executable(simple_unittest simple_unittest.cpp) -target_link_libraries(simple_unittest tlsh) -set_target_properties(simple_unittest PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set_target_properties(simple_unittest PROPERTIES OUTPUT_NAME simple_unittest${BUILD_POSTFIX}) +target_link_libraries(simple_unittest PRIVATE tlsh ${EXTRALIBS}) add_executable(tlsh_unittest tlsh_unittest.cpp) -target_link_libraries(tlsh_unittest tlsh) -set_target_properties(tlsh_unittest PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set_target_properties(tlsh_unittest PROPERTIES OUTPUT_NAME tlsh_unittest${BUILD_POSTFIX}) +target_link_libraries(tlsh_unittest PRIVATE tlsh ${EXTRALIBS}) set_target_properties(tlsh_unittest PROPERTIES SKIP_BUILD_RPATH TRUE) add_executable(timing_unittest timing_unittest.cpp) -target_link_libraries(timing_unittest tlsh) -set_target_properties(timing_unittest PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set_target_properties(timing_unittest PROPERTIES OUTPUT_NAME timing_unittest${BUILD_POSTFIX}) +target_link_libraries(timing_unittest PRIVATE tlsh ${EXTRALIBS}) set_target_properties(timing_unittest PROPERTIES SKIP_BUILD_RPATH TRUE) add_executable(order_bug order_bug.cpp) -target_link_libraries(order_bug tlsh) -set_target_properties(order_bug PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set_target_properties(order_bug PROPERTIES OUTPUT_NAME order_bug${BUILD_POSTFIX}) +target_link_libraries(order_bug PRIVATE tlsh ${EXTRALIBS}) set_target_properties(order_bug PROPERTIES SKIP_BUILD_RPATH TRUE) add_executable(tlsh_pattern tlsh_pattern.cpp) -target_link_libraries(tlsh_pattern tlsh) -set_target_properties(tlsh_pattern PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set_target_properties(tlsh_pattern PROPERTIES OUTPUT_NAME tlsh_pattern${BUILD_POSTFIX}) +target_link_libraries(tlsh_pattern PRIVATE tlsh ${EXTRALIBS}) set_target_properties(tlsh_pattern PROPERTIES SKIP_BUILD_RPATH TRUE) diff --git a/test/order_bug.cpp b/test/order_bug.cpp index 933d7df..a2825e3 100644 --- a/test/order_bug.cpp +++ b/test/order_bug.cpp @@ -67,7 +67,10 @@ #include #include #include + +#ifndef WINDOWS #include +#endif #include "tlsh.h" diff --git a/test/timing_unittest.cpp b/test/timing_unittest.cpp index 9379a8a..0e375ed 100644 --- a/test/timing_unittest.cpp +++ b/test/timing_unittest.cpp @@ -67,7 +67,12 @@ #include #include #include -#include + +#ifndef WINDOWS + #include +#else + #include "WinFunctions.h" +#endif #include "tlsh.h" diff --git a/test/tlsh_unittest.cpp b/test/tlsh_unittest.cpp index 3ab5556..24d2a43 100644 --- a/test/tlsh_unittest.cpp +++ b/test/tlsh_unittest.cpp @@ -68,6 +68,7 @@ #include #ifdef WINDOWS +#include #include #else // for directory processing on Unix / Linux @@ -229,56 +230,40 @@ static int read_file_eval_tlsh(char *fname, Tlsh *th, int show_details, int forc return(0); } +#ifndef WINDOWS bool is_dir(char *dirname) { DIR *dip; if (dirname == NULL) { return(false); } -#ifndef WINDOWS dip = opendir(dirname); -#else - WIN32_FIND_DATA data; - HANDLE h = FindFirstFile(dirname, &data); - if (h != nullptr) - { - dip = new DIR(); - dip->hFind = h; - } -#endif if (dip == NULL) { return(false); } -#ifndef WINDOWS closedir(dip); -#else - FindClose(dip->hFind); - delete dip; -#endif return(true); } +#else +bool is_dir(char *dirname) +{ + DWORD dw = GetFileAttributes(dirname); + return dw & FILE_ATTRIBUTE_DIRECTORY ? true : false; +} +#endif struct FileName { char *tlsh; // Only used with -l parameter char *name; }; +#ifndef WINDOWS static int count_files_in_dir(char *dirname) { DIR *dip; struct dirent *dit; -#ifndef WINDOWS dip = opendir(dirname); -#else - WIN32_FIND_DATA data; - HANDLE h = FindFirstFile(dirname, &data); - if (h != nullptr) - { - dip = new DIR(); - dip->hFind = h; - } -#endif if (dip == NULL) { return(0); } @@ -301,14 +286,29 @@ struct dirent *dit; } dit = readdir(dip); } -#ifndef WINDOWS closedir(dip); -#else - FindClose(dip->hFind); - delete dip; -#endif return(n_file); } +#else +static int count_files_in_dir(char *dirname) +{ + TCHAR szDirWildcard[MAX_PATH]; + StringCbCopy(szDirWildcard, sizeof(szDirWildcard), dirname); + StringCbCat(szDirWildcard, sizeof(szDirWildcard) / sizeof(TCHAR), "\\*"); + + WIN32_FIND_DATA data; + HANDLE hDir = FindFirstFile(szDirWildcard, &data); + if (hDir == INVALID_HANDLE_VALUE) + return 0; + + int file_count = 0; + while (FindNextFile(hDir, &data)) + ++file_count; + + FindClose(hDir); + return file_count; +} +#endif static int recursive_read_files_from_dir(char *dirname, struct FileName *fnames, int max_fnames, int *n_file) { diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 0fccb4d..b255be3 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -53,7 +53,12 @@ add_executable(rand_tags rand_tags.cpp) -target_link_libraries(rand_tags tlsh) -set_target_properties(rand_tags PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) + set_target_properties(rand_tags PROPERTIES OUTPUT_NAME rand_tags${BUILD_POSTFIX}) +if(MSVC) + set(EXTRALIBS winfunc) + add_compile_options(/EHsc) +endif() + +target_link_libraries(rand_tags PRIVATE tlsh ${EXTRALIBS}) diff --git a/utils/rand_tags.cpp b/utils/rand_tags.cpp index b53041c..597028c 100644 --- a/utils/rand_tags.cpp +++ b/utils/rand_tags.cpp @@ -7,6 +7,10 @@ #include #include "tlsh.h" +#ifdef WINDOWS + #include "WinFunctions.h" +#endif + static void html_contents(std::string &htmls); static void html_table(std::string &htmls, int *ntags);