diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..1511c7c --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,246 @@ +variables: + GIT_STRATEGY: "clone" + BUILD_IMAGE_CENTOS7: "git.mesalab.cn:7443/mesa_platform/build-env:master" + BUILD_IMAGE_CENTOS8: "git.mesalab.cn:7443/mesa_platform/build-env:rockylinux" + BUILD_PADDING_PREFIX: /tmp/padding_for_CPACK_RPM_BUILD_SOURCE_DIRS_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX_PREFIX/ + INSTALL_DEPENDENCY_LIBRARY: libasan sudo + INSTALL_DEPENDENCY_FRAMEWORK: sapp-devel libMESA_handle_logger-devel libcjson-devel libMESA_field_stat2-devel framework_env libMESA_prof_load-devel libbreakpad_mini-devel libMESA_htable-devel libMESA_jump_layer libMESA_htable libnsl + INSTALL_PREFIX: "/opt/MESA/" + +stages: +- build +- test +- package + +run_test_for_centos7: + stage: test + extends: .build_by_travis_for_centos7 + script: + - yum makecache + - ./ci/travis.sh + - cd build + - ctest --verbose + +run_test_for_centos8: + stage: test + extends: .build_by_travis_for_centos8 + script: + - yum makecache + - ./ci/travis.sh + - cd build + - ctest3 --verbose + +.build_before_script: + before_script: + - mkdir -p $BUILD_PADDING_PREFIX/$CI_PROJECT_NAMESPACE/ + - ln -s $CI_PROJECT_DIR $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH + - cd $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH + - chmod +x ./ci/travis.sh + - yum makecache + - yum install -y elfutils-libelf-devel + + +.build_by_travis_for_centos7: + stage: build + image: $BUILD_IMAGE_CENTOS7 + extends: .build_before_script + script: + - yum install -y libmnl-devel + - yum install -y libnfnetlink-devel + - ./ci/travis.sh + - cd build + tags: + - share + +.build_by_travis_for_centos8: + stage: build + image: $BUILD_IMAGE_CENTOS8 + extends: .build_before_script + script: + - dnf --enablerepo=powertools install -y libmnl-devel + - dnf --enablerepo=powertools install -y libnfnetlink-devel + - ./ci/travis.sh + tags: + - share + +branch_build_debug_for_centos7: + stage: build + extends: .build_by_travis_for_centos7 + variables: + BUILD_TYPE: Debug + except: + - /^develop.*$/i + - /^master.*$/i + - tags + +branch_build_release_for_centos7: + stage: build + variables: + BUILD_TYPE: RelWithDebInfo + extends: .build_by_travis_for_centos7 + except: + - /^develop.*$/i + - /^master.*$/i + - tags + +develop_build_debug_for_centos7: + stage: build + extends: .build_by_travis_for_centos7 + variables: + BUILD_TYPE: Debug + PACKAGE: 1 + UPLOAD_RPM: 1 + ASAN_OPTION: ADDRESS + TESTING_VERSION_BUILD: 1 + PULP3_REPO_NAME: framework-testing-x86_64.el7 + PULP3_DIST_NAME: framework-testing-x86_64.el7 + artifacts: + name: "mesa_sts-$CI_COMMIT_REF_NAME-debug" + paths: + - build/*.rpm + only: + - /^develop.*$/i + - /^master.*$/i + +develop_build_release_for_centos7: + stage: build + extends: .build_by_travis_for_centos7 + variables: + BUILD_TYPE: RelWithDebInfo + PACKAGE: 1 + UPLOAD_RPM: 1 + TESTING_VERSION_BUILD: 1 + PULP3_REPO_NAME: framework-testing-x86_64.el7 + PULP3_DIST_NAME: framework-testing-x86_64.el7 + artifacts: + name: "mesa_sts-$CI_COMMIT_REF_NAME-release" + paths: + - build/*.rpm + only: + - /^develop.*$/i + - /^master.*$/i + +release_build_debug_for_centos7: + stage: package + variables: + BUILD_TYPE: Debug + PACKAGE: 1 + UPLOAD_RPM: 1 + PULP3_REPO_NAME: framework-stable-x86_64.el7 + PULP3_DIST_NAME: framework-stable-x86_64.el7 + extends: .build_by_travis_for_centos7 + artifacts: + name: "mesa_sts-$CI_COMMIT_REF_NAME-debug" + paths: + - build/*.rpm + only: + - tags + +release_build_release_for_centos7: + stage: package + variables: + BUILD_TYPE: RelWithDebInfo + PACKAGE: 1 + UPLOAD_RPM: 1 + UPLOAD_SYMBOL_FILES: 1 + SYMBOL_TARGET: libmesa_rsts + PULP3_REPO_NAME: framework-stable-x86_64.el7 + PULP3_DIST_NAME: framework-stable-x86_64.el7 + extends: .build_by_travis_for_centos7 + artifacts: + name: "mesa_sts-$CI_COMMIT_REF_NAME-release" + paths: + - build/*.rpm + only: + - tags + +branch_build_debug_for_centos8: + stage: build + extends: .build_by_travis_for_centos8 + variables: + BUILD_TYPE: Debug + except: + - /^develop.*$/i + - /^master.*$/i + - tags + +branch_build_release_for_centos8: + stage: build + variables: + BUILD_TYPE: RelWithDebInfo + extends: .build_by_travis_for_centos8 + except: + - /^develop.*$/i + - /^master.*$/i + - tags + +develop_build_debug_for_centos8: + stage: build + extends: .build_by_travis_for_centos8 + variables: + BUILD_TYPE: Debug + PACKAGE: 1 + UPLOAD_RPM: 1 + ASAN_OPTION: ADDRESS + TESTING_VERSION_BUILD: 1 + PULP3_REPO_NAME: framework-testing-x86_64.el8 + PULP3_DIST_NAME: framework-testing-x86_64.el8 + artifacts: + name: "mesa_sts-$CI_COMMIT_REF_NAME-debug" + paths: + - build/*.rpm + only: + - /^develop.*$/i + - /^master.*$/i + +develop_build_release_for_centos8: + stage: build + extends: .build_by_travis_for_centos8 + variables: + BUILD_TYPE: RelWithDebInfo + PACKAGE: 1 + UPLOAD_RPM: 1 + TESTING_VERSION_BUILD: 1 + PULP3_REPO_NAME: framework-testing-x86_64.el8 + PULP3_DIST_NAME: framework-testing-x86_64.el8 + artifacts: + name: "mesa_sts-$CI_COMMIT_REF_NAME-release" + paths: + - build/*.rpm + only: + - /^develop.*$/i + - /^master.*$/i + +release_build_debug_for_centos8: + stage: package + variables: + BUILD_TYPE: Debug + PACKAGE: 1 + UPLOAD_RPM: 1 + PULP3_REPO_NAME: framework-stable-x86_64.el8 + PULP3_DIST_NAME: framework-stable-x86_64.el8 + extends: .build_by_travis_for_centos8 + artifacts: + name: "mesa_sts-$CI_COMMIT_REF_NAME-debug" + paths: + - build/*.rpm + only: + - tags + +release_build_release_for_centos8: + stage: package + variables: + BUILD_TYPE: RelWithDebInfo + PACKAGE: 1 + UPLOAD_RPM: 1 + UPLOAD_SYMBOL_FILES: 1 + SYMBOL_TARGET: libmesa_rsts + PULP3_REPO_NAME: framework-stable-x86_64.el8 + PULP3_DIST_NAME: framework-stable-x86_64.el8 + extends: .build_by_travis_for_centos8 + artifacts: + name: "mesa_sts-$CI_COMMIT_REF_NAME-release" + paths: + - build/*.rpm + only: + - tags diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4b357ab --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,97 @@ +cmake_minimum_required (VERSION 2.8) + +set(lib_name mesa_sts) + +project (${lib_name}) + +set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) +include(Version) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) +set(CMAKE_MACOSX_RPATH 0) +set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall) + +if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX "/opt/MESA" CACHE PATH "default install path" FORCE) +endif() + +find_program(CMAKE_CXX_CPPCHECK NAMES cppcheck) +if (CMAKE_CXX_CPPCHECK) + list( + APPEND CMAKE_CXX_CPPCHECK + "--enable=all" + "--error-exitcode=1" + "--suppress=unreachableCode" + "--suppress=unusedFunction" + "--suppress=missingInclude" + "--suppress=uselessAssignmentPtrArg" + "--suppress=unmatchedSuppression" + "--suppress=internalAstError" + "--suppress=unmatchedSuppression" + "--suppress=unreadVariable" + "--suppress=memleakOnRealloc" + "--suppress=redundantAssignment" + "--suppress=constParameter" + "--suppress=unsignedLessThanZero" + "--suppress=variableScope" + "--suppress=cstyleCast" + "--suppress=objectIndex" + "--suppress=shadowVariable" + "--suppress=nullPointerRedundantCheck" + "--suppress=ctunullpointer" + "--suppress=shadowFunction" + "--suppress=unusedStructMember" + ) + set(CMAKE_C_CPPCHECK ${CMAKE_CXX_CPPCHECK}) +else() + message(FATAL_ERROR "Could not find the program cppcheck.") +endif() + +#for ASAN +set(ASAN_OPTION "OFF" CACHE STRING " set asan type chosen by the user, using OFF as default") +set_property(CACHE ASAN_OPTION PROPERTY STRINGS OFF ADDRESS THREAD) +message(STATUS "ASAN_OPTION='${ASAN_OPTION}'") + +if(ASAN_OPTION MATCHES "ADDRESS") + set(CMAKE_C_FLAGS "${CMAKADDRESS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=address -fno-omit-frame-pointer") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=address -fno-omit-frame-pointer") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan") +elseif(ASAN_OPTION MATCHES "THREAD") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=thread -fno-omit-frame-pointer") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DCMAKE_BUILD_TYPE=Debug -fsanitize=thread -fno-omit-frame-pointer") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan") +endif() +# end of for ASAN + +include_directories(${PROJECT_SOURCE_DIR}/inc/) +include_directories(${PROJECT_SOURCE_DIR}/src/) + +file(GLOB SRC + "src/*.c" + "src/*.cpp" +) + +add_subdirectory(vendor) +add_subdirectory(test) +enable_testing() + +# Shared Library Output +add_library(${lib_name}_shared SHARED ${SRC}) +set_target_properties(${lib_name}_shared PROPERTIES LINK_FLAGS + "-Wl,--version-script=${PROJECT_SOURCE_DIR}/src/version.map") +if(DEFINED MESA_SHARED_INSTALL_DIR) + set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name} LIBRARY_OUTPUT_DIRECTORY ${MESA_SHARED_INSTALL_DIR}) +else() + set_target_properties(${lib_name}_shared PROPERTIES OUTPUT_NAME ${lib_name}) +endif() + +# static Library Output +add_library(${lib_name}_static STATIC ${SRC}) +set_target_properties(${lib_name}_static PROPERTIES OUTPUT_NAME ${lib_name}) + +install(TARGETS ${lib_name}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARIES) +install(TARGETS ${lib_name}_static LIBRARY ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib COMPONENT LIBRARIES) +install(FILES inc/mesa_sts.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/MESA COMPONENT HEADER) + +include(Package) \ No newline at end of file diff --git a/README.md b/README.md index 410aaa6..548483f 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,26 @@ -# MESA_sts +# mesa_sts简介 +mesa_sts是一个统计检验(随机性检测)套件(Statistical Test Suite),包括NIST-STS、国密的共计19种检验算法。 +# nist-sts -## Getting started +This is a slightly updated version of [NIST Statistical Test Suite (STS)](http://csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.html) tool for randomness testing. Main reason for this fork is that the original source code provided by NIST doesn't compile cleanly on Windows using MSVC. Main reason is that MSVC doesn't provide erf() and erfc() functions in standard math library. I've added implementation of these functions and created a project file. You should be now able to compile STS using standard Microsoft Visual C/C++ suite. -To make it easy for you to get started with GitLab, here's a list of recommended next steps. +##Building +This version should compile cleanly under MSVC 2008. I haven't tested it under other versions and MSVC Express, but it's ANSI C so it should work. -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! +The solution is configured to compile using extended instruction set (SSE2) and optimize for speed. -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://git.mesalab.cn/liuchang/mesa_sts.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://git.mesalab.cn/liuchang/mesa_sts/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. +After build is completed you will get a single _assess.exe_ binary which is the test suite. ## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. +You probably still want to [download](http://csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.html) the original NIST ZIP distribution and use their test files. Reason why I'm not including them here is that the archive is over 40 MB big and most of that is the test data. -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. +After unpacking the ZIP place _assess.exe_ in the top directory. The program expects to have the subdirectories _experiments, templates_ etc in the same directory. -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. +STS has somewhat old school terminal interface. Simple tutorial can be found in section 5-1 of [NIST SP800-22](http://csrc.nist.gov/groups/ST/toolkit/rng/documents/SP800-22rev1a.pdf). -## Contributing -State if you are open to contributions and what your requirements are for accepting them. +Remember that testing results are written to _experiments\AlgorithmTesting\finalAnalysisReport.txt_ if you load tested data from a file. STS has a number of built-in generators, in which case the report will be written to a corresponding subdirectory of _experiments_. -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. +PS:the directories:data/experiments/templates are from https://download.csdn.net/download/yuanhunhai/8985409 -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. diff --git a/autorelease.sh b/autorelease.sh new file mode 100644 index 0000000..95945e2 --- /dev/null +++ b/autorelease.sh @@ -0,0 +1,32 @@ +#!/bin/sh +if [ $# -lt 7 ] ; then + echo "USAGE: ./autorelease.sh [API_V4_URL] [PROJECT_URL] + [PROJECT_ID] [TOKEN] + [COMMIT_TAG] [JOB] [PROJECT_NAME]" +exit 1; +fi + +CI_API_V4_URL=$1 +CI_PROJECT_URL=$2 +CI_PROJECT_ID=$3 +CI_TOKEN=$4 +CI_COMMIT_TAG=$5 +ARTIFACTS_JOB=$6 +CI_PROJECT_NAME=$7 + +res=`echo -e "curl --header \"PRIVATE-TOKEN: $CI_TOKEN\" $CI_API_V4_URL/projects/$CI_PROJECT_ID/releases/$CI_COMMIT_TAG -o /dev/null -s -w %{http_code}"| /bin/bash` + +if [[ $res == "200" ]]; then + eval $(echo -e "curl --request POST --header \"PRIVATE-TOKEN: $CI_TOKEN\" \ + --data name=\"$CI_PROJECT_NAME-$CI_COMMIT_TAG-artifacts.zip\" \ + --data url=\"$CI_PROJECT_URL/-/jobs/artifacts/$CI_COMMIT_TAG/download?job=$ARTIFACTS_JOB\"\ + $CI_API_V4_URL/projects/$CI_PROJECT_ID/releases/$CI_COMMIT_TAG/assets/links") +else + eval $(echo -e "curl --header 'Content-Type: application/json' --header \ + \"PRIVATE-TOKEN: $CI_TOKEN\" --data '{ \"name\": \"$CI_COMMIT_TAG\", \ + \"tag_name\": \"$CI_COMMIT_TAG\", \"description\": \"auto_release\",\ + \"assets\": { \"links\": [{ \"name\": \ + \"$CI_PROJECT_NAME-$CI_COMMIT_TAG-artifacts.zip\", \"url\": \ + \"$CI_PROJECT_URL/-/jobs/artifacts/$CI_COMMIT_TAG/download?job=$ARTIFACTS_JOB\"\ + }] } }' --request POST $CI_API_V4_URL/projects/$CI_PROJECT_ID/releases/") +fi diff --git a/autorevision.sh b/autorevision.sh new file mode 100644 index 0000000..3baa179 --- /dev/null +++ b/autorevision.sh @@ -0,0 +1,1268 @@ +#!/bin/sh + +# Copyright (c) 2012 - 2016 dak180 and contributors. See +# https://opensource.org/licenses/mit-license.php or the included +# COPYING.md for licence terms. +# +# autorevision - extracts metadata about the head version from your +# repository. + +# Usage message. +arUsage() { + cat > "/dev/stderr" << EOF +usage: autorevision {-t output-type | -s symbol} [-o cache-file [-f] ] [-V] + Options include: + -t output-type = specify output type + -s symbol = specify symbol output + -o cache-file = specify cache file location + -f = force the use of cache data + -U = check for untracked files in svn + -V = emit version and exit + -? = help message + +The following are valid output types: + clojure = clojure file + c = C/C++ file + h = Header for use with c/c++ + hpp = Alternate C++ header strings with namespace + ini = INI file + java = Java file + javaprop = Java properties file + js = javascript file + json = JSON file + lua = Lua file + m4 = m4 file + matlab = matlab file + octave = octave file + php = PHP file + pl = Perl file + py = Python file + rpm = rpm file + scheme = scheme file + sh = Bash sytax + swift = Swift file + tex = (La)TeX file + xcode = Header useful for populating info.plist files + cmake = CMake file + + +The following are valid symbols: + VCS_TYPE + VCS_BASENAME + VCS_UUID + VCS_NUM + VCS_DATE + VCS_BRANCH + VCS_TAG + VCS_TICK + VCS_EXTRA + VCS_FULL_HASH + VCS_SHORT_HASH + VCS_WC_MODIFIED + VCS_ACTION_STAMP +EOF + exit 1 +} + +# Config +ARVERSION="&&ARVERSION&&" +TARGETFILE="/dev/stdout" +while getopts ":t:o:s:VfU" OPTION; do + case "${OPTION}" in + t) + AFILETYPE="${OPTARG}" + ;; + o) + CACHEFILE="${OPTARG}" + ;; + f) + CACHEFORCE="1" + ;; + s) + VAROUT="${OPTARG}" + ;; + U) + UNTRACKEDFILES="1" + ;; + V) + echo "autorevision ${ARVERSION}" + exit 0 + ;; + ?) + # If an unknown flag is used (or -?): + arUsage + ;; + esac +done + +if [ ! -z "${VAROUT}" ] && [ ! -z "${AFILETYPE}" ]; then + # If both -s and -t are specified: + echo "error: Improper argument combination." 1>&2 + exit 1 +elif [ -z "${VAROUT}" ] && [ -z "${AFILETYPE}" ]; then + # If neither -s or -t are specified: + arUsage +elif [ -z "${CACHEFILE}" ] && [ "${CACHEFORCE}" = "1" ]; then + # If -f is specified without -o: + arUsage +elif [ ! -f "${CACHEFILE}" ] && [ "${CACHEFORCE}" = "1" ]; then + # If we are forced to use the cache but it does not exist. + echo "error: Cache forced but no cache found." 1>&2 + exit 1 +fi + +# Make sure that the path we are given is one we can source +# (dash, we are looking at you). +if [ ! -z "${CACHEFILE}" ] && ! echo "${CACHEFILE}" | grep -q '^\.*/'; then + CACHEFILE="./${CACHEFILE}" +fi + +GENERATED_HEADER="Generated by autorevision - do not hand-hack!" + +# Functions to extract data from different repo types. +# For git repos +# shellcheck disable=SC2039,SC2164,SC2155 +gitRepo() { + local oldPath="${PWD}" + + cd "$(git rev-parse --show-toplevel)" + + VCS_TYPE="git" + + VCS_BASENAME="$(basename "${PWD}")" + + VCS_UUID="$(git rev-list --max-parents=0 --date-order --reverse HEAD 2>/dev/null | sed -n 1p)" + if [ -z "${VCS_UUID}" ]; then + VCS_UUID="$(git rev-list --topo-order HEAD | tail -n 1)" + fi + + # Is the working copy clean? + test -z "$(git status --untracked-files=normal --porcelain)" + VCS_WC_MODIFIED="${?}" + + # Enumeration of changesets + VCS_NUM="$(git rev-list --count HEAD 2>/dev/null)" + if [ -z "${VCS_NUM}" ]; then + echo "warning: Counting the number of revisions may be slower due to an outdated git version less than 1.7.2.3. If something breaks, please update it." 1>&2 + VCS_NUM="$(git rev-list HEAD | wc -l)" + fi + + # This may be a git-svn remote. If so, report the Subversion revision. + if [ -z "$(git config svn-remote.svn.url 2>/dev/null)" ]; then + # The full revision hash + VCS_FULL_HASH="$(git rev-parse HEAD)" + + # The short hash + VCS_SHORT_HASH="$(echo "${VCS_FULL_HASH}" | cut -b 1-7)" + else + # The git-svn revision number + VCS_FULL_HASH="$(git svn find-rev HEAD)" + VCS_SHORT_HASH="${VCS_FULL_HASH}" + fi + + # Current branch + VCS_BRANCH="$(git rev-parse --symbolic-full-name --verify "$(git name-rev --name-only --no-undefined HEAD 2>/dev/null)" 2>/dev/null | sed -e 's:refs/heads/::' | sed -e 's:refs/::')" + + # Cache the description + local DESCRIPTION="$(git describe --long --tags 2>/dev/null)" + + # Current or last tag ancestor (empty if no tags) + VCS_TAG="$(echo "${DESCRIPTION}" | sed -e "s:-g${VCS_SHORT_HASH}\$::" -e 's:-[0-9]*$::')" + + # Distance to last tag or an alias of VCS_NUM if there is no tag + if [ ! -z "${DESCRIPTION}" ]; then + VCS_TICK="$(echo "${DESCRIPTION}" | sed -e "s:${VCS_TAG}-::" -e "s:-g${VCS_SHORT_HASH}::")" + else + VCS_TICK="${VCS_NUM}" + fi + + # Date of the current commit + VCS_DATE="$(TZ=UTC git show -s --date=iso-strict-local --pretty=format:%ad | sed -e 's|+00:00|Z|')" + if [ -z "${VCS_DATE}" ]; then + echo "warning: Action stamps require git version 2.7+." 1>&2 + VCS_DATE="$(git log -1 --pretty=format:%ci | sed -e 's: :T:' -e 's: ::' -e 's|+00:00|Z|')" + local ASdis="1" + fi + + # Action Stamp + if [ -z "${ASdis}" ]; then + VCS_ACTION_STAMP="${VCS_DATE}!$(git show -s --pretty=format:%cE)" + else + VCS_ACTION_STAMP="" + fi + + cd "${oldPath}" +} + +# For hg repos +# shellcheck disable=SC2039,SC2164 +hgRepo() { + local oldPath="${PWD}" + + cd "$(hg root)" + + VCS_TYPE="hg" + + VCS_BASENAME="$(basename "${PWD}")" + + VCS_UUID="$(hg log -r "0" -l 1 --template '{node}\n')" + + # Is the working copy clean? + test -z "$(hg status -duram)" + VCS_WC_MODIFIED="${?}" + + # Enumeration of changesets + VCS_NUM="$(hg id -n | tr -d '+')" + + # The full revision hash + VCS_FULL_HASH="$(hg log -r "${VCS_NUM}" -l 1 --template '{node}\n')" + + # The short hash + VCS_SHORT_HASH="$(hg id -i | tr -d '+')" + + # Current bookmark (bookmarks are roughly equivalent to git's branches) + # or branch if no bookmark + VCS_BRANCH="$(hg id -B | cut -d ' ' -f 1)" + # Fall back to the branch if there are no bookmarks + if [ -z "${VCS_BRANCH}" ]; then + VCS_BRANCH="$(hg id -b)" + fi + + # Current or last tag ancestor (excluding auto tags, empty if no tags) + VCS_TAG="$(hg log -r "${VCS_NUM}" -l 1 --template '{latesttag}\n' 2>/dev/null | sed -e 's:qtip::' -e 's:tip::' -e 's:qbase::' -e 's:qparent::' -e "s:$(hg --config 'extensions.color=' --config 'extensions.mq=' --color never qtop 2>/dev/null)::" | cut -d ' ' -f 1)" + + # Distance to last tag or an alias of VCS_NUM if there is no tag + if [ ! -z "${VCS_TAG}" ]; then + VCS_TICK="$(hg log -r "${VCS_NUM}" -l 1 --template '{latesttagdistance}\n' 2>/dev/null)" + else + VCS_TICK="${VCS_NUM}" + fi + + # Date of the current commit + VCS_DATE="$(hg log -r "${VCS_NUM}" -l 1 --template '{date|isodatesec}\n' 2>/dev/null | sed -e 's: :T:' -e 's: ::' -e 's|+00:00|Z|')" + + # Action Stamp + VCS_ACTION_STAMP="$(TZ=UTC hg log -r "${VCS_NUM}" -l 1 --template '{date|localdate|rfc3339date}\n' 2>/dev/null | sed -e 's|+00:00|Z|')!$(hg log -r "${VCS_NUM}" -l 1 --template '{author|email}\n' 2>/dev/null)" + + cd "${oldPath}" +} + +# For bzr repos +# shellcheck disable=SC2039,SC2164 +bzrRepo() { + local oldPath="${PWD}" + + cd "$(bzr root)" + + VCS_TYPE="bzr" + + VCS_BASENAME="$(basename "${PWD}")" + + # Currently unimplemented because more investigation is needed. + VCS_UUID="" + + # Is the working copy clean? + bzr version-info --custom --template='{clean}\n' | grep -q '1' + VCS_WC_MODIFIED="${?}" + + # Enumeration of changesets + VCS_NUM="$(bzr revno)" + + # The full revision hash + VCS_FULL_HASH="$(bzr version-info --custom --template='{revision_id}\n')" + + # The short hash + VCS_SHORT_HASH="${VCS_NUM}" + + # Nick of the current branch + VCS_BRANCH="$(bzr nick)" + + # Current or last tag ancestor (excluding auto tags, empty if no tags) + VCS_TAG="$(bzr tags --sort=time | sed '/?$/d' | tail -n1 | cut -d ' ' -f1)" + + # Distance to last tag or an alias of VCS_NUM if there is no tag + if [ ! -z "${VCS_TAG}" ]; then + VCS_TICK="$(bzr log --line -r "tag:${VCS_TAG}.." | tail -n +2 | wc -l | sed -e 's:^ *::')" + else + VCS_TICK="${VCS_NUM}" + fi + + # Date of the current commit + VCS_DATE="$(bzr version-info --custom --template='{date}\n' | sed -e 's: :T:' -e 's: ::')" + + # Action Stamp + # Currently unimplemented because more investigation is needed. + VCS_ACTION_STAMP="" + + cd "${oldPath}" +} + +# For svn repos +# shellcheck disable=SC2039,SC2164,SC2155 +svnRepo() { + local oldPath="${PWD}" + + VCS_TYPE="svn" + + case "${PWD}" in + /*trunk*|/*branches*|/*tags*) + local fn="${PWD}" + while [ "$(basename "${fn}")" != 'trunk' ] && [ "$(basename "${fn}")" != 'branches' ] && [ "$(basename "${fn}")" != 'tags' ] && [ "$(basename "${fn}")" != '/' ]; do + local fn="$(dirname "${fn}")" + done + local fn="$(dirname "${fn}")" + if [ "${fn}" = '/' ]; then + VCS_BASENAME="$(basename "${PWD}")" + else + VCS_BASENAME="$(basename "${fn}")" + fi + ;; + *) VCS_BASENAME="$(basename "${PWD}")" ;; + esac + + VCS_UUID="$(svn info --xml | sed -n -e 's:::' -e 's:::p')" + + # Cache svnversion output + local SVNVERSION="$(svnversion)" + + # Is the working copy clean? + echo "${SVNVERSION}" | grep -q "M" + case "${?}" in + 0) + VCS_WC_MODIFIED="1" + ;; + 1) + if [ ! -z "${UNTRACKEDFILES}" ]; then + # `svnversion` does not detect untracked files and `svn status` is really slow, so only run it if we really have to. + if [ -z "$(svn status)" ]; then + VCS_WC_MODIFIED="0" + else + VCS_WC_MODIFIED="1" + fi + else + VCS_WC_MODIFIED="0" + fi + ;; + esac + + # Enumeration of changesets + VCS_NUM="$(echo "${SVNVERSION}" | cut -d : -f 1 | sed -e 's:M::' -e 's:S::' -e 's:P::')" + + # The full revision hash + VCS_FULL_HASH="${SVNVERSION}" + + # The short hash + VCS_SHORT_HASH="${VCS_NUM}" + + # Current branch + case "${PWD}" in + /*trunk*|/*branches*|/*tags*) + local lastbase="" + local fn="${PWD}" + while : + do + base="$(basename "${fn}")" + if [ "${base}" = 'trunk' ]; then + VCS_BRANCH='trunk' + break + elif [ "${base}" = 'branches' ] || [ "${base}" = 'tags' ]; then + VCS_BRANCH="${lastbase}" + break + elif [ "${base}" = '/' ]; then + VCS_BRANCH="" + break + fi + local lastbase="${base}" + local fn="$(dirname "${fn}")" + done + ;; + *) VCS_BRANCH="" ;; + esac + + # Current or last tag ancestor (empty if no tags). But "current + # tag" can't be extracted reliably because Subversion doesn't + # have tags the way other VCSes do. + VCS_TAG="" + VCS_TICK="" + + # Date of the current commit + VCS_DATE="$(svn info --xml | sed -n -e 's:::' -e 's:::p')" + + # Action Stamp + VCS_ACTION_STAMP="${VCS_DATE}!$(svn log --xml -l 1 -r "${VCS_SHORT_HASH}" | sed -n -e 's:::' -e 's:::p')" + + cd "${oldPath}" +} + + +# Functions to output data in different formats. +# For bash output +shOutput() { + cat > "${TARGETFILE}" << EOF +# ${GENERATED_HEADER} + +VCS_TYPE="${VCS_TYPE}" +VCS_BASENAME="${VCS_BASENAME}" +VCS_UUID="${VCS_UUID}" +VCS_NUM="${VCS_NUM}" +VCS_DATE="${VCS_DATE}" +VCS_BRANCH="${VCS_BRANCH}" +VCS_TAG="${VCS_TAG}" +VCS_TICK="${VCS_TICK}" +VCS_EXTRA="${VCS_EXTRA}" + +VCS_ACTION_STAMP="${VCS_ACTION_STAMP}" +VCS_FULL_HASH="${VCS_FULL_HASH}" +VCS_SHORT_HASH="${VCS_SHORT_HASH}" + +VCS_WC_MODIFIED="${VCS_WC_MODIFIED}" + +# end +EOF +} + +# For source C output +cOutput() { + cat > "${TARGETFILE}" << EOF +/* ${GENERATED_HEADER} */ + +const char *VCS_TYPE = "${VCS_TYPE}"; +const char *VCS_BASENAME = "${VCS_BASENAME}"; +const char *VCS_UUID = "${VCS_UUID}"; +const int VCS_NUM = ${VCS_NUM}; +const char *VCS_DATE = "${VCS_DATE}"; +const char *VCS_BRANCH = "${VCS_BRANCH}"; +const char *VCS_TAG = "${VCS_TAG}"; +const int VCS_TICK = ${VCS_TICK}; +const char *VCS_EXTRA = "${VCS_EXTRA}"; + +const char *VCS_ACTION_STAMP = "${VCS_ACTION_STAMP}"; +const char *VCS_FULL_HASH = "${VCS_FULL_HASH}"; +const char *VCS_SHORT_HASH = "${VCS_SHORT_HASH}"; + +const int VCS_WC_MODIFIED = ${VCS_WC_MODIFIED}; + +/* end */ +EOF +} + +# For header output +hOutput() { + cat > "${TARGETFILE}" << EOF +/* ${GENERATED_HEADER} */ +#ifndef AUTOREVISION_H +#define AUTOREVISION_H + +#define VCS_TYPE "${VCS_TYPE}" +#define VCS_BASENAME "${VCS_BASENAME}" +#define VCS_UUID "${VCS_UUID}" +#define VCS_NUM ${VCS_NUM} +#define VCS_DATE "${VCS_DATE}" +#define VCS_BRANCH "${VCS_BRANCH}" +#define VCS_TAG "${VCS_TAG}" +#define VCS_TICK ${VCS_TICK} +#define VCS_EXTRA "${VCS_EXTRA}" + +#define VCS_ACTION_STAMP "${VCS_ACTION_STAMP}" +#define VCS_FULL_HASH "${VCS_FULL_HASH}" +#define VCS_SHORT_HASH "${VCS_SHORT_HASH}" + +#define VCS_WC_MODIFIED ${VCS_WC_MODIFIED} + +#endif + +/* end */ +EOF +} + +# A header output for use with xcode to populate info.plist strings +xcodeOutput() { + cat > "${TARGETFILE}" << EOF +/* ${GENERATED_HEADER} */ +#ifndef AUTOREVISION_H +#define AUTOREVISION_H + +#define VCS_TYPE ${VCS_TYPE} +#define VCS_BASENAME ${VCS_BASENAME} +#define VCS_UUID ${VCS_UUID} +#define VCS_NUM ${VCS_NUM} +#define VCS_DATE ${VCS_DATE} +#define VCS_BRANCH ${VCS_BRANCH} +#define VCS_TAG ${VCS_TAG} +#define VCS_TICK ${VCS_TICK} +#define VCS_EXTRA ${VCS_EXTRA} + +#define VCS_ACTION_STAMP ${VCS_ACTION_STAMP} +#define VCS_FULL_HASH ${VCS_FULL_HASH} +#define VCS_SHORT_HASH ${VCS_SHORT_HASH} + +#define VCS_WC_MODIFIED ${VCS_WC_MODIFIED} + +#endif + +/* end */ +EOF +} + +# For Swift output +swiftOutput() { + case "${VCS_WC_MODIFIED}" in + 0) VCS_WC_MODIFIED="false" ;; + 1) VCS_WC_MODIFIED="true" ;; + esac + # For values that may not exist depending on the type of repo we + # have read from, set them to `nil` when they are empty. + if [ -z "${VCS_UUID}" ]; then + VCS_UUID="nil" + else + VCS_UUID="\"${VCS_UUID}\"" + fi + if [ -z "${VCS_TAG}" ]; then + VCS_TAG="nil" + else + VCS_TAG="\"${VCS_TAG}\"" + fi + : "${VCS_TICK:="nil"}" + if [ -z "${VCS_EXTRA}" ]; then + VCS_EXTRA="nil" + else + VCS_EXTRA="\"${VCS_EXTRA}\"" + fi + if [ -z "${VCS_ACTION_STAMP}" ]; then + VCS_ACTION_STAMP="nil" + else + VCS_ACTION_STAMP="\"${VCS_ACTION_STAMP}\"" + fi + cat > "${TARGETFILE}" << EOF +/* ${GENERATED_HEADER} */ + +let VCS_TYPE = "${VCS_TYPE}" +let VCS_BASENAME = "${VCS_BASENAME}" +let VCS_UUID: String? = ${VCS_UUID} +let VCS_NUM: Int = ${VCS_NUM} +let VCS_DATE = "${VCS_DATE}" +let VCS_BRANCH: String = "${VCS_BRANCH}" +let VCS_TAG: String? = ${VCS_TAG} +let VCS_TICK: Int? = ${VCS_TICK} +let VCS_EXTRA: String? = ${VCS_EXTRA} + +let VCS_ACTION_STAMP: String? = ${VCS_ACTION_STAMP} +let VCS_FULL_HASH: String = "${VCS_FULL_HASH}" +let VCS_SHORT_HASH: String = "${VCS_SHORT_HASH}" + +let VCS_WC_MODIFIED: Bool = ${VCS_WC_MODIFIED} + +/* end */ +EOF +} + +# For Python output +pyOutput() { + case "${VCS_WC_MODIFIED}" in + 0) VCS_WC_MODIFIED="False" ;; + 1) VCS_WC_MODIFIED="True" ;; + esac + cat > "${TARGETFILE}" << EOF +# ${GENERATED_HEADER} + +VCS_TYPE = "${VCS_TYPE}" +VCS_BASENAME = "${VCS_BASENAME}" +VCS_UUID = "${VCS_UUID}" +VCS_NUM = ${VCS_NUM} +VCS_DATE = "${VCS_DATE}" +VCS_BRANCH = "${VCS_BRANCH}" +VCS_TAG = "${VCS_TAG}" +VCS_TICK = ${VCS_TICK} +VCS_EXTRA = "${VCS_EXTRA}" + +VCS_ACTION_STAMP = "${VCS_ACTION_STAMP}" +VCS_FULL_HASH = "${VCS_FULL_HASH}" +VCS_SHORT_HASH = "${VCS_SHORT_HASH}" + +VCS_WC_MODIFIED = ${VCS_WC_MODIFIED} + +# end +EOF +} + +# For Perl output +plOutput() { + cat << EOF +# ${GENERATED_HEADER} + +\$VCS_TYPE = '${VCS_TYPE}'; +\$VCS_BASENAME = '${VCS_BASENAME}'; +\$VCS_UUID = '${VCS_UUID}'; +\$VCS_NUM = ${VCS_NUM}; +\$VCS_DATE = '${VCS_DATE}'; +\$VCS_BRANCH = '${VCS_BRANCH}'; +\$VCS_TAG = '${VCS_TAG}'; +\$VCS_TICK = ${VCS_TICK}; +\$VCS_EXTRA = '${VCS_EXTRA}'; + +\$VCS_ACTION_STAMP = '${VCS_ACTION_STAMP}'; +\$VCS_FULL_HASH = '${VCS_FULL_HASH}'; +\$VCS_SHORT_HASH = '${VCS_SHORT_HASH}'; + +\$VCS_WC_MODIFIED = ${VCS_WC_MODIFIED}; + +# end +1; +EOF +} + +# For lua output +luaOutput() { + case "${VCS_WC_MODIFIED}" in + 0) VCS_WC_MODIFIED="false" ;; + 1) VCS_WC_MODIFIED="true" ;; + esac + cat > "${TARGETFILE}" << EOF +-- ${GENERATED_HEADER} + +VCS_TYPE = "${VCS_TYPE}" +VCS_BASENAME = "${VCS_BASENAME}" +VCS_UUID = "${VCS_UUID}" +VCS_NUM = ${VCS_NUM} +VCS_DATE = "${VCS_DATE}" +VCS_BRANCH = "${VCS_BRANCH}" +VCS_TAG = "${VCS_TAG}" +VCS_TICK = ${VCS_TICK} +VCS_EXTRA = "${VCS_EXTRA}" + +VCS_ACTION_STAMP = "${VCS_ACTION_STAMP}" +VCS_FULL_HASH = "${VCS_FULL_HASH}" +VCS_SHORT_HASH = "${VCS_SHORT_HASH}" + +VCS_WC_MODIFIED = ${VCS_WC_MODIFIED} + +-- end +EOF +} + +# For php output +phpOutput() { + case "${VCS_WC_MODIFIED}" in + 0) VCS_WC_MODIFIED="false" ;; + 1) VCS_WC_MODIFIED="true" ;; + esac + cat > "${TARGETFILE}" << EOF + "${VCS_TYPE}", + "VCS_BASENAME" => "${VCS_BASENAME}", + "VCS_UUID" => "${VCS_UUID}", + "VCS_NUM" => ${VCS_NUM}, + "VCS_DATE" => "${VCS_DATE}", + "VCS_BRANCH" => "${VCS_BRANCH}", + "VCS_TAG" => "${VCS_TAG}", + "VCS_TICK" => ${VCS_TICK}, + "VCS_EXTRA" => "${VCS_EXTRA}", + "VCS_ACTION_STAMP" => "${VCS_ACTION_STAMP}", + "VCS_FULL_HASH" => "${VCS_FULL_HASH}", + "VCS_SHORT_HASH" => "${VCS_SHORT_HASH}", + "VCS_WC_MODIFIED" => ${VCS_WC_MODIFIED} +); + +# end +?> +EOF +} + +# For ini output +iniOutput() { + case "${VCS_WC_MODIFIED}" in + 0) VCS_WC_MODIFIED="false" ;; + 1) VCS_WC_MODIFIED="true" ;; + esac + cat > "${TARGETFILE}" << EOF +; ${GENERATED_HEADER} +[VCS] +VCS_TYPE = "${VCS_TYPE}" +VCS_BASENAME = "${VCS_BASENAME}" +VCS_UUID = "${VCS_UUID}" +VCS_NUM = ${VCS_NUM} +VCS_DATE = "${VCS_DATE}" +VCS_BRANCH = "${VCS_BRANCH}" +VCS_TAG = "${VCS_TAG}" +VCS_TICK = ${VCS_TICK} +VCS_EXTRA = "${VCS_EXTRA}" +VCS_ACTION_STAMP = "${VCS_ACTION_STAMP}" +VCS_FULL_HASH = "${VCS_FULL_HASH}" +VCS_SHORT_HASH = "${VCS_SHORT_HASH}" +VCS_WC_MODIFIED = ${VCS_WC_MODIFIED} +; end +EOF +} + +# For javascript output +jsOutput() { + case "${VCS_WC_MODIFIED}" in + 1) VCS_WC_MODIFIED="true" ;; + 0) VCS_WC_MODIFIED="false" ;; + esac + cat > "${TARGETFILE}" << EOF +/** ${GENERATED_HEADER} */ + +var autorevision = { + VCS_TYPE: "${VCS_TYPE}", + VCS_BASENAME: "${VCS_BASENAME}", + VCS_UUID: "${VCS_UUID}", + VCS_NUM: ${VCS_NUM}, + VCS_DATE: "${VCS_DATE}", + VCS_BRANCH: "${VCS_BRANCH}", + VCS_TAG: "${VCS_TAG}", + VCS_TICK: ${VCS_TICK}, + VCS_EXTRA: "${VCS_EXTRA}", + + VCS_ACTION_STAMP: "${VCS_ACTION_STAMP}", + VCS_FULL_HASH: "${VCS_FULL_HASH}", + VCS_SHORT_HASH: "${VCS_SHORT_HASH}", + + VCS_WC_MODIFIED: ${VCS_WC_MODIFIED} +}; + +/** Node.js compatibility */ +if (typeof module !== 'undefined') { + module.exports = autorevision; +} + +/** end */ +EOF +} + +# For JSON output +jsonOutput() { + case "${VCS_WC_MODIFIED}" in + 1) VCS_WC_MODIFIED="true" ;; + 0) VCS_WC_MODIFIED="false" ;; + esac + cat > "${TARGETFILE}" << EOF +{ + "_comment": "${GENERATED_HEADER}", + "VCS_TYPE": "${VCS_TYPE}", + "VCS_BASENAME": "${VCS_BASENAME}", + "VCS_UUID": "${VCS_UUID}", + "VCS_NUM": ${VCS_NUM}, + "VCS_DATE": "${VCS_DATE}", + "VCS_BRANCH":"${VCS_BRANCH}", + "VCS_TAG": "${VCS_TAG}", + "VCS_TICK": ${VCS_TICK}, + "VCS_EXTRA": "${VCS_EXTRA}", + + "VCS_ACTION_STAMP": "${VCS_ACTION_STAMP}", + "VCS_FULL_HASH": "${VCS_FULL_HASH}", + "VCS_SHORT_HASH": "${VCS_SHORT_HASH}", + + "VCS_WC_MODIFIED": ${VCS_WC_MODIFIED} +} +EOF +} + +# For Java output +javaOutput() { + case "${VCS_WC_MODIFIED}" in + 1) VCS_WC_MODIFIED="true" ;; + 0) VCS_WC_MODIFIED="false" ;; + esac + cat > "${TARGETFILE}" << EOF +/* ${GENERATED_HEADER} */ + +public class autorevision { + public static final String VCS_TYPE = "${VCS_TYPE}"; + public static final String VCS_BASENAME = "${VCS_BASENAME}"; + public static final String VCS_UUID = "${VCS_UUID}"; + public static final long VCS_NUM = ${VCS_NUM}; + public static final String VCS_DATE = "${VCS_DATE}"; + public static final String VCS_BRANCH = "${VCS_BRANCH}"; + public static final String VCS_TAG = "${VCS_TAG}"; + public static final long VCS_TICK = ${VCS_TICK}; + public static final String VCS_EXTRA = "${VCS_EXTRA}"; + + public static final String VCS_ACTION_STAMP = "${VCS_ACTION_STAMP}"; + public static final String VCS_FULL_HASH = "${VCS_FULL_HASH}"; + public static final String VCS_SHORT_HASH = "${VCS_SHORT_HASH}"; + + public static final boolean VCS_WC_MODIFIED = ${VCS_WC_MODIFIED}; +} +EOF +} + +# For Java properties output +javapropOutput() { + case "${VCS_WC_MODIFIED}" in + 1) VCS_WC_MODIFIED="true" ;; + 0) VCS_WC_MODIFIED="false" ;; + esac + cat > "${TARGETFILE}" << EOF +# ${GENERATED_HEADER} + +VCS_TYPE=${VCS_TYPE} +VCS_BASENAME=${VCS_BASENAME} +VCS_UUID=${VCS_UUID} +VCS_NUM=${VCS_NUM} +VCS_DATE=${VCS_DATE} +VCS_BRANCH=${VCS_BRANCH} +VCS_TAG=${VCS_TAG} +VCS_TICK=${VCS_TICK} +VCS_EXTRA=${VCS_EXTRA} + +VCS_ACTION_STAMP=${VCS_ACTION_STAMP} +VCS_FULL_HASH=${VCS_FULL_HASH} +VCS_SHORT_HASH=${VCS_SHORT_HASH} + +VCS_WC_MODIFIED=${VCS_WC_MODIFIED} +EOF +} + +# For m4 output +m4Output() { + cat > "${TARGETFILE}" << EOF +dnl ${GENERATED_HEADER} +define(\`VCS_TYPE', \`${VCS_TYPE}')dnl +define(\`VCS_BASENAME', \`${VCS_BASENAME}')dnl +define(\`VCS_UUID', \`${VCS_UUID}')dnl +define(\`VCS_NUM', \`${VCS_NUM}')dnl +define(\`VCS_DATE', \`${VCS_DATE}')dnl +define(\`VCS_BRANCH', \`${VCS_BRANCH}')dnl +define(\`VCS_TAG', \`${VCS_TAG}')dnl +define(\`VCS_TICK', \`${VCS_TICK}')dnl +define(\`VCS_EXTRA', \`${VCS_EXTRA}')dnl +define(\`VCS_ACTIONSTAMP', \`${VCS_ACTION_STAMP}')dnl +define(\`VCS_FULLHASH', \`${VCS_FULL_HASH}')dnl +define(\`VCS_SHORTHASH', \`${VCS_SHORT_HASH}')dnl +define(\`VCS_WC_MODIFIED', \`${VCS_WC_MODIFIED}')dnl +EOF +} + +# For (La)TeX output +texOutput() { + case "${VCS_WC_MODIFIED}" in + 0) VCS_WC_MODIFIED="false" ;; + 1) VCS_WC_MODIFIED="true" ;; + esac + cat > "${TARGETFILE}" << EOF +% ${GENERATED_HEADER} +\def \vcsType {${VCS_TYPE}} +\def \vcsBasename {${VCS_BASENAME}} +\def \vcsUUID {${VCS_UUID}} +\def \vcsNum {${VCS_NUM}} +\def \vcsDate {${VCS_DATE}} +\def \vcsBranch {${VCS_BRANCH}} +\def \vcsTag {${VCS_TAG}} +\def \vcsTick {${VCS_TICK}} +\def \vcsExtra {${VCS_EXTRA}} +\def \vcsACTIONSTAMP {${VCS_ACTION_STAMP}} +\def \vcsFullHash {${VCS_FULL_HASH}} +\def \vcsShortHash {${VCS_SHORT_HASH}} +\def \vcsWCModified {${VCS_WC_MODIFIED}} +\endinput +EOF +} + +# For scheme output +schemeOutput() { + case "${VCS_WC_MODIFIED}" in + 0) VCS_WC_MODIFIED="#f" ;; + 1) VCS_WC_MODIFIED="#t" ;; + esac + cat > "${TARGETFILE}" << EOF +;; ${GENERATED_HEADER} +(define VCS_TYPE "${VCS_TYPE}") +(define VCS_BASENAME "${VCS_BASENAME}") +(define VCS_UUID "${VCS_UUID}") +(define VCS_NUM ${VCS_NUM}) +(define VCS_DATE "${VCS_DATE}") +(define VCS_BRANCH "${VCS_BRANCH}") +(define VCS_TAG "${VCS_TAG}") +(define VCS_TICK ${VCS_TICK}) +(define VCS_EXTRA "${VCS_EXTRA}") + +(define VCS_ACTION_STAMP "${VCS_ACTION_STAMP}") +(define VCS_FULL_HASH "${VCS_FULL_HASH}") +(define VCS_SHORT_HASH "${VCS_SHORT_HASH}") + +(define VCS_WC_MODIFIED ${VCS_WC_MODIFIED}) +;; end +EOF +} + +# For clojure output +clojureOutput() { + case "${VCS_WC_MODIFIED}" in + 0) VCS_WC_MODIFIED="false" ;; + 1) VCS_WC_MODIFIED="true" ;; + esac + cat > "${TARGETFILE}" << EOF +;; ${GENERATED_HEADER} +(def VCS_TYPE "${VCS_TYPE}") +(def VCS_BASENAME "${VCS_BASENAME}") +(def VCS_UUID "${VCS_UUID}") +(def VCS_NUM ${VCS_NUM}) +(def VCS_DATE "${VCS_DATE}") +(def VCS_BRANCH "${VCS_BRANCH}") +(def VCS_TAG "${VCS_TAG}") +(def VCS_TICK ${VCS_TICK}) +(def VCS_EXTRA "${VCS_EXTRA}") + +(def VCS_ACTION_STAMP "${VCS_ACTION_STAMP}") +(def VCS_FULL_HASH "${VCS_FULL_HASH}") +(def VCS_SHORT_HASH "${VCS_SHORT_HASH}") + +(def VCS_WC_MODIFIED ${VCS_WC_MODIFIED}) +;; end +EOF +} + +# For rpm spec file output +rpmOutput() { + cat > "${TARGETFILE}" << EOF +# ${GENERATED_HEADER} +$([ "${VCS_TYPE}" ] && echo "%define vcs_type ${VCS_TYPE}") +$([ "${VCS_BASENAME}" ] && echo "%define vcs_basename ${VCS_BASENAME}") +$([ "${VCS_UUID}" ] && echo "%define vcs_uuid ${VCS_UUID}") +$([ "${VCS_NUM}" ] && echo "%define vcs_num ${VCS_NUM}") +$([ "${VCS_DATE}" ] && echo "%define vcs_date ${VCS_DATE}") +$([ "${VCS_BRANCH}" ] && echo "%define vcs_branch ${VCS_BRANCH}") +$([ "${VCS_TAG}" ] && echo "%define vcs_tag ${VCS_TAG}") +$([ "${VCS_TICK}" ] && echo "%define vcs_tick ${VCS_TICK}") +$([ "${VCS_EXTRA}" ] && echo "%define vcs_extra ${VCS_EXTRA}") + +$([ "${VCS_ACTION_STAMP}" ] && echo "%define vcs_action_stamp ${VCS_ACTION_STAMP}") +$([ "${VCS_FULL_HASH}" ] && echo "%define vcs_full_hash ${VCS_FULL_HASH}") +$([ "${VCS_SHORT_HASH}" ] && echo "%define vcs_short_hash ${VCS_SHORT_HASH}") + +$([ "${VCS_WC_MODIFIED}" ] && echo "%define vcs_wc_modified ${VCS_WC_MODIFIED}") +# end +EOF +} + +# shellcheck disable=SC2155,SC2039 +hppOutput() { + local NAMESPACE="$(echo "${VCS_BASENAME}" | sed -e 's:_::g' | tr '[:lower:]' '[:upper:]')" + cat > "${TARGETFILE}" << EOF +/* ${GENERATED_HEADER} */ + +#ifndef ${NAMESPACE}_AUTOREVISION_H +#define ${NAMESPACE}_AUTOREVISION_H + +#include + +namespace $(echo "${NAMESPACE}" | tr '[:upper:]' '[:lower:]') +{ + const std::string VCS_TYPE = "${VCS_TYPE}"; + const std::string VCS_BASENAME = "${VCS_BASENAME}"; + const std::string VCS_UUID = "${VCS_UUID}"; + const int VCS_NUM = ${VCS_NUM}; + const std::string VCS_DATE = "${VCS_DATE}"; + const std::string VCS_BRANCH = "${VCS_BRANCH}"; + const std::string VCS_TAG = "${VCS_TAG}"; + const int VCS_TICK = ${VCS_TICK}; + const std::string VCS_EXTRA = "${VCS_EXTRA}"; + + const std::string VCS_ACTION_STAMP = "${VCS_ACTION_STAMP}"; + const std::string VCS_FULL_HASH = "${VCS_FULL_HASH}"; + const std::string VCS_SHORT_HASH = "${VCS_SHORT_HASH}"; + + const int VCS_WC_MODIFIED = ${VCS_WC_MODIFIED}; +} + +#endif + +/* end */ +EOF +} + +matlabOutput() { + case "${VCS_WC_MODIFIED}" in + 0) VCS_WC_MODIFIED="FALSE" ;; + 1) VCS_WC_MODIFIED="TRUE" ;; + esac + cat > "${TARGETFILE}" << EOF +% ${GENERATED_HEADER} + +VCS_TYPE = '${VCS_TYPE}'; +VCS_BASENAME = '${VCS_BASENAME}'; +VCS_UUID = '${VCS_UUID}'; +VCS_NUM = ${VCS_NUM}; +VCS_DATE = '${VCS_DATE}'; +VCS_BRANCH = '${VCS_BRANCH}'; +VCS_TAG = '${VCS_TAG}'; +VCS_TICK = ${VCS_TICK}; +VCS_EXTRA = '${VCS_EXTRA}'; + +VCS_ACTION_STAMP = '${VCS_ACTION_STAMP}'; +VCS_FULL_HASH = '${VCS_FULL_HASH}'; +VCS_SHORT_HASH = '${VCS_SHORT_HASH}'; + +VCS_WC_MODIFIED = ${VCS_WC_MODIFIED}; + +% end +EOF +} + +octaveOutput() { + cat > "${TARGETFILE}" << EOF +% ${GENERATED_HEADER} + +VCS_TYPE = '${VCS_TYPE}'; +VCS_BASENAME = '${VCS_BASENAME}'; +VCS_UUID = '${VCS_UUID}'; +VCS_NUM = ${VCS_NUM}; +VCS_DATE = '${VCS_DATE}'; +VCS_BRANCH = '${VCS_BRANCH}'; +VCS_TAG = '${VCS_TAG}'; +VCS_TICK = ${VCS_TICK}; +VCS_EXTRA = '${VCS_EXTRA}'; + +VCS_ACTION_STAMP = '${VCS_ACTION_STAMP}'; +VCS_FULL_HASH = '${VCS_FULL_HASH}'; +VCS_SHORT_HASH = '${VCS_SHORT_HASH}'; + +VCS_WC_MODIFIED = ${VCS_WC_MODIFIED}; + +% end +EOF +} + +cmakeOutput() { + cat > "${TARGETFILE}" << EOF +# ${GENERATED_HEADER} + +set(VCS_TYPE ${VCS_TYPE}) +set(VCS_BASENAME ${VCS_BASENAME}) +set(VCS_UUID ${VCS_UUID}) +set(VCS_NUM ${VCS_NUM}) +set(VCS_DATE ${VCS_DATE}) +set(VCS_BRANCH ${VCS_BRANCH}) +set(VCS_TAG ${VCS_TAG}) +set(VCS_TICK ${VCS_TICK}) +set(VCS_EXTRA ${VCS_EXTRA}) + +set(VCS_ACTION_STAMP ${VCS_ACTION_STAMP}) +set(VCS_FULL_HASH ${VCS_FULL_HASH}) +set(VCS_SHORT_HASH ${VCS_SHORT_HASH}) + +set(VCS_WC_MODIFIED ${VCS_WC_MODIFIED}) + +# end +EOF +} + + +# Helper functions +# Count path segments +# shellcheck disable=SC2039 +pathSegment() { + local pathz="${1}" + local depth="0" + + if [ ! -z "${pathz}" ]; then + # Continue until we are at / or there are no path separators left. + while [ ! "${pathz}" = "/" ] && [ ! "${pathz}" = "$(echo "${pathz}" | sed -e 's:/::')" ]; do + pathz="$(dirname "${pathz}")" + depth="$((depth+1))" + done + fi + echo "${depth}" +} + +# Largest of four numbers +# shellcheck disable=SC2039 +multiCompare() { + local larger="${1}" + local numA="${2}" + local numB="${3}" + local numC="${4}" + + [ "${numA}" -gt "${larger}" ] && larger="${numA}" + [ "${numB}" -gt "${larger}" ] && larger="${numB}" + [ "${numC}" -gt "${larger}" ] && larger="${numC}" + echo "${larger}" +} + +# Test for repositories +# shellcheck disable=SC2155,SC2039 +repoTest() { + REPONUM="0" + if [ ! -z "$(git rev-parse HEAD 2>/dev/null)" ]; then + local gitPath="$(git rev-parse --show-toplevel)" + local gitDepth="$(pathSegment "${gitPath}")" + REPONUM="$((REPONUM+1))" + else + local gitDepth="0" + fi + if [ ! -z "$(hg root 2>/dev/null)" ]; then + local hgPath="$(hg root 2>/dev/null)" + local hgDepth="$(pathSegment "${hgPath}")" + REPONUM="$((REPONUM+1))" + else + local hgDepth="0" + fi + if [ ! -z "$(bzr root 2>/dev/null)" ]; then + local bzrPath="$(bzr root 2>/dev/null)" + local bzrDepth="$(pathSegment "${bzrPath}")" + REPONUM="$((REPONUM+1))" + else + local bzrDepth="0" + fi + if [ ! -z "$(svn info 2>/dev/null)" ]; then + local stringz="" + local stringx="" + local svnPath="$(svn info --xml | sed -n -e "s:${stringz}::" -e "s:${stringx}::p")" + # An old enough svn will not be able give us a path; default + # to 1 for that case. + if [ -z "${svnPath}" ]; then + local svnDepth="1" + else + local svnDepth="$(pathSegment "${svnPath}")" + fi + REPONUM="$((REPONUM+1))" + else + local svnDepth="0" + fi + + # Do not do more work then we have to. + if [ "${REPONUM}" = "0" ]; then + return + fi + + # Figure out which repo is the deepest and use it. + local wonRepo="$(multiCompare "${gitDepth}" "${hgDepth}" "${bzrDepth}" "${svnDepth}")" + if [ "${wonRepo}" = "${gitDepth}" ]; then + gitRepo + elif [ "${wonRepo}" = "${hgDepth}" ]; then + hgRepo + elif [ "${wonRepo}" = "${bzrDepth}" ]; then + bzrRepo + elif [ "${wonRepo}" = "${svnDepth}" ]; then + svnRepo + fi +} + + + +# Detect which repos we are in and gather data. +# shellcheck source=/dev/null +if [ -f "${CACHEFILE}" ] && [ "${CACHEFORCE}" = "1" ]; then + # When requested only read from the cache to populate our symbols. + . "${CACHEFILE}" +else + # If a value is not set through the environment set VCS_EXTRA to nothing. + : "${VCS_EXTRA:=""}" + repoTest + + if [ -f "${CACHEFILE}" ] && [ "${REPONUM}" = "0" ]; then + # We are not in a repo; try to use a previously generated cache to populate our symbols. + . "${CACHEFILE}" + # Do not overwrite the cache if we know we are not going to write anything new. + CACHEFORCE="1" + elif [ "${REPONUM}" = "0" ]; then + echo "error: No repo or cache detected." 1>&2 + exit 1 + fi +fi + + +# -s output is handled here. +if [ ! -z "${VAROUT}" ]; then + if [ "${VAROUT}" = "VCS_TYPE" ]; then + echo "${VCS_TYPE}" + elif [ "${VAROUT}" = "VCS_BASENAME" ]; then + echo "${VCS_BASENAME}" + elif [ "${VAROUT}" = "VCS_NUM" ]; then + echo "${VCS_NUM}" + elif [ "${VAROUT}" = "VCS_DATE" ]; then + echo "${VCS_DATE}" + elif [ "${VAROUT}" = "VCS_BRANCH" ]; then + echo "${VCS_BRANCH}" + elif [ "${VAROUT}" = "VCS_TAG" ]; then + echo "${VCS_TAG}" + elif [ "${VAROUT}" = "VCS_TICK" ]; then + echo "${VCS_TICK}" + elif [ "${VAROUT}" = "VCS_FULL_HASH" ]; then + echo "${VCS_FULL_HASH}" + elif [ "${VAROUT}" = "VCS_SHORT_HASH" ]; then + echo "${VCS_SHORT_HASH}" + elif [ "${VAROUT}" = "VCS_WC_MODIFIED" ]; then + echo "${VCS_WC_MODIFIED}" + elif [ "${VAROUT}" = "VCS_ACTION_STAMP" ]; then + echo "${VCS_ACTION_STAMP}" + else + echo "error: Not a valid output symbol." 1>&2 + exit 1 + fi +fi + + +# Detect requested output type and use it. +if [ ! -z "${AFILETYPE}" ]; then + if [ "${AFILETYPE}" = "c" ]; then + cOutput + elif [ "${AFILETYPE}" = "h" ]; then + hOutput + elif [ "${AFILETYPE}" = "xcode" ]; then + xcodeOutput + elif [ "${AFILETYPE}" = "swift" ]; then + swiftOutput + elif [ "${AFILETYPE}" = "sh" ]; then + shOutput + elif [ "${AFILETYPE}" = "py" ] || [ "${AFILETYPE}" = "python" ]; then + pyOutput + elif [ "${AFILETYPE}" = "pl" ] || [ "${AFILETYPE}" = "perl" ]; then + plOutput + elif [ "${AFILETYPE}" = "lua" ]; then + luaOutput + elif [ "${AFILETYPE}" = "php" ]; then + phpOutput + elif [ "${AFILETYPE}" = "ini" ]; then + iniOutput + elif [ "${AFILETYPE}" = "js" ]; then + jsOutput + elif [ "${AFILETYPE}" = "json" ]; then + jsonOutput + elif [ "${AFILETYPE}" = "java" ]; then + javaOutput + elif [ "${AFILETYPE}" = "javaprop" ]; then + javapropOutput + elif [ "${AFILETYPE}" = "tex" ]; then + texOutput + elif [ "${AFILETYPE}" = "m4" ]; then + m4Output + elif [ "${AFILETYPE}" = "scheme" ]; then + schemeOutput + elif [ "${AFILETYPE}" = "clojure" ]; then + clojureOutput + elif [ "${AFILETYPE}" = "rpm" ]; then + rpmOutput + elif [ "${AFILETYPE}" = "hpp" ]; then + hppOutput + elif [ "${AFILETYPE}" = "matlab" ]; then + matlabOutput + elif [ "${AFILETYPE}" = "octave" ]; then + octaveOutput + elif [ "${AFILETYPE}" = "cmake" ]; then + cmakeOutput + else + echo "error: Not a valid output type." 1>&2 + exit 1 + fi +fi + + +# If requested, make a cache file. +if [ ! -z "${CACHEFILE}" ] && [ ! "${CACHEFORCE}" = "1" ]; then + TARGETFILE="${CACHEFILE}.tmp" + shOutput + + # Check to see if there have been any actual changes. + if [ ! -f "${CACHEFILE}" ]; then + mv -f "${CACHEFILE}.tmp" "${CACHEFILE}" + elif cmp -s "${CACHEFILE}.tmp" "${CACHEFILE}"; then + rm -f "${CACHEFILE}.tmp" + else + mv -f "${CACHEFILE}.tmp" "${CACHEFILE}" + fi +fi diff --git a/ci/get-nprocessors.sh b/ci/get-nprocessors.sh new file mode 100644 index 0000000..43635e7 --- /dev/null +++ b/ci/get-nprocessors.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# Copyright 2017 Google Inc. +# All Rights Reserved. +# +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * 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. +# * Neither the name of Google Inc. 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 +# OWNER 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. + +# This file is typically sourced by another script. +# if possible, ask for the precise number of processors, +# otherwise take 2 processors as reasonable default; see +# https://docs.travis-ci.com/user/speeding-up-the-build/#Makefile-optimization +if [ -x /usr/bin/getconf ]; then + NPROCESSORS=$(/usr/bin/getconf _NPROCESSORS_ONLN) +else + NPROCESSORS=2 +fi + +# as of 2017-09-04 Travis CI reports 32 processors, but GCC build +# crashes if parallelized too much (maybe memory consumption problem), +# so limit to 4 processors for the time being. +if [ $NPROCESSORS -gt 4 ] ; then + echo "$0:Note: Limiting processors to use by make from $NPROCESSORS to 4." + NPROCESSORS=4 +fi diff --git a/ci/perpare_pulp3_netrc.sh b/ci/perpare_pulp3_netrc.sh new file mode 100644 index 0000000..8414bbb --- /dev/null +++ b/ci/perpare_pulp3_netrc.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env sh +set -evx +echo "machine ${PULP3_SERVER_URL}\nlogin ${PULP3_SERVER_LOGIN}\npassword ${PULP3_SERVER_PASSWORD}\n" > ~/.netrc diff --git a/ci/travis.sh b/ci/travis.sh new file mode 100644 index 0000000..3640f4b --- /dev/null +++ b/ci/travis.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env sh +set -evx + +chmod +x ci/get-nprocessors.sh +. ci/get-nprocessors.sh + +# if possible, ask for the precise number of processors, +# otherwise take 2 processors as reasonable default; see +# https://docs.travis-ci.com/user/speeding-up-the-build/#Makefile-optimization +if [ -x /usr/bin/getconf ]; then + NPROCESSORS=$(/usr/bin/getconf _NPROCESSORS_ONLN) +else + NPROCESSORS=2 +fi + +# as of 2017-09-04 Travis CI reports 32 processors, but GCC build +# crashes if parallelized too much (maybe memory consumption problem), +# so limit to 4 processors for the time being. +if [ $NPROCESSORS -gt 4 ] ; then + echo "$0:Note: Limiting processors to use by make from $NPROCESSORS to 4." + NPROCESSORS=4 +fi + +# Tell make to use the processors. No preceding '-' required. +MAKEFLAGS="j${NPROCESSORS}" +export MAKEFLAGS + +env | sort + +# Set default values to OFF for these variables if not specified. +: "${NO_EXCEPTION:=OFF}" +: "${NO_RTTI:=OFF}" +: "${COMPILER_IS_GNUCXX:=OFF}" + +# Install dependency from YUM +if [ -n "${INSTALL_DEPENDENCY_LIBRARY}" ]; then + yum install -y $INSTALL_DEPENDENCY_LIBRARY +fi + +if [ -n "${INSTALL_DEPENDENCY_FRAMEWORK}" ]; then + yum install -y $INSTALL_DEPENDENCY_FRAMEWORK + source /etc/profile.d/framework.sh +fi + + + +if [ $ASAN_OPTION ] && [ -f "/opt/rh/devtoolset-7/enable" ] ;then + source /opt/rh/devtoolset-7/enable +fi + + + +mkdir build || true +cd build + +cmake3 -DCMAKE_CXX_FLAGS=$CXX_FLAGS \ + -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ + -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \ + -DENABLE_DEVEL=$ENABLE_DEVEL_SWITCH \ + -DASAN_OPTION=$ASAN_OPTION \ + -DVERSION_DAILY_BUILD=$TESTING_VERSION_BUILD \ + .. + +make + + +if [ -n "${PACKAGE}" ]; then + make package +fi + +if [ -n "${UPLOAD_RPM}" ]; then + cp ~/rpm_upload_tools.py ./ + python3 rpm_upload_tools.py ${PULP3_REPO_NAME} ${PULP3_DIST_NAME} *.rpm +fi + +if [ -n "${UPLOAD_SYMBOL_FILES}" ]; then + rpm -i $SYMBOL_TARGET*debuginfo*.rpm + _symbol_file=`find /usr/lib/debug/ -name "$SYMBOL_TARGET*.so*.debug"` + cp $_symbol_file ${_symbol_file}info.${CI_COMMIT_SHORT_SHA} + sentry-cli upload-dif -t elf ${_symbol_file}info.${CI_COMMIT_SHORT_SHA} +fi diff --git a/cmake/Package.cmake b/cmake/Package.cmake new file mode 100644 index 0000000..6187cb8 --- /dev/null +++ b/cmake/Package.cmake @@ -0,0 +1,50 @@ +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(MY_RPM_NAME_PREFIX "lib${lib_name}-debug") +else() + set(MY_RPM_NAME_PREFIX "lib${lib_name}") +endif() + +message(STATUS "Package: ${MY_RPM_NAME_PREFIX}") + +set(CPACK_PACKAGE_VECDOR "MESA") +set(CPACK_PACKAGE_VERSION_MAJOR "${VERSION_MAJOR}") +set(CPACK_PACKAGE_VERSION_MINOR "${VERSION_MINOR}") +set(CPACK_PACKAGE_VERSION_PATCH "${VERSION_PATCH}.${VERSION_BUILD}") +set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}) +set(CPACK_PACKAGE_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${VERSION_BUILD}") +execute_process(COMMAND bash -c "echo -ne \"`uname -r | awk -F'.' '{print $5\".\"$6\".\"$7}'`\"" OUTPUT_VARIABLE SYSTEM_VERSION) + +# RPM Build +set(CPACK_GENERATOR "RPM") +set(CPACK_RPM_AUTO_GENERATED_FILE_NAME ON) +set(CPACK_RPM_FILE_NAME "RPM-DEFAULT") +set(CPACK_RPM_PACKAGE_VENDOR "MESA") +set(CPACK_RPM_PACKAGE_AUTOREQPROV "yes") +set(CPACK_RPM_PACKAGE_RELEASE_DIST "on") +set(CPACK_RPM_DEBUGINFO_PACKAGE "on") + +set(CPACK_RPM_COMPONENT_INSTALL ON) +set(CPACK_COMPONENTS_IGNORE_GROUPS 1) +set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP) +set(CPACK_COMPONENT_HEADER_DISPLAY_NAME "develop") + +set(CPACK_COMPONENT_LIBRARIES_REQUIRED TRUE) +set(CPACK_RPM_LIBRARIES_PACKAGE_NAME ${MY_RPM_NAME_PREFIX}) +set(CPACK_COMPONENT_LIBRARIES_GROUP "LIBRARIES") + +set(CPACK_COMPONENT_HEADER_REQUIRED TRUE) +set(CPACK_RPM_HEADER_PACKAGE_NAME "${MY_RPM_NAME_PREFIX}-devel") +set(CPACK_COMPONENT_HEADER_GROUP "header") + +set(CPACK_RPM_HEADER_PACKAGE_REQUIRES_PRE ${CPACK_RPM_LIBRARIES_PACKAGE_NAME}) +set(CPACK_RPM_HEADER_PACKAGE_CONFLICTS ${CPACK_RPM_HEADER_PACKAGE_NAME}) + +set(CPACK_COMPONENTS_ALL LIBRARIES HEADER) + +set(CPACK_BUILD_SOURCE_DIRS "${CMAKE_SOURCE_DIR}") + +# Must uninstall the debug package before install release package +set(CPACK_RPM_PACKAGE_CONFLICTS ${MY_RPM_NAME_PREFIX}) + +# set(CPACK_STRIP_FILES TRUE) +include(CPack) diff --git a/cmake/Version.cmake b/cmake/Version.cmake new file mode 100644 index 0000000..a47944c --- /dev/null +++ b/cmake/Version.cmake @@ -0,0 +1,54 @@ + +# Using autorevision.sh to generate version information + +set(__SOURCE_AUTORESIVISION ${CMAKE_SOURCE_DIR}/autorevision.sh) +set(__AUTORESIVISION ${CMAKE_BINARY_DIR}/autorevision.sh) +set(__VERSION_CACHE ${CMAKE_BINARY_DIR}/version.txt) +set(__VERSION_CONFIG ${CMAKE_BINARY_DIR}/version.cmake) + +file(COPY ${__SOURCE_AUTORESIVISION} DESTINATION ${CMAKE_BINARY_DIR} + FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE + WORLD_READ WORLD_EXECUTE) + +# execute autorevision.sh to generate version information +execute_process(COMMAND ${__AUTORESIVISION} -t cmake -o ${__VERSION_CACHE} + OUTPUT_FILE ${__VERSION_CONFIG} ERROR_QUIET) +include(${__VERSION_CONFIG}) + +# extract major, minor, patch version from git tag +string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${VCS_TAG}") +string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${VCS_TAG}") +string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${VCS_TAG}") +string(REGEX REPLACE "[T\\:\\+\\-]" "" VERSION_DATE "${VCS_DATE}") + +if(VERSION_DAILY_BUILD) + set(VERSION_PATCH ${VERSION_PATCH}.${VERSION_DATE}) +endif() + +if(NOT VERSION_MAJOR) + set(VERSION_MAJOR 1) +endif() + +if(NOT VERSION_MINOR) + set(VERSION_MINOR 0) +endif() + +if(NOT VERSION_PATCH) + set(VERSION_PATCH 0) +endif() + +set(VERSION "${VERSION_MAJOR}_${VERSION_MINOR}_${VERSION_PATCH}") +set(VERSION_BUILD "${VCS_SHORT_HASH}") + +# print information +message(STATUS "Version: ${VERSION}-${VERSION_BUILD}") + +option(DEFINE_GIT_VERSION "Set DEFINE_GIT_VERSION to TRUE or FALSE" TRUE) + +if(DEFINE_GIT_VERSION) + set(GIT_VERSION + "${VERSION}-${CMAKE_BUILD_TYPE}-${VERSION_BUILD}-${VCS_BRANCH}-${VCS_TAG}-${VCS_DATE}") + string(REGEX REPLACE "[-:+/\\.]" "_" GIT_VERSION ${GIT_VERSION}) + + add_definitions(-DGIT_VERSION=${GIT_VERSION}) +endif() diff --git a/inc/mesa_sts.h b/inc/mesa_sts.h new file mode 100644 index 0000000..5a2f5f1 --- /dev/null +++ b/inc/mesa_sts.h @@ -0,0 +1,28 @@ +#ifndef _MESA_STS_H_ +#define _MESA_STS_H_ + +struct sts_result { + unsigned char frequency; + unsigned char block_frequency; + unsigned char cumulative_sums; + unsigned char runs; + unsigned char longest_run; + unsigned char rank; + unsigned char discrete_fourier_transform; + unsigned char non_overlapping_template_matching; + unsigned char overlapping_template_matching; + unsigned char universal; + unsigned char approximate_entropy; + unsigned char random_excursions; + unsigned char random_excursions_variant; + unsigned char serial; + unsigned char linear_complexity; + unsigned char poker_detect; + unsigned char runs_distribution; + unsigned char self_correlation; + unsigned char binary_derivative; +}; + +int mesa_statistical_test_suite(void* data,unsigned int datalen, struct sts_result* result); + +#endif /* _MESA_STS_H_ */ \ No newline at end of file diff --git a/src/approximateEntropy.c b/src/approximateEntropy.c new file mode 100644 index 0000000..b386056 --- /dev/null +++ b/src/approximateEntropy.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + A P P R O X I M A T E E N T R O P Y T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +ApproximateEntropy(int m, int n, BitSequence *epsilon) +{ + int i, j, k, r, blockSize, seqLength, powLen, index; + double sum, numOfBlocks, ApEn[2], apen, chi_squared, p_value; + unsigned int *P; + + seqLength = n; + r = 0; + + for ( blockSize=m; blockSize<=m+1; blockSize++ ) { + if ( blockSize == 0 ) { + ApEn[0] = 0.00; + r++; + } + else { + numOfBlocks = (double)seqLength; + powLen = (int)pow(2, blockSize+1)-1; + if ( (P = (unsigned int*)calloc(powLen,sizeof(unsigned int)))== NULL ) { + return 0; + } + for ( i=1; i 0 ) + sum += P[index]*log(P[index]/numOfBlocks); + index++; + } + sum /= numOfBlocks; + ApEn[r] = sum; + r++; + free(P); + } + } + apen = ApEn[0] - ApEn[1]; + + chi_squared = 2.0*seqLength*(log(2) - apen); + p_value = cephes_igamc(pow(2, m-1), chi_squared/2.0); + + if ( m > (int)(log(seqLength)/log(2)-5) ) { + return 0; + } + + if (p_value < ALPHA) { + return 0; + } else { + return 1; + } +} \ No newline at end of file diff --git a/src/binaryDerivate.c b/src/binaryDerivate.c new file mode 100644 index 0000000..6d2dc4a --- /dev/null +++ b/src/binaryDerivate.c @@ -0,0 +1,30 @@ +#include +#include "include/stat_fncs.h" + +int BinaryDerivate(int k, int n, BitSequence *epsilon, int epsilon_l) +{ + int i = 0, j = 0; + int Sn_k = 0; + int n_k = n - k; + double V = 0.0, p_value = 0.0, sqrt2 = 1.41421356237309504880; + + for (i = 0; i < k; ++i) { + for (j = 0; j < epsilon_l - 1; ++j) { + epsilon[j] = epsilon[j] ^ epsilon[j + 1]; + } + } + + for (i = 0; i < n_k; ++i) { + Sn_k += (2 * (int)epsilon[i]) - 1; + } + + V = fabs(Sn_k) / sqrt(n_k); + + p_value = erfc(fabs(V) / sqrt2); + + if (p_value < ALPHA) { + return 0; + } else { + return 1; + } +} \ No newline at end of file diff --git a/src/blockFrequency.c b/src/blockFrequency.c new file mode 100644 index 0000000..ba4ad34 --- /dev/null +++ b/src/blockFrequency.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + B L O C K F R E Q U E N C Y T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +BlockFrequency(int M, int n, BitSequence *epsilon) +{ + int i, j, N, blockSum; + double p_value, sum, pi, v, chi_squared; + + N = n/M; /* # OF SUBSTRING BLOCKS */ + sum = 0.0; + + for ( i=0; i +#include +#include "include/cephes.h" + +static const double rel_error = 1E-12; + +double MACHEP = 1.11022302462515654042E-16; // 2**-53 +double MAXLOG = 7.09782712893383996732224E2; // log(MAXNUM) +double MAXNUM = 1.7976931348623158E308; // 2**1024*(1-MACHEP) +double PI = 3.14159265358979323846; // pi, duh! + +static double big = 4.503599627370496e15; +static double biginv = 2.22044604925031308085e-16; + +int sgngam = 0; + +double +cephes_igamc(double a, double x) +{ + double ans, ax, c, yc, r, t, y, z; + double pk, pkm1, pkm2, qk, qkm1, qkm2; + + if ( (x <= 0) || ( a <= 0) ) + return( 1.0 ); + + if ( (x < 1.0) || (x < a) ) + return( 1.e0 - cephes_igam(a,x) ); + + ax = a * log(x) - x - cephes_lgam(a); + + if ( ax < -MAXLOG ) { + printf("igamc: UNDERFLOW\n"); + return 0.0; + } + ax = exp(ax); + + /* continued fraction */ + y = 1.0 - a; + z = x + y + 1.0; + c = 0.0; + pkm2 = 1.0; + qkm2 = x; + pkm1 = x + 1.0; + qkm1 = z * x; + ans = pkm1/qkm1; + + do { + c += 1.0; + y += 1.0; + z += 2.0; + yc = y * c; + pk = pkm1 * z - pkm2 * yc; + qk = qkm1 * z - qkm2 * yc; + if ( qk != 0 ) { + r = pk/qk; + t = fabs( (ans - r)/r ); + ans = r; + } + else + t = 1.0; + pkm2 = pkm1; + pkm1 = pk; + qkm2 = qkm1; + qkm1 = qk; + if ( fabs(pk) > big ) { + pkm2 *= biginv; + pkm1 *= biginv; + qkm2 *= biginv; + qkm1 *= biginv; + } + } while ( t > MACHEP ); + + return ans*ax; +} + +double +cephes_igam(double a, double x) +{ + double ans, ax, c, r; + + if ( (x <= 0) || ( a <= 0) ) + return 0.0; + + if ( (x > 1.0) && (x > a ) ) + return 1.e0 - cephes_igamc(a,x); + + /* Compute x**a * exp(-x) / gamma(a) */ + ax = a * log(x) - x - cephes_lgam(a); + if ( ax < -MAXLOG ) { + printf("igam: UNDERFLOW\n"); + return 0.0; + } + ax = exp(ax); + + /* power series */ + r = a; + c = 1.0; + ans = 1.0; + + do { + r += 1.0; + c *= x/r; + ans += c; + } while ( c/ans > MACHEP ); + + return ans * ax/a; +} + +union A_Array { + double d[5]; + unsigned short us[20]; +}; + + +union BC_Array { + double d[6]; + unsigned short us[24]; +}; + +/* A[]: Stirling's formula expansion of log gamma + * B[], C[]: log gamma function between 2 and 3 + */ + +/**/ +static union A_Array A = { + .us = { + 0x6661,0x2733,0x9850,0x3f4a, + 0xe943,0xb580,0x7fbd,0xbf43, + 0x5ebb,0x20dc,0x019f,0x3f4a, + 0xa5a1,0x16b0,0xc16c,0xbf66, + 0x554b,0x5555,0x5555,0x3fb5 + } +}; +static union BC_Array B = { + .us = { + 0x6761,0x8ff3,0x8901,0xc095, + 0xb93e,0x355b,0xf234,0xc0e2, + 0x89e5,0xf890,0x3d73,0xc114, + 0xdb51,0xf994,0xbc82,0xc131, + 0xf20b,0x0219,0x4589,0xc13a, + 0x055e,0x5418,0x0c67,0xc12a + } +}; +static union BC_Array C = { + /*0x0000,0x0000,0x0000,0x3ff0,*/ + .us = { + 0x12b2,0x1cf3,0xfd0d,0xc075, + 0xd757,0x7b89,0xaa0d,0xc0d0, + 0x4c9b,0xb974,0xeb84,0xc10a, + 0x0043,0x7195,0x6286,0xc131, + 0xf34c,0x892f,0x5255,0xc143, + 0xe14a,0x6a11,0xce4b,0xc13e + } +}; + +#define MAXLGM 2.556348e305 + + +/* Logarithm of gamma function */ +double +cephes_lgam(double x) +{ + double p, q, u, w, z; + int i; + + sgngam = 1; + + if ( x < -34.0 ) { + q = -x; + w = cephes_lgam(q); /* note this modifies sgngam! */ + p = floor(q); + if ( p == q ) { +lgsing: + goto loverf; + } + i = (int)p; + if ( (i & 1) == 0 ) + sgngam = -1; + else + sgngam = 1; + z = q - p; + if ( z > 0.5 ) { + p += 1.0; + z = p - q; + } + z = q * sin( PI * z ); + if ( z == 0.0 ) + goto lgsing; + /* z = log(PI) - log( z ) - w;*/ + z = log(PI) - log( z ) - w; + return z; + } + + if ( x < 13.0 ) { + z = 1.0; + p = 0.0; + u = x; + while ( u >= 3.0 ) { + p -= 1.0; + u = x + p; + z *= u; + } + while ( u < 2.0 ) { + if ( u == 0.0 ) + goto lgsing; + z /= u; + p += 1.0; + u = x + p; + } + if ( z < 0.0 ) { + sgngam = -1; + z = -z; + } + else + sgngam = 1; + if ( u == 2.0 ) + return( log(z) ); + p -= 2.0; + x = x + p; + p = x * cephes_polevl( x, (double *)B.d, 5 ) / cephes_p1evl( x, (double *)C.d, 6); + + return log(z) + p; + } + + if ( x > MAXLGM ) { +loverf: + printf("lgam: OVERFLOW\n"); + + return sgngam * MAXNUM; + } + + q = ( x - 0.5 ) * log(x) - x + log( sqrt( 2*PI ) ); + if ( x > 1.0e8 ) + return q; + + p = 1.0/(x*x); + if ( x >= 1000.0 ) + q += (( 7.9365079365079365079365e-4 * p + - 2.7777777777777777777778e-3) *p + + 0.0833333333333333333333) / x; + else + q += cephes_polevl( p, (double *)A.d, 4 ) / x; + + return q; +} + +double +cephes_polevl(double x, double *coef, int N) +{ + double ans; + int i; + double *p; + + p = coef; + ans = *p++; + i = N; + + do + ans = ans * x + *p++; + while ( --i ); + + return ans; +} + +double +cephes_p1evl(double x, double *coef, int N) +{ + double ans; + double *p; + int i; + + p = coef; + ans = x + *p++; + i = N-1; + + do + ans = ans * x + *p++; + while ( --i ); + + return ans; +} + +double +cephes_erf(double x) +{ + static const double two_sqrtpi = 1.128379167095512574; + double sum = x, term = x, xsqr = x * x; + int j = 1; + + if ( fabs(x) > 2.2 ) + return 1.0 - cephes_erfc(x); + + do { + term *= xsqr/j; + sum -= term/(2*j+1); + j++; + term *= xsqr/j; + sum += term/(2*j+1); + j++; + } while ( fabs(term)/sum > rel_error ); + + return two_sqrtpi*sum; +} + +double +cephes_erfc(double x) +{ + static const double one_sqrtpi = 0.564189583547756287; + double a = 1, b = x, c = x, d = x*x + 0.5; + double q1, q2 = b/d, n = 1.0, t; + + if ( fabs(x) < 2.2 ) + return 1.0 - cephes_erf(x); + if ( x < 0 ) + return 2.0 - cephes_erfc(-x); + + do { + t = a*n + b*x; + a = b; + b = t; + t = c*n + d*x; + c = d; + d = t; + n += 0.5; + q1 = q2; + q2 = b/d; + } while ( fabs(q1-q2)/q2 > rel_error ); + + return one_sqrtpi*exp(-x*x)*q2; +} + + +double +cephes_normal(double x) +{ + double arg, result, sqrt2=1.414213562373095048801688724209698078569672; + + if (x > 0) { + arg = x/sqrt2; + result = 0.5 * ( 1 + erf(arg) ); + } + else { + arg = -x/sqrt2; + result = 0.5 * (erfc(arg) ); + } + + return( result); +} diff --git a/src/cusum.c b/src/cusum.c new file mode 100644 index 0000000..d9be4e1 --- /dev/null +++ b/src/cusum.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + C U M U L A T I V E S U M S T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +CumulativeSums(int n, BitSequence *epsilon) +{ + int S, sup, inf, z=1, zrev, k; + double sum1, sum2, p_value; + + S = 0; + sup = 0; + inf = 0; + for ( k=0; k sup ) + sup++; + if ( S < inf ) + inf--; + z = (sup > -inf) ? sup : -inf; + zrev = (sup-S > S-inf) ? sup-S : S-inf; + } + + // forward + sum1 = 0.0; + for ( k=(-n/z+1)/4; k<=(n/z-1)/4; k++ ) { + sum1 += cephes_normal(((4*k+1)*z)/sqrt(n)); + sum1 -= cephes_normal(((4*k-1)*z)/sqrt(n)); + } + sum2 = 0.0; + for ( k=(-n/z-3)/4; k<=(n/z-1)/4; k++ ) { + sum2 += cephes_normal(((4*k+3)*z)/sqrt(n)); + sum2 -= cephes_normal(((4*k+1)*z)/sqrt(n)); + } + + p_value = 1.0 - sum1 + sum2; + +// fprintf(stats[TEST_CUSUM], "\t\t CUMULATIVE SUMS (FORWARD) TEST\n"); +// fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n"); +// fprintf(stats[TEST_CUSUM], "\t\tCOMPUTATIONAL INFORMATION:\n"); +// fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n"); +// fprintf(stats[TEST_CUSUM], "\t\t(a) The maximum partial sum = %d\n", z); +// fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n"); + + if ( isNegative(p_value) || isGreaterThanOne(p_value) ) { +// fprintf(stats[TEST_CUSUM], "\t\tWARNING: P_VALUE IS OUT OF RANGE\n"); + return 0; + } + +// fprintf(stats[TEST_CUSUM], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value); +// fprintf(results[TEST_CUSUM], "%f\n", p_value); + if (p_value < ALPHA) { + return 0; + } + + // backwards + sum1 = 0.0; + for ( k=(-n/zrev+1)/4; k<=(n/zrev-1)/4; k++ ) { + sum1 += cephes_normal(((4*k+1)*zrev)/sqrt(n)); + sum1 -= cephes_normal(((4*k-1)*zrev)/sqrt(n)); + } + sum2 = 0.0; + for ( k=(-n/zrev-3)/4; k<=(n/zrev-1)/4; k++ ) { + sum2 += cephes_normal(((4*k+3)*zrev)/sqrt(n)); + sum2 -= cephes_normal(((4*k+1)*zrev)/sqrt(n)); + } + p_value = 1.0 - sum1 + sum2; + +// fprintf(stats[TEST_CUSUM], "\t\t CUMULATIVE SUMS (REVERSE) TEST\n"); +// fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n"); +// fprintf(stats[TEST_CUSUM], "\t\tCOMPUTATIONAL INFORMATION:\n"); +// fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n"); +// fprintf(stats[TEST_CUSUM], "\t\t(a) The maximum partial sum = %d\n", zrev); +// fprintf(stats[TEST_CUSUM], "\t\t-------------------------------------------\n"); + + if ( isNegative(p_value) || isGreaterThanOne(p_value) ) { +// fprintf(stats[TEST_CUSUM], "\t\tWARNING: P_VALUE IS OUT OF RANGE\n"); + return 0; + } + +// fprintf(stats[TEST_CUSUM], "%s\t\tp_value = %f\n\n", p_value < ALPHA ? "FAILURE" : "SUCCESS", p_value); fflush(stats[TEST_CUSUM]); +// fprintf(results[TEST_CUSUM], "%f\n", p_value); fflush(results[TEST_CUSUM]); + if (p_value < ALPHA) { + return 0; + } else { + return 1; + } +} \ No newline at end of file diff --git a/src/dfft.c b/src/dfft.c new file mode 100644 index 0000000..71c0efd --- /dev/null +++ b/src/dfft.c @@ -0,0 +1,1462 @@ +/* Notes from RFB: + +Looks like the user-level routines are: + +Real FFT + +void __ogg_fdrffti(int n, double *wsave, int *ifac) +void __ogg_fdrfftf(int n, double *r, double *wsave, int *ifac) +void __ogg_fdrfftb(int n, double *r, double *wsave, int *ifac) + +__ogg_fdrffti == initialization +__ogg_fdrfftf == forward transform +__ogg_fdrfftb == backward transform + +Parameters are +n == length of sequence +r == sequence to be transformed (input) +== transformed sequence (output) +wsave == work array of length 2n (allocated by caller) +ifac == work array of length 15 (allocated by caller) + +Cosine quarter-wave FFT + +void __ogg_fdcosqi(int n, double *wsave, int *ifac) +void __ogg_fdcosqf(int n, double *x, double *wsave, int *ifac) +void __ogg_fdcosqb(int n, double *x, double *wsave, int *ifac) +*/ + +/******************************************************************** +* * +* THIS FILE IS PART OF THE OggSQUISH SOFTWARE CODEC SOURCE CODE. * +* * +******************************************************************** + +file: fft.c +function: Fast discrete Fourier and cosine transforms and inverses +author: Monty +modifications by: Monty +last modification date: Jul 1 1996 + +********************************************************************/ + +/* These Fourier routines were originally based on the Fourier +routines of the same names from the NETLIB bihar and fftpack +fortran libraries developed by Paul N. Swarztrauber at the National +Center for Atmospheric Research in Boulder, CO USA. They have been +reimplemented in C and optimized in a few ways for OggSquish. */ + +/* As the original fortran libraries are public domain, the C Fourier +routines in this file are hereby released to the public domain as +well. The C routines here produce output exactly equivalent to the +original fortran routines. Of particular interest are the facts +that (like the original fortran), these routines can work on +arbitrary length vectors that need not be powers of two in +length. */ + +#include +#define STIN static + +static void drfti1(int n, double *wa, int *ifac) +{ + static const int ntryh[4] = { 4,2,3,5 }; + static double tpi = 6.28318530717958647692528676655900577; + double arg, argh, argld, fi; + int ntry=0,i,j=-1; + int k1, l1, l2, ib; + int ld, ii, ip, is, nq, nr; + int ido, ipm, nfm1; + int nl=n; + int nf=0; + +L101: + j++; + if ( j < 4 ) + ntry = ntryh[j]; + else + ntry += 2; + +L104: + nq = nl/ntry; + nr = nl-ntry*nq; + if ( nr != 0 ) + goto L101; + + nf++; + ifac[nf+1] = ntry; + nl = nq; + if ( ntry != 2 ) + goto L107; + if ( nf == 1 ) + goto L107; + + for ( i=1; i>1; + ipp2 = ip; + idp2 = ido; + nbd = (ido-1)>>1; + t0 = l1*ido; + t10 = ip*ido; + + if ( ido == 1 ) + goto L119; + for ( ik=0; ik l1 ) { + for ( j=1; j>1; + np2 = n; + + kc = np2; + for ( k=1; k>1; + ipp2 = ip; + ipph = (ip+1)>>1; + if ( ido < l1 ) + goto L103; + + t1 = 0; + t2 = 0; + for ( k=0; k l1 ) + goto L139; + + is = -ido-1; + t1 =0; + for ( j=1; j>1; + np2 = n; + + for ( i=2; i +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + D I S C R E T E F O U R I E R T R A N S F O R M T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +void __ogg_fdrffti(int n, double *wsave, int *ifac); +void __ogg_fdrfftf(int n, double *X, double *wsave, int *ifac); + +int +DiscreteFourierTransform(int n, BitSequence *epsilon) +{ + double p_value, upperBound, percentile, N_l, N_o, d, *m, *X, *wsave; + int i, count, ifac[15]; + + if ( ((X = (double*) calloc(n,sizeof(double))) == NULL) || + ((wsave = (double *)calloc(2*n,sizeof(double))) == NULL) || + ((m = (double*)calloc(n/2+1, sizeof(double))) == NULL) ) { + if( X != NULL ) + free(X); + if( wsave != NULL ) + free(wsave); + if( m != NULL ) + free(m); + return 0; + } + for ( i=0; i +#include +#include +#include "include/externs.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + F R E Q U E N C Y T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +Frequency(int n, BitSequence *epsilon) +{ + int i; + double f, s_obs, p_value, sum, sqrt2 = 1.41421356237309504880; + + sum = 0.0; + for ( i=0; i y * +** * +** Returns TRUE (1) if x greater than y, * +** otherwise FALSE (0). * +** * +** Parameters: * +** * +** x Address of array x * +** y Address of array y * +** l Length both x and y in bytes * +** * +******************************************/ +int greater(BYTE *x, BYTE *y, int l) +{ + int i; + + for ( i=0; i y[i] ) + return 1; + + return 0; +} + + +/***************************************** +** less - Test if x < y * +** * +** Returns TRUE (1) if x less than y, * +** otherwise FALSE (0). * +** * +** Parameters: * +** * +** x Address of array x * +** y Address of array y * +** l Length both x and y in bytes * +** * +******************************************/ +int less(BYTE *x, BYTE *y, int l) +{ + int i; + + for ( i=0; i>= 1; /* shift the word right once (ms bit = 0) */ + if ( c1 ) + *p |= 0x80; + c1 = c2; + c2 = 0; + p++; + } + + *p >>= 1; /* shift the word right once (ms bit = 0) */ + if ( c1 ) + *p |= 0x80; +} + + +/***************************************** +** Mult - Multiply two integers * +** * +** A = B * C * +** * +** Parameters: * +** * +** A Address of the result * +** B Address of the multiplier * +** C Address of the multiplicand * +** LB Length of B in bytes * +** LC Length of C in bytes * +** * +** NOTE: A MUST be LB+LC in length * +** * +******************************************/ +int Mult(BYTE *A, BYTE *B, int LB, BYTE *C, int LC) +{ + int i, j, k, LA; + DIGIT result; + + LA = LB + LC; + + for ( i=LB-1; i>=0; i-- ) { + result = 0; + for ( j=LC-1; j>=0; j-- ) { + k = i+j+1; + result = (DIGIT)A[k] + ((DIGIT)(B[i] * C[j])) + (result >> 8); + A[k] = (BYTE)result; + } + A[--k] = (BYTE)(result >> 8); + } + + return 0; +} + + +void ModSqr(BYTE *A, BYTE *B, int LB, BYTE *M, int LM) +{ + + Square(A, B, LB); + Mod(A, 2*LB, M, LM); +} + +void ModMult(BYTE *A, BYTE *B, int LB, BYTE *C, int LC, BYTE *M, int LM) +{ + Mult(A, B, LB, C, LC); + Mod(A, (LB+LC), M, LM); +} + + +/***************************************** +** smult - Multiply array by a scalar. * +** * +** A = b * C * +** * +** Parameters: * +** * +** A Address of the result * +** b Scalar (1 BYTE) * +** C Address of the multiplicand * +** L Length of C in bytes * +** * +** NOTE: A MUST be L+1 in length * +** * +******************************************/ +void smult(BYTE *A, BYTE b, BYTE *C, int L) +{ + int i; + DIGIT result; + + result = 0; + for ( i=L-1; i>0; i-- ) { + result = A[i] + ((DIGIT)b * C[i]) + (result >> 8); + A[i] = (BYTE)(result & 0xff); + A[i-1] = (BYTE)(result >> 8); + } +} + +/***************************************** +** Square() - Square an integer * +** * +** A = B^2 * +** * +** Parameters: * +** * +** A Address of the result * +** B Address of the operand * +** L Length of B in bytes * +** * +** NOTE: A MUST be 2*L in length * +** * +******************************************/ +void Square(BYTE *A, BYTE *B, int L) +{ + Mult(A, B, L, B, L); +} + +/***************************************** +** ModExp - Modular Exponentiation * +** * +** A = B ** C (MOD M) * +** * +** Parameters: * +** * +** A Address of result * +** B Address of mantissa * +** C Address of exponent * +** M Address of modulus * +** LB Length of B in bytes * +** LC Length of C in bytes * +** LM Length of M in bytes * +** * +** NOTE: The integer B must be less * +** than the modulus M. * +** NOTE: A must be at least 3*LM * +** bytes long. However, the * +** result stored in A will be * +** only LM bytes long. * +******************************************/ +void ModExp(BYTE *A, BYTE *B, int LB, BYTE *C, int LC, BYTE *M, int LM) +{ + BYTE wmask; + int bits; + + bits = LC*8; + wmask = 0x80; + + A[LM-1] = 1; + + while ( !sniff_bit(C,wmask) ) { + wmask >>= 1; + bits--; + if ( !wmask ) { + wmask = 0x80; + C++; + } + } + + while ( bits-- ) { + memset(A+LM, 0x00, LM*2); + + /* temp = A*A (MOD M) */ + ModSqr(A+LM, A,LM, M,LM); + + /* A = lower L bytes of temp */ + memcpy(A, A+LM*2, LM); + memset(A+LM, 0x00, 2*LM); + + if ( sniff_bit(C,wmask) ) { + memset(A+LM, 0x00, (LM+LB)); + ModMult(A+LM, B,LB, A,LM, M,LM); /* temp = B * A (MOD M) */ + memcpy(A, A+LM+(LM+LB)-LM, LM); /* A = lower LM bytes of temp */ + memset(A+LM, 0x00, 2*LM); + } + + wmask >>= 1; + if ( !wmask ) { + wmask = 0x80; + C++; + } + } +} + + +/* DivMod: + * + * computes: + * quot = x / n + * rem = x % n + * returns: + * length of "quot" + * + * len of rem is lenx+1 + */ +int DivMod(BYTE *x, int lenx, BYTE *n, int lenn, BYTE *quot, BYTE *rem) +{ + BYTE *tx, *tn, *ttx, *ts, bmult[1]; + int i, shift, lgth_x, lgth_n, t_len, lenq; + DIGIT tMSn, mult; + unsigned long tMSx; + int underflow; + + tx = x; + tn = n; + + /* point to the MSD of n */ + for ( i=0, lgth_n=lenn; i n, WATCH OUT if lgth_x == lgth_n */ + while ( (lgth_x > lgth_n) || ((lgth_x == lgth_n) && !less(tx, tn, lgth_n)) ) { + shift = 1; + if ( lgth_n == 1 ) { + if ( *tx < *tn ) { + tMSx = (DIGIT) (((*tx) << 8) | *(tx+1)); + tMSn = *tn; + shift = 0; + } + else { + tMSx = *tx; + tMSn = *tn; + } + } + else if ( lgth_n > 1 ) { + tMSx = (DIGIT) (((*tx) << 8) | *(tx+1)); + tMSn = (DIGIT) (((*tn) << 8) | *(tn+1)); + if ( (tMSx < tMSn) || ((tMSx == tMSn) && less(tx, tn, lgth_n)) ) { + tMSx = (tMSx << 8) | *(tx+2); + shift = 0; + } + } + else { + tMSx = (DIGIT) (((*tx) << 8) | *(tx+1)); + tMSn = *tn; + shift = 0; + } + + mult = (DIGIT) (tMSx / tMSn); + if ( mult > 0xff ) + mult = 0xff; + bmult[0] = mult & 0xff; + + ts = rem; + do { + memset(ts, 0x00, lgth_x+1); + Mult(ts, tn, lgth_n, bmult, 1); + + underflow = 0; + if ( shift ) { + if ( ts[0] != 0 ) + underflow = 1; + else { + for ( i=0; i= 0) ) { + accum += A[tL]; + A[tL--] = (BYTE)(accum & 0xff); + accum = accum >> 8; + } + + return accum; +} + + +/* + * add() + * + * A = A + B + * + * LB must be <= LA + * + */ +BYTE add(BYTE *A, int LA, BYTE *B, int LB) +{ + int i, indexA, indexB; + DIGIT accum; + + indexA = LA - 1; /* LSD of result */ + indexB = LB - 1; /* LSD of B */ + + accum = 0; + for ( i = 0; i < LB; i++ ) { + accum += A[indexA]; + accum += B[indexB--]; + A[indexA--] = (BYTE)(accum & 0xff); + accum = accum >> 8; + } + + if ( LA > LB ) + while ( accum && (indexA >= 0) ) { + accum += A[indexA]; + A[indexA--] = (BYTE)(accum & 0xff); + accum = accum >> 8; + } + + return (BYTE)accum; +} + + +void prettyprintBstr(char *S, BYTE *A, int L) +{ + int i, extra, ctrb, ctrl; + + if ( L == 0 ) + printf("%s ", S); + else + printf("%s\n\t", S); + extra = L % 24; + if ( extra ) { + ctrb = 0; + for ( i=0; i<24-extra; i++ ) { + printf(" "); + if ( ++ctrb == 4) { + printf(" "); + ctrb = 0; + } + } + + for ( i=0; i> 16 ); + buffer[ count ] = ( ( value & 0xFF00FF00L ) >> 8 ) | ( ( value & 0x00FF00FFL ) << 8 ); + } +} + +void +ahtopb (char *ascii_hex, BYTE *p_binary, int bin_len) +{ + BYTE nibble; + int i; + + for ( i=0; i 'F' ) + nibble -= 0x20; + if ( nibble > '9' ) + nibble -= 7; + nibble -= '0'; + p_binary[i] = nibble << 4; + + nibble = ascii_hex[i * 2 + 1]; + if ( nibble > 'F' ) + nibble -= 0x20; + if ( nibble > '9' ) + nibble -= 7; + nibble -= '0'; + p_binary[i] += nibble; + } +} \ No newline at end of file diff --git a/src/include/cephes.h b/src/include/cephes.h new file mode 100644 index 0000000..64c2b1f --- /dev/null +++ b/src/include/cephes.h @@ -0,0 +1,13 @@ +#ifndef _NIST_CEPHES_H_ +#define _NIST_CEPHES_H_ + +double cephes_igamc(double a, double x); +double cephes_igam(double a, double x); +double cephes_lgam(double x); +double cephes_p1evl(double x, double *coef, int N); +double cephes_polevl(double x, double *coef, int N); +double cephes_erf(double x); +double cephes_erfc(double x); +double cephes_normal(double x); + +#endif /* _CEPHES_H_ */ diff --git a/src/include/config.h b/src/include/config.h new file mode 100644 index 0000000..f31f50f --- /dev/null +++ b/src/include/config.h @@ -0,0 +1,52 @@ + +#if defined(__cplusplus) +extern "C" { +#endif + +#ifndef _NIST_CONFIG_H_ +#define _NIST_CONFIG_H_ + +#define WINDOWS32 +//#define PROTOTYPES +//#define LITTLE_ENDIAN +//#define LOWHI + +/* + * AUTO DEFINES (DON'T TOUCH!) + */ + +#ifndef CSTRTD +typedef char *CSTRTD; +#endif +#ifndef BSTRTD +typedef unsigned char *BSTRTD; +#endif + +#ifndef BYTE +typedef unsigned char BYTE; +#endif +#ifndef UINT +typedef unsigned int UINT; +#endif +#ifndef USHORT +typedef unsigned short USHORT; +#endif +//#ifndef ULONG +//typedef unsigned long ULONG; +//#endif +#ifndef DIGIT +typedef USHORT DIGIT; /* 16-bit word */ +#endif +#ifndef DBLWORD +typedef unsigned long DBLWORD; /* 32-bit word */ +#endif + +#ifndef WORD64 +typedef unsigned long WORD64[2]; /* 64-bit word */ +#endif + +#endif /* _CONFIG_H_ */ + +#if defined(__cplusplus) +} +#endif \ No newline at end of file diff --git a/src/include/decls.h b/src/include/decls.h new file mode 100644 index 0000000..510ae47 --- /dev/null +++ b/src/include/decls.h @@ -0,0 +1,24 @@ +#ifndef _NIST_DECLS_H_ +#define _NIST_DECLS_H_ + +#include +#include "defs.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + G L O B A L D A T A S T R U C T U R E S + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +BitSequence *epsilon; // BIT STREAM +TP tp; // TEST PARAMETER STRUCTURE +FILE *stats[NUMOFTESTS+1]; // FILE OUTPUT STREAM +FILE *results[NUMOFTESTS+1]; // FILE OUTPUT STREAM +FILE *freqfp; // FILE OUTPUT STREAM +FILE *summary; // FILE OUTPUT STREAM +int testVector[NUMOFTESTS+1]; + +char generatorDir[NUMOFGENERATORS][20] = { "AlgorithmTesting", "LCG", "QCG1", "QCG2","CCG", "XOR", + "MODEXP", "BBS", "MS", "G-SHA1" }; + + + +#endif \ No newline at end of file diff --git a/src/include/defs.h b/src/include/defs.h new file mode 100644 index 0000000..e0e93f7 --- /dev/null +++ b/src/include/defs.h @@ -0,0 +1,73 @@ +#ifndef _NIST_DEFS_H_ +#define _NIST_DEFS_H_ + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + D E B U G G I N G A I D E S + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "config.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + M A C R O S + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#define MAX(x,y) ((x) < (y) ? (y) : (x)) +#define MIN(x,y) ((x) > (y) ? (y) : (x)) +#define isNonPositive(x) ((x) <= 0.e0 ? 1 : 0) +#define isPositive(x) ((x) > 0.e0 ? 1 : 0) +#define isNegative(x) ((x) < 0.e0 ? 1 : 0) +#define isGreaterThanOne(x) ((x) > 1.e0 ? 1 : 0) +#define isZero(x) ((x) == 0.e0 ? 1 : 0) +#define isOne(x) ((x) == 1.e0 ? 1 : 0) + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + G L O B A L C O N S T A N T S + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#define ALPHA 0.01 /* SIGNIFICANCE LEVEL */ +#define MAXNUMOFTEMPLATES 148 /* APERIODIC TEMPLATES: 148=>temp_length=9 */ + +#define NUMOFGENERATORS 10 /* MAX PRNGs */ +#define MAXFILESPERMITTEDFORPARTITION 148 + +#define TEST_FREQUENCY 1 +#define TEST_BLOCK_FREQUENCY 2 +#define TEST_CUSUM 3 +#define TEST_RUNS 4 +#define TEST_LONGEST_RUN 5 +#define TEST_RANK 6 +#define TEST_FFT 7 +#define TEST_NONPERIODIC 8 +#define TEST_OVERLAPPING 9 +#define TEST_UNIVERSAL 10 +#define TEST_APEN 11 +#define TEST_RND_EXCURSION 12 +#define TEST_RND_EXCURSION_VAR 13 +#define TEST_SERIAL 14 +#define TEST_LINEARCOMPLEXITY 15 +#define TEST_POKER_DETECT 16 +#define TEST_RUNS_DISTRIBUTION 17 +#define TEST_BIN_DERIVATE 18 +#define TEST_SELF_CORR 19 + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + G L O B A L D A T A S T R U C T U R E S + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +typedef unsigned char BitSequence; + +typedef struct _testParameters { + int n; + int blockFrequencyBlockLength; + int nonOverlappingTemplateBlockLength; + int overlappingTemplateBlockLength; + int serialBlockLength; + int linearComplexitySequenceLength; + int approximateEntropyBlockLength; + int PokerDetectMLength; + int BinaryDerivateKLength; + int SelfCorrelationDLength; + int numOfBitStreams; +} TP; + +#endif \ No newline at end of file diff --git a/src/include/externs.h b/src/include/externs.h new file mode 100644 index 0000000..9f6ea9b --- /dev/null +++ b/src/include/externs.h @@ -0,0 +1,21 @@ +#ifndef _NIST_EXTERNS_H_ +#define _NIST_EXTERNS_H_ + +#include "defs.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + G L O B A L D A T A S T R U C T U R E S + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +//extern BitSequence *epsilon; // BIT STREAM +//extern TP tp; // TEST PARAMETER STRUCTURE +//extern FILE *stats[NUMOFTESTS+1]; // FILE OUTPUT STREAM +//extern FILE *results[NUMOFTESTS+1]; // FILE OUTPUT STREAM +//extern FILE *freqfp; // FILE OUTPUT STREAM +//extern FILE *summary; // FILE OUTPUT STREAM +//extern int testVector[NUMOFTESTS+1]; +// +//extern char generatorDir[NUMOFGENERATORS][20]; +//extern char testNames[NUMOFTESTS+1][32]; + +#endif \ No newline at end of file diff --git a/src/include/generators.h b/src/include/generators.h new file mode 100644 index 0000000..183d05e --- /dev/null +++ b/src/include/generators.h @@ -0,0 +1,78 @@ +#ifndef _NIST_GENERATORS_H_ +#define _NIST_GENERATORS_H_ + + +/* The circular shifts. */ +#define CS1(x) ((((ULONG)x)<<1)|(((ULONG)x)>>31)) +#define CS5(x) ((((ULONG)x)<<5)|(((ULONG)x)>>27)) +#define CS30(x) ((((ULONG)x)<<30)|(((ULONG)x)>>2)) + +/* K constants */ + +#define K0 0x5a827999L +#define K1 0x6ed9eba1L +#define K2 0x8f1bbcdcL +#define K3 0xca62c1d6L + +#define f1(x,y,z) ( (x & (y ^ z)) ^ z ) + +#define f3(x,y,z) ( (x & ( y ^ z )) ^ (z & y) ) + +#define f2(x,y,z) ( x ^ y ^ z ) /* Rounds 20-39 */ + +#define expand(x) Wbuff[x%16] = CS1(Wbuff[(x - 3)%16 ] ^ Wbuff[(x - 8)%16 ] ^ Wbuff[(x - 14)%16] ^ Wbuff[x%16]) + +#define sub1Round1(count) { \ + temp = CS5(A) + f1(B, C, D) + E + Wbuff[count] + K0; \ + E = D; \ + D = C; \ + C = CS30( B ); \ + B = A; \ + A = temp; \ + } \ + +#define sub2Round1(count) \ + { \ + expand(count); \ + temp = CS5(A) + f1(B, C, D) + E + Wbuff[count%16] + K0; \ + E = D; \ + D = C; \ + C = CS30( B ); \ + B = A; \ + A = temp; \ + } \ + +#define Round2(count) \ + { \ + expand(count); \ + temp = CS5( A ) + f2( B, C, D ) + E + Wbuff[count%16] + K1; \ + E = D; \ + D = C; \ + C = CS30( B ); \ + B = A; \ + A = temp; \ + } \ + +#define Round3(count) \ + { \ + expand(count); \ + temp = CS5( A ) + f3( B, C, D ) + E + Wbuff[count%16] + K2; \ + E = D; \ + D = C; \ + C = CS30( B ); \ + B = A; \ + A = temp; \ + } + +#define Round4(count) \ + { \ + expand(count); \ + temp = CS5( A ) + f2( B, C, D ) + E + Wbuff[count%16] + K3; \ + E = D; \ + D = C; \ + C = CS30( B ); \ + B = A; \ + A = temp; \ + } + +#endif \ No newline at end of file diff --git a/src/include/genutils.h b/src/include/genutils.h new file mode 100644 index 0000000..3d61ed4 --- /dev/null +++ b/src/include/genutils.h @@ -0,0 +1,47 @@ +#ifndef _NIST_GENUTILS_H_ +#define _NIST_GENUTILS_H_ + +#include +#include +#include +#include "config.h" + +typedef struct _MP_struct { + int size; /* in bytes */ + int bitlen; /* in bits, duh */ + BYTE *val; + } MP; + +#define FREE(A) if ( (A) ) { free((A)); (A) = NULL; } +#define ASCII2BIN(ch) ( (((ch) >= '0') && ((ch) <= '9')) ? ((ch) - '0') : (((ch) >= 'A') && ((ch) <= 'F')) ? ((ch) - 'A' + 10) : ((ch) - 'a' + 10) ) + +#ifndef EXPWD +#define EXPWD ((DBLWORD)1< +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +int +LinearComplexity(int M, int n, BitSequence *epsilon) +{ + int i, ii, j, d, N, L, m, N_, parity, sign, K = 6; + double p_value, T_, mean, nu[7], chi2; + const double pi[7] = { 0.01047, 0.03125, 0.12500, 0.50000, 0.25000, 0.06250, 0.020833 }; + BitSequence *T, *P, *B_, *C; + + N = (int)floor(n/M); + if ( ((B_ = (BitSequence *) calloc(M, sizeof(BitSequence))) == NULL) || + ((C = (BitSequence *) calloc(M, sizeof(BitSequence))) == NULL) || + ((P = (BitSequence *) calloc(M, sizeof(BitSequence))) == NULL) || + ((T = (BitSequence *) calloc(M, sizeof(BitSequence))) == NULL) ) { + printf("Insufficient Memory for Work Space:: Linear Complexity Test\n"); + if ( B_!= NULL ) + free(B_); + if ( C != NULL ) + free(C); + if ( P != NULL ) + free(P); + if ( T != NULL ) + free(T); + return 0; + } + + +// fprintf(stats[TEST_LINEARCOMPLEXITY], "-----------------------------------------------------\n"); +// fprintf(stats[TEST_LINEARCOMPLEXITY], "\tL I N E A R C O M P L E X I T Y\n"); +// fprintf(stats[TEST_LINEARCOMPLEXITY], "-----------------------------------------------------\n"); +// fprintf(stats[TEST_LINEARCOMPLEXITY], "\tM (substring length) = %d\n", M); +// fprintf(stats[TEST_LINEARCOMPLEXITY], "\tN (number of substrings) = %d\n", N); +// fprintf(stats[TEST_LINEARCOMPLEXITY], "-----------------------------------------------------\n"); +// fprintf(stats[TEST_LINEARCOMPLEXITY], " F R E Q U E N C Y \n"); +// fprintf(stats[TEST_LINEARCOMPLEXITY], "-----------------------------------------------------\n"); +// fprintf(stats[TEST_LINEARCOMPLEXITY], " C0 C1 C2 C3 C4 C5 C6 CHI2 P-value\n"); +// fprintf(stats[TEST_LINEARCOMPLEXITY], "-----------------------------------------------------\n"); +// fprintf(stats[TEST_LINEARCOMPLEXITY], "\tNote: %d bits were discarded!\n", n%M); + + for ( i=0; i +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + L O N G E S T R U N S T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +LongestRunOfOnes(int n, BitSequence *epsilon) +{ + double pval, chi2, pi[7]; + int run, v_n_obs, N, i, j, K, M, V[7]; + unsigned int nu[7] = { 0, 0, 0, 0, 0, 0, 0 }; + + if ( n < 128 ) { +// fprintf(stats[TEST_LONGEST_RUN], "\t\t\t LONGEST RUNS OF ONES TEST\n"); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n"); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t n=%d is too short\n", n); + return 0; + } + if ( n < 6272 ) { + K = 3; + M = 8; + V[0] = 1; V[1] = 2; V[2] = 3; V[3] = 4; + pi[0] = 0.21484375; + pi[1] = 0.3671875; + pi[2] = 0.23046875; + pi[3] = 0.1875; + } + else if ( n < 750000 ) { + K = 5; + M = 128; + V[0] = 4; V[1] = 5; V[2] = 6; V[3] = 7; V[4] = 8; V[5] = 9; + pi[0] = 0.1174035788; + pi[1] = 0.242955959; + pi[2] = 0.249363483; + pi[3] = 0.17517706; + pi[4] = 0.102701071; + pi[5] = 0.112398847; + } + else { + K = 6; + M = 10000; + V[0] = 10; V[1] = 11; V[2] = 12; V[3] = 13; V[4] = 14; V[5] = 15; V[6] = 16; + pi[0] = 0.0882; + pi[1] = 0.2092; + pi[2] = 0.2483; + pi[3] = 0.1933; + pi[4] = 0.1208; + pi[5] = 0.0675; + pi[6] = 0.0727; + } + + N = n/M; + for ( i=0; i v_n_obs ) + v_n_obs = run; + } + else + run = 0; + } + if ( v_n_obs < V[0] ) + nu[0]++; + for ( j=0; j<=K; j++ ) { + if ( v_n_obs == V[j] ) + nu[j]++; + } + if ( v_n_obs > V[K] ) + nu[K]++; + } + + chi2 = 0.0; + for ( i=0; i<=K; i++ ) + chi2 += ((nu[i] - N * pi[i]) * (nu[i] - N * pi[i])) / (N * pi[i]); + + pval = cephes_igamc((double)(K/2.0), chi2 / 2.0); + +// fprintf(stats[TEST_LONGEST_RUN], "\t\t\t LONGEST RUNS OF ONES TEST\n"); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n"); +// fprintf(stats[TEST_LONGEST_RUN], "\t\tCOMPUTATIONAL INFORMATION:\n"); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n"); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t(a) N (# of substrings) = %d\n", N); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t(b) M (Substring Length) = %d\n", M); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t(c) Chi^2 = %f\n", chi2); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n"); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t F R E Q U E N C Y\n"); +// fprintf(stats[TEST_LONGEST_RUN], "\t\t---------------------------------------------\n"); + +// if ( K == 3 ) { +// fprintf(stats[TEST_LONGEST_RUN], "\t\t <=1 2 3 >=4 P-value Assignment"); +// fprintf(stats[TEST_LONGEST_RUN], "\n\t\t %3d %3d %3d %3d ", nu[0], nu[1], nu[2], nu[3]); +// } +// else if ( K == 5 ) { +// fprintf(stats[TEST_LONGEST_RUN], "\t\t<=4 5 6 7 8 >=9 P-value Assignment"); +// fprintf(stats[TEST_LONGEST_RUN], "\n\t\t %3d %3d %3d %3d %3d %3d ", nu[0], nu[1], nu[2], +// nu[3], nu[4], nu[5]); +// } +// else { +// fprintf(stats[TEST_LONGEST_RUN],"\t\t<=10 11 12 13 14 15 >=16 P-value Assignment"); +// fprintf(stats[TEST_LONGEST_RUN],"\n\t\t %3d %3d %3d %3d %3d %3d %3d ", nu[0], nu[1], nu[2], +// nu[3], nu[4], nu[5], nu[6]); +// } + if ( isNegative(pval) || isGreaterThanOne(pval) ) { +// fprintf(stats[TEST_LONGEST_RUN], "WARNING: P_VALUE IS OUT OF RANGE.\n"); + return 0; + } + +// fprintf(stats[TEST_LONGEST_RUN], "%s\t\tp_value = %f\n\n", pval < ALPHA ? "FAILURE" : "SUCCESS", pval); fflush(stats[TEST_LONGEST_RUN]); +// fprintf(results[TEST_LONGEST_RUN], "%f\n", pval); fflush(results[TEST_LONGEST_RUN]); + if (pval < ALPHA) { + return 0; + } else { + return 1; + } +} diff --git a/src/matrix.c b/src/matrix.c new file mode 100644 index 0000000..c456187 --- /dev/null +++ b/src/matrix.c @@ -0,0 +1,170 @@ +#include +#include +#include "include/externs.h" +#include "include/matrix.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +R A N K A L G O R I T H M R O U T I N E S +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#define MATRIX_FORWARD_ELIMINATION 0 +#define MATRIX_BACKWARD_ELIMINATION 1 + +int +computeRank(int M, int Q, BitSequence **matrix) +{ + int i, rank, m=MIN(M,Q); + + /* FORWARD APPLICATION OF ELEMENTARY ROW OPERATIONS */ + for ( i=0; i0; i-- ) { + if ( matrix[i][i] == 1 ) + perform_elementary_row_operations(MATRIX_BACKWARD_ELIMINATION, i, M, Q, matrix); + else { /* matrix[i][i] = 0 */ + if ( find_unit_element_and_swap(MATRIX_BACKWARD_ELIMINATION, i, M, Q, matrix) == 1 ) + perform_elementary_row_operations(MATRIX_BACKWARD_ELIMINATION, i, M, Q, matrix); + } + } + + rank = determine_rank(m, M, Q, matrix); + + return rank; +} + +void +perform_elementary_row_operations(int flag, int i, int M, int Q, BitSequence **A) +{ + int j, k; + + if ( flag == MATRIX_FORWARD_ELIMINATION ) { + for ( j=i+1; j=0; j-- ) + if ( A[j][i] == 1 ) + for ( k=0; k= 0) && (A[index][i] == 0) ) + index--; + if ( index >= 0 ) + row_op = swap_rows(i, index, Q, A); + } + + return row_op; +} + +int +swap_rows(int i, int index, int Q, BitSequence **A) +{ + int p; + BitSequence temp; + + for ( p=0; p +#include "include/utilities.h" +#include "include/stat_fncs.h" +#include "mesa_sts.h" + +int mesa_statistical_test_suite(void* data,unsigned int datalen, struct sts_result* result) +{ + TP tp; + tp.n = datalen; + tp.blockFrequencyBlockLength = 128; + tp.nonOverlappingTemplateBlockLength = 9; + tp.overlappingTemplateBlockLength = 9; + tp.approximateEntropyBlockLength = 10; + tp.serialBlockLength = 16; + tp.linearComplexitySequenceLength = 500; + tp.numOfBitStreams = 1; + tp.PokerDetectMLength = 8; + tp.BinaryDerivateKLength = 3; + tp.SelfCorrelationDLength = 8; + + BitSequence* epsilon = (BitSequence *)calloc(tp.n,sizeof(BitSequence)); + int done, num_0s, num_1s, bitsRead; + num_0s = 0; + num_1s = 0; + bitsRead = 0; + done = 0; + done = convertToBits((BYTE*)data,datalen,tp.n,&num_0s,&num_1s,&bitsRead,epsilon); + result->frequency = Frequency(tp.n,epsilon); + result->block_frequency = BlockFrequency(tp.blockFrequencyBlockLength, tp.n,epsilon); + result->cumulative_sums = CumulativeSums(tp.n,epsilon); + result->runs = Runs(tp.n,epsilon); + result->longest_run = LongestRunOfOnes(tp.n,epsilon); + result->rank = Rank(tp.n,epsilon); + //result->discrete_fourier_transform = DiscreteFourierTransform(tp.n,epsilon);//cost too much time + result->non_overlapping_template_matching = NonOverlappingTemplateMatchings(tp.nonOverlappingTemplateBlockLength, tp.n,epsilon); + result->overlapping_template_matching = OverlappingTemplateMatchings(tp.overlappingTemplateBlockLength, tp.n,epsilon); + result->universal = Universal(tp.n,epsilon); + //result->approximate_entropy = ApproximateEntropy(tp.approximateEntropyBlockLength, tp.n,epsilon);//cost too much time + result->random_excursions = RandomExcursions(tp.n,epsilon); + result->random_excursions_variant = RandomExcursionsVariant(tp.n,epsilon); + //result->serial = Serial(tp.serialBlockLength,tp.n,epsilon);//cost too much time + //sresult->linear_complexity = LinearComplexity(tp.linearComplexitySequenceLength, tp.n,epsilon);//cost too much time + result->poker_detect = PokerDetect(tp.PokerDetectMLength,tp.n,epsilon); + result->runs_distribution = RunsDistribution(tp.n,epsilon); + result->self_correlation = SelfCorrelation(tp.SelfCorrelationDLength,tp.n,epsilon); + result->binary_derivative = BinaryDerivate(tp.BinaryDerivateKLength,tp.n,epsilon,tp.n);//this function will change the value of epsilon, must be the last one + + free(epsilon); + epsilon = NULL; + + return 0; +} \ No newline at end of file diff --git a/src/nonOverlappingTemplateMatchings.c b/src/nonOverlappingTemplateMatchings.c new file mode 100644 index 0000000..7f5d24b --- /dev/null +++ b/src/nonOverlappingTemplateMatchings.c @@ -0,0 +1,131 @@ +#include +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + N O N O V E R L A P P I N G T E M P L A T E T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +NonOverlappingTemplateMatchings(int m, int n, BitSequence *epsilon) +{ + int ret = 0; + int numOfTemplates[100] = {0, 0, 2, 4, 6, 12, 20, 40, 74, 148, 284, 568, 1116, + 2232, 4424, 8848, 17622, 35244, 70340, 140680, 281076, 562152}; + /*---------------------------------------------------------------------------- + NOTE: Should additional templates lengths beyond 21 be desired, they must + first be constructed, saved into files and then the corresponding + number of nonperiodic templates for that file be stored in the m-th + position in the numOfTemplates variable. + ----------------------------------------------------------------------------*/ + unsigned int bit, W_obs, nu[6], *Wj = NULL; + FILE *fp = NULL; + double sum, chi2, p_value, lambda, pi[6], varWj; + int i, j, jj, k, match, SKIP, M, N, K = 5; + char directory[100]; + BitSequence *sequence = NULL; + + N = 8; + M = n/N; + + if ( (Wj = (unsigned int*)calloc(N, sizeof(unsigned int))) == NULL ) { + return 0; + } + lambda = (M-m+1)/pow(2, m); + varWj = M*(1.0/pow(2.0, m) - (2.0*m-1.0)/pow(2.0, 2.0*m)); + sprintf(directory, "templates/template%d", m); + + if ( ((isNegative(lambda)) || (isZero(lambda))) || + ((fp = fopen(directory, "r")) == NULL) || + ((sequence = (BitSequence *) calloc(m, sizeof(BitSequence))) == NULL) ) { + goto end; + } + else { + if ( numOfTemplates[m] < MAXNUMOFTEMPLATES ) + SKIP = 1; + else + SKIP = (int)(numOfTemplates[m]/MAXNUMOFTEMPLATES); + numOfTemplates[m] = (int)numOfTemplates[m]/SKIP; + + sum = 0.0; + for ( i=0; i<2; i++ ) { /* Compute Probabilities */ + pi[i] = exp(-lambda+i*log(lambda)-cephes_lgam(i+1)); + sum += pi[i]; + } + pi[0] = sum; + for ( i=2; i<=K; i++ ) { /* Compute Probabilities */ + pi[i-1] = exp(-lambda+i*log(lambda)-cephes_lgam(i+1)); + sum += pi[i-1]; + } + pi[K] = 1 - sum; + + for( jj=0; jj 1 ) + fseek(fp, (long)(SKIP-1)*2*m, SEEK_CUR); +// fprintf(results[TEST_NONPERIODIC], "%f\n", p_value); fflush(results[TEST_NONPERIODIC]); + if (p_value < ALPHA) { + goto end; + } + } + } + + ret = 1; +// fprintf(stats[TEST_NONPERIODIC], "\n"); fflush(stats[TEST_NONPERIODIC]); + + end: + if (Wj != NULL) { + free(Wj); + } + if (fp != NULL) { + fclose(fp); + } + if (sequence != NULL) { + free(sequence); + } + return ret; +} diff --git a/src/overlappingTemplateMatchings.c b/src/overlappingTemplateMatchings.c new file mode 100644 index 0000000..70eabdf --- /dev/null +++ b/src/overlappingTemplateMatchings.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + O V E R L A P P I N G T E M P L A T E T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +double Pr(int u, double eta); + +int +OverlappingTemplateMatchings(int m, int n, BitSequence *epsilon) +{ + int ret = 0; + int i, k, match; + double W_obs, eta, sum, chi2, p_value, lambda; + int M, N, j, K = 5; + unsigned int nu[6] = { 0, 0, 0, 0, 0, 0 }; + double pi[6] = { 0.143783, 0.139430, 0.137319, 0.124314, 0.106209, 0.348945 }; + BitSequence *sequence; + + M = 1032; + N = n/M; + + if ( (sequence = (BitSequence *) calloc(m, sizeof(BitSequence))) == NULL ) { + return 0; + } + else { + for (i = 0; i < m; i++) + sequence[i] = 1; + } + + lambda = (double)(M-m+1)/pow(2,m); + eta = lambda/2.0; + sum = 0.0; + for ( i=0; i +#include +#include "include/stat_fncs.h" +#include "include/cephes.h" + +typedef struct _PokerNi { + unsigned int flag; + unsigned int count; +} PokerNi; + +unsigned char toByte(BitSequence *subEpsilon, int M) +{ + int i = 0; + unsigned char result = 0; + for (i = 0; i < M; ++i) { + result |= (subEpsilon[i] << (M - i - 1)); + } + return result; +} + +int findIndex(PokerNi *tab, int tabSize, unsigned int flag) +{ + int i = 0; + for (i = 0; i < tabSize; ++i) { + if (tab[i].flag == flag) { + return i; + } + } + return -1; +} + +int PokerDetect(int M, int n, BitSequence *epsilon) +{ + int ret = 0; + int i = 0, j = 0, N = n / M, index = 0, c = 0; + int maxElements = (int) pow(2, M); + double p_value = 0.0, sum_ni = 0.0, mp = 0.0, V = 0.0; + unsigned int flag = 0; + PokerNi *tab = NULL; + + if (M > 8) { + return 0; + } + + tab = (PokerNi *)calloc(maxElements, sizeof(PokerNi)); + if (NULL == tab) { + return 0; + } + for (i = 0; i < maxElements; ++i) { + tab[i].flag = (unsigned int) i; + tab[i].count = 0; + } + + for (i = 0, j = 0; j < N; ++j, i += M) { + flag = toByte(epsilon + i, M); + index = findIndex(tab, maxElements, flag); + if (-1 == index) { + goto end; + } + tab[index].count += 1; + } + + for (i = 0; i < maxElements; ++i) { + sum_ni += pow(tab[i].count, 2); + } + mp = (double)maxElements / N; + V = mp * sum_ni - N; + + p_value = cephes_igamc((double)(maxElements - 1) / 2, V / 2); + if (p_value < ALPHA) { + goto end; + } + + ret = 1; + + end: + free(tab); + return ret; +} \ No newline at end of file diff --git a/src/randomExcursions.c b/src/randomExcursions.c new file mode 100644 index 0000000..135396d --- /dev/null +++ b/src/randomExcursions.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + R A N D O M E X C U R S I O N S T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +RandomExcursions(int n, BitSequence *epsilon) +{ + int ret = 0; + int b, i, j, k, J, x; + int cycleStart, cycleStop, *cycle = NULL, *S_k = NULL; + const int stateX[8] = { -4, -3, -2, -1, 1, 2, 3, 4 }; + int counter[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + double p_value, sum, constraint, nu[6][8]; + double pi[5][6] = { {0.0000000000, 0.00000000000, 0.00000000000, 0.00000000000, 0.00000000000, 0.0000000000}, + {0.5000000000, 0.25000000000, 0.12500000000, 0.06250000000, 0.03125000000, 0.0312500000}, + {0.7500000000, 0.06250000000, 0.04687500000, 0.03515625000, 0.02636718750, 0.0791015625}, + {0.8333333333, 0.02777777778, 0.02314814815, 0.01929012346, 0.01607510288, 0.0803755143}, + {0.8750000000, 0.01562500000, 0.01367187500, 0.01196289063, 0.01046752930, 0.0732727051} }; + + if ( ((S_k = (int *)calloc(n, sizeof(int))) == NULL) || + ((cycle = (int *)calloc(MAX(1000, n/100), sizeof(int))) == NULL) ) { + printf("Random Excursions Test: Insufficent Work Space Allocated.\n"); + if ( S_k != NULL ) + free(S_k); + if ( cycle != NULL ) + free(cycle); + return 0; + } + + J = 0; /* DETERMINE CYCLES */ + S_k[0] = 2*(int)epsilon[0] - 1; + for( i=1; i MAX(1000, n/100) ) { + printf("ERROR IN FUNCTION randomExcursions: EXCEEDING THE MAX NUMBER OF CYCLES EXPECTED\n."); + goto end; + } + cycle[J] = i; + } + } + if ( S_k[n-1] != 0 ) + J++; + cycle[J] = n; + + constraint = MAX(0.005*pow(n, 0.5), 500); + if (J < constraint) { + ret = 1; //TODO + goto end; + } + else { + cycleStart = 0; + cycleStop = cycle[1]; + for ( k=0; k<6; k++ ) + for ( i=0; i<8; i++ ) + nu[k][i] = 0.; + for ( j=1; j<=J; j++ ) { /* FOR EACH CYCLE */ + for ( i=0; i<8; i++ ) + counter[i] = 0; + for ( i=cycleStart; i= 1 && S_k[i] <= 4) || (S_k[i] >= -4 && S_k[i] <= -1) ) { + if ( S_k[i] < 0 ) + b = 4; + else + b = 3; + counter[S_k[i]+b]++; + } + } + cycleStart = cycle[j]+1; + if ( j < J ) + cycleStop = cycle[j+1]; + + for ( i=0; i<8; i++ ) { + if ( (counter[i] >= 0) && (counter[i] <= 4) ) + nu[counter[i]][i]++; + else if ( counter[i] >= 5 ) + nu[5][i]++; + } + } + + for ( i=0; i<8; i++ ) { + x = stateX[i]; + sum = 0.; + for ( k=0; k<6; k++ ) + sum += pow(nu[k][i] - J*pi[(int)fabs(x)][k], 2) / (J*pi[(int)fabs(x)][k]); + p_value = cephes_igamc(2.5, sum/2.0); + + if ( isNegative(p_value) || isGreaterThanOne(p_value) ) { +// fprintf(stats[TEST_RND_EXCURSION], "WARNING: P_VALUE IS OUT OF RANGE.\n"); + goto end; + } + +// fprintf(stats[TEST_RND_EXCURSION], "%s\t\tx = %2d chi^2 = %9.6f p_value = %f\n", +// p_value < ALPHA ? "FAILURE" : "SUCCESS", x, sum, p_value); +// fprintf(results[TEST_RND_EXCURSION], "%f\n", p_value); fflush(results[TEST_RND_EXCURSION]); + if (p_value < ALPHA) { + goto end; + } + } + } +// fprintf(stats[TEST_RND_EXCURSION], "\n"); fflush(stats[TEST_RND_EXCURSION]); + ret = 1; + + end: + free(S_k); + free(cycle); + return ret; +} diff --git a/src/randomExcursionsVariant.c b/src/randomExcursionsVariant.c new file mode 100644 index 0000000..447ddf6 --- /dev/null +++ b/src/randomExcursionsVariant.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + R A N D O M E X C U R S I O N S V A R I A N T T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +RandomExcursionsVariant(int n, BitSequence *epsilon) +{ + int ret = 0; + int i, p, J, x, constraint, count, *S_k = NULL; + const int stateX[18] = { -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + double p_value; + + if ( (S_k = (int *)calloc(n, sizeof(int))) == NULL ) { + return 0; + } + J = 0; + S_k[0] = 2*(int)epsilon[0] - 1; + for ( i=1; i +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" +#include "include/matrix.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + R A N K T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +Rank(int n, BitSequence *epsilon) +{ + int ret = 0; + int N, i, k, r; + double p_value, product, chi_squared, arg1, p_32, p_31, p_30, R, F_32, F_31, F_30; + BitSequence **matrix = create_matrix(32, 32); + + N = n/(32*32); + if ( isZero(N) ) { + p_value = 0.00; + } + else { + r = 32; /* COMPUTE PROBABILITIES */ + product = 1; + for ( i=0; i<=r-1; i++ ) + product *= ((1.e0-pow(2, i-32))*(1.e0-pow(2, i-32)))/(1.e0-pow(2, i-r)); + p_32 = pow(2, r*(32+32-r)-32*32) * product; + + r = 31; + product = 1; + for ( i=0; i<=r-1; i++ ) + product *= ((1.e0-pow(2, i-32))*(1.e0-pow(2, i-32)))/(1.e0-pow(2, i-r)); + p_31 = pow(2, r*(32+32-r)-32*32) * product; + + p_30 = 1 - (p_32+p_31); + + F_32 = 0; + F_31 = 0; + for ( k=0; k +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + R U N S T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +Runs(int n, BitSequence *epsilon) +{ + int S, k; + double pi, V, erfc_arg, p_value; + + S = 0; + for ( k=0; k (2.0 / sqrt(n)) ) { + p_value = 0.0; + } + else { + + V = 1; + for ( k=1; k +#include +#include "include/stat_fncs.h" +#include "include/cephes.h" + +int RunsDistribution(int n, BitSequence *epsilon) +{ + int ret = 0; + int i = 0, j = 0, k = 0; + unsigned char runFlag = 0x00; + double p_value = 0.0, sum_bi = 0.0, sum_gi = 0.0, V = 0.0; + double *bi = NULL, *gi = NULL, *e = NULL; + double bit = 0.0, git = 0.0, et = 0.0; + + bi = (double *)calloc(n, sizeof(double)); + if (NULL == bi) { + goto end; + } + gi = (double *)calloc(n, sizeof(double)); + if (NULL == gi) { + goto end; + } + e = (double *)calloc(n, sizeof(double)); + if (NULL == e) { + goto end; + } + + for (i = 1; i <= n; ++i) { + e[i - 1] = (double)(n - i + 3) / pow(2, i + 2); + if (e[i - 1] >= 5) { + k = i; + } + } + + runFlag = epsilon[0]; + j = 1; + for (i = 1; i < n; ++i) { + if (epsilon[i] != runFlag) { + if (runFlag == 0x00) { + gi[j - 1] += 1; + } else if (runFlag == 0x01) { + bi[j - 1] += 1; + } + runFlag = epsilon[i]; + j = 1; + } else { + ++j; + } + } + + for (i = 0; i < k; ++i) { + bit = bi[i]; + et = e[i]; + sum_bi += pow(bit - et, 2) / et; + } + for (i = 0; i < k; ++i) { + git = gi[i]; + et = e[i]; + sum_gi += pow(git - et, 2) / et; + } + V = sum_bi + sum_gi; + + p_value = cephes_igamc(k - 1, V / 2); + if (p_value < ALPHA) { + goto end; + } + + ret = 1; + + end: + if (NULL != bi) { + free(bi); + } + if (NULL != gi) { + free(gi); + } + if (NULL != e) { + free(e); + } + return ret; +} + diff --git a/src/selfCorrelation.c b/src/selfCorrelation.c new file mode 100644 index 0000000..d47a67b --- /dev/null +++ b/src/selfCorrelation.c @@ -0,0 +1,25 @@ +#include +#include "include/stat_fncs.h" + +int SelfCorrelation(int d, int n, BitSequence *epsilon) +{ + int i = 0; + int n_d = n - d; + int Ad = 0; + double V = 0.0, p_value = 0.0, sqrt2 = 1.41421356237309504880; + + for (i = 0; i < n_d - 1; ++i) { + Ad += (epsilon[i] ^ epsilon[i + d]); + } + + V = 2 * ((double)Ad - ((double)n_d / 2)) / sqrt(n_d); + + p_value = erfc(fabs(V) / sqrt2); + + if (p_value < ALPHA) { + return 0; + } else { + return 1; + } +} + diff --git a/src/serial.c b/src/serial.c new file mode 100644 index 0000000..3d37272 --- /dev/null +++ b/src/serial.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +double psi2(int m, int n, BitSequence *epsilon); + +int +Serial(int m, int n, BitSequence *epsilon) +{ + double p_value1, p_value2, psim0, psim1, psim2, del1, del2; + + psim0 = psi2(m, n, epsilon); + psim1 = psi2(m - 1, n, epsilon); + psim2 = psi2(m - 2, n, epsilon); + del1 = psim0 - psim1; + del2 = psim0 - 2.0*psim1 + psim2; + p_value1 = cephes_igamc(pow(2, m-1)/2, del1/2.0); + p_value2 = cephes_igamc(pow(2, m-2)/2, del2/2.0); + + if (p_value1 < ALPHA || p_value2 < ALPHA) { + return 0; + } else { + return 1; + } +} + +double +psi2(int m, int n, BitSequence *epsilon) +{ + int i, j, k, powLen; + double sum, numOfBlocks; + unsigned int *P; + + if ( (m == 0) || (m == -1) ) + return 0.0; + numOfBlocks = n; + powLen = (int)pow(2, m+1)-1; + if ( (P = (unsigned int*)calloc(powLen,sizeof(unsigned int)))== NULL ) { +// fprintf(stats[TEST_SERIAL], "Serial Test: Insufficient memory available.\n"); +// fflush(stats[TEST_SERIAL]); + return 0.0; + } + for ( i=1; i +#include +#include +#include +#include "include/externs.h" +#include "include/cephes.h" + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + U N I V E R S A L T E S T + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int +Universal(int n, BitSequence *epsilon) +{ + int ret = 0; + int i, j, p, L, Q, K; + double arg, sqrt2, sigma, phi, sum, p_value, c; + long *T, decRep; + const double expected_value[17] = { 0, 0, 0, 0, 0, 0, 5.2177052, 6.1962507, 7.1836656, + 8.1764248, 9.1723243, 10.170032, 11.168765, + 12.168070, 13.167693, 14.167488, 15.167379 }; + const double variance[17] = { 0, 0, 0, 0, 0, 0, 2.954, 3.125, 3.238, 3.311, 3.356, 3.384, + 3.401, 3.410, 3.416, 3.419, 3.421 }; + + /* * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * THE FOLLOWING REDEFINES L, SHOULD THE CONDITION: n >= 1010*2^L*L * + * NOT BE MET, FOR THE BLOCK LENGTH L. * + * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + L = 5; + if ( n >= 387840 ) L = 6; + if ( n >= 904960 ) L = 7; + if ( n >= 2068480 ) L = 8; + if ( n >= 4654080 ) L = 9; + if ( n >= 10342400 ) L = 10; + if ( n >= 22753280 ) L = 11; + if ( n >= 49643520 ) L = 12; + if ( n >= 107560960 ) L = 13; + if ( n >= 231669760 ) L = 14; + if ( n >= 496435200 ) L = 15; + if ( n >= 1059061760 ) L = 16; + + Q = 10*(int)pow(2, L); + K = (int) (floor(n/L) - (double)Q); /* BLOCKS TO TEST */ + + p = (int)pow(2, L); + if ( (L < 6) || (L > 16) || ((double)Q < 10*pow(2, L)) || + ((T = (long *)calloc(p, sizeof(long))) == NULL) ) { + return 0; + } + + /* COMPUTE THE EXPECTED: Formula 16, in Marsaglia's Paper */ + c = 0.7 - 0.8/(double)L + (4 + 32/(double)L)*pow(K, -3/(double)L)/15; + sigma = c * sqrt(variance[L]/(double)K); + sqrt2 = sqrt(2); + sum = 0.0; + for ( i=0; i +#include +#include +#include +#include "include/utilities.h" + +int +convertToBits(BYTE *x, int xBitLength, int bitsNeeded, int *num_0s, int *num_1s, int *bitsRead, BitSequence* epsilon) +{ + int i, j, count, bit; + BYTE mask; + int zeros, ones; + + count = 0; + zeros = ones = 0; + for ( i=0; i<(xBitLength+7)/8; i++ ) { + mask = 0x80; + for ( j=0; j<8; j++ ) { + if ( *(x+i) & mask ) { + bit = 1; + (*num_1s)++; + ones++; + } + else { + bit = 0; + (*num_0s)++; + zeros++; + } + mask >>= 1; + epsilon[*bitsRead] = bit; + (*bitsRead)++; + if ( *bitsRead == bitsNeeded ) + return 1; + if ( ++count == xBitLength ) + return 0; + } + } + + return 0; +} + diff --git a/src/version.map b/src/version.map new file mode 100644 index 0000000..d4433ce --- /dev/null +++ b/src/version.map @@ -0,0 +1,6 @@ +{ + global: + mesa_statistical_test_suite; + GIT_VERSION*; + local: *; +}; \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..7a89809 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required (VERSION 2.8) + +include_directories(${CMAKE_SOURCE_DIR}/inc) +include_directories(/opt/MESA/include/MESA/) +include_directories(/opt/MESA/include) +include_directories(/usr/include/) +include_directories(${PROJECT_SOURCE_DIR}/src/) + +link_directories(/opt/MESA/lib) +link_directories("${CMAKE_BINARY_DIR}") + +add_executable(gtest_mesa gtest_mesa_sts.cpp) +target_link_libraries(gtest_mesa gtest gtest_main pcap pthread mesa_sts) + +file(COPY pcap DESTINATION ./) \ No newline at end of file diff --git a/test/gtest_mesa_sts.cpp b/test/gtest_mesa_sts.cpp new file mode 100644 index 0000000..d16b8d1 --- /dev/null +++ b/test/gtest_mesa_sts.cpp @@ -0,0 +1,257 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "gtest/gtest.h" + +extern "C" { +#include "mesa_sts.h" +} + +#define MAX_PKT_CNT 1 + +static int read_pcap_and_judge_randomness(const char* pcap_file, struct sts_result* result) +{ + pcap_t *handle; + struct pcap_pkthdr *header; // pcap报文头部结构 + const u_char *packet; // 报文数据指针 + char errbuf[PCAP_ERRBUF_SIZE]; + char content[2048] = {0}; + int content_len = 0; + int payload_len; + char *payload; + int pkt_cnt = 0; + + handle = pcap_open_offline(pcap_file, errbuf); + while (pcap_next_ex(handle, &header, &packet) > 0) { + unsigned short eth_type = ntohs(*(unsigned short *)(packet + 12)); + if (eth_type == ETH_P_IP) { + int l4_proto = *(unsigned char *)(packet + sizeof(struct ethhdr) + 9); + if (l4_proto == IPPROTO_TCP) { + payload_len = header->caplen - sizeof(struct ethhdr) - sizeof(struct iphdr) - sizeof(struct tcphdr); + payload = (char *)packet + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr); + } else if (l4_proto == IPPROTO_UDP) { + payload_len = header->caplen - sizeof(struct ethhdr) - sizeof(struct iphdr) - sizeof(struct udphdr); + payload = (char *)packet + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct udphdr); + } else { + continue; + } + + } else if (eth_type == ETH_P_IPV6) { + int l4_proto = *(unsigned char *)(packet + sizeof(struct ethhdr) + 6); + if (l4_proto == IPPROTO_TCP) { + payload_len = header->caplen - sizeof(struct ethhdr) - sizeof(struct ip6_hdr) - sizeof(struct tcphdr); + payload = (char *)packet + sizeof(struct ethhdr) + sizeof(struct ip6_hdr) + sizeof(struct tcphdr); + } else if (l4_proto == IPPROTO_UDP) { + payload_len = header->caplen - sizeof(struct ethhdr) - sizeof(struct ip6_hdr) - sizeof(struct udphdr); + payload = (char *)packet + sizeof(struct ethhdr) + sizeof(struct ip6_hdr) + sizeof(struct udphdr); + } else { + continue; + } + } + + if (payload_len < 100) { + continue; + } + + memcpy(content + content_len, payload, payload_len); + content_len += payload_len; + pkt_cnt++; + if (pkt_cnt == MAX_PKT_CNT) { + break; + } + } + + mesa_statistical_test_suite(content, content_len, result); + + pcap_close(handle); + + return 0; +} + +TEST(random_looking, telegram_mtproto_ipv4_key1) +{ + struct sts_result result; + read_pcap_and_judge_randomness("pcap/telegram_mtproto_ipv4_key_1.pcap", &result); + + EXPECT_EQ(result.frequency, 1); + EXPECT_EQ(result.block_frequency, 1); + EXPECT_EQ(result.cumulative_sums, 1); + EXPECT_EQ(result.runs, 1); + EXPECT_EQ(result.longest_run, 1); + EXPECT_EQ(result.rank, 0); + EXPECT_EQ(result.non_overlapping_template_matching, 0); + EXPECT_EQ(result.overlapping_template_matching, 1); + EXPECT_EQ(result.universal, 0); + EXPECT_EQ(result.random_excursions, 1); + EXPECT_EQ(result.random_excursions_variant, 1); + EXPECT_EQ(result.poker_detect, 1); + EXPECT_EQ(result.runs_distribution, 1); + EXPECT_EQ(result.self_correlation, 1); + EXPECT_EQ(result.binary_derivative, 1); +} + +TEST(random_looking, telegram_mtproto_ipv4_key2) +{ + struct sts_result result; + read_pcap_and_judge_randomness("pcap/telegram_mtproto_ipv4_key_2_dd.pcap", &result); + + EXPECT_EQ(result.frequency, 1); + EXPECT_EQ(result.block_frequency, 1); + EXPECT_EQ(result.cumulative_sums, 1); + EXPECT_EQ(result.runs, 1); + EXPECT_EQ(result.longest_run, 1); + EXPECT_EQ(result.rank, 0); + EXPECT_EQ(result.non_overlapping_template_matching, 0); + EXPECT_EQ(result.overlapping_template_matching, 1); + EXPECT_EQ(result.universal, 0); + EXPECT_EQ(result.random_excursions, 1); + EXPECT_EQ(result.random_excursions_variant, 1); + EXPECT_EQ(result.poker_detect, 1); + EXPECT_EQ(result.runs_distribution, 1); + EXPECT_EQ(result.self_correlation, 1); + EXPECT_EQ(result.binary_derivative, 1); +} + +TEST(random_looking, telegram_mtproto_ipv4_key3) +{ + struct sts_result result; + read_pcap_and_judge_randomness("pcap/telegram_mtproto_ipv4_key_3_ee.pcap", &result); + + EXPECT_EQ(result.frequency, 1); + EXPECT_EQ(result.block_frequency, 0); + EXPECT_EQ(result.cumulative_sums, 1); + EXPECT_EQ(result.runs, 0); + EXPECT_EQ(result.longest_run, 1); + EXPECT_EQ(result.rank, 0); + EXPECT_EQ(result.non_overlapping_template_matching, 0); + EXPECT_EQ(result.overlapping_template_matching, 1); + EXPECT_EQ(result.universal, 0); + EXPECT_EQ(result.random_excursions, 1); + EXPECT_EQ(result.random_excursions_variant, 1); + EXPECT_EQ(result.poker_detect, 0); + EXPECT_EQ(result.runs_distribution, 1); + EXPECT_EQ(result.self_correlation, 1); + EXPECT_EQ(result.binary_derivative, 1); +} + +TEST(random_looking, telegram_mtproto_ipv6_key1) +{ + struct sts_result result; + read_pcap_and_judge_randomness("pcap/telegram_mtproto_ipv6_key_1.pcap", &result); + + EXPECT_EQ(result.frequency, 1); + EXPECT_EQ(result.block_frequency, 1); + EXPECT_EQ(result.cumulative_sums, 1); + EXPECT_EQ(result.runs, 1); + EXPECT_EQ(result.longest_run, 1); + EXPECT_EQ(result.rank, 0); + EXPECT_EQ(result.non_overlapping_template_matching, 0); + EXPECT_EQ(result.overlapping_template_matching, 1); + EXPECT_EQ(result.universal, 0); + EXPECT_EQ(result.random_excursions, 1); + EXPECT_EQ(result.random_excursions_variant, 1); + EXPECT_EQ(result.poker_detect, 1); + EXPECT_EQ(result.runs_distribution, 1); + EXPECT_EQ(result.self_correlation, 1); + EXPECT_EQ(result.binary_derivative, 1); +} + +TEST(random_looking, telegram_mtproto_ipv6_key2) +{ + struct sts_result result; + read_pcap_and_judge_randomness("pcap/telegram_mtproto_ipv6_key_2_dd.pcap", &result); + + EXPECT_EQ(result.frequency, 1); + EXPECT_EQ(result.block_frequency, 1); + EXPECT_EQ(result.cumulative_sums, 1); + EXPECT_EQ(result.runs, 1); + EXPECT_EQ(result.longest_run, 1); + EXPECT_EQ(result.rank, 0); + EXPECT_EQ(result.non_overlapping_template_matching, 0); + EXPECT_EQ(result.overlapping_template_matching, 1); + EXPECT_EQ(result.universal, 0); + EXPECT_EQ(result.random_excursions, 1); + EXPECT_EQ(result.random_excursions_variant, 1); + EXPECT_EQ(result.poker_detect, 1); + EXPECT_EQ(result.runs_distribution, 1); + EXPECT_EQ(result.self_correlation, 1); + EXPECT_EQ(result.binary_derivative, 1); +} + +TEST(random_looking, telegram_mtproto_ipv6_key3) +{ + struct sts_result result; + read_pcap_and_judge_randomness("pcap/telegram_mtproto_ipv6_key_3_ee.pcap", &result); + + EXPECT_EQ(result.frequency, 1); + EXPECT_EQ(result.block_frequency, 0); + EXPECT_EQ(result.cumulative_sums, 1); + EXPECT_EQ(result.runs, 1); + EXPECT_EQ(result.longest_run, 1); + EXPECT_EQ(result.rank, 0); + EXPECT_EQ(result.non_overlapping_template_matching, 0); + EXPECT_EQ(result.overlapping_template_matching, 1); + EXPECT_EQ(result.universal, 0); + EXPECT_EQ(result.random_excursions, 1); + EXPECT_EQ(result.random_excursions_variant, 1); + EXPECT_EQ(result.poker_detect, 0); + EXPECT_EQ(result.runs_distribution, 1); + EXPECT_EQ(result.self_correlation, 1); + EXPECT_EQ(result.binary_derivative, 1); +} + +TEST(non_random_looking, wechat_voice_call) +{ + struct sts_result result; + read_pcap_and_judge_randomness("pcap/202202161604_win_wifi_30M_pure_wechat_wechat3.5.0.46_voice-call_120s_2_multinat.pcap", &result); + + EXPECT_EQ(result.frequency, 0); + EXPECT_EQ(result.block_frequency, 1); + EXPECT_EQ(result.cumulative_sums, 0); + EXPECT_EQ(result.runs, 0); + EXPECT_EQ(result.longest_run, 0); + EXPECT_EQ(result.rank, 0); + EXPECT_EQ(result.non_overlapping_template_matching, 0); + EXPECT_EQ(result.overlapping_template_matching, 1); + EXPECT_EQ(result.universal, 0); + EXPECT_EQ(result.random_excursions, 1); + EXPECT_EQ(result.random_excursions_variant, 1); + EXPECT_EQ(result.poker_detect, 1); + EXPECT_EQ(result.runs_distribution, 0); + EXPECT_EQ(result.self_correlation, 0); + EXPECT_EQ(result.binary_derivative, 1); +} + +TEST(non_random_looking, http) +{ + struct sts_result result; + read_pcap_and_judge_randomness("pcap/xingongsuo_kouling_http_C2S.pcap", &result); + + EXPECT_EQ(result.frequency, 0); + EXPECT_EQ(result.block_frequency, 0); + EXPECT_EQ(result.cumulative_sums, 0); + EXPECT_EQ(result.runs, 1); + EXPECT_EQ(result.longest_run, 0); + EXPECT_EQ(result.rank, 1); + EXPECT_EQ(result.non_overlapping_template_matching, 0); + EXPECT_EQ(result.overlapping_template_matching, 1); + EXPECT_EQ(result.universal, 0); + EXPECT_EQ(result.random_excursions, 1); + EXPECT_EQ(result.random_excursions_variant, 1); + EXPECT_EQ(result.poker_detect, 0); + EXPECT_EQ(result.runs_distribution, 0); + EXPECT_EQ(result.self_correlation, 0); + EXPECT_EQ(result.binary_derivative, 1); +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + //testing::GTEST_FLAG(filter) = "random_looking.telegram_mtproto_ipv6_key1"; + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/pcap/202202161604_win_wifi_30M_pure_wechat_wechat3.5.0.46_voice-call_120s_2_multinat.pcap b/test/pcap/202202161604_win_wifi_30M_pure_wechat_wechat3.5.0.46_voice-call_120s_2_multinat.pcap new file mode 100644 index 0000000..63787b7 Binary files /dev/null and b/test/pcap/202202161604_win_wifi_30M_pure_wechat_wechat3.5.0.46_voice-call_120s_2_multinat.pcap differ diff --git a/test/pcap/telegram_mtproto_ipv4_key_1.pcap b/test/pcap/telegram_mtproto_ipv4_key_1.pcap new file mode 100644 index 0000000..7b966dd Binary files /dev/null and b/test/pcap/telegram_mtproto_ipv4_key_1.pcap differ diff --git a/test/pcap/telegram_mtproto_ipv4_key_2_dd.pcap b/test/pcap/telegram_mtproto_ipv4_key_2_dd.pcap new file mode 100644 index 0000000..59eb480 Binary files /dev/null and b/test/pcap/telegram_mtproto_ipv4_key_2_dd.pcap differ diff --git a/test/pcap/telegram_mtproto_ipv4_key_3_ee.pcap b/test/pcap/telegram_mtproto_ipv4_key_3_ee.pcap new file mode 100644 index 0000000..8c70a1b Binary files /dev/null and b/test/pcap/telegram_mtproto_ipv4_key_3_ee.pcap differ diff --git a/test/pcap/telegram_mtproto_ipv6_key_1.pcap b/test/pcap/telegram_mtproto_ipv6_key_1.pcap new file mode 100644 index 0000000..e0b3144 Binary files /dev/null and b/test/pcap/telegram_mtproto_ipv6_key_1.pcap differ diff --git a/test/pcap/telegram_mtproto_ipv6_key_2_dd.pcap b/test/pcap/telegram_mtproto_ipv6_key_2_dd.pcap new file mode 100644 index 0000000..0659281 Binary files /dev/null and b/test/pcap/telegram_mtproto_ipv6_key_2_dd.pcap differ diff --git a/test/pcap/telegram_mtproto_ipv6_key_3_ee.pcap b/test/pcap/telegram_mtproto_ipv6_key_3_ee.pcap new file mode 100644 index 0000000..03a3678 Binary files /dev/null and b/test/pcap/telegram_mtproto_ipv6_key_3_ee.pcap differ diff --git a/test/pcap/xingongsuo_kouling_http_C2S.pcap b/test/pcap/xingongsuo_kouling_http_C2S.pcap new file mode 100644 index 0000000..cd05da7 Binary files /dev/null and b/test/pcap/xingongsuo_kouling_http_C2S.pcap differ diff --git a/vendor/CMakeLists.txt b/vendor/CMakeLists.txt new file mode 100644 index 0000000..0035fd9 --- /dev/null +++ b/vendor/CMakeLists.txt @@ -0,0 +1,36 @@ +# CMakeFiles for 3rd vendor library + +include(ExternalProject) +# libgtest +ExternalProject_Add(libgtest PREFIX libgtest + URL ${CMAKE_SOURCE_DIR}/vendor/googletest-release-1.10.0.tar.gz + URL_MD5 ecd1fa65e7de707cd5c00bdac56022cd + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX= -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}) +ExternalProject_Get_Property(libgtest INSTALL_DIR) +file(MAKE_DIRECTORY ${INSTALL_DIR}/include) +add_library(gtest-static STATIC IMPORTED GLOBAL) +add_dependencies(gtest-static libgtest) +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set_property(TARGET gtest-static PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib64/libgtestd.a) +else() + set_property(TARGET gtest-static PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib64/libgtest.a) +endif() +set_property(TARGET gtest-static PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include) +set_property(TARGET gtest-static PROPERTY INTERFACE_LINK_LIBRARIES pthread) + +# # libmsgpackc +# ExternalProject_Add(libmsgpackc PREFIX libmsgpackc +# URL ${CMAKE_SOURCE_DIR}/vendor/msgpack-c-6.0.0.tar.gz +# URL_MD5 adc08f48550ce772fe24c0b41166b0de +# CMAKE_ARGS -DCMAKE_INSTALL_PREFIX= -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} +# -DMSGPACK_BUILD_TESTS=OFF -DMSGPACK_USE_BOOST=OFF) +# ExternalProject_Get_Property(libmsgpackc INSTALL_DIR) +# file(MAKE_DIRECTORY ${INSTALL_DIR}/include) +# add_library(msgpackc-static STATIC IMPORTED GLOBAL) +# add_dependencies(msgpackc-static libmsgpackc) + +# set_property(TARGET msgpackc-static PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libmsgpack-c.a) +# set_property(TARGET msgpackc-static PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include) + + + diff --git a/vendor/googletest-release-1.10.0.tar.gz b/vendor/googletest-release-1.10.0.tar.gz new file mode 100644 index 0000000..ab10868 Binary files /dev/null and b/vendor/googletest-release-1.10.0.tar.gz differ