diff --git a/.ci/detect_os_arch_package_format b/.ci/detect_os_arch_package_format index dc76376..6b12647 100755 --- a/.ci/detect_os_arch_package_format +++ b/.ci/detect_os_arch_package_format @@ -4,8 +4,10 @@ package_name = ARGV.first DEB_PACKAGE_REGEX = %r!(?[^/]+)/kasmvncserver_.+?_(?.+?).(?deb)! RPM_PACKAGE_REGEX = %r!(?[^/]+)/kasmvncserver-.+?\.(?[^.]+).(?rpm)! +ALPINE_PACKAGE_REGEX = %r!(?[^/]+)/kasmvncserver-(doc-)?.+?-r\d+_(?[^.]+)\.(?apk)! if matches = package_name.match(DEB_PACKAGE_REGEX) +elsif matches = package_name.match(ALPINE_PACKAGE_REGEX) else matches = package_name.match(RPM_PACKAGE_REGEX) end diff --git a/.ci/upload.sh b/.ci/upload.sh index fc6aeb1..7541b84 100644 --- a/.ci/upload.sh +++ b/.ci/upload.sh @@ -3,7 +3,7 @@ is_kasmvnc() { local package="$1"; - echo "$package" | grep -qP 'kasmvncserver(_|-)[0-9]' + echo "$package" | grep -qP 'kasmvncserver(_|-)(doc-)?[0-9]' } detect_deb_package_arch() { @@ -27,6 +27,13 @@ fetch_xvnc_md5sum() { cat DEBIAN/md5sums | grep bin/Xkasmvnc | cut -d' ' -f 1 } +detect_alpine_doc_package() { + is_alpine_doc_package= + if [[ $package =~ kasmvncserver-doc ]]; then + is_alpine_doc_package=1 + fi +} + function prepare_upload_filename() { local package="$1"; @@ -44,32 +51,90 @@ function prepare_upload_filename() { REVISION="_${REVISION}" fi + detect_alpine_doc_package + if [ -n "$RELEASE_BRANCH" ]; then - export upload_filename="kasmvncserver_${PACKAGE_OS}_${RELEASE_VERSION}${REVISION}_${OS_ARCH}.${PACKAGE_FORMAT}"; + export upload_filename="kasmvncserver${is_alpine_doc_package:+_doc}_${PACKAGE_OS}_${RELEASE_VERSION}${REVISION}_${OS_ARCH}.${PACKAGE_FORMAT}"; else export SANITIZED_BRANCH="$(echo $CI_COMMIT_REF_NAME | sed 's/\//_/g')"; - export upload_filename="kasmvncserver_${PACKAGE_OS}_${RELEASE_VERSION}_${SANITIZED_BRANCH}_${CI_COMMIT_SHA:0:6}${REVISION}_${OS_ARCH}.${PACKAGE_FORMAT}"; + export upload_filename="kasmvncserver${is_alpine_doc_package:+_doc}_${PACKAGE_OS}_${RELEASE_VERSION}_${SANITIZED_BRANCH}_${CI_COMMIT_SHA:0:6}${REVISION}_${OS_ARCH}.${PACKAGE_FORMAT}"; fi }; +list_files_in_directory() { + local dir="$1" + find "$1" -mindepth 1 +} + +upload_directory_to_s3() { + local dir_to_upload="$1" + local s3_directory="$2"; + local s3_bucket="$3"; + + for file_to_upload in $(list_files_in_directory "$dir_to_upload"); do + upload_to_s3 "$file_to_upload" "$s3_directory/$file_to_upload" "$s3_bucket" + done +} + +prepare_functional_tests_source_and_cd_into_it() { + git clone https://gitlab-ci-token:$CI_JOB_TOKEN@gitlab.com/kasm-technologies/internal/kasmvnc-functional-tests.git + cd kasmvnc-functional-tests + mkdir output && chown 1000:1000 output + mkdir report && chown 1000:1000 report +} + +upload_report_to_s3() { + s3_tests_directory="kasmvnc/${CI_COMMIT_SHA}/tests" + upload_directory_to_s3 report "$s3_tests_directory" "$S3_BUCKET" + aws s3 cp report/index.html "s3://${S3_BUCKET}/${s3_tests_directory}/report/index.html" --metadata-directive REPLACE --content-type "text/html" +} + +put_report_into_ci_pipeline() { + report_name="Functional%20test%20report" + report_url="https://${S3_BUCKET}.s3.amazonaws.com/${s3_tests_directory}/report/index.html" + curl --request POST --header "PRIVATE-TOKEN:${GITLAB_API_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/statuses/${CI_COMMIT_SHA}?state=success&name=${report_name}&target_url=${report_url}" +} + +prepare_kasmvnc_built_packages_to_replace_workspaces_image_packages() { + cp -r ../output/jammy output/ +} + +prepare_to_run_functional_tests() { + install_packages_needed_for_functional_tests + prepare_functional_tests_source_and_cd_into_it + prepare_s3_uploader + prepare_kasmvnc_built_packages_to_replace_workspaces_image_packages +} + +install_packages_needed_for_functional_tests() { + export DEBIAN_FRONTEND=noninteractive + apt-get update && apt-get install -y git tree curl docker.io awscli + apt-get install -y ruby3.1 wget + apt-get install -y python3 python3-pip python3-boto3 curl pkg-config libxmlsec1-dev +} + function upload_to_s3() { - local package="$1"; - local upload_filename="$2"; + local file_to_upload="$1"; + local s3_url_for_file="$2"; local s3_bucket="$3"; # Transfer to S3 - python3 amazon-s3-bitbucket-pipelines-python/s3_upload.py "${s3_bucket}" "$package" "${upload_filename}"; + python3 amazon-s3-bitbucket-pipelines-python/s3_upload.py "$s3_bucket" "$file_to_upload" "$s3_url_for_file"; # Use the Gitlab API to tell Gitlab where the artifact was stored - export S3_URL="https://${s3_bucket}.s3.amazonaws.com/${upload_filename}"; + export S3_URL="https://${s3_bucket}.s3.amazonaws.com/${s3_url_for_file}"; }; +function prepare_s3_uploader() { + git clone https://bitbucket.org/awslabs/amazon-s3-bitbucket-pipelines-python.git +} + function prepare_to_run_scripts_and_s3_uploads() { - export DEBIAN_FRONTEND=noninteractive; - apt-get update; - apt-get install -y ruby2.7 git wget; - apt-get install -y python3 python3-pip python3-boto3 curl pkg-config libxmlsec1-dev; - git clone https://bitbucket.org/awslabs/amazon-s3-bitbucket-pipelines-python.git; -}; + export DEBIAN_FRONTEND=noninteractive + apt-get update + apt-get install -y ruby2.7 git wget + apt-get install -y python3 python3-pip python3-boto3 curl pkg-config libxmlsec1-dev + prepare_s3_uploader +} detect_release_branch() { if echo $CI_COMMIT_REF_NAME | grep -Pq '^release/([\d.]+)$'; then diff --git a/.gitignore b/.gitignore index f4a278e..3f92e8f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.lo .deps .libs +*.swp CMakeFiles CMakeCache.txt @@ -12,6 +13,10 @@ Makefile Makefile.in config.h +libjpeg-turbo/ +xorg.build/ +install_manifest.txt + builder/build/ builder/www/ spec/tmp @@ -23,3 +28,10 @@ debian/kasmvncserver.substvars debian/kasmvncserver/ .pc .vscode/ + +# --run-test artifacts +run_test/ + +alpine/.abuild/kasmvnc_signing_key.rsa +alpine/.abuild/kasmvnc_signing_key.rsa.pub +alpine/packages/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6dd1bf6..ebf0947 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,7 +7,7 @@ variables: GITLAB_SHARED_DIND_DIR: /builds/$CI_PROJECT_PATH/shared GIT_SUBMODULE_STRATEGY: normal GIT_FETCH_EXTRA_FLAGS: --tags --force - # E.g. BUILD_JOBS: build_debian_buster,build_ubuntu_bionic. This will include + # E.g. BUILD_JOBS: build_debian_buster,build_ubuntu_focal. This will include # arm builds, because build_debian_buster_arm matches build_debian_buster. # "BUILD_JOBS: none" won't build any build jobs, nor www. BUILD_JOBS: all @@ -22,6 +22,8 @@ workflow: stages: - www - build + - functional_test + - run_test - test - upload @@ -29,6 +31,7 @@ stages: - pwd - apk add bash - mkdir -p "$GITLAB_SHARED_DIND_DIR" && chmod 777 "$GITLAB_SHARED_DIND_DIR" + - docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD .prepare_www: &prepare_www - tar -zxf output/www/kasm_www.tar.gz -C builder/ @@ -41,6 +44,24 @@ default: tags: - oci-fixed-amd +functional_test: + stage: functional_test + image: debian:bookworm + tags: + - oci-fixed-amd + before_script: + - . .ci/upload.sh + script: + - prepare_to_run_functional_tests + - ./functional-test + - upload_report_to_s3 + - put_report_into_ci_pipeline + dependencies: + - build_amd64 + artifacts: + paths: + - kasmvnc-functional-tests/output/ + build_www: stage: www allow_failure: false @@ -66,7 +87,7 @@ build_www: paths: - output/ -build_ubuntu_bionic: +build_amd64: stage: build allow_failure: true tags: @@ -77,17 +98,20 @@ build_ubuntu_bionic: after_script: - *prepare_artfacts script: - - bash builder/build-package ubuntu bionic + - bash builder/build-package $DISTRO; only: variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME + - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ "$DISTRO" artifacts: paths: - output/ + parallel: + matrix: + - DISTRO: [ 'ubuntu focal', 'ubuntu jammy', 'ubuntu noble', 'debian bullseye', 'debian bookworm', 'kali kali-rolling', 'oracle 8', 'oracle 9', 'opensuse 15', 'fedora forty', 'fedora fortyone', 'alpine 318', 'alpine 319', 'alpine 320', 'alpine 321' ] -build_ubuntu_bionic_arm: +build_arm64: stage: build - allow_failure: false + allow_failure: true tags: - oci-fixed-arm before_script: @@ -96,785 +120,74 @@ build_ubuntu_bionic_arm: after_script: - *prepare_artfacts script: - - bash builder/build-package ubuntu bionic + - bash builder/build-package $DISTRO; only: variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME + - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ "$DISTRO" artifacts: paths: - output/ + parallel: + matrix: + - DISTRO: [ 'ubuntu focal', 'ubuntu jammy', 'ubuntu noble', 'debian bullseye', 'debian bookworm', 'kali kali-rolling', 'oracle 8', 'oracle 9', 'opensuse 15', 'fedora forty', 'fedora fortyone', 'alpine 318', 'alpine 319', 'alpine 320', 'alpine 321' ] -build_ubuntu_focal: - stage: build - allow_failure: true +run_test_amd64: + stage: run_test tags: - oci-fixed-amd before_script: - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts script: - - bash builder/build-package ubuntu focal; + - bash builder/test-barebones --run-test $DISTRO only: variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME + - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ "$DISTRO" + dependencies: + - build_amd64 artifacts: - paths: - - output/ + reports: + junit: + - run_test/*.xml + parallel: + matrix: + - DISTRO: [ 'ubuntu focal', 'ubuntu jammy', 'ubuntu noble', 'debian bullseye', 'debian bookworm', 'kali kali-rolling', 'oracle 8', 'oracle 9', 'opensuse 15', 'fedora forty', 'fedora fortyone', 'alpine 318', 'alpine 319', 'alpine 320', 'alpine 321' ] -build_ubuntu_focal_arm: - stage: build - allow_failure: true +run_test_arm64: + stage: run_test tags: - oci-fixed-arm before_script: - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts script: - - bash builder/build-package ubuntu focal; + - bash builder/test-barebones --run-test $DISTRO only: variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME + - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ "$DISTRO" + dependencies: + - build_arm64 artifacts: - paths: - - output/ + reports: + junit: + - run_test/*.xml + parallel: + matrix: + - DISTRO: [ 'ubuntu focal', 'ubuntu jammy', 'ubuntu noble', 'debian bullseye', 'debian bookworm', 'kali kali-rolling', 'oracle 8', 'oracle 9', 'opensuse 15', 'fedora forty', 'fedora fortyone', 'alpine 318', 'alpine 319', 'alpine 320', 'alpine 321' ] -build_ubuntu_jammy: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package ubuntu jammy; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_ubuntu_jammy_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package ubuntu jammy; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_ubuntu_noble: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package ubuntu noble; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_ubuntu_noble_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package ubuntu noble; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_debian_buster: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package debian buster; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_debian_buster_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package debian buster; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_debian_bullseye: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package debian bullseye; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_debian_bullseye_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package debian bullseye; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - - -build_debian_bookworm: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package debian bookworm; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_debian_bookworm_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package debian bookworm; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_kali_rolling: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package kali kali-rolling; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_kali_rolling_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package kali kali-rolling; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_oracle_8: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package oracle 8; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_oracle_8_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package oracle 8; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_oracle_9: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package oracle 9; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_oracle_9_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package oracle 9; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_opensuse_15: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package opensuse 15; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_opensuse_15_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package opensuse 15; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_thirtyseven: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora thirtyseven; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_thirtyseven_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora thirtyseven; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_thirtyeight: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora thirtyeight; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_thirtyeight_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora thirtyeight; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_thirtynine: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora thirtynine; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_thirtynine_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora thirtynine; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_forty: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora forty; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_forty_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora forty; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_fortyone: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora fortyone; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_fedora_fortyone_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package fedora fortyone; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_alpine_317: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 317; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_alpine_317_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 317; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -test: +spec_test: stage: test tags: - - oci-fixed-amd + - kasmvnc-x86 before_script: - *prepare_build + artifacts: + reports: + junit: + - SelfBench.xml + - Benchmark.xml script: - bash builder/test-vncserver -build_alpine_318: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 318; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_alpine_318_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 318; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_alpine_319: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 319; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_alpine_319_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 319; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_alpine_320: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 320; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_alpine_320_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 320; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_alpine_321: - stage: build - allow_failure: true - tags: - - oci-fixed-amd - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 321; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - -build_alpine_321_arm: - stage: build - allow_failure: true - tags: - - oci-fixed-arm - before_script: - - *prepare_build - - *prepare_www - after_script: - - *prepare_artfacts - script: - - bash builder/build-package alpine 321; - only: - variables: - - $BUILD_JOBS == 'all' || $BUILD_JOBS =~ $CI_JOB_NAME - artifacts: - paths: - - output/ - upload: stage: upload image: ubuntu:focal @@ -900,7 +213,7 @@ upload: - export S3_BUILD_DIRECTORY="kasmvnc/${CI_COMMIT_SHA}" - export RELEASE_VERSION=$(.ci/next_release_version "$CI_COMMIT_REF_NAME") - uploaded_files=() - - for package in `find output/ -type f -name '*.deb' -or -name '*.rpm' -or -name '*.tgz'`; do + - for package in `find output/ -type f -name '*.deb' -or -name '*.rpm' -or -name '*.apk'`; do prepare_upload_filename "$package"; upload_filename="${S3_BUILD_DIRECTORY}/$upload_filename"; echo; diff --git a/.gitmodules b/.gitmodules index 95fb845..74a470e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "kasmweb"] path = kasmweb url = https://github.com/kasmtech/noVNC.git - branch = release/1.2.2 + branch = release/1.2.3 diff --git a/BUILDING.txt b/BUILDING.txt index b0f3782..4cd1a8e 100644 --- a/BUILDING.txt +++ b/BUILDING.txt @@ -48,7 +48,7 @@ Build Requirements (Windows) You also need the Inno Setup Preprocessor, which is available in the Inno Setup QuickStart Pack. - Add the directory containing iscc.exe (for instance, + Add the directory containing iscc.exe (for instance, C:\Program Files\Inno Setup 5) to the system or user PATH environment variable prior to building KasmVNC. @@ -71,6 +71,67 @@ For in-tree builds, these directories are the same. Building KasmVNC ================= +Building the KasmVNC Server using Docker +---------------------------------------- + +```bash +git submodule init +git submodule update --remote --merge +sudo docker build -t kasmvnc:dev -f builder/dockerfile.ubuntu_jammy.dev . +sudo docker run -it --rm -v ./:/src -p 6901:6901 -p 8443:8443 --name kasmvnc_dev kasmvnc:dev +``` + +Now from inside the container. + +```bash +# build frontend +cd kasmweb +npm install +npm run build # <-- only run this on subsequent changes to front-end code +cd .. +# build dependencies, this is optional as they are pre-built in the docker image. Only rebuild if you made version changes and need to test. +# sudo builder/scripts/build-webp +# sudo builder/scripts/build-libjpeg-turbo +# Build KasmVNC +builder/build.sh +``` + +Now run Xvnc and Xfce4 from inside the container + +```bash +/src/xorg.build/bin/Xvnc -interface 0.0.0.0 -PublicIP 127.0.0.1 -disableBasicAuth -RectThreads 0 -Log *:stdout:100 -httpd /src/kasmweb/dist -sslOnly 0 -SecurityTypes None -websocketPort 6901 :1 & +/usr/bin/xfce4-session --display :1 +``` + +Now open a browser and navigate to your dev VM on port 6901. + +Running noVNC from source +------------------------- +If you need to debug or make changes to the UI code, use the following procedures to use npm to serve the web code. The code will automatically rebuild when changes are made and the code will not be packaged. +These steps assume you are inside the kasmvnc:dev container started in the above steps. + +Now from inside the container. **This assumes KasmVNC is already built, follow steps above if you need to build KasmVNC** + +```bash +# Run KasmVNC +/src/xorg.build/bin/Xvnc -interface 0.0.0.0 -PublicIP 127.0.0.1 -disableBasicAuth -RectThreads 0 -Log *:stdout:100 -httpd /src/kasmweb/dist -sslOnly 0 -SecurityTypes None -websocketPort 6901 :1 & +/usr/bin/xfce4-session --display :1 & + +sudo nginx +cd kasmweb +npm install # only needs done first time +npm run serve # <-- Needs to run in foreground +``` + +Now open a browser and navigate to your dev VM on port 8443 over https. + +NGINX is proxying the websocket to KasmVNC and all other requests go to the node server. NGINX listens on 8443 with ssl. + +Since `npm run serve` needs to run in the foreground, you may need to exec into the container from another terminal to run additional commands like stopping Xvnc, rebuilding KasmVNC, etc. + +```bash +sudo docker exec -it kasmvnc_dev /bin/bash +``` Building the KasmVNC Server on Modern Unix/Linux Systems --------------------------------------------------------- @@ -90,7 +151,7 @@ but the general outline is as follows. > cp -R {xorg_source}/* unix/xserver/ (NOTE: {xorg_source} is the directory containing the Xorg source for the machine on which you are building KasmVNC. The most recent versions of - Red Hat/CentOS/Fedora, for instance, provide an RPM called + Red Hat/Fedora, for instance, provide an RPM called "xorg-x11-server-source", which installs the Xorg source under /usr/share/xorg-x11-server-source.) @@ -113,8 +174,8 @@ but the general outline is as follows. --with-serverconfig-path=/usr/lib[64]/xorg \ --with-dri-driver-path=/usr/lib[64]/dri \ {additional configure options} - (NOTE: This is merely an example that works with Red Hat Enterprise/CentOS - 6 and recent Fedora releases. You should customize it for your particular + (NOTE: This is merely an example that works with Red Hat Enterprise + and recent Fedora releases. You should customize it for your particular system. In particular, it will be necessary to customize the font, XKB, and DRI directories.) @@ -187,7 +248,7 @@ Building TLS Support ====================================== TLS requires GnuTLS, which is supplied with most Linux distributions and -with MinGW for Windows and can be built from source on OS X and other +with MinGW for Windows and can be built from source on OS X and other Unix variants. However, GnuTLS versions > 2.12.x && < 3.3.x should be avoided because of potential incompatibilities during initial handshaking. @@ -314,7 +375,7 @@ X server source (for instance, --host=i686-pc-linux-gnu). Add -DCMAKE_OSX_SYSROOT=/Developer/SDKs/MacOSX10.5.sdk \ - -DCMAKE_OSX_DEPLOYMENT_TARGET=10.5 + -DCMAKE_OSX_DEPLOYMENT_TARGET=10.5 to the CMake command line. The OS X 10.5 SDK must be installed. @@ -406,7 +467,7 @@ Distribution-Specific Packaging =============================== -RPM Packages for RHEL / CentOS +RPM Packages for RHEL ------------------------------ The RPM spec files and patches used to create the nightly builds diff --git a/CMakeLists.txt b/CMakeLists.txt index 3807fc4..0ef404d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ include(CheckCSourceRuns) include(CMakeMacroLibtoolFile) -project(kasmvnc) +project(kasmvnc LANGUAGES C CXX) set(VERSION 0.9) # The RC version must always be four comma-separated numbers @@ -74,13 +74,10 @@ set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -UNDEBUG") # Make sure we get a sane C version set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") +set(CMAKE_CXX_STANDARD 20) # Enable OpenMP set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp") - -# Enable C++ 11 -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") # Tell the compiler to be stringent set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wformat=2") @@ -230,6 +227,7 @@ include_directories(${CMAKE_BINARY_DIR}) include(cmake/StaticBuild.cmake) +add_subdirectory(third_party) add_subdirectory(common) if(WIN32) @@ -242,11 +240,12 @@ else() endif() if(ENABLE_NLS) - add_subdirectory(po) + add_subdirectory(po) endif() -add_subdirectory(tests) - +if (TESTS) + add_subdirectory(tests) +endif() include(cmake/BuildPackages.cmake) diff --git a/README.md b/README.md index 5efc230..4f89163 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # KasmVNC - Linux Web Remote Desktop - + KasmVNC provides remote web-based access to a Desktop or application. While VNC is in the name, KasmVNC differs from other VNC variants such as TigerVNC, RealVNC, and TurboVNC. KasmVNC has broken from the RFB specification which defines VNC, in order to support modern technologies and increase security. KasmVNC is accessed by users from any modern browser and does not support legacy VNC viewer applications. KasmVNC uses a modern YAML based configuration at the server and user level, allowing for ease of management. @@ -10,7 +10,7 @@ KasmVNC provides remote web-based access to a Desktop or application. While VNC **Do not use the README from the master branch**, unless you are compiling KasmVNC yourself from the tip of master. Use the documentation for your specific release. - - [KasmVNC 1.0.0 Documentation](https://www.kasmweb.com/kasmvnc/docs/1.0.0/index.html) + - [KasmVNC Documentation](https://www.kasmweb.com/kasmvnc/docs/latest/index.html) For beta releases prior to version 1.0.0, use the README in this github project on the tagged commit for that release. @@ -46,22 +46,6 @@ sudo dnf localinstall ./kasmvncserver_*.rpm sudo usermod -a -G kasmvnc-cert $USER ``` -### CentOS 7 - -```sh -# Please choose the package for your distro here (under Assets): -# https://github.com/kasmtech/KasmVNC/releases -wget - -# Ensure KasmVNC dependencies are available -sudo yum install epel-release - -sudo yum install ./kasmvncserver_*.rpm - -# Add your user to the kasmvnc-cert group -sudo usermod -a -G kasmvnc-cert $USER -``` - ## Getting Started The following examples provide basic usage of KasmVNC with the tools provided. For full documentation on all the utilities and the runtime environment, see our [KasmVNC Documentation](https://www.kasmweb.com/kasmvnc/docs/latest/index.html) @@ -250,7 +234,7 @@ command_line: - Keyboard input rate limit - Screen region selection - Deb packages for Debian, Ubuntu, and Kali Linux included in release. - - RPM packages for CentOS, Oracle, OpenSUSE, Fedora. RPM packages are currently not updatable and not released, though you can build and install them. See build documentation. + - RPM packages for Oracle, OpenSUSE, Fedora. RPM packages are currently not updatable and not released, though you can build and install them. See build documentation. - Web [API](https://github.com/kasmtech/KasmVNC/wiki/API) added for remotely controlling and getting information from KasmVNC - Multi-User support with permissions that can be changed via the API - Web UI uses a webpack for faster load times. diff --git a/alpine/.abuild/abuild.conf b/alpine/.abuild/abuild.conf new file mode 100644 index 0000000..4be7a96 --- /dev/null +++ b/alpine/.abuild/abuild.conf @@ -0,0 +1,2 @@ +PACKAGER="Kasm Technologies LLC " +PACKAGER_PRIVKEY="/src/alpine/.abuild/kasmvnc_signing_key.rsa" diff --git a/alpine/alpine-devenv.dockerfile b/alpine/alpine-devenv.dockerfile new file mode 100644 index 0000000..63e2bff --- /dev/null +++ b/alpine/alpine-devenv.dockerfile @@ -0,0 +1,11 @@ +FROM alpine:3.21 + +RUN apk add abuild sudo less +ENV HOME /src +WORKDIR /src/kasmvncserver + +RUN adduser --disabled-password docker +RUN adduser docker abuild +RUN echo "docker ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers + +USER docker diff --git a/alpine/apk-del-add b/alpine/apk-del-add new file mode 100755 index 0000000..f4840cd --- /dev/null +++ b/alpine/apk-del-add @@ -0,0 +1,8 @@ +#!/bin/sh + +set -e + +sudo apk del kasmvncserver || true +rm -r ../packages +abuild -r || true +sudo apk add ../packages/src/x86_64/kasmvncserver-1.3.3-r0.apk --allow-untrusted diff --git a/alpine/build b/alpine/build new file mode 100755 index 0000000..0658148 --- /dev/null +++ b/alpine/build @@ -0,0 +1 @@ +docker build -f alpine-devenv.dockerfile -t alpine-devenv . diff --git a/alpine/kasmvncserver/APKBUILD b/alpine/kasmvncserver/APKBUILD new file mode 100644 index 0000000..f5fb900 --- /dev/null +++ b/alpine/kasmvncserver/APKBUILD @@ -0,0 +1,120 @@ +#!/bin/bash + +# Contributor: +# Maintainer: Kasm Technologies LLC +pkgname=kasmvncserver +pkgver=1.3.4 +pkgrel=0 +pkgdesc="KasmVNC provides remote web-based access to a Desktop or application." +url="https://github.com/kasmtech/KasmVNC" +arch="x86_64 aarch64" +license="GPL-2.0-or-later" +depends=" + bash + libgomp + libjpeg-turbo + libwebp + libxfont2 + libxshmfence + libxtst + mcookie + mesa-gbm + openssl + pciutils-libs + perl + perl-datetime + perl-hash-merge-simple + perl-list-moreutils + perl-switch + perl-try-tiny + perl-yaml-tiny + perl-datetime + perl-datetime-timezone + pixman + py3-xdg + setxkbmap + xauth + xf86-video-amdgpu + xf86-video-ati + xf86-video-nouveau + xkbcomp + xkeyboard-config + xterm + " +if [ $(arch) = x86_64 ]; then + depends="$depends xf86-video-intel" +fi +makedepends=" + rsync + binutils + mesa-gl + libxcursor + gzip + " +checkdepends="" +install="$pkgname.post-install $pkgname.post-deinstall" +subpackages="$pkgname-doc" +source="" +builddir="$srcdir/" + + +build() { + local alpine_version=$(cat /etc/alpine-release | awk -F. '{ print $1$2 }') + tar -xzf "/src/builder/build/kasmvnc.alpine_$alpine_version.tar.gz" -C "$srcdir"; +} + +check() { + # Replace with proper check command(s). + # Remove and add !check option if there is no check command. + : +} + +package() { + export SRC="$srcdir/usr/local"; + export SRC_BIN="$SRC/bin"; + export DESTDIR="$pkgdir"; + + echo "installing files"; + mkdir -p $DESTDIR/usr/bin $DESTDIR/usr/lib \ + $DESTDIR/usr/share/perl5/vendor_perl $DESTDIR/etc/kasmvnc; + cp $SRC_BIN/Xvnc $DESTDIR/usr/bin/Xkasmvnc; + cd $DESTDIR/usr/bin/ && ln -s Xkasmvnc Xvnc; + cp $SRC_BIN/vncserver $DESTDIR/usr/bin/kasmvncserver; + cd $DESTDIR/usr/bin/ && ln -s kasmvncserver vncserver; + cp -r $SRC_BIN/KasmVNC $DESTDIR/usr/share/perl5/vendor_perl; + cp $SRC_BIN/vncconfig $DESTDIR/usr/bin/kasmvncconfig; + cp $SRC_BIN/kasmvncpasswd $DESTDIR/usr/bin/; + cd $DESTDIR/usr/bin/ && ln -s kasmvncpasswd vncpasswd; + cp $SRC_BIN/kasmxproxy $DESTDIR/usr/bin/; + cp -r $SRC/lib/kasmvnc/ $DESTDIR/usr/lib/kasmvncserver; + rsync -r --links --safe-links --exclude '.git*' --exclude po2js \ + --exclude xgettext-html --exclude www/utils/ --exclude .eslintrc \ + $SRC/share/kasmvnc $DESTDIR/usr/share; + sed -i -e 's!pem_certificate: .\+$!pem_certificate: /etc/ssl/private/kasmvnc.pem!' \ + $DESTDIR/usr/share/kasmvnc/kasmvnc_defaults.yaml + sed -i -e 's!pem_key: .\+$!pem_key: /etc/ssl/private/kasmvnc.pem!' \ + $DESTDIR/usr/share/kasmvnc/kasmvnc_defaults.yaml + sed -e 's/^\([^#]\)/# \1/' $SRC/share/kasmvnc/kasmvnc_defaults.yaml > \ + $DESTDIR/etc/kasmvnc/kasmvnc.yaml; +} + +doc() { + set -e + export SRC="$srcdir/usr/local"; + export SRC_BIN="$SRC/bin"; + export DESTDIR="$subpkgdir"; + export DST_MAN="$DESTDIR/usr/share/man/man1"; + + mkdir -p $DESTDIR/usr/share/man/man1 \ + $DESTDIR/usr/share/doc/kasmvncserver + cp -r $SRC/share/doc/kasmvnc*/* $DESTDIR/usr/share/doc/kasmvncserver/ + cp $SRC/man/man1/Xvnc.1 $DESTDIR/usr/share/man/man1/Xkasmvnc.1 + cp $SRC/share/man/man1/vncserver.1 $DST_MAN/kasmvncserver.1 + cp $SRC/share/man/man1/kasmxproxy.1 $DST_MAN/kasmxproxy.1 + cp $SRC/share/man/man1/vncpasswd.1 $DST_MAN/kasmvncpasswd.1 + cp $SRC/share/man/man1/vncconfig.1 $DST_MAN/kasmvncconfig.1 + gzip -9 $DST_MAN/* + cd $DST_MAN && ln -s Xkasmvnc.1.gz Xvnc.1.gz && \ + ln -s kasmvncserver.1.gz vncserver.1.gz && \ + ln -s kasmvncpasswd.1.gz vncpasswd.1.gz +} diff --git a/alpine/kasmvncserver/kasmvncserver.post-deinstall b/alpine/kasmvncserver/kasmvncserver.post-deinstall new file mode 100755 index 0000000..b30760c --- /dev/null +++ b/alpine/kasmvncserver/kasmvncserver.post-deinstall @@ -0,0 +1,3 @@ +#!/bin/sh + +rm -f /etc/ssl/private/kasmvnc.pem diff --git a/alpine/kasmvncserver/kasmvncserver.post-install b/alpine/kasmvncserver/kasmvncserver.post-install new file mode 100755 index 0000000..1d9f546 --- /dev/null +++ b/alpine/kasmvncserver/kasmvncserver.post-install @@ -0,0 +1,26 @@ +#!/bin/bash + +set -e + +kasmvnc_group="kasmvnc-cert" + +create_kasmvnc_group() { + if ! getent group "$kasmvnc_group" >/dev/null; then + addgroup --system "$kasmvnc_group" + fi +} + +make_self_signed_certificate() { + local cert_file=/etc/ssl/private/kasmvnc.pem + [ -f "$cert_file" ] && return 0 + + openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \ + -keyout "$cert_file" \ + -out "$cert_file" -subj \ + "/C=US/ST=VA/L=None/O=None/OU=DoFu/CN=kasm/emailAddress=none@none.none" + chgrp "$kasmvnc_group" "$cert_file" + chmod 640 "$cert_file" +} + +create_kasmvnc_group +make_self_signed_certificate diff --git a/builder/README.md b/builder/README.md index ea05cc1..64b8513 100644 --- a/builder/README.md +++ b/builder/README.md @@ -7,7 +7,7 @@ Docker CE # os_codename is what "lsb_release -c" outputs, e.g. buster, focal. # # build_tag allows building multiple versions of deb package (rpm not supported) -# targeting a single distro release (e.g. Ubuntu Bionic). If build_tag is given, +# targeting a single distro release (e.g. Ubuntu Focal). If build_tag is given, # the package name will include build_tag as part of Debian revision. For # example: # * with build_tag: kasmvncserver_0.9.1~beta-1+libjpeg-turbo-latest_amd64.deb @@ -16,19 +16,17 @@ Docker CE # # Packages will be placed under builder/build/ -builder/build-package ubuntu bionic builder/build-package ubuntu focal builder/build-package debian buster builder/build-package debian bullseye builder/build-package kali kali-rolling -builder/build-package centos core # CentOS 7 builder/build-package fedora thirtythree ``` # Build and test a package ``` builder/build-and-test-deb ubuntu focal -builder/build-and-test-rpm centos core +builder/build-and-test-rpm oracle 8 ``` Open browser and point to https://localhost:443/ or https://\:443/ @@ -118,7 +116,7 @@ locally by doing stuff like this: ``` bash -c ' . .ci/upload.sh; -prepare_upload_filename "bionic/kasmvncserver_0.9.1~beta-1+libjpeg-turbo-latest_amd64.deb"; +prepare_upload_filename "focal/kasmvncserver_0.9.1~beta-1+libjpeg-turbo-latest_amd64.deb"; echo $upload_filename;' ``` @@ -178,7 +176,7 @@ These instructions assume KasmVNC has been cloned at $HOME and ```kasm_www.tar.g cd ~ tar -zxf kasm_www.tar.gz -C KasmVNC/builder/ cd KasmVNC -sudo builder/build-package ubuntu bionic +sudo builder/build-package ubuntu focal ``` -The resulting deb package can be found under ~/KasmVNC/builder/build/bionic -Replace ```bionic``` with ```focal``` to build for Ubuntu 20.04LTS. At this time, only Ubuntu Bionic has been tested, however, other Debian based builds we support should also work. +The resulting deb package can be found under ~/KasmVNC/builder/build/focal +Replace ```focal``` with ```noble``` to build for Ubuntu 24.04LTS. diff --git a/builder/build-apk b/builder/build-apk index 73fa677..8b49160 100755 --- a/builder/build-apk +++ b/builder/build-apk @@ -2,16 +2,35 @@ set -e +copy_signing_key_to_user_abuild_dir() { + docker run --rm -v $PWD/alpine/.abuild:/abuild --user $L_UID:$L_GID \ + $builder_image \ + cp /etc/apk/keys/kasmvnc_signing_key.rsa.pub \ + /etc/apk/keys/kasmvnc_signing_key.rsa /abuild +} + . builder/os_ver_cli.sh cd "$(dirname "$0")/.." -docker build -t kasmvnc_apkbuilder_${os}:${os_codename} -f \ - builder/dockerfile.${os}_${os_codename}.apk.build . - -source_dir=$(echo $PWD) L_UID=$(id -u) L_GID=$(id -g) +ABUILD_GID=300 +if [ "$L_UID" = 0 ]; then + L_UID=1000 + L_GID=1000 +fi + +builder_image=kasmvnc_apkbuilder_${os}:${os_codename} +docker build -t $builder_image \ + --build-arg KASMVNC_ALPINE_PRIVATE_KEY \ + --build-arg KASMVNC_ALPINE_PUBLIC_KEY \ + -f builder/dockerfile.${os}_${os_codename}.apk.build . +copy_signing_key_to_user_abuild_dir + +source_dir=$(echo $PWD) docker run --rm -v "$source_dir":/src --user $L_UID:$L_GID \ + --group-add $ABUILD_GID \ + -e CI \ kasmvnc_apkbuilder_${os}:${os_codename} /bin/bash -c \ '/src/builder/build-apk-inside-docker' diff --git a/builder/build-apk-inside-docker b/builder/build-apk-inside-docker index 08617f7..4d7841b 100755 --- a/builder/build-apk-inside-docker +++ b/builder/build-apk-inside-docker @@ -2,10 +2,27 @@ set -e +add_arch_to_apk_package() { + local package_name="$1" + + new_package_name=$(echo "$package_name" | sed -e 's/\(-r[[:digit:]]\+\)/\1_'$(arch)/) + $sudo_cmd mv "$package_name" "$new_package_name" +} + +add_arch_to_apk_packages() { + for package_name in $(ls *.apk); do + add_arch_to_apk_package "$package_name" + done +} + os=alpine os_codename=$(cat /etc/os-release | awk '/VERSION_ID/' | grep -o '[[:digit:]]' | tr -d '\n' | head -c 3) +apkbuild_dir=/src/alpine/kasmvncserver/ -mkdir -p /src/builder/build/${os}_${os_codename} -mv \ - /src/builder/build/kasmvnc.${os}_${os_codename}.tar.gz \ - /src/builder/build/${os}_${os_codename}/kasmvnc.${os}_${os_codename}_$(uname -m).tgz +cd "$apkbuild_dir" && abuild -r + +[ -n "$CI" ] && sudo_cmd=sudo || sudo_cmd= +$sudo_cmd mkdir -p /src/builder/build/${os}_${os_codename} +( cd /src/alpine/packages/alpine/$(arch)/ && add_arch_to_apk_packages ) +$sudo_cmd mv \ + /src/alpine/packages/alpine/$(arch)/*.apk /src/builder/build/${os}_${os_codename}/ diff --git a/builder/build-package b/builder/build-package index 598230d..5f142f9 100755 --- a/builder/build-package +++ b/builder/build-package @@ -6,14 +6,7 @@ os="$1" codename="$2" build_tag="$3" -detect_package_format() { - package_format=rpm - if ls builder/dockerfile*"$os"* | grep -q .deb.build; then - package_format=deb - elif ls builder/dockerfile*"$os"* | grep -q .apk.build; then - package_format=apk - fi -} +. ./builder/common.sh warn_build_tag_not_supported_for_rpm_and_exit() { if [[ "$build_tag" && "$package_format" = "rpm" ]]; then diff --git a/builder/build.sh b/builder/build.sh index cb0541b..24d66c0 100755 --- a/builder/build.sh +++ b/builder/build.sh @@ -9,13 +9,6 @@ detect_quilt() { fi } -ensure_crashpad_can_fetch_line_number_by_address() { - if [ ! -f /etc/centos-release ]; then - export LDFLAGS="$LDFLAGS -no-pie" - fi -} - - fail_on_gcc_12() { if [[ -n "$CC" && -n "$CXX" ]]; then return; @@ -51,49 +44,47 @@ if [[ "${XORG_VER}" == 21* ]]; then else XORG_PATCH=$(echo "$XORG_VER" | grep -Po '^\d.\d+' | sed 's#\.##') fi -wget --no-check-certificate https://www.x.org/archive/individual/xserver/xorg-server-${XORG_VER}.tar.gz + +TARBALL="xorg-server-${XORG_VER}.tar.gz" + +if [ ! -f "$TARBALL" ]; then + wget --no-check-certificate https://www.x.org/archive/individual/xserver/"$TARBALL" +fi #git clone https://kasmweb@bitbucket.org/kasmtech/kasmvnc.git #cd kasmvnc #git checkout dynjpeg cd /src -# We only want the server, so FLTK and manual tests aren't useful. -# Alternatively, install fltk 1.3 and its dev packages. -sed -i -e '/find_package(FLTK/s@^@#@' \ - -e '/add_subdirectory(tests/s@^@#@' \ - CMakeLists.txt - cmake -D CMAKE_BUILD_TYPE=RelWithDebInfo . -DBUILD_VIEWER:BOOL=OFF \ -DENABLE_GNUTLS:BOOL=OFF -make -j5 +make -j"$(nproc)" -tar -C unix/xserver -xf /tmp/xorg-server-${XORG_VER}.tar.gz --strip-components=1 +if [ ! -d unix/xserver/include ]; then + tar -C unix/xserver -xf /tmp/"$TARBALL" --strip-components=1 -cd unix/xserver -# Apply patches -patch -Np1 -i ../xserver${XORG_PATCH}.patch -case "$XORG_VER" in - 1.20.*) - patch -s -p0 < ../CVE-2022-2320-v1.20.patch - if [ -f ../xserver120.7.patch ]; then - patch -Np1 -i ../xserver120.7.patch - fi ;; - 1.19.*) - patch -s -p0 < ../CVE-2022-2320-v1.19.patch - ;; -esac + cd unix/xserver + # Apply patches + patch -Np1 -i ../xserver"${XORG_PATCH}".patch + case "$XORG_VER" in + 1.20.*) + patch -s -p0 < ../CVE-2022-2320-v1.20.patch + if [ -f ../xserver120.7.patch ]; then + patch -Np1 -i ../xserver120.7.patch + fi ;; + 1.19.*) + patch -s -p0 < ../CVE-2022-2320-v1.19.patch + ;; + esac +else + cd unix/xserver +fi autoreconf -i # Configuring Xorg is long and has many distro-specific paths. # The distro paths start after prefix and end with the font path, # everything after that is based on BUILDING.txt to remove unneeded # components. -ensure_crashpad_can_fetch_line_number_by_address -# Centos7 is too old for dri3 -if [ ! "${KASMVNC_BUILD_OS}" == "centos" ]; then - CONFIG_OPTIONS="--enable-dri3" -fi # remove gl check for opensuse if [ "${KASMVNC_BUILD_OS}" == "opensuse" ] || ([ "${KASMVNC_BUILD_OS}" == "oracle" ] && [ "${KASMVNC_BUILD_OS_CODENAME}" == 9 ]); then sed -i 's/LIBGL="gl >= 7.1.0"/LIBGL="gl >= 1.1"/g' configure @@ -122,33 +113,38 @@ fi --with-sha1=libcrypto \ --with-xkb-bin-directory=/usr/bin \ --with-xkb-output=/var/lib/xkb \ - --with-xkb-path=/usr/share/X11/xkb ${CONFIG_OPTIONS} + --with-xkb-path=/usr/share/X11/xkb "${CONFIG_OPTIONS}" # remove array bounds errors for new versions of GCC find . -name "Makefile" -exec sed -i 's/-Werror=array-bounds//g' {} \; -make -j5 + + +make -j"$(nproc)" # modifications for the servertarball cd /src mkdir -p xorg.build/bin +mkdir -p xorg.build/lib cd xorg.build/bin/ -ln -s /src/unix/xserver/hw/vnc/Xvnc Xvnc +ln -sfn /src/unix/xserver/hw/vnc/Xvnc Xvnc cd .. mkdir -p man/man1 touch man/man1/Xserver.1 cp /src/unix/xserver/hw/vnc/Xvnc.man man/man1/Xvnc.1 -mkdir lib + +mkdir -p lib + cd lib if [ -d /usr/lib/x86_64-linux-gnu/dri ]; then - ln -s /usr/lib/x86_64-linux-gnu/dri dri + ln -sfn /usr/lib/x86_64-linux-gnu/dri dri elif [ -d /usr/lib/aarch64-linux-gnu/dri ]; then - ln -s /usr/lib/aarch64-linux-gnu/dri dri + ln -sfn /usr/lib/aarch64-linux-gnu/dri dri elif [ -d /usr/lib/arm-linux-gnueabihf/dri ]; then - ln -s /usr/lib/arm-linux-gnueabihf/dri dri + ln -sfn /usr/lib/arm-linux-gnueabihf/dri dri elif [ -d /usr/lib/xorg/modules/dri ]; then - ln -s /usr/lib/xorg/modules/dri dri + ln -sfn /usr/lib/xorg/modules/dri dri else - ln -s /usr/lib64/dri dri + ln -sfn /usr/lib64/dri dri fi cd /src diff --git a/builder/build_www.sh b/builder/build_www.sh index ec54f6c..b084dcf 100755 --- a/builder/build_www.sh +++ b/builder/build_www.sh @@ -1,18 +1,10 @@ -#!/bin/bash +#!/bin/sh # clear previous build rm -rf /build/* # build webpack npm run build -# remove node stuff from directory -rm -rf node_modules/ -# copy all to build dir -cp -R ./* /build/ -# remove unneccesary files -cd /build -rm *.md -rm AUTHORS -rm vnc.html -ln -s index.html vnc.html +# copy all to build dir +cp -R ./dist/* /build/ diff --git a/builder/bump-package-version b/builder/bump-package-version index 4e09e42..6f6824e 100755 --- a/builder/bump-package-version +++ b/builder/bump-package-version @@ -7,6 +7,11 @@ update_version_to_meet_packaging_standards() { sed -e 's/\([0-9]\)-\([a-zA-Z]\)/\1~\2/') } +bump_apk() { + builder/bump-package-version-apk "$new_version" +} + + bump_rpm() { builder/bump-package-version-rpm "$new_version" } @@ -33,3 +38,4 @@ update_version_to_meet_packaging_standards bump_xvnc_binary bump_rpm bump_deb +bump_apk diff --git a/builder/bump-package-version-apk b/builder/bump-package-version-apk new file mode 100755 index 0000000..359fcd3 --- /dev/null +++ b/builder/bump-package-version-apk @@ -0,0 +1,13 @@ +#!/bin/bash + +set -eo pipefail + +new_version="$1" +spec_file=alpine/kasmvncserver/APKBUILD + +bump_version() { + sed -i "s/^pkgver=.\+/pkgver=$new_version/" "$1" + sed -i "s/^pkgrel=.\+/pkgrel=0/" "$1" +} + +bump_version $spec_file diff --git a/builder/bump-package-version-rpm b/builder/bump-package-version-rpm index fe3fc4d..2ef427d 100755 --- a/builder/bump-package-version-rpm +++ b/builder/bump-package-version-rpm @@ -3,7 +3,7 @@ set -eo pipefail new_version="$1" -spec_dirs=(centos oracle opensuse fedora) +spec_dirs=(oracle opensuse fedora) spec_files() { for d in "${spec_dirs[@]}"; do diff --git a/builder/common.sh b/builder/common.sh index 50f2c06..83003dc 100644 --- a/builder/common.sh +++ b/builder/common.sh @@ -1 +1,24 @@ VNC_PORT=8443 + +detect_build_dir() { + if [ -n "$CI" ]; then + build_dir=output + else + build_dir=builder/build + fi +} + +detect_interactive() { + if [ -z "$run_test" ]; then + interactive=-it + fi +} + +detect_package_format() { + package_format=rpm + if ls builder/dockerfile*"$os"* | grep -q .deb.build; then + package_format=deb + elif ls builder/dockerfile*"$os"* | grep -q .apk.build; then + package_format=apk + fi +} diff --git a/builder/conf/nginx_kasm.conf b/builder/conf/nginx_kasm.conf new file mode 100644 index 0000000..a341c59 --- /dev/null +++ b/builder/conf/nginx_kasm.conf @@ -0,0 +1,42 @@ +server { + listen 8443 ssl; + ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem; + ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key; + + location / { + proxy_pass http://127.0.0.1:5173; + } + + location /api/ { + proxy_pass https://127.0.0.1:6901; + } + + location /websockify { + # The following configurations must be configured when proxying to Kasm Workspaces + + # WebSocket Support + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + + # Host and X headers + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # Connectivity Options + proxy_http_version 1.1; + proxy_read_timeout 1800s; + proxy_send_timeout 1800s; + proxy_connect_timeout 1800s; + proxy_buffering off; + + # Allow large requests to support file uploads to sessions + client_max_body_size 10M; + + # # Proxy to KasmVNC using SSL + #proxy_pass https://127.0.0.1:6901; + # Proxy to KasmVNC without SSL + proxy_pass http://127.0.0.1:6901; + } + } \ No newline at end of file diff --git a/builder/dockerfile.alpine.barebones.apk.test b/builder/dockerfile.alpine.barebones.apk.test new file mode 100644 index 0000000..061a54f --- /dev/null +++ b/builder/dockerfile.alpine.barebones.apk.test @@ -0,0 +1,24 @@ +ARG BASE_IMAGE +FROM $BASE_IMAGE + +RUN apk add bash + +ENV STARTUPDIR=/dockerstartup + +COPY ./builder/scripts/ /tmp/scripts/ +COPY alpine/kasmvncserver/APKBUILD /tmp + +ARG KASMVNC_PACKAGE_DIR +COPY $KASMVNC_PACKAGE_DIR/kasmvncserver-*.apk /tmp/ +RUN /tmp/scripts/install_kasmvncserver_package + +ARG RUN_TEST +RUN [ "$RUN_TEST" = 1 ] || apk add xterm + +RUN mkdir -p $STARTUPDIR +COPY builder/startup/vnc_startup_barebones.sh $STARTUPDIR + +RUN adduser -D -s/bin/bash foo && addgroup foo kasmvnc-cert +USER foo + +ENTRYPOINT "/$STARTUPDIR/vnc_startup_barebones.sh" diff --git a/builder/dockerfile.alpine_317.apk.build b/builder/dockerfile.alpine_317.apk.build index 732f5f0..c0294c0 100644 --- a/builder/dockerfile.alpine_317.apk.build +++ b/builder/dockerfile.alpine_317.apk.build @@ -1,7 +1,19 @@ FROM alpine:3.17 RUN apk add shadow bash +RUN apk add abuild sudo less + +ENV HOME /src/alpine +WORKDIR $HOME/kasmvncserver + +ARG KASMVNC_ALPINE_PRIVATE_KEY +ARG KASMVNC_ALPINE_PUBLIC_KEY + +ENV SCRIPTS_DIR=/tmp/scripts +COPY builder/scripts $SCRIPTS_DIR +RUN $SCRIPTS_DIR/install_alpine_signing_keys RUN useradd -m docker && echo "docker:docker" | chpasswd +RUN echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers USER docker diff --git a/builder/dockerfile.alpine_317.build b/builder/dockerfile.alpine_317.build deleted file mode 100644 index 0b23bcb..0000000 --- a/builder/dockerfile.alpine_317.build +++ /dev/null @@ -1,82 +0,0 @@ -FROM alpine:3.17 - -ENV KASMVNC_BUILD_OS alpine -ENV KASMVNC_BUILD_OS_CODENAME 317 -ENV XORG_VER 21.1.8 - -RUN \ - echo "**** install build deps ****" && \ - apk add \ - alpine-release \ - alpine-sdk \ - autoconf \ - automake \ - bash \ - ca-certificates \ - cmake \ - coreutils \ - curl \ - eudev-dev \ - font-cursor-misc \ - font-misc-misc \ - font-util-dev \ - git \ - grep \ - jq \ - libdrm-dev \ - libepoxy-dev \ - libjpeg-turbo-dev \ - libjpeg-turbo-static \ - libpciaccess-dev \ - libtool \ - libwebp-dev \ - libx11-dev \ - libxau-dev \ - libxcb-dev \ - libxcursor-dev \ - libxcvt-dev \ - libxdmcp-dev \ - libxext-dev \ - libxfont2-dev \ - libxkbfile-dev \ - libxrandr-dev \ - libxshmfence-dev \ - libxtst-dev \ - mesa-dev \ - mesa-dri-gallium \ - meson \ - nettle-dev \ - openssl-dev \ - pixman-dev \ - procps \ - shadow \ - tar \ - tzdata \ - wayland-dev \ - wayland-protocols \ - xcb-util-dev \ - xcb-util-image-dev \ - xcb-util-keysyms-dev \ - xcb-util-renderutil-dev \ - xcb-util-wm-dev \ - xinit \ - xkbcomp \ - xkbcomp-dev \ - xkeyboard-config \ - xorgproto \ - xorg-server-common \ - xorg-server-dev \ - xtrans - - -ENV SCRIPTS_DIR=/tmp/scripts -COPY builder/scripts $SCRIPTS_DIR -RUN $SCRIPTS_DIR/build-webp -RUN $SCRIPTS_DIR/build-libjpeg-turbo - -RUN useradd -m docker && echo "docker:docker" | chpasswd - -COPY --chown=docker:docker . /src/ - -USER docker -ENTRYPOINT ["/src/builder/build.sh"] diff --git a/builder/dockerfile.alpine_318.apk.build b/builder/dockerfile.alpine_318.apk.build index 9504897..f55ac41 100644 --- a/builder/dockerfile.alpine_318.apk.build +++ b/builder/dockerfile.alpine_318.apk.build @@ -1,7 +1,19 @@ FROM alpine:3.18 RUN apk add shadow bash +RUN apk add abuild sudo less + +ENV HOME /src/alpine +WORKDIR $HOME/kasmvncserver + +ARG KASMVNC_ALPINE_PRIVATE_KEY +ARG KASMVNC_ALPINE_PUBLIC_KEY + +ENV SCRIPTS_DIR=/tmp/scripts +COPY builder/scripts $SCRIPTS_DIR +RUN $SCRIPTS_DIR/install_alpine_signing_keys RUN useradd -m docker && echo "docker:docker" | chpasswd +RUN echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers USER docker diff --git a/builder/dockerfile.alpine_318.barebones.apk.test b/builder/dockerfile.alpine_318.barebones.apk.test new file mode 120000 index 0000000..388647d --- /dev/null +++ b/builder/dockerfile.alpine_318.barebones.apk.test @@ -0,0 +1 @@ +dockerfile.alpine.barebones.apk.test \ No newline at end of file diff --git a/builder/dockerfile.alpine_318.build b/builder/dockerfile.alpine_318.build index 8a01ac8..f8620ba 100644 --- a/builder/dockerfile.alpine_318.build +++ b/builder/dockerfile.alpine_318.build @@ -14,6 +14,7 @@ RUN \ bash \ ca-certificates \ cmake \ + nasm \ coreutils \ curl \ eudev-dev \ @@ -66,7 +67,8 @@ RUN \ xorgproto \ xorg-server-common \ xorg-server-dev \ - xtrans + xtrans \ + ffmpeg-dev ENV SCRIPTS_DIR=/tmp/scripts diff --git a/builder/dockerfile.alpine_319.apk.build b/builder/dockerfile.alpine_319.apk.build index a0930b4..95b6bc6 100644 --- a/builder/dockerfile.alpine_319.apk.build +++ b/builder/dockerfile.alpine_319.apk.build @@ -1,7 +1,19 @@ FROM alpine:3.19 RUN apk add shadow bash +RUN apk add abuild sudo less + +ENV HOME /src/alpine +WORKDIR $HOME/kasmvncserver + +ARG KASMVNC_ALPINE_PRIVATE_KEY +ARG KASMVNC_ALPINE_PUBLIC_KEY + +ENV SCRIPTS_DIR=/tmp/scripts +COPY builder/scripts $SCRIPTS_DIR +RUN $SCRIPTS_DIR/install_alpine_signing_keys RUN useradd -m docker && echo "docker:docker" | chpasswd +RUN echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers USER docker diff --git a/builder/dockerfile.alpine_319.barebones.apk.test b/builder/dockerfile.alpine_319.barebones.apk.test new file mode 120000 index 0000000..388647d --- /dev/null +++ b/builder/dockerfile.alpine_319.barebones.apk.test @@ -0,0 +1 @@ +dockerfile.alpine.barebones.apk.test \ No newline at end of file diff --git a/builder/dockerfile.alpine_319.build b/builder/dockerfile.alpine_319.build index a73670b..d003855 100644 --- a/builder/dockerfile.alpine_319.build +++ b/builder/dockerfile.alpine_319.build @@ -14,6 +14,7 @@ RUN \ bash \ ca-certificates \ cmake \ + nasm \ coreutils \ curl \ eudev-dev \ @@ -66,7 +67,8 @@ RUN \ xorgproto \ xorg-server-common \ xorg-server-dev \ - xtrans + xtrans \ + ffmpeg-dev ENV SCRIPTS_DIR=/tmp/scripts diff --git a/builder/dockerfile.alpine_320.apk.build b/builder/dockerfile.alpine_320.apk.build index d99e67e..8791c8b 100644 --- a/builder/dockerfile.alpine_320.apk.build +++ b/builder/dockerfile.alpine_320.apk.build @@ -1,7 +1,19 @@ FROM alpine:3.20 RUN apk add shadow bash +RUN apk add abuild sudo less + +ENV HOME /src/alpine +WORKDIR $HOME/kasmvncserver + +ARG KASMVNC_ALPINE_PRIVATE_KEY +ARG KASMVNC_ALPINE_PUBLIC_KEY + +ENV SCRIPTS_DIR=/tmp/scripts +COPY builder/scripts $SCRIPTS_DIR +RUN $SCRIPTS_DIR/install_alpine_signing_keys RUN useradd -m docker && echo "docker:docker" | chpasswd +RUN echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers USER docker diff --git a/builder/dockerfile.alpine_320.barebones.apk.test b/builder/dockerfile.alpine_320.barebones.apk.test new file mode 120000 index 0000000..388647d --- /dev/null +++ b/builder/dockerfile.alpine_320.barebones.apk.test @@ -0,0 +1 @@ +dockerfile.alpine.barebones.apk.test \ No newline at end of file diff --git a/builder/dockerfile.alpine_320.build b/builder/dockerfile.alpine_320.build index 42995f1..b282e6d 100644 --- a/builder/dockerfile.alpine_320.build +++ b/builder/dockerfile.alpine_320.build @@ -14,6 +14,7 @@ RUN \ bash \ ca-certificates \ cmake \ + nasm \ coreutils \ curl \ eudev-dev \ @@ -66,7 +67,8 @@ RUN \ xorgproto \ xorg-server-common \ xorg-server-dev \ - xtrans + xtrans \ + ffmpeg-dev ENV SCRIPTS_DIR=/tmp/scripts diff --git a/builder/dockerfile.alpine_321.apk.build b/builder/dockerfile.alpine_321.apk.build index b67ef79..6f41bde 100644 --- a/builder/dockerfile.alpine_321.apk.build +++ b/builder/dockerfile.alpine_321.apk.build @@ -1,7 +1,19 @@ FROM alpine:3.21 RUN apk add shadow bash +RUN apk add abuild sudo less ffmpeg-dev + +ENV HOME /src/alpine +WORKDIR $HOME/kasmvncserver + +ARG KASMVNC_ALPINE_PRIVATE_KEY +ARG KASMVNC_ALPINE_PUBLIC_KEY + +ENV SCRIPTS_DIR=/tmp/scripts +COPY builder/scripts $SCRIPTS_DIR +RUN $SCRIPTS_DIR/install_alpine_signing_keys RUN useradd -m docker && echo "docker:docker" | chpasswd +RUN echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers USER docker diff --git a/builder/dockerfile.alpine_321.barebones.apk.test b/builder/dockerfile.alpine_321.barebones.apk.test new file mode 120000 index 0000000..388647d --- /dev/null +++ b/builder/dockerfile.alpine_321.barebones.apk.test @@ -0,0 +1 @@ +dockerfile.alpine.barebones.apk.test \ No newline at end of file diff --git a/builder/dockerfile.alpine_321.build b/builder/dockerfile.alpine_321.build index e73d31f..47a2cc7 100644 --- a/builder/dockerfile.alpine_321.build +++ b/builder/dockerfile.alpine_321.build @@ -14,6 +14,7 @@ RUN \ bash \ ca-certificates \ cmake \ + nasm \ coreutils \ curl \ eudev-dev \ @@ -66,7 +67,8 @@ RUN \ xorgproto \ xorg-server-common \ xorg-server-dev \ - xtrans + xtrans \ + ffmpeg-dev ENV SCRIPTS_DIR=/tmp/scripts diff --git a/builder/dockerfile.centos_core.barebones.rpm.test b/builder/dockerfile.centos_core.barebones.rpm.test deleted file mode 100644 index 84dcd08..0000000 --- a/builder/dockerfile.centos_core.barebones.rpm.test +++ /dev/null @@ -1,20 +0,0 @@ -FROM centos:centos7 - -ENV STARTUPDIR=/dockerstartup - -RUN yum install -y xterm -RUN yum install -y vim less -RUN yum install -y redhat-lsb-core -RUN yum install -y epel-release - -ARG KASMVNC_PACKAGE_DIR -COPY $KASMVNC_PACKAGE_DIR/*.rpm /tmp/ -RUN yum localinstall -y /tmp/*.rpm - -RUN mkdir -p $STARTUPDIR -COPY startup/vnc_startup_barebones.sh $STARTUPDIR - -RUN useradd -m foo -USER foo:kasmvnc-cert - -ENTRYPOINT "/$STARTUPDIR/vnc_startup_barebones.sh" diff --git a/builder/dockerfile.centos_core.build b/builder/dockerfile.centos_core.build deleted file mode 100644 index cb6d4fa..0000000 --- a/builder/dockerfile.centos_core.build +++ /dev/null @@ -1,35 +0,0 @@ -FROM centos:centos7 - -ENV KASMVNC_BUILD_OS centos -ENV KASMVNC_BUILD_OS_CODENAME core - -RUN yum install -y ca-certificates -RUN yum install -y build-dep xorg-server libxfont-dev sudo -RUN yum install -y gcc cmake git libgnutls28-dev vim wget tightvncserver -RUN yum install -y libpng-dev libtiff-dev libgif-dev libavcodec-dev openssl-devel -RUN yum install -y make -RUN yum group install -y "Development Tools" -RUN yum install -y xorg-x11-server-devel zlib-devel -RUN yum install -y libxkbfile-devel libXfont2-devel xorg-x11-font-utils \ - xorg-x11-xtrans-devel xorg-x11-xkb-utils-devel libXrandr-devel pam-devel \ - gnutls-devel libX11-devel libXtst-devel libXcursor-devel -RUN yum install -y mesa-dri-drivers -RUN yum install -y ca-certificates - -# Additions for webp -RUN cd /tmp && wget https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.0.2.tar.gz -RUN cd /tmp && tar -xzf /tmp/libwebp-* -RUN cd /tmp/libwebp-1.0.2 && \ - ./configure --enable-static --disable-shared && \ - make && make install - -ENV SCRIPTS_DIR=/tmp/scripts -COPY builder/scripts $SCRIPTS_DIR -RUN $SCRIPTS_DIR/build-libjpeg-turbo - -RUN useradd -m docker && echo "docker:docker" | chpasswd - -COPY --chown=docker:docker . /src/ - -USER docker -ENTRYPOINT ["/src/builder/build.sh"] diff --git a/builder/dockerfile.centos_core.rpm.build b/builder/dockerfile.centos_core.rpm.build deleted file mode 100644 index 7a4e208..0000000 --- a/builder/dockerfile.centos_core.rpm.build +++ /dev/null @@ -1,12 +0,0 @@ -FROM centos:centos7 - -RUN yum install -y rpm* gpg* rng-tools rpmlint -RUN yum install -y tree vim less -RUN yum install -y redhat-lsb-core - -COPY centos/*.spec /tmp -RUN yum-builddep -y /tmp/*.spec - -RUN useradd -m docker && echo "docker:docker" | chpasswd - -USER docker diff --git a/builder/dockerfile.centos_core.rpm.test b/builder/dockerfile.centos_core.rpm.test deleted file mode 100644 index 4ae4377..0000000 --- a/builder/dockerfile.centos_core.rpm.test +++ /dev/null @@ -1,64 +0,0 @@ -FROM centos:centos7 - -ENV DISPLAY=:1 \ - VNC_PORT=8443 \ - VNC_RESOLUTION=1280x720 \ - MAX_FRAME_RATE=24 \ - VNCOPTIONS="-PreferBandwidth -DynamicQualityMin=4 -DynamicQualityMax=7" \ - HOME=/home/user \ - TERM=xterm \ - STARTUPDIR=/dockerstartup \ - INST_SCRIPTS=/dockerstartup/install \ - KASM_RX_HOME=/dockerstartup/kasmrx \ - DEBIAN_FRONTEND=noninteractive \ - VNC_COL_DEPTH=24 \ - VNC_RESOLUTION=1280x1024 \ - VNC_PW=vncpassword \ - VNC_USER=user \ - VNC_VIEW_ONLY_PW=vncviewonlypassword \ - LD_LIBRARY_PATH=/usr/local/lib/ \ - OMP_WAIT_POLICY=PASSIVE \ - SHELL=/bin/bash \ - SINGLE_APPLICATION=0 \ - KASMVNC_BUILD_OS=centos \ - KASMVNC_BUILD_OS_CODENAME=core - -EXPOSE $VNC_PORT - -WORKDIR $HOME - -### REQUIRED STUFF ### - -RUN yum install -y openssl xterm gettext wget -RUN yum install -y centos-release-scl-rh && yum install -y nss_wrapper -RUN yum install -y xorg-x11-server xorg-x11-xauth xorg-x11-xkb-utils \ - xkeyboard-config xorg-x11-server-utils -RUN yum install -y epel-release && yum groupinstall xfce -y -RUN yum erase -y pm-utils xscreensaver* -RUN yum install -y redhat-lsb-core -RUN yum install -y vim less - -RUN echo 'source $STARTUPDIR/generate_container_user' >> $HOME/.bashrc - -RUN mkdir -p $STARTUPDIR -COPY builder/startup/ $STARTUPDIR - -### START CUSTOM STUFF #### - -COPY ./builder/scripts/ /tmp/scripts/ -COPY ./centos/kasmvncserver.spec /tmp - -ARG KASMVNC_PACKAGE_DIR -COPY $KASMVNC_PACKAGE_DIR/*.rpm /tmp/ -RUN /tmp/scripts/install_kasmvncserver_package - -### END CUSTOM STUFF ### - -RUN chown -R 1000:0 $HOME -USER 1000:kasmvnc-cert -WORKDIR $HOME - -RUN mkdir ~/.vnc && echo '/usr/bin/xfce4-session &' >> ~/.vnc/xstartup && \ - chmod +x ~/.vnc/xstartup - -ENTRYPOINT [ "/dockerstartup/vnc_startup.sh" ] diff --git a/builder/dockerfile.debian.barebones.deb.test b/builder/dockerfile.debian.barebones.deb.test new file mode 100644 index 0000000..927f52d --- /dev/null +++ b/builder/dockerfile.debian.barebones.deb.test @@ -0,0 +1,22 @@ +ARG BASE_IMAGE +FROM $BASE_IMAGE + +ENV STARTUPDIR=/dockerstartup + +COPY ./builder/scripts/ /tmp/scripts/ +COPY ./debian/changelog /tmp + +ARG KASMVNC_PACKAGE_DIR +COPY $KASMVNC_PACKAGE_DIR/kasmvncserver_*.deb /tmp/ +RUN /tmp/scripts/install_kasmvncserver_package + +ARG RUN_TEST +RUN if [ "$RUN_TEST" != 1 ]; then apt-get update && apt-get -y install xterm lsb-release; fi + +RUN mkdir -p $STARTUPDIR +COPY builder/startup/vnc_startup_barebones.sh $STARTUPDIR + +RUN useradd -m foo && adduser foo ssl-cert +USER foo + +ENTRYPOINT "/$STARTUPDIR/vnc_startup_barebones.sh" diff --git a/builder/dockerfile.debian_bookworm.barebones.deb.test b/builder/dockerfile.debian_bookworm.barebones.deb.test new file mode 120000 index 0000000..9ec488c --- /dev/null +++ b/builder/dockerfile.debian_bookworm.barebones.deb.test @@ -0,0 +1 @@ +dockerfile.debian.barebones.deb.test \ No newline at end of file diff --git a/builder/dockerfile.debian_bookworm.build b/builder/dockerfile.debian_bookworm.build index 8c0e8de..98034c6 100644 --- a/builder/dockerfile.debian_bookworm.build +++ b/builder/dockerfile.debian_bookworm.build @@ -22,8 +22,9 @@ RUN apt-get update && \ RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata RUN apt-get update && apt-get -y build-dep xorg-server libxfont-dev -RUN apt-get update && apt-get -y install cmake git libgnutls28-dev vim wget tightvncserver curl -RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev libxcursor-dev +RUN apt-get update && apt-get -y install ninja-build cmake nasm git libgnutls28-dev vim wget tightvncserver curl +RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev \ + libxcursor-dev libavformat-dev libswscale-dev ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR diff --git a/builder/dockerfile.debian_bullseye.barebones.deb.test b/builder/dockerfile.debian_bullseye.barebones.deb.test new file mode 120000 index 0000000..9ec488c --- /dev/null +++ b/builder/dockerfile.debian_bullseye.barebones.deb.test @@ -0,0 +1 @@ +dockerfile.debian.barebones.deb.test \ No newline at end of file diff --git a/builder/dockerfile.debian_bullseye.build b/builder/dockerfile.debian_bullseye.build index f626bb7..d9ce288 100644 --- a/builder/dockerfile.debian_bullseye.build +++ b/builder/dockerfile.debian_bullseye.build @@ -12,8 +12,22 @@ RUN apt-get update && \ RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata RUN apt-get update && apt-get -y build-dep xorg-server libxfont-dev -RUN apt-get update && apt-get -y install cmake git libgnutls28-dev vim wget tightvncserver curl -RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev libxcursor-dev +RUN apt-get update && apt-get -y install ninja-build nasm git libgnutls28-dev vim wget tightvncserver curl +RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev \ + libxcursor-dev libavformat-dev libswscale-dev + +RUN CMAKE_URL="https://cmake.org/files/v3.22/cmake-3.22.0" && \ + ARCH=$(arch) && \ + if [ "$ARCH" = "x86_64" ]; then \ + CMAKE_URL="${CMAKE_URL}-linux-x86_64.sh"; \ + elif [ "$ARCH" = "aarch64" ]; then \ + CMAKE_URL="${CMAKE_URL}-linux-aarch64.sh"; \ + else \ + echo "Unsupported architecture: $ARCH" && exit 1; \ + fi && \ + curl -fsSL $CMAKE_URL -o cmake.sh && \ + (echo y; echo n) | bash cmake.sh --prefix=/usr/local --skip-license && \ + rm cmake.sh ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR diff --git a/builder/dockerfile.debian_buster.build b/builder/dockerfile.debian_buster.build index c8f8008..a1e2afd 100644 --- a/builder/dockerfile.debian_buster.build +++ b/builder/dockerfile.debian_buster.build @@ -12,7 +12,7 @@ RUN apt-get update && \ RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata RUN apt-get update && apt-get -y build-dep xorg-server libxfont-dev -RUN apt-get update && apt-get -y install cmake git libgnutls28-dev vim wget tightvncserver curl +RUN apt-get update && apt-get -y install ninja-build cmake nasm git libgnutls28-dev vim wget tightvncserver curl RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev libxcursor-dev ENV SCRIPTS_DIR=/tmp/scripts diff --git a/builder/dockerfile.fedora_forty.barebones.rpm.test b/builder/dockerfile.fedora_forty.barebones.rpm.test new file mode 100644 index 0000000..de0c4ec --- /dev/null +++ b/builder/dockerfile.fedora_forty.barebones.rpm.test @@ -0,0 +1,25 @@ +FROM fedora:40 + +ENV STARTUPDIR=/dockerstartup + +ARG RUN_TEST +RUN [ "$RUN_TEST" = 1 ] || dnf install -y \ + less \ + redhat-lsb-core \ + vim \ + xterm + +COPY ./builder/scripts/ /tmp/scripts/ + +ARG KASMVNC_PACKAGE_DIR +COPY $KASMVNC_PACKAGE_DIR/kasmvncserver-*.rpm /tmp/ +COPY fedora/kasmvncserver.spec /tmp/ +RUN /tmp/scripts/install_kasmvncserver_package + +RUN mkdir -p $STARTUPDIR +COPY builder/startup/vnc_startup_barebones.sh $STARTUPDIR + +RUN useradd -m foo +USER foo:kasmvnc-cert + +ENTRYPOINT "/$STARTUPDIR/vnc_startup_barebones.sh" diff --git a/builder/dockerfile.fedora_forty.build b/builder/dockerfile.fedora_forty.build index d5ad068..983a88f 100644 --- a/builder/dockerfile.fedora_forty.build +++ b/builder/dockerfile.fedora_forty.build @@ -16,6 +16,7 @@ RUN \ byacc \ bzip2 \ cmake \ + nasm \ diffutils \ doxygen \ file \ @@ -71,7 +72,9 @@ RUN \ xorg-x11-server-common \ xorg-x11-server-devel \ xorg-x11-xtrans-devel \ - xsltproc + xsltproc \ + libavformat-free-devel \ + libswscale-free-devel ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR diff --git a/builder/dockerfile.fedora_fortyone.barebones.rpm.test b/builder/dockerfile.fedora_fortyone.barebones.rpm.test new file mode 100644 index 0000000..a2b94d1 --- /dev/null +++ b/builder/dockerfile.fedora_fortyone.barebones.rpm.test @@ -0,0 +1,25 @@ +FROM fedora:41 + +ENV STARTUPDIR=/dockerstartup + +ARG RUN_TEST +RUN [ "$RUN_TEST" = 1 ] || dnf install -y \ + less \ + redhat-lsb-core \ + vim \ + xterm + +COPY ./builder/scripts/ /tmp/scripts/ + +ARG KASMVNC_PACKAGE_DIR +COPY $KASMVNC_PACKAGE_DIR/kasmvncserver-*.rpm /tmp/ +COPY fedora/kasmvncserver.spec /tmp/ +RUN /tmp/scripts/install_kasmvncserver_package + +RUN mkdir -p $STARTUPDIR +COPY builder/startup/vnc_startup_barebones.sh $STARTUPDIR + +RUN useradd -m foo +USER foo:kasmvnc-cert + +ENTRYPOINT "/$STARTUPDIR/vnc_startup_barebones.sh" diff --git a/builder/dockerfile.fedora_fortyone.build b/builder/dockerfile.fedora_fortyone.build index 5c2bf48..336b064 100644 --- a/builder/dockerfile.fedora_fortyone.build +++ b/builder/dockerfile.fedora_fortyone.build @@ -17,6 +17,7 @@ RUN \ byacc \ bzip2 \ cmake \ + nasm \ diffutils \ doxygen \ file \ @@ -72,7 +73,9 @@ RUN \ xorg-x11-server-common \ xorg-x11-server-devel \ xorg-x11-xtrans-devel \ - xsltproc + xsltproc \ + libavformat-free-devel \ + libswscale-free-devel ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR diff --git a/builder/dockerfile.fedora_thirtyeight.barebones.rpm.test b/builder/dockerfile.fedora_thirtyeight.barebones.rpm.test deleted file mode 100644 index 4252609..0000000 --- a/builder/dockerfile.fedora_thirtyeight.barebones.rpm.test +++ /dev/null @@ -1,19 +0,0 @@ -FROM fedora:38 - -ENV STARTUPDIR=/dockerstartup - -RUN dnf install -y xterm -RUN dnf install -y vim less -RUN yum install -y redhat-lsb-core - -ARG KASMVNC_PACKAGE_DIR -COPY $KASMVNC_PACKAGE_DIR/*.rpm /tmp/ -RUN dnf localinstall -y /tmp/*.rpm - -RUN mkdir -p $STARTUPDIR -COPY startup/vnc_startup_barebones.sh $STARTUPDIR - -RUN useradd -m foo -USER foo:kasmvnc-cert - -ENTRYPOINT "/$STARTUPDIR/vnc_startup_barebones.sh" diff --git a/builder/dockerfile.fedora_thirtyeight.build b/builder/dockerfile.fedora_thirtyeight.build deleted file mode 100644 index dbfae34..0000000 --- a/builder/dockerfile.fedora_thirtyeight.build +++ /dev/null @@ -1,86 +0,0 @@ -FROM fedora:38 - -ENV KASMVNC_BUILD_OS fedora -ENV KASMVNC_BUILD_OS_CODENAME thirtyeight -ENV XORG_VER 1.20.14 - -RUN \ - echo "**** install build deps ****" && \ - dnf group install -y \ - "C Development Tools and Libraries" \ - "Development Tools" && \ - dnf install -y \ - autoconf \ - automake \ - bison \ - byacc \ - bzip2 \ - cmake \ - diffutils \ - doxygen \ - file \ - flex \ - fop \ - gcc \ - gcc-c++ \ - git \ - glibc-devel \ - libdrm-devel \ - libepoxy-devel \ - libmd-devel \ - libpciaccess-devel \ - libtool \ - libwebp-devel \ - libX11-devel \ - libXau-devel \ - libxcb-devel \ - libXcursor-devel \ - libxcvt-devel \ - libXdmcp-devel \ - libXext-devel \ - libXfont2-devel \ - libxkbfile-devel \ - libXrandr-devel \ - libxshmfence-devel \ - libXtst-devel \ - mesa-libEGL-devel \ - mesa-libgbm-devel \ - mesa-libGL-devel \ - meson \ - mingw64-binutils \ - mt-st \ - nettle-devel \ - openssl-devel \ - patch \ - pixman-devel \ - wayland-devel \ - wget \ - which \ - xcb-util-devel \ - xcb-util-image-devel \ - xcb-util-keysyms-devel \ - xcb-util-renderutil-devel \ - xcb-util-wm-devel \ - xinit \ - xkbcomp \ - xkbcomp-devel \ - xkeyboard-config \ - xmlto \ - xorg-x11-font-utils \ - xorg-x11-proto-devel \ - xorg-x11-server-common \ - xorg-x11-server-devel \ - xorg-x11-xtrans-devel \ - xsltproc - -ENV SCRIPTS_DIR=/tmp/scripts -COPY builder/scripts $SCRIPTS_DIR -RUN $SCRIPTS_DIR/build-webp -RUN $SCRIPTS_DIR/build-libjpeg-turbo - -RUN useradd -m docker && echo "docker:docker" | chpasswd - -COPY --chown=docker:docker . /src/ - -USER docker -ENTRYPOINT ["/src/builder/build.sh"] diff --git a/builder/dockerfile.fedora_thirtyeight.rpm.build b/builder/dockerfile.fedora_thirtyeight.rpm.build deleted file mode 100644 index 8fc556f..0000000 --- a/builder/dockerfile.fedora_thirtyeight.rpm.build +++ /dev/null @@ -1,13 +0,0 @@ -FROM fedora:38 - -RUN dnf install -y fedora-packager fedora-review -RUN dnf install -y tree vim less -RUN dnf install -y redhat-lsb-core -RUN dnf install -y dnf-plugins-core - -COPY fedora/*.spec /tmp -RUN dnf builddep -y /tmp/*.spec - -RUN useradd -m docker && echo "docker:docker" | chpasswd - -USER docker diff --git a/builder/dockerfile.fedora_thirtyeight.rpm.test b/builder/dockerfile.fedora_thirtyeight.rpm.test deleted file mode 100644 index 16975a0..0000000 --- a/builder/dockerfile.fedora_thirtyeight.rpm.test +++ /dev/null @@ -1,62 +0,0 @@ -FROM fedora:38 - -ENV DISPLAY=:1 \ - VNC_PORT=8443 \ - VNC_RESOLUTION=1280x720 \ - MAX_FRAME_RATE=24 \ - VNCOPTIONS="-PreferBandwidth -DynamicQualityMin=4 -DynamicQualityMax=7" \ - HOME=/home/user \ - TERM=xterm \ - STARTUPDIR=/dockerstartup \ - INST_SCRIPTS=/dockerstartup/install \ - KASM_RX_HOME=/dockerstartup/kasmrx \ - DEBIAN_FRONTEND=noninteractive \ - VNC_COL_DEPTH=24 \ - VNC_RESOLUTION=1280x1024 \ - VNC_PW=vncpassword \ - VNC_USER=user \ - VNC_VIEW_ONLY_PW=vncviewonlypassword \ - LD_LIBRARY_PATH=/usr/local/lib/ \ - OMP_WAIT_POLICY=PASSIVE \ - SHELL=/bin/bash \ - SINGLE_APPLICATION=0 \ - KASMVNC_BUILD_OS=fedora \ - KASMVNC_BUILD_OS_CODENAME=thirtythree - -EXPOSE $VNC_PORT - -WORKDIR $HOME - -### REQUIRED STUFF ### - -RUN dnf install -y openssl xterm gettext wget -RUN dnf install -y nss_wrapper -RUN dnf install -y xorg-x11-xauth xkeyboard-config -# xorg-x11-server-Xorg -# RUN dnf install -y @xfce-desktop-environment -RUN dnf erase -y pm-utils xscreensaver* -RUN dnf install -y redhat-lsb-core -RUN dnf install -y vim less -RUN dnf install -y @xfce-desktop-environment - -RUN echo 'source $STARTUPDIR/generate_container_user' >> $HOME/.bashrc - -RUN mkdir -p $STARTUPDIR -COPY builder/startup/ $STARTUPDIR - -### START CUSTOM STUFF #### -COPY ./builder/scripts/ /tmp/scripts/ -COPY ./fedora/kasmvncserver.spec /tmp - -ARG KASMVNC_PACKAGE_DIR -COPY $KASMVNC_PACKAGE_DIR/*.rpm /tmp/ -# RUN dnf remove -y tigervnc-server-minimal -RUN /tmp/scripts/install_kasmvncserver_package - -### END CUSTOM STUFF ### - -RUN chown -R 1000:0 $HOME -USER 1000:kasmvnc-cert -WORKDIR $HOME - -ENTRYPOINT [ "/dockerstartup/vnc_startup.sh" ] diff --git a/builder/dockerfile.fedora_thirtynine.build b/builder/dockerfile.fedora_thirtynine.build index 0854956..d1985de 100644 --- a/builder/dockerfile.fedora_thirtynine.build +++ b/builder/dockerfile.fedora_thirtynine.build @@ -16,6 +16,7 @@ RUN \ byacc \ bzip2 \ cmake \ + nasm \ diffutils \ doxygen \ file \ @@ -71,7 +72,9 @@ RUN \ xorg-x11-server-common \ xorg-x11-server-devel \ xorg-x11-xtrans-devel \ - xsltproc + xsltproc \ + libavformat-free-devel \ + libswscale-free-devel ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR diff --git a/builder/dockerfile.fedora_thirtyseven.barebones.rpm.test b/builder/dockerfile.fedora_thirtyseven.barebones.rpm.test deleted file mode 100644 index ba97bf8..0000000 --- a/builder/dockerfile.fedora_thirtyseven.barebones.rpm.test +++ /dev/null @@ -1,19 +0,0 @@ -FROM fedora:37 - -ENV STARTUPDIR=/dockerstartup - -RUN dnf install -y xterm -RUN dnf install -y vim less -RUN yum install -y redhat-lsb-core - -ARG KASMVNC_PACKAGE_DIR -COPY $KASMVNC_PACKAGE_DIR/*.rpm /tmp/ -RUN dnf localinstall -y /tmp/*.rpm - -RUN mkdir -p $STARTUPDIR -COPY startup/vnc_startup_barebones.sh $STARTUPDIR - -RUN useradd -m foo -USER foo:kasmvnc-cert - -ENTRYPOINT "/$STARTUPDIR/vnc_startup_barebones.sh" diff --git a/builder/dockerfile.fedora_thirtyseven.build b/builder/dockerfile.fedora_thirtyseven.build deleted file mode 100644 index 4557431..0000000 --- a/builder/dockerfile.fedora_thirtyseven.build +++ /dev/null @@ -1,86 +0,0 @@ -FROM fedora:37 - -ENV KASMVNC_BUILD_OS fedora -ENV KASMVNC_BUILD_OS_CODENAME thirtyseven -ENV XORG_VER 1.20.14 - -RUN \ - echo "**** install build deps ****" && \ - dnf group install -y \ - "C Development Tools and Libraries" \ - "Development Tools" && \ - dnf install -y \ - autoconf \ - automake \ - bison \ - byacc \ - bzip2 \ - cmake \ - diffutils \ - doxygen \ - file \ - flex \ - fop \ - gcc \ - gcc-c++ \ - git \ - glibc-devel \ - libdrm-devel \ - libepoxy-devel \ - libmd-devel \ - libpciaccess-devel \ - libtool \ - libwebp-devel \ - libX11-devel \ - libXau-devel \ - libxcb-devel \ - libXcursor-devel \ - libxcvt-devel \ - libXdmcp-devel \ - libXext-devel \ - libXfont2-devel \ - libxkbfile-devel \ - libXrandr-devel \ - libxshmfence-devel \ - libXtst-devel \ - mesa-libEGL-devel \ - mesa-libgbm-devel \ - mesa-libGL-devel \ - meson \ - mingw64-binutils \ - mt-st \ - nettle-devel \ - openssl-devel \ - patch \ - pixman-devel \ - wayland-devel \ - wget \ - which \ - xcb-util-devel \ - xcb-util-image-devel \ - xcb-util-keysyms-devel \ - xcb-util-renderutil-devel \ - xcb-util-wm-devel \ - xinit \ - xkbcomp \ - xkbcomp-devel \ - xkeyboard-config \ - xmlto \ - xorg-x11-font-utils \ - xorg-x11-proto-devel \ - xorg-x11-server-common \ - xorg-x11-server-devel \ - xorg-x11-xtrans-devel \ - xsltproc - -ENV SCRIPTS_DIR=/tmp/scripts -COPY builder/scripts $SCRIPTS_DIR -RUN $SCRIPTS_DIR/build-webp -RUN $SCRIPTS_DIR/build-libjpeg-turbo - -RUN useradd -m docker && echo "docker:docker" | chpasswd - -COPY --chown=docker:docker . /src/ - -USER docker -ENTRYPOINT ["/src/builder/build.sh"] diff --git a/builder/dockerfile.fedora_thirtyseven.rpm.build b/builder/dockerfile.fedora_thirtyseven.rpm.build deleted file mode 100644 index 8384b10..0000000 --- a/builder/dockerfile.fedora_thirtyseven.rpm.build +++ /dev/null @@ -1,13 +0,0 @@ -FROM fedora:37 - -RUN dnf install -y fedora-packager fedora-review -RUN dnf install -y tree vim less -RUN dnf install -y redhat-lsb-core -RUN dnf install -y dnf-plugins-core - -COPY fedora/*.spec /tmp -RUN dnf builddep -y /tmp/*.spec - -RUN useradd -m docker && echo "docker:docker" | chpasswd - -USER docker diff --git a/builder/dockerfile.fedora_thirtyseven.rpm.test b/builder/dockerfile.fedora_thirtyseven.rpm.test deleted file mode 100644 index 5c7dea3..0000000 --- a/builder/dockerfile.fedora_thirtyseven.rpm.test +++ /dev/null @@ -1,62 +0,0 @@ -FROM fedora:37 - -ENV DISPLAY=:1 \ - VNC_PORT=8443 \ - VNC_RESOLUTION=1280x720 \ - MAX_FRAME_RATE=24 \ - VNCOPTIONS="-PreferBandwidth -DynamicQualityMin=4 -DynamicQualityMax=7" \ - HOME=/home/user \ - TERM=xterm \ - STARTUPDIR=/dockerstartup \ - INST_SCRIPTS=/dockerstartup/install \ - KASM_RX_HOME=/dockerstartup/kasmrx \ - DEBIAN_FRONTEND=noninteractive \ - VNC_COL_DEPTH=24 \ - VNC_RESOLUTION=1280x1024 \ - VNC_PW=vncpassword \ - VNC_USER=user \ - VNC_VIEW_ONLY_PW=vncviewonlypassword \ - LD_LIBRARY_PATH=/usr/local/lib/ \ - OMP_WAIT_POLICY=PASSIVE \ - SHELL=/bin/bash \ - SINGLE_APPLICATION=0 \ - KASMVNC_BUILD_OS=fedora \ - KASMVNC_BUILD_OS_CODENAME=thirtythree - -EXPOSE $VNC_PORT - -WORKDIR $HOME - -### REQUIRED STUFF ### - -RUN dnf install -y openssl xterm gettext wget -RUN dnf install -y nss_wrapper -RUN dnf install -y xorg-x11-xauth xkeyboard-config -# xorg-x11-server-Xorg -# RUN dnf install -y @xfce-desktop-environment -RUN dnf erase -y pm-utils xscreensaver* -RUN dnf install -y redhat-lsb-core -RUN dnf install -y vim less -RUN dnf install -y @xfce-desktop-environment - -RUN echo 'source $STARTUPDIR/generate_container_user' >> $HOME/.bashrc - -RUN mkdir -p $STARTUPDIR -COPY builder/startup/ $STARTUPDIR - -### START CUSTOM STUFF #### -COPY ./builder/scripts/ /tmp/scripts/ -COPY ./fedora/kasmvncserver.spec /tmp - -ARG KASMVNC_PACKAGE_DIR -COPY $KASMVNC_PACKAGE_DIR/*.rpm /tmp/ -# RUN dnf remove -y tigervnc-server-minimal -RUN /tmp/scripts/install_kasmvncserver_package - -### END CUSTOM STUFF ### - -RUN chown -R 1000:0 $HOME -USER 1000:kasmvnc-cert -WORKDIR $HOME - -ENTRYPOINT [ "/dockerstartup/vnc_startup.sh" ] diff --git a/builder/dockerfile.kali_kali-rolling.barebones.deb.test b/builder/dockerfile.kali_kali-rolling.barebones.deb.test new file mode 120000 index 0000000..9ec488c --- /dev/null +++ b/builder/dockerfile.kali_kali-rolling.barebones.deb.test @@ -0,0 +1 @@ +dockerfile.debian.barebones.deb.test \ No newline at end of file diff --git a/builder/dockerfile.kali_kali-rolling.build b/builder/dockerfile.kali_kali-rolling.build index 0812308..717e8f9 100644 --- a/builder/dockerfile.kali_kali-rolling.build +++ b/builder/dockerfile.kali_kali-rolling.build @@ -13,8 +13,9 @@ RUN apt-get update && \ RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends tzdata RUN apt-get update && apt-get -y build-dep xorg-server libxfont-dev RUN apt-get update && apt-get -y install gcc g++ curl -RUN apt-get update && apt-get -y install cmake git libgnutls28-dev vim wget tightvncserver -RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev libxcursor-dev +RUN apt-get update && apt-get -y install ninja-build cmake nasm git libgnutls28-dev vim wget tightvncserver +RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev \ + libxcursor-dev libavformat-dev libswscale-dev ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR diff --git a/builder/dockerfile.opensuse_15.barebones.rpm.test b/builder/dockerfile.opensuse_15.barebones.rpm.test index 3501b64..fa4f435 100644 --- a/builder/dockerfile.opensuse_15.barebones.rpm.test +++ b/builder/dockerfile.opensuse_15.barebones.rpm.test @@ -3,7 +3,8 @@ FROM opensuse/leap:15.5 ENV STARTUPDIR=/dockerstartup # base tools -RUN zypper -n install -y \ +ARG RUN_TEST +RUN [ "$RUN_TEST" = 1 ] || zypper -n install -y \ less \ vim \ xterm @@ -15,7 +16,7 @@ COPY $KASMVNC_PACKAGE_DIR/*.rpm /tmp RUN zypper install -y --allow-unsigned-rpm /tmp/*.rpm RUN mkdir -p $STARTUPDIR -COPY startup/vnc_startup_barebones.sh $STARTUPDIR +COPY builder/startup/vnc_startup_barebones.sh $STARTUPDIR RUN useradd -m foo USER foo:kasmvnc-cert diff --git a/builder/dockerfile.opensuse_15.build b/builder/dockerfile.opensuse_15.build index db2f130..63d9358 100644 --- a/builder/dockerfile.opensuse_15.build +++ b/builder/dockerfile.opensuse_15.build @@ -8,13 +8,17 @@ ENV XORG_VER 1.20.3 RUN zypper install -ny \ bdftopcf \ bigreqsproto-devel \ + ninja \ cmake \ + nasm \ curl \ ffmpeg-4-libavcodec-devel \ + ffmpeg-4-libswscale-devel \ + ffmpeg-4-libavformat-devel \ fonttosfnt \ font-util \ - gcc \ - gcc-c++ \ + gcc14 \ + gcc14-c++ \ giflib-devel \ git \ gzip \ @@ -45,17 +49,32 @@ RUN zypper install -ny \ xorg-x11-util-devel \ zlib-devel -ENV SCRIPTS_DIR=/tmp/scripts -COPY builder/scripts $SCRIPTS_DIR -RUN $SCRIPTS_DIR/build-webp -RUN $SCRIPTS_DIR/build-libjpeg-turbo +RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 140 \ + --slave /usr/bin/g++ g++ /usr/bin/g++-14 \ + --slave /usr/bin/gcov gcov /usr/bin/gcov-14 RUN useradd -u 1000 docker && \ groupadd -g 1000 docker && \ usermod -a -G docker docker +RUN ARCH=$(arch) && \ + CMAKE_URL="https://cmake.org/files/v3.22/cmake-3.22.0" && \ + if [ "$ARCH" = "x86_64" ]; then \ + CMAKE_URL="${CMAKE_URL}-linux-x86_64.sh"; \ + elif [ "$ARCH" = "aarch64" ]; then \ + CMAKE_URL="${CMAKE_URL}-linux-aarch64.sh"; \ + else \ + echo "Unsupported architecture: $ARCH" && exit 1; \ + fi && \ + curl -fsSL $CMAKE_URL -o cmake.sh && \ + (echo y; echo n) | bash cmake.sh --prefix=/usr/local --skip-license && \ + rm cmake.sh + +ENV SCRIPTS_DIR=/tmp/scripts +COPY builder/scripts $SCRIPTS_DIR +RUN $SCRIPTS_DIR/build-webp && $SCRIPTS_DIR/build-libjpeg-turbo + COPY --chown=docker:docker . /src/ - USER docker -ENTRYPOINT ["/src/builder/build.sh"] +ENTRYPOINT ["bash", "-l", "-c", "/src/builder/build.sh"] diff --git a/builder/dockerfile.oracle_8.barebones.rpm.test b/builder/dockerfile.oracle_8.barebones.rpm.test index d4cc293..3904e2c 100644 --- a/builder/dockerfile.oracle_8.barebones.rpm.test +++ b/builder/dockerfile.oracle_8.barebones.rpm.test @@ -2,7 +2,8 @@ FROM oraclelinux:8 ENV STARTUPDIR=/dockerstartup -RUN dnf install -y \ +ARG RUN_TEST +RUN [ "$RUN_TEST" = 1 ] || dnf install -y \ less \ redhat-lsb-core \ vim \ @@ -10,12 +11,15 @@ RUN dnf install -y \ RUN dnf config-manager --set-enabled ol8_codeready_builder RUN dnf install -y oracle-epel-release-el8 +COPY ./builder/scripts/ /tmp/scripts/ + ARG KASMVNC_PACKAGE_DIR -COPY $KASMVNC_PACKAGE_DIR/*.rpm /tmp -RUN dnf localinstall -y /tmp/*.rpm +COPY $KASMVNC_PACKAGE_DIR/kasmvncserver-*.rpm /tmp/ +COPY fedora/kasmvncserver.spec /tmp/ +RUN /tmp/scripts/install_kasmvncserver_package RUN mkdir -p $STARTUPDIR -COPY startup/vnc_startup_barebones.sh $STARTUPDIR +COPY builder/startup/vnc_startup_barebones.sh $STARTUPDIR RUN useradd -m foo USER foo:kasmvnc-cert diff --git a/builder/dockerfile.oracle_8.build b/builder/dockerfile.oracle_8.build index 4a98a19..0a9ecfe 100644 --- a/builder/dockerfile.oracle_8.build +++ b/builder/dockerfile.oracle_8.build @@ -11,10 +11,13 @@ RUN \ dnf install -y \ bzip2-devel \ ca-certificates \ + ninja-build \ cmake \ + nasm \ dnf-plugins-core \ gcc \ gcc-c++ \ + gcc-toolset-14 \ git \ gnutls-devel \ libjpeg-turbo-devel \ @@ -38,6 +41,7 @@ RUN dnf install -y --nogpgcheck https://mirrors.rpmfusion.org/free/el/rpmfusion- # Install from new repos RUN dnf install -y \ + tbb-devel \ ffmpeg-devel \ giflib-devel \ lbzip2 \ @@ -48,16 +52,16 @@ RUN dnf install -y \ xorg-x11-xtrans-devel \ libXrandr-devel \ libXtst-devel \ - libXcursor-devel + libXcursor-devel \ + libSM-devel ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR -RUN $SCRIPTS_DIR/build-webp -RUN $SCRIPTS_DIR/build-libjpeg-turbo - -RUN useradd -m docker && echo "docker:docker" | chpasswd +RUN echo "source /opt/rh/gcc-toolset-14/enable" > /etc/profile.d/gcc-toolset.sh && \ + $SCRIPTS_DIR/build-webp && $SCRIPTS_DIR/build-libjpeg-turbo && \ + useradd -m docker && echo "docker:docker" | chpasswd COPY --chown=docker:docker . /src/ USER docker -ENTRYPOINT ["/src/builder/build.sh"] +ENTRYPOINT ["bash", "-l", "-c", "/src/builder/build.sh"] diff --git a/builder/dockerfile.oracle_9.barebones.rpm.test b/builder/dockerfile.oracle_9.barebones.rpm.test index 2e8f3b5..e4fa885 100644 --- a/builder/dockerfile.oracle_9.barebones.rpm.test +++ b/builder/dockerfile.oracle_9.barebones.rpm.test @@ -2,7 +2,7 @@ FROM oraclelinux:9 ENV STARTUPDIR=/dockerstartup -RUN dnf install -y \ +RUN [ "$RUN_TEST" = 1 ] || dnf install -y \ less \ vim \ xterm @@ -17,7 +17,7 @@ RUN dnf install -y crypto-policies-scripts RUN update-crypto-policies --set FIPS:SHA1 RUN mkdir -p $STARTUPDIR -COPY startup/vnc_startup_barebones.sh $STARTUPDIR +COPY builder/startup/vnc_startup_barebones.sh $STARTUPDIR RUN useradd -m foo USER foo:kasmvnc-cert diff --git a/builder/dockerfile.oracle_9.build b/builder/dockerfile.oracle_9.build index ca94b69..674e2f0 100644 --- a/builder/dockerfile.oracle_9.build +++ b/builder/dockerfile.oracle_9.build @@ -11,10 +11,13 @@ RUN \ dnf install -y \ bzip2-devel \ ca-certificates \ + ninja-build \ cmake \ + nasm \ dnf-plugins-core \ gcc \ gcc-c++ \ + gcc-toolset-14 \ git \ gnutls-devel \ libjpeg-turbo-devel \ @@ -40,6 +43,7 @@ RUN dnf install -y --nogpgcheck https://mirrors.rpmfusion.org/free/el/rpmfusion- # Install from new repos RUN dnf install -y \ giflib-devel \ + ffmpeg-devel \ lbzip2 \ libXfont2-devel \ libxkbfile-devel \ @@ -47,17 +51,16 @@ RUN dnf install -y \ xorg-x11-xtrans-devel \ libXrandr-devel \ libXtst-devel \ - libXcursor-devel - + libXcursor-devel \ + libSM-devel ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR -RUN $SCRIPTS_DIR/build-webp -RUN $SCRIPTS_DIR/build-libjpeg-turbo - -RUN useradd -m docker && echo "docker:docker" | chpasswd +RUN echo "source /opt/rh/gcc-toolset-14/enable" > /etc/profile.d/gcc-toolset.sh && \ + $SCRIPTS_DIR/build-webp && $SCRIPTS_DIR/build-libjpeg-turbo && \ + useradd -m docker && echo "docker:docker" | chpasswd COPY --chown=docker:docker . /src/ USER docker -ENTRYPOINT ["/src/builder/build.sh"] +ENTRYPOINT ["bash", "-l", "-c", "/src/builder/build.sh"] diff --git a/builder/dockerfile.ubuntu_bionic.build b/builder/dockerfile.ubuntu_bionic.build deleted file mode 100644 index c76b4eb..0000000 --- a/builder/dockerfile.ubuntu_bionic.build +++ /dev/null @@ -1,32 +0,0 @@ -FROM ubuntu:18.04 - -ENV KASMVNC_BUILD_OS ubuntu -ENV KASMVNC_BUILD_OS_CODENAME bionic -ENV XORG_VER 1.20.10 - -RUN sed -i 's$# deb-src$deb-src$' /etc/apt/sources.list - -RUN apt-get update && \ - apt-get -y install sudo - -RUN apt-get update && apt-get -y build-dep xorg-server libxfont-dev -RUN apt-get update && apt-get -y install cmake git libgnutls28-dev vim wget tightvncserver curl -RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev libxcursor-dev - -ENV SCRIPTS_DIR=/tmp/scripts -COPY builder/scripts $SCRIPTS_DIR -RUN $SCRIPTS_DIR/build-webp -RUN $SCRIPTS_DIR/build-libjpeg-turbo - -# Fix for older required libs -#RUN cd /tmp && wget http://launchpadlibrarian.net/347526424/libxfont1-dev_1.5.2-4ubuntu2_amd64.deb && \ -# wget http://launchpadlibrarian.net/347526425/libxfont1_1.5.2-4ubuntu2_amd64.deb && \ -# dpkg -i libxfont1_1.5.2-4ubuntu2_amd64.deb && \ -# dpkg -i libxfont1-dev_1.5.2-4ubuntu2_amd64.deb - -RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo - -COPY --chown=docker:docker . /src/ - -USER docker -ENTRYPOINT ["/src/builder/build.sh"] diff --git a/builder/dockerfile.ubuntu_bionic.deb.build b/builder/dockerfile.ubuntu_bionic.deb.build deleted file mode 100644 index a554ecb..0000000 --- a/builder/dockerfile.ubuntu_bionic.deb.build +++ /dev/null @@ -1,17 +0,0 @@ -FROM ubuntu:bionic - -RUN apt-get update && \ - apt-get -y install vim build-essential devscripts equivs - -# Install build-deps for the package. -COPY ./debian/control /tmp -RUN apt-get update && echo YYY | mk-build-deps --install --remove /tmp/control - -ARG L_UID -RUN if [ "$L_UID" -eq 0 ]; then \ - useradd -m docker; \ - else \ - useradd -m docker -u $L_UID;\ - fi - -USER docker diff --git a/builder/dockerfile.ubuntu_bionic.deb.test b/builder/dockerfile.ubuntu_bionic.deb.test deleted file mode 100644 index 8d68055..0000000 --- a/builder/dockerfile.ubuntu_bionic.deb.test +++ /dev/null @@ -1,57 +0,0 @@ -FROM ubuntu:bionic - -ENV DISPLAY=:1 \ - VNC_PORT=8443 \ - VNC_RESOLUTION=1280x720 \ - MAX_FRAME_RATE=24 \ - VNCOPTIONS="-PreferBandwidth -DynamicQualityMin=4 -DynamicQualityMax=7" \ - HOME=/home/user \ - TERM=xterm \ - STARTUPDIR=/dockerstartup \ - INST_SCRIPTS=/dockerstartup/install \ - KASM_RX_HOME=/dockerstartup/kasmrx \ - DEBIAN_FRONTEND=noninteractive \ - VNC_COL_DEPTH=24 \ - VNC_RESOLUTION=1280x1024 \ - VNC_PW=vncpassword \ - VNC_USER=user \ - VNC_VIEW_ONLY_PW=vncviewonlypassword \ - LD_LIBRARY_PATH=/usr/local/lib/ \ - OMP_WAIT_POLICY=PASSIVE \ - SHELL=/bin/bash \ - SINGLE_APPLICATION=0 \ - KASMVNC_BUILD_OS=ubuntu \ - KASMVNC_BUILD_OS_CODENAME=bionic - -EXPOSE $VNC_PORT - -WORKDIR $HOME - -### REQUIRED STUFF ### - -RUN apt-get update && apt-get install -y supervisor xfce4 xfce4-terminal xterm libnss-wrapper gettext wget -RUN apt-get purge -y pm-utils xscreensaver* -RUN apt-get update && apt-get install -y vim less -RUN apt-get update && apt-get -y install lsb-release - -RUN echo 'source $STARTUPDIR/generate_container_user' >> $HOME/.bashrc - -RUN mkdir -p $STARTUPDIR -COPY builder/startup/ $STARTUPDIR - -### START CUSTOM STUFF #### - -COPY ./builder/scripts/ /tmp/scripts/ -COPY ./debian/changelog /tmp - -ARG KASMVNC_PACKAGE_DIR -COPY $KASMVNC_PACKAGE_DIR/kasmvncserver_*.deb /tmp/ -RUN /tmp/scripts/install_kasmvncserver_package - -### END CUSTOM STUFF ### - -RUN chown -R 1000:0 $HOME -USER 1000:ssl-cert -WORKDIR $HOME - -ENTRYPOINT [ "/dockerstartup/vnc_startup.sh" ] diff --git a/builder/dockerfile.ubuntu_bionic.test b/builder/dockerfile.ubuntu_bionic.test deleted file mode 100644 index a9c2049..0000000 --- a/builder/dockerfile.ubuntu_bionic.test +++ /dev/null @@ -1,51 +0,0 @@ -FROM ubuntu:18.04 - -ENV DISPLAY=:1 \ - VNC_PORT=8443 \ - VNC_RESOLUTION=1280x720 \ - MAX_FRAME_RATE=24 \ - VNCOPTIONS="-PreferBandwidth -DynamicQualityMin=4 -DynamicQualityMax=7" \ - HOME=/home/user \ - TERM=xterm \ - STARTUPDIR=/dockerstartup \ - INST_SCRIPTS=/dockerstartup/install \ - KASM_RX_HOME=/dockerstartup/kasmrx \ - DEBIAN_FRONTEND=noninteractive \ - VNC_COL_DEPTH=24 \ - VNC_RESOLUTION=1280x1024 \ - VNC_PW=vncpassword \ - VNC_USER=user \ - VNC_VIEW_ONLY_PW=vncviewonlypassword \ - LD_LIBRARY_PATH=/usr/local/lib/ \ - OMP_WAIT_POLICY=PASSIVE \ - SHELL=/bin/bash \ - SINGLE_APPLICATION=0 \ - KASMVNC_BUILD_OS=ubuntu \ - KASMVNC_BUILD_OS_CODENAME=bionic - -EXPOSE $VNC_PORT - -WORKDIR $HOME - -### REQUIRED STUFF ### - -RUN apt-get update && apt-get install -y supervisor xfce4 xfce4-terminal xterm libnss-wrapper gettext wget -RUN apt-get purge -y pm-utils xscreensaver* - -RUN echo 'source $STARTUPDIR/generate_container_user' >> $HOME/.bashrc - -RUN mkdir -p $STARTUPDIR -COPY builder/startup/ $STARTUPDIR - -### START CUSTOM STUFF #### - -COPY build/kasmvnc.${KASMVNC_BUILD_OS}_${KASMVNC_BUILD_OS_CODENAME}.tar.gz /tmp/ -RUN tar -xzvf /tmp/kasmvnc.${KASMVNC_BUILD_OS}_${KASMVNC_BUILD_OS_CODENAME}.tar.gz --strip 1 -C / - -### END CUSTOM STUFF ### - -RUN chown -R 1000:0 $HOME -USER 1000 -WORKDIR $HOME - -ENTRYPOINT [ "/dockerstartup/vnc_startup.sh" ] diff --git a/builder/dockerfile.ubuntu_focal.barebones.deb.test b/builder/dockerfile.ubuntu_focal.barebones.deb.test deleted file mode 100644 index d9effe0..0000000 --- a/builder/dockerfile.ubuntu_focal.barebones.deb.test +++ /dev/null @@ -1,20 +0,0 @@ -FROM ubuntu:focal - -ENV STARTUPDIR=/dockerstartup - -COPY ./builder/scripts/ /tmp/scripts/ -COPY ./debian/changelog /tmp - -ARG KASMVNC_PACKAGE_DIR -COPY $KASMVNC_PACKAGE_DIR/kasmvncserver_*.deb /tmp/ -RUN /tmp/scripts/install_kasmvncserver_package - -RUN apt-get update && apt-get -y install xterm lsb-release - -RUN mkdir -p $STARTUPDIR -COPY builder/startup/vnc_startup_barebones.sh $STARTUPDIR - -RUN useradd -m foo && addgroup foo ssl-cert -USER foo - -ENTRYPOINT "/$STARTUPDIR/vnc_startup_barebones.sh" diff --git a/builder/dockerfile.ubuntu_focal.barebones.deb.test b/builder/dockerfile.ubuntu_focal.barebones.deb.test new file mode 120000 index 0000000..9ec488c --- /dev/null +++ b/builder/dockerfile.ubuntu_focal.barebones.deb.test @@ -0,0 +1 @@ +dockerfile.debian.barebones.deb.test \ No newline at end of file diff --git a/builder/dockerfile.ubuntu_focal.build b/builder/dockerfile.ubuntu_focal.build index c734715..57c7373 100644 --- a/builder/dockerfile.ubuntu_focal.build +++ b/builder/dockerfile.ubuntu_focal.build @@ -12,16 +12,31 @@ RUN apt-get update && \ RUN apt-get update && apt-get install -y --no-install-recommends tzdata RUN apt-get update && apt-get -y build-dep xorg-server libxfont-dev -RUN apt-get update && apt-get -y install cmake git vim wget curl -RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev libxcursor-dev +RUN apt-get update && apt-get -y install ninja-build nasm git vim wget curl +RUN apt-get update && apt-get -y install libtbb-dev libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev \ + libxcursor-dev libavformat-dev libswscale-dev ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR -RUN $SCRIPTS_DIR/build-webp -RUN $SCRIPTS_DIR/build-libjpeg-turbo RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo +RUN ARCH=$(arch) && \ + CMAKE_URL="https://cmake.org/files/v3.22/cmake-3.22.0" && \ + if [ "$ARCH" = "x86_64" ]; then \ + CMAKE_URL="${CMAKE_URL}-linux-x86_64.sh"; \ + elif [ "$ARCH" = "aarch64" ]; then \ + CMAKE_URL="${CMAKE_URL}-linux-aarch64.sh"; \ + else \ + echo "Unsupported architecture: $ARCH" && exit 1; \ + fi && \ + curl -fsSL $CMAKE_URL -o cmake.sh && \ + (echo y; echo n) | bash cmake.sh --prefix=/usr/local --skip-license && \ + rm cmake.sh + +RUN $SCRIPTS_DIR/build-webp +RUN $SCRIPTS_DIR/build-libjpeg-turbo + COPY --chown=docker:docker . /src/ USER docker diff --git a/builder/dockerfile.ubuntu_focal.deb.build b/builder/dockerfile.ubuntu_focal.deb.build index 8a4db12..ffe6402 100644 --- a/builder/dockerfile.ubuntu_focal.deb.build +++ b/builder/dockerfile.ubuntu_focal.deb.build @@ -3,7 +3,7 @@ FROM ubuntu:focal ENV DEBIAN_FRONTEND noninteractive RUN apt-get update && \ - apt-get -y install vim build-essential devscripts equivs + apt-get -y install vim build-essential devscripts equivs libtbb-dev # Install build-deps for the package. COPY ./debian/control /tmp diff --git a/builder/dockerfile.ubuntu_focal.deb.test b/builder/dockerfile.ubuntu_focal.deb.test index 16cd8a0..1ee3ad0 100644 --- a/builder/dockerfile.ubuntu_focal.deb.test +++ b/builder/dockerfile.ubuntu_focal.deb.test @@ -21,7 +21,7 @@ ENV DISPLAY=:1 \ SHELL=/bin/bash \ SINGLE_APPLICATION=0 \ KASMVNC_BUILD_OS=ubuntu \ - KASMVNC_BUILD_OS_CODENAME=bionic + KASMVNC_BUILD_OS_CODENAME=focal EXPOSE $VNC_PORT diff --git a/builder/dockerfile.ubuntu_focal.specs.test b/builder/dockerfile.ubuntu_focal.specs.test index 663fe3d..414aebc 100644 --- a/builder/dockerfile.ubuntu_focal.specs.test +++ b/builder/dockerfile.ubuntu_focal.specs.test @@ -6,7 +6,7 @@ RUN apt-get update && apt-get install -y vim less RUN apt-get update && apt-get install -y python3-pip RUN apt-get update && apt-get install -y strace silversearcher-ag xfonts-base RUN apt-get update && apt-get install -y cinnamon -RUN apt-get update && apt-get install -y mate +RUN apt-get update && apt-get install -y mate wget RUN useradd -m docker diff --git a/builder/dockerfile.ubuntu_jammy.barebones.deb.test b/builder/dockerfile.ubuntu_jammy.barebones.deb.test new file mode 120000 index 0000000..9ec488c --- /dev/null +++ b/builder/dockerfile.ubuntu_jammy.barebones.deb.test @@ -0,0 +1 @@ +dockerfile.debian.barebones.deb.test \ No newline at end of file diff --git a/builder/dockerfile.ubuntu_jammy.build b/builder/dockerfile.ubuntu_jammy.build index 933fc90..11eafc7 100644 --- a/builder/dockerfile.ubuntu_jammy.build +++ b/builder/dockerfile.ubuntu_jammy.build @@ -12,8 +12,9 @@ RUN apt-get update && \ RUN apt-get update && apt-get install -y --no-install-recommends tzdata RUN apt-get update && apt-get -y build-dep xorg-server libxfont-dev -RUN apt-get update && apt-get -y install cmake git libgnutls28-dev vim wget tightvncserver curl -RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev libxcursor-dev +RUN apt-get update && apt-get -y install ninja-build cmake nasm git libgnutls28-dev vim wget tightvncserver curl +RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev \ + libxcursor-dev libavformat-dev libswscale-dev ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR diff --git a/builder/dockerfile.ubuntu_jammy.deb.test b/builder/dockerfile.ubuntu_jammy.deb.test index 4e5f0dd..831a7c8 100644 --- a/builder/dockerfile.ubuntu_jammy.deb.test +++ b/builder/dockerfile.ubuntu_jammy.deb.test @@ -21,7 +21,7 @@ ENV DISPLAY=:1 \ SHELL=/bin/bash \ SINGLE_APPLICATION=0 \ KASMVNC_BUILD_OS=ubuntu \ - KASMVNC_BUILD_OS_CODENAME=bionic + KASMVNC_BUILD_OS_CODENAME=jammy EXPOSE $VNC_PORT diff --git a/builder/dockerfile.ubuntu_jammy.dev b/builder/dockerfile.ubuntu_jammy.dev new file mode 100644 index 0000000..08ae041 --- /dev/null +++ b/builder/dockerfile.ubuntu_jammy.dev @@ -0,0 +1,73 @@ +FROM kasmweb/ubuntu-jammy-desktop:develop + +ENV KASMVNC_BUILD_OS ubuntu +ENV KASMVNC_BUILD_OS_CODENAME jammy +ENV XORG_VER 21.1.3 +ENV XORG_PATCH 21 +ENV DEBIAN_FRONTEND noninteractive + +EXPOSE 6901 + +USER root + +COPY builder/conf/nginx_kasm.conf /etc/nginx/conf.d/ + +RUN sed -i 's$# deb-src$deb-src$' /etc/apt/sources.list && \ + apt update && \ + apt install -y \ + ninja-build \ + gdb \ + valgrind \ + rsync \ + dos2unix \ + socat \ + sudo \ + libxfont-dev \ + cmake \ + nasm \ + git \ + libgnutls28-dev \ + vim \ + wget \ + tightvncserver \ + curl \ + libpng-dev \ + libtiff-dev \ + libgif-dev \ + libavformat-dev \ + libavcodec-dev \ + libswscale-dev \ + libssl-dev \ + libxrandr-dev \ + libxcursor-dev \ + pkg-config \ + libfreetype6-dev \ + libxtst-dev \ + autoconf \ + automake \ + libtool \ + xutils-dev \ + libpixman-1-dev \ + libxshmfence-dev \ + libxcvt-dev \ + libxkbfile-dev \ + x11proto-dev \ + libgbm-dev \ + inotify-tools && \ + echo "kasm-user ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers + +RUN curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - +RUN apt install -y nodejs nginx + +COPY builder/scripts/build-webp /tmp +COPY builder/scripts/build-libjpeg-turbo /tmp +COPY builder/common.sh /tmp + +RUN chmod +x /tmp/build-webp && /tmp/build-webp +RUN chmod +x /tmp/build-libjpeg-turbo && /tmp/build-libjpeg-turbo + +USER 1000 + +WORKDIR /src + +ENTRYPOINT /bin/bash diff --git a/builder/dockerfile.ubuntu_noble.barebones.deb.test b/builder/dockerfile.ubuntu_noble.barebones.deb.test new file mode 120000 index 0000000..9ec488c --- /dev/null +++ b/builder/dockerfile.ubuntu_noble.barebones.deb.test @@ -0,0 +1 @@ +dockerfile.debian.barebones.deb.test \ No newline at end of file diff --git a/builder/dockerfile.ubuntu_noble.build b/builder/dockerfile.ubuntu_noble.build index a71ad86..dfb796c 100644 --- a/builder/dockerfile.ubuntu_noble.build +++ b/builder/dockerfile.ubuntu_noble.build @@ -12,8 +12,9 @@ RUN apt-get update && \ RUN apt-get update && apt-get install -y --no-install-recommends tzdata RUN apt-get update && apt-get -y build-dep xorg-server libxfont-dev -RUN apt-get update && apt-get -y install cmake git libgnutls28-dev vim wget curl -RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev libxcursor-dev +RUN apt-get update && apt-get -y install ninja-build cmake nasm git libgnutls28-dev vim wget curl +RUN apt-get update && apt-get -y install libpng-dev libtiff-dev libgif-dev libavcodec-dev libssl-dev libxrandr-dev \ + libxcursor-dev libavformat-dev libswscale-dev ENV SCRIPTS_DIR=/tmp/scripts COPY builder/scripts $SCRIPTS_DIR diff --git a/builder/dockerfile.www.build b/builder/dockerfile.www.build index be30b4b..e5a1845 100644 --- a/builder/dockerfile.www.build +++ b/builder/dockerfile.www.build @@ -1,6 +1,8 @@ -FROM node:12-buster +FROM alpine -COPY kasmweb/ /src/www/ +RUN apk add npm nodejs + +COPY kasmweb/ /src/www COPY builder/build_www.sh /src/ WORKDIR /src/www diff --git a/builder/os_ver_cli.sh b/builder/os_ver_cli.sh index 534810d..cc19427 100644 --- a/builder/os_ver_cli.sh +++ b/builder/os_ver_cli.sh @@ -1,7 +1,7 @@ #!/bin/bash default_os=${default_os:-ubuntu} -default_os_codename=${default_os_codename:-bionic} +default_os_codename=${default_os_codename:-noble} os=${1:-$default_os} os_codename=${2:-$default_os_codename} diff --git a/builder/process_test_options.sh b/builder/process_test_options.sh index da8b55c..7f7d659 100644 --- a/builder/process_test_options.sh +++ b/builder/process_test_options.sh @@ -6,7 +6,7 @@ usage() { } process_options() { - local sorted_options=$(getopt -o psh --long perf-test --long shell --long help -- "$@") + local sorted_options=$(getopt -o prsh --long perf-test --long run-test --long shell --long help -- "$@") eval set -- $sorted_options while : ; do @@ -16,6 +16,10 @@ process_options() { entrypoint_executable="--entrypoint=/usr/bin/Xvnc" shift ;; + -r|--run-test) + run_test=1 + shift + ;; -s|--shell) entrypoint_executable="--entrypoint=bash" shift diff --git a/builder/scripts/build-libjpeg-turbo b/builder/scripts/build-libjpeg-turbo index 1510f24..ed8a737 100755 --- a/builder/scripts/build-libjpeg-turbo +++ b/builder/scripts/build-libjpeg-turbo @@ -3,25 +3,14 @@ set -euo pipefail build_and_install() { - export MAKEFLAGS=-j`nproc` - export CFLAGS="-fpic" - cmake -DCMAKE_INSTALL_PREFIX=/usr/local -G"Unix Makefiles" - make - make install -} - -install_build_dependencies() { - install_packages cmake gcc - ensure_libjpeg_is_fast -} - -ensure_libjpeg_is_fast() { - install_packages nasm + cmake -B build -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_POSITION_INDEPENDENT_CODE=ON -GNinja . + ninja -C build install } prepare_libjpeg_source() { export JPEG_TURBO_RELEASE=$(curl -sX GET "https://api.github.com/repos/libjpeg-turbo/libjpeg-turbo/releases/latest" \ | awk '/tag_name/{print $4;exit}' FS='[""]') + [ -d ./libjpeg-turbo ] && rm -rf ./libjpeg-turbo mkdir libjpeg-turbo curl -Ls "https://github.com/libjpeg-turbo/libjpeg-turbo/archive/${JPEG_TURBO_RELEASE}.tar.gz" | \ tar xzvf - -C libjpeg-turbo/ --strip-components=1 @@ -31,6 +20,5 @@ prepare_libjpeg_source() { source_dir=$(dirname "$0") . "$source_dir/common.sh" -install_build_dependencies prepare_libjpeg_source build_and_install diff --git a/builder/scripts/build-webp b/builder/scripts/build-webp index 82b07ba..e5e19f1 100755 --- a/builder/scripts/build-webp +++ b/builder/scripts/build-webp @@ -2,22 +2,30 @@ set -euo pipefail -webp_tar_url=https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.2.4.tar.gz +WEBP_VERSION="1.5.0" +WEBP_TAR_URL="https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-${WEBP_VERSION}.tar.gz" +WEBP_TAR_FILE="/tmp/libwebp-${WEBP_VERSION}.tar.gz" +WEBP_SRC_DIR="/tmp/libwebp-${WEBP_VERSION}" prepare_source() { cd /tmp - wget "$webp_tar_url" - tar -xzf /tmp/libwebp-* - rm /tmp/libwebp-*.tar.gz - cd /tmp/libwebp-* + + # Remove old files if they exist + [ -f "$WEBP_TAR_FILE" ] && rm "$WEBP_TAR_FILE" + [ -d "$WEBP_SRC_DIR" ] && rm -rf "$WEBP_SRC_DIR" + + wget "$WEBP_TAR_URL" + tar -xzf "$WEBP_TAR_FILE" + cd "$WEBP_SRC_DIR" } build_and_install() { - export MAKEFLAGS=-j`nproc` - ./configure --enable-static --disable-shared + export MAKEFLAGS=-j$(nproc) + ./configure --enable-static --disable-shared --enable-threading --enable-sse2 --enable-neon make make install } prepare_source build_and_install + diff --git a/builder/scripts/common.sh b/builder/scripts/common.sh index 4440b66..e22deef 100644 --- a/builder/scripts/common.sh +++ b/builder/scripts/common.sh @@ -1,9 +1,7 @@ #!/bin/bash detect_distro() { - if [ -f /etc/centos-release ]; then - DISTRO=centos - elif [ -f /etc/oracle-release ]; then + if [ -f /etc/oracle-release ]; then DISTRO=oracle elif [ -f /etc/fedora-release ]; then DISTRO=fedora @@ -20,7 +18,6 @@ install_packages() { local install_cmd=no-command-defined case "$DISTRO" in - centos) install_cmd="yum install -y" ;; oracle) install_cmd="dnf install -y" ;; fedora) install_cmd="dnf install -y" ;; opensuse) install_cmd="zypper install -y" ;; diff --git a/builder/scripts/install_alpine_signing_keys b/builder/scripts/install_alpine_signing_keys new file mode 100755 index 0000000..63c3630 --- /dev/null +++ b/builder/scripts/install_alpine_signing_keys @@ -0,0 +1,30 @@ +#!/bin/bash + +set -e + +install_private_signing_key() { + if echo "$KASMVNC_ALPINE_PRIVATE_KEY" | grep -q -- "$BEGIN_PRIVATE_KEY"; then + echo "$KASMVNC_ALPINE_PRIVATE_KEY" > $APK_KEYS_DIR/kasmvnc_signing_key.rsa + else + echo -e "$BEGIN_PRIVATE_KEY\n$KASMVNC_ALPINE_PRIVATE_KEY\n$END_PRIVATE_KEY" > \ + $APK_KEYS_DIR/kasmvnc_signing_key.rsa + fi +} + +install_public_signing_key() { + if echo "$KASMVNC_ALPINE_PUBLIC_KEY" | grep -q -- "$BEGIN_PUBLIC_KEY"; then \ + echo "$KASMVNC_ALPINE_PUBLIC_KEY" > $APK_KEYS_DIR/kasmvnc_signing_key.rsa.pub + else + echo -e "$BEGIN_PUBLIC_KEY\n$KASMVNC_ALPINE_PUBLIC_KEY\n$END_PUBLIC_KEY" > \ + $APK_KEYS_DIR/kasmvnc_signing_key.rsa.pub + fi +} + +APK_KEYS_DIR=/etc/apk/keys +BEGIN_PRIVATE_KEY='-----BEGIN PRIVATE KEY-----' +END_PRIVATE_KEY='-----END PRIVATE KEY-----' +BEGIN_PUBLIC_KEY='-----BEGIN PUBLIC KEY-----' +END_PUBLIC_KEY='-----END PUBLIC KEY-----' + +install_private_signing_key +install_public_signing_key diff --git a/builder/scripts/install_kasmvncserver_package b/builder/scripts/install_kasmvncserver_package index 7537d57..dcdf79c 100755 --- a/builder/scripts/install_kasmvncserver_package +++ b/builder/scripts/install_kasmvncserver_package @@ -10,6 +10,10 @@ is_debian() { [[ -f /etc/debian_version ]] } +is_alpine() { + [[ -f /etc/alpine-release ]] +} + check_package_version_exists() { if ! stat /tmp/kasmvncserver_"$package_version"*.deb; then >&2 echo "No package found for version $package_version" @@ -42,7 +46,16 @@ install_package_built_for_current_branch_package_version_deb() { --file /tmp/changelog) check_package_version_exists - apt-get install -y /tmp/kasmvncserver_"$package_version"*"$tag"*.deb + dpkg_arch=$(dpkg-architecture -q DEB_BUILD_ARCH) + apt-get install -y /tmp/kasmvncserver_"$package_version"*"$tag"*_${dpkg_arch}.deb +} + +detect_dnf_command() { + if command -v dnf5 >/dev/null; then + echo dnf install -y --allowerasing + else + echo dnf localinstall -y --allowerasing + fi } install_package_built_for_current_branch_package_version_rpm() { @@ -50,15 +63,24 @@ install_package_built_for_current_branch_package_version_rpm() { $rpm_package_manager install -y rpmdevtools package_version=$(rpmspec -q --qf '%{version}\n' /tmp/kasmvncserver.spec 2>/dev/null) + package_name=/tmp/kasmvncserver-"$package_version"*.$(arch).rpm if [[ $rpm_package_manager = "dnf" ]]; then - dnf localinstall -y --allowerasing /tmp/kasmvncserver-"$package_version"*.rpm + local dnf_cmd=$(detect_dnf_command) + $dnf_cmd $package_name else - yum install -y /tmp/kasmvncserver-"$package_version"*.rpm + yum install -y $package_name fi } +install_package_built_for_current_branch_package_version_apk() { + package_version=$(sed -n 's/pkgver=\(.\+\)/\1/p' /tmp/APKBUILD ) + apk add /tmp/kasmvncserver-"$package_version"*.apk /tmp/kasmvncserver-doc-"$package_version"*.apk --allow-untrusted +} + if is_debian ; then install_package_built_for_current_branch_package_version_deb +elif is_alpine; then + install_package_built_for_current_branch_package_version_apk else install_package_built_for_current_branch_package_version_rpm fi diff --git a/builder/startup/vnc_startup_barebones.sh b/builder/startup/vnc_startup_barebones.sh index 2fea7f8..8306941 100755 --- a/builder/startup/vnc_startup_barebones.sh +++ b/builder/startup/vnc_startup_barebones.sh @@ -19,4 +19,9 @@ set_xterm_to_run create_kasm_user vncserver -select-de manual -websocketPort "$VNC_PORT" +vncserver_exit_code=$? +if [ "$RUN_TEST" = 1 ]; then + exit "$vncserver_exit_code" +fi + tail -f "$config_dir"/*.log diff --git a/builder/test-apk-barebones b/builder/test-apk-barebones new file mode 100755 index 0000000..945412c --- /dev/null +++ b/builder/test-apk-barebones @@ -0,0 +1,30 @@ +#!/bin/bash + +set -e + +detect_base_image() { + BASE_IMAGE=$(echo "${os}:${os_codename}" | sed 's/\([0-9]\{2\}\)$/.\1/') +} + +cd "$(dirname "$0")/.." +. ./builder/process_test_options.sh +. ./builder/common.sh +os="${1:-alpine}" +os_codename="${2:-321}" + +detect_build_dir +detect_base_image +docker build --build-arg KASMVNC_PACKAGE_DIR="${build_dir}/${os}_${os_codename}" \ + --build-arg RUN_TEST="$run_test" \ + --build-arg BASE_IMAGE="$BASE_IMAGE" \ + -t kasmvnctester_barebones_${os}:$os_codename \ + -f builder/dockerfile.${os}_${os_codename}.barebones.apk.test . +echo + +detect_interactive +docker run $interactive -p "443:$VNC_PORT" --rm -e "VNC_USER=foo" -e "VNC_PW=foobar" \ + -e "VNC_PORT=$VNC_PORT" \ + -e RUN_TEST="$run_test" \ + $entrypoint_executable \ + kasmvnctester_barebones_${os}:$os_codename \ + $entrypoint_args diff --git a/builder/test-barebones b/builder/test-barebones new file mode 100755 index 0000000..aee9055 --- /dev/null +++ b/builder/test-barebones @@ -0,0 +1,42 @@ +#!/bin/bash + +set -eo pipefail + +create_gitlab_report() { + local error="$1" +failure_report=$(cat < + + + ${error} + + +EOF +) +} + +write_gitlab_report() { + echo "$failure_report" > run_test/"${os}_${os_codename}.xml" +} + +saved_options=("$@") +. ./builder/process_test_options.sh +. ./builder/common.sh + +os="$1" +os_codename="$2" +os_fullname="${os}_${os_codename}" + +detect_package_format +if [ "$run_test" != 1 ]; then + builder/test-${package_format}-barebones "${saved_options[@]}" + exit $? +fi + +mkdir -p run_test +if ! builder/test-${package_format}-barebones "${saved_options[@]}" 2>&1 | \ + tee run_test/"${os_fullname}.log"; then + create_gitlab_report "$(tail -1 run_test/${os_fullname}.log)" + write_gitlab_report + exit 1 +fi diff --git a/builder/test-deb-barebones b/builder/test-deb-barebones index 9ffdb5d..593ce5c 100755 --- a/builder/test-deb-barebones +++ b/builder/test-deb-barebones @@ -2,18 +2,33 @@ set -e +detect_base_image() { + if [ "$os" = kali ]; then + BASE_IMAGE=kalilinux/kali-rolling:latest + return + fi + BASE_IMAGE="${os}:${os_codename}" +} + cd "$(dirname "$0")/.." . ./builder/process_test_options.sh . ./builder/common.sh os="${1:-debian}" os_codename="${2:-buster}" -docker build --build-arg KASMVNC_PACKAGE_DIR="builder/build/${os_codename}" \ +detect_build_dir +detect_base_image +docker build --build-arg KASMVNC_PACKAGE_DIR="${build_dir}/${os_codename}" \ + --build-arg RUN_TEST="$run_test" \ + --build-arg BASE_IMAGE="$BASE_IMAGE" \ -t kasmvnctester_barebones_${os}:$os_codename \ -f builder/dockerfile.${os}_${os_codename}.barebones.deb.test . echo -docker run -it -p "443:$VNC_PORT" --rm -e "VNC_USER=foo" -e "VNC_PW=foobar" \ + +detect_interactive +docker run $interactive -p "443:$VNC_PORT" --rm -e "VNC_USER=foo" -e "VNC_PW=foobar" \ -e "VNC_PORT=$VNC_PORT" \ + -e RUN_TEST="$run_test" \ $entrypoint_executable \ kasmvnctester_barebones_${os}:$os_codename \ $entrypoint_args diff --git a/builder/test-rpm-barebones b/builder/test-rpm-barebones index 6318bda..1151222 100755 --- a/builder/test-rpm-barebones +++ b/builder/test-rpm-barebones @@ -2,17 +2,22 @@ set -e -cd "$(dirname "$0")" -. ./process_test_options.sh -. ./common.sh -os="${1:-centos}" -os_codename="${2:-core}" +cd "$(dirname "$0")/.." +. ./builder/process_test_options.sh +. ./builder/common.sh +os="${1:-oracle}" +os_codename="${2:-8}" -docker build --build-arg KASMVNC_PACKAGE_DIR="build/${os}_${os_codename}" \ +detect_build_dir +docker build --build-arg KASMVNC_PACKAGE_DIR="${build_dir}/${os}_${os_codename}" \ + --build-arg RUN_TEST="$run_test" \ -t kasmvnctester_barebones_${os}:$os_codename \ - -f dockerfile.${os}_${os_codename}.barebones.rpm.test . -docker run -it -p "443:$VNC_PORT" --rm -e "VNC_USER=foo" -e "VNC_PW=foobar" \ + -f builder/dockerfile.${os}_${os_codename}.barebones.rpm.test . + +detect_interactive +docker run $interactive -p "443:$VNC_PORT" --rm -e "VNC_USER=foo" -e "VNC_PW=foobar" \ -e "VNC_PORT=$VNC_PORT" \ + -e RUN_TEST="$run_test" \ $entrypoint_executable \ kasmvnctester_barebones_${os}:$os_codename \ $entrypoint_args diff --git a/centos/kasmvncserver.spec b/centos/kasmvncserver.spec deleted file mode 100644 index b860978..0000000 --- a/centos/kasmvncserver.spec +++ /dev/null @@ -1,158 +0,0 @@ -Name: kasmvncserver -Version: 1.3.3 -Release: 1%{?dist} -Summary: VNC server accessible from a web browser - -License: GPLv2+ -URL: https://github.com/kasmtech/KasmVNC - -BuildRequires: rsync -Requires: xorg-x11-xauth, xorg-x11-xkb-utils, xkeyboard-config, xorg-x11-server-utils, openssl, perl, perl-Switch, perl-YAML-Tiny, perl-Hash-Merge-Simple, perl-Scalar-List-Utils, perl-List-MoreUtils, perl-Try-Tiny, perl-DateTime-TimeZone -Conflicts: tigervnc-server, tigervnc-server-minimal - -%description -KasmVNC provides remote web-based access to a Desktop or application. -While VNC is in the name, KasmVNC differs from other VNC variants such -as TigerVNC, RealVNC, and TurboVNC. KasmVNC has broken from the RFB -specification which defines VNC, in order to support modern technologies -and increase security. KasmVNC is accessed by users from any modern -browser and does not support legacy VNC viewer applications. KasmVNC -uses a modern YAML based configuration at the server and user level, -allowing for ease of management. KasmVNC is maintained by Kasm -Technologies Corp, www.kasmweb.com. - -WARNING: this package requires EPEL. - -%prep - -%install -rm -rf $RPM_BUILD_ROOT - -TARGET_OS=$(lsb_release -is | tr '[:upper:]' '[:lower:]') -TARGET_OS_CODENAME=$(lsb_release -cs | tr '[:upper:]' '[:lower:]') -TARBALL=$RPM_SOURCE_DIR/kasmvnc.${TARGET_OS}_${TARGET_OS_CODENAME}.tar.gz -TAR_DATA=$(mktemp -d) -tar -xzf "$TARBALL" -C "$TAR_DATA" - -SRC=$TAR_DATA/usr/local -SRC_BIN=$SRC/bin -DESTDIR=$RPM_BUILD_ROOT -DST_MAN=$DESTDIR/usr/share/man/man1 - -mkdir -p $DESTDIR/usr/bin $DESTDIR/usr/share/man/man1 \ - $DESTDIR/usr/share/doc/kasmvncserver $DESTDIR/usr/lib \ - $DESTDIR/usr/share/perl5 $DESTDIR/etc/kasmvnc - -cp $SRC_BIN/Xvnc $DESTDIR/usr/bin; -cp $SRC_BIN/vncserver $DESTDIR/usr/bin; -cp -a $SRC_BIN/KasmVNC $DESTDIR/usr/share/perl5/ -cp $SRC_BIN/vncconfig $DESTDIR/usr/bin; -cp $SRC_BIN/kasmvncpasswd $DESTDIR/usr/bin; -cp $SRC_BIN/kasmxproxy $DESTDIR/usr/bin; -cp -r $SRC/lib/kasmvnc/ $DESTDIR/usr/lib/kasmvncserver -cd $DESTDIR/usr/bin && ln -s kasmvncpasswd vncpasswd; -cp -r $SRC/share/doc/kasmvnc*/* $DESTDIR/usr/share/doc/kasmvncserver/ -rsync -r --links --safe-links --exclude '.git*' --exclude po2js --exclude xgettext-html \ - --exclude www/utils/ --exclude .eslintrc --exclude configure \ - $SRC/share/kasmvnc $DESTDIR/usr/share - -sed -i -e 's!pem_certificate: .\+$!pem_certificate: /etc/pki/tls/private/kasmvnc.pem!' \ - $DESTDIR/usr/share/kasmvnc/kasmvnc_defaults.yaml -sed -i -e 's!pem_key: .\+$!pem_key: /etc/pki/tls/private/kasmvnc.pem!' \ - $DESTDIR/usr/share/kasmvnc/kasmvnc_defaults.yaml -sed -e 's/^\([^#]\)/# \1/' $DESTDIR/usr/share/kasmvnc/kasmvnc_defaults.yaml > \ - $DESTDIR/etc/kasmvnc/kasmvnc.yaml -cp $SRC/man/man1/Xvnc.1 $DESTDIR/usr/share/man/man1/; -cp $SRC/share/man/man1/vncserver.1 $DST_MAN; -cp $SRC/share/man/man1/vncconfig.1 $DST_MAN; -cp $SRC/share/man/man1/vncpasswd.1 $DST_MAN; -cp $SRC/share/man/man1/kasmxproxy.1 $DST_MAN; -cd $DST_MAN && ln -s vncpasswd.1 kasmvncpasswd.1; - - -%files -%config(noreplace) /etc/kasmvnc - -/usr/bin/* -/usr/lib/kasmvncserver -/usr/share/man/man1/* -/usr/share/perl5/KasmVNC -/usr/share/kasmvnc - -%license /usr/share/doc/kasmvncserver/LICENSE.TXT -%doc /usr/share/doc/kasmvncserver/README.md - -%changelog -* Fri Oct 25 2024 KasmTech - 1.3.3-1 -- Allow disabling IP blacklist -- Downloads API for detailed file downloads information -* Tue Sep 24 2024 KasmTech - 1.3.2-1 -- Disable seamless clipboard on Firefox by default, due to the Firefox overlaying a Paste menu over the canvas. -- Fixed CVE-2024-38449, directory traversal bug in built-in web server. -- Allow for larger header sizes, up to 16k. Provide better logging and handling for requests that contain HTTP headers that are larger than the 16k limit. -- Fixed memory leak in kasmproxy. -- Fixed mime types of downloads to ensure the browser interprets them as downloads. -* Tue Mar 12 2024 KasmTech - 1.3.1-1 -- Fix exception thrown on Firefox 124 and higher -- Fix artifacts on high resolution secondary screens -- Fixes for touch support on primary and secondary screens -- Fix for Oculus keyboard input -* Mon Feb 05 2024 KasmTech - 1.3.0-1 -- Multi-monitor support. -- Increased performance with watermark enabled. -- Added support for Fedora 39 and Alpine 319. -- Allow special characters in usernames. -- Better logging of client settings when client connects or changes settings. -- Add support for rotation of text-based watermark. -* Fri Aug 25 2023 KasmTech - 1.2.0-1 -- Add support for Unix relays for bidirectional communication between noVNC - and containerized applications. -- Text based watermark overlays with date and time support. -- New builds for Bookworm, Alpine 3.18, and Fedora 38. -- Multi-language support. -- Add support for rendering pixmaps via DRI3 GPU acceleration allowing - compositing and other 3d accelerated workloads in a KasmVNC session. -- Fix crash that can occur. -- Fixed tearing when compositing is enabled with DRI3 hardware acceleration. -- Fix stuck command key on MacOS clients. -* Wed Apr 05 2023 KasmTech - 1.1.0-1 -- Upstream release -* Tue Nov 29 2022 KasmTech - 1.0.0-1 -- WebRTC UDP transit support with support of STUN servers -- Lossless compression using multi-threaded WASM QOI decoder client side -- New yaml based configuration -- Significantly improved FPS through both client-side and server-side improvements. -- Support for the admin to define arbitrary http response headers for the built in web server -- Support for additional mouse buttons -- Refinement of vncserver checks and user prompts -- Added send_full_frame to developer API, forces full frame to be sent to all connected users that have at least read permission. -* Tue Mar 22 2022 KasmTech - 0.9.3~beta-1 -* Fri Feb 12 2021 KasmTech - 0.9.1~beta-1 -- Initial release of the rpm package. - -%post - kasmvnc_group="kasmvnc-cert" - - create_kasmvnc_group() { - if ! getent group "$kasmvnc_group" >/dev/null; then - groupadd --system "$kasmvnc_group" - fi - } - - make_self_signed_certificate() { - local cert_file=/etc/pki/tls/private/kasmvnc.pem - [ -f "$cert_file" ] && return 0 - - openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \ - -keyout "$cert_file" \ - -out "$cert_file" -subj \ - "/C=US/ST=VA/L=None/O=None/OU=DoFu/CN=kasm/emailAddress=none@none.none" - chgrp "$kasmvnc_group" "$cert_file" - chmod 640 "$cert_file" - } - - create_kasmvnc_group - make_self_signed_certificate - -%postun - rm -f /etc/pki/tls/private/kasmvnc.pem diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index e4489f6..1fbdc22 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -9,7 +9,7 @@ add_subdirectory(rfb) # because PIC code does not exist on that platform and MinGW complains if -fPIC # is passed (additionally, libvnc is not used on Windows.) -if(NOT WIN32) - set_target_properties(os rdr network Xregion rfb - PROPERTIES COMPILE_FLAGS -fPIC) -endif() +if (NOT WIN32) + set_target_properties(os rdr network Xregion rfb + PROPERTIES COMPILE_FLAGS -fPIC) +endif () diff --git a/common/network/jsonescape.c b/common/network/jsonescape.c index 4bb0ef8..748e9e1 100644 --- a/common/network/jsonescape.c +++ b/common/network/jsonescape.c @@ -24,30 +24,40 @@ #include "cJSON.h" void JSON_escape(const char *in, char *out) { + if (!in) + return; + for (; *in; in++) { - if (in[0] == '\b') { - *out++ = '\\'; - *out++ = 'b'; - } else if (in[0] == '\f') { - *out++ = '\\'; - *out++ = 'f'; - } else if (in[0] == '\n') { - *out++ = '\\'; - *out++ = 'n'; - } else if (in[0] == '\r') { - *out++ = '\\'; - *out++ = 'r'; - } else if (in[0] == '\t') { - *out++ = '\\'; - *out++ = 't'; - } else if (in[0] == '"') { - *out++ = '\\'; - *out++ = '"'; - } else if (in[0] == '\\') { - *out++ = '\\'; - *out++ = '\\'; - } else { - *out++ = *in; + switch (*in) { + case '\b': + *out++ = '\\'; + *out++ = 'b'; + break; + case '\f': + *out++ = '\\'; + *out++ = 'f'; + case '\n': + *out++ = '\\'; + *out++ = 'n'; + break; + case '\r': + *out++ = '\\'; + *out++ = 'r'; + break; + case '\t': + *out++ = '\\'; + *out++ = 't'; + break; + case '"': + *out++ = '\\'; + *out++ = '"'; + break; + case '\\': + *out++ = '\\'; + *out++ = '\\'; + break; + default: + *out++ = *in; } } diff --git a/common/network/websocket.c b/common/network/websocket.c index 49d3a25..fe3eb84 100644 --- a/common/network/websocket.c +++ b/common/network/websocket.c @@ -70,6 +70,8 @@ void fatal(char *msg) exit(1); } +#define WS_MAX_BUF_SIZE 4096 + // 2022-05-18 19:51:26,810 [INFO] websocket 0: 71.62.44.0 172.12.15.5 - "GET /api/get_frame_stats?client=auto HTTP/1.1" 403 2 static void weblog(const unsigned code, const unsigned websocket, const uint8_t debug, @@ -834,7 +836,7 @@ static uint8_t isValidIp(const char *str, const unsigned len) { static void dirlisting(ws_ctx_t *ws_ctx, const char fullpath[], const char path[], const char * const user, const char * const ip, const char * const origip) { - char buf[4096]; + char buf[WS_MAX_BUF_SIZE]; char enc[PATH_MAX * 3 + 1]; unsigned i; @@ -895,7 +897,7 @@ static void dirlisting(ws_ctx_t *ws_ctx, const char fullpath[], const char path[ static void servefile(ws_ctx_t *ws_ctx, const char *in, const char * const user, const char * const ip, const char * const origip) { - char buf[4096], path[4096], fullpath[4096]; + char buf[WS_MAX_BUF_SIZE], path[PATH_MAX], fullpath[PATH_MAX]; //fprintf(stderr, "http servefile input '%s'\n", in); @@ -965,7 +967,7 @@ static void servefile(ws_ctx_t *ws_ctx, const char *in, const char * const user, //fprintf(stderr, "http servefile output '%s'\n", buf); unsigned count; - while ((count = fread(buf, 1, 4096, f))) { + while ((count = fread(buf, 1, WS_MAX_BUF_SIZE, f))) { ws_send(ws_ctx, buf, count); } fclose(f); @@ -1020,7 +1022,7 @@ notfound: } static void send403(ws_ctx_t *ws_ctx, const char * const origip, const char * const ip) { - char buf[4096]; + char buf[WS_MAX_BUF_SIZE]; sprintf(buf, "HTTP/1.1 403 Forbidden\r\n" "Server: KasmVNC/4.0\r\n" "Connection: close\r\n" @@ -1034,7 +1036,7 @@ static void send403(ws_ctx_t *ws_ctx, const char * const origip, const char * co static void send400(ws_ctx_t *ws_ctx, const char * const origip, const char * const ip, const char *info) { - char buf[4096]; + char buf[WS_MAX_BUF_SIZE]; sprintf(buf, "HTTP/1.1 400 Bad Request\r\n" "Server: KasmVNC/4.0\r\n" "Connection: close\r\n" @@ -1048,7 +1050,7 @@ static void send400(ws_ctx_t *ws_ctx, const char * const origip, const char * co static uint8_t ownerapi_post(ws_ctx_t *ws_ctx, const char *in, const char * const user, const char * const ip, const char * const origip) { - char buf[4096], path[4096]; + char buf[WS_MAX_BUF_SIZE], path[PATH_MAX]; uint8_t ret = 0; // 0 = continue checking in += 5; @@ -1237,7 +1239,7 @@ nope: static uint8_t ownerapi(ws_ctx_t *ws_ctx, const char *in, const char * const user, const char * const ip, const char * const origip) { - char buf[4096], path[4096], args[4096] = "", origpath[4096]; + char buf[WS_MAX_BUF_SIZE], path[PATH_MAX], args[PATH_MAX] = "", origpath[PATH_MAX]; uint8_t ret = 0; // 0 = continue checking if (strncmp(in, "GET ", 4)) { @@ -1671,11 +1673,11 @@ static uint8_t ownerapi(ws_ctx_t *ws_ctx, const char *in, const char * const use } sprintf(buf, "HTTP/1.1 200 OK\r\n" - "Server: KasmVNC/4.0\r\n" - "Connection: close\r\n" - "Content-type: text/json\r\n" - "%s" - "\r\n", extra_headers ? extra_headers : ""); + "Server: KasmVNC/4.0\r\n" + "Connection: close\r\n" + "Content-type: text/json\r\n" + "%s" + "\r\n", extra_headers ? extra_headers : ""); ws_send(ws_ctx, buf, strlen(buf)); len = 15; @@ -1687,7 +1689,7 @@ static uint8_t ownerapi(ws_ctx_t *ws_ctx, const char *in, const char * const use if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) continue; - sprintf(path, "%s/%s", allpath, ent->d_name); + snprintf(path, PATH_MAX, "%s/%s", allpath, ent->d_name); struct stat st; if (lstat(path, &st)) continue; @@ -1709,23 +1711,35 @@ static uint8_t ownerapi(ws_ctx_t *ws_ctx, const char *in, const char * const use strcpy(grp, grpt.gr_name); } - sprintf(buf, "%s{ \"filename\": \"%s\", " - "\"date_modified\": %lu, " - "\"date_created\": %lu, " - "\"is_dir\": %s, " - "\"size\": %lu, " - "\"owner\": \"%s\", " - "\"group\": \"%s\", " - "\"perms\": \"%s\" }", - sent ? ",\n" : "", - ent->d_name, - st.st_mtime, - st.st_ctime, - S_ISDIR(st.st_mode) ? "true" : "false", - S_ISDIR(st.st_mode) ? 0 : st.st_size, - own, - grp, - perms); + sprintf(buf, "%s{ \"filename\": \"", sent ? ",\n" : ""); + ws_send(ws_ctx, buf, strlen(buf)); + len += strlen(buf); + + size_t max_out_length = 2 * strlen(ent->d_name) + 1; // worst case scenario + char *filename = malloc(max_out_length); + + JSON_escape(ent->d_name, filename); + size_t size = strlen(filename); + ws_send(ws_ctx, filename, size); + len += size; + + free(filename); + + sprintf(buf, "\", " + "\"date_modified\": %lu, " + "\"date_created\": %lu, " + "\"is_dir\": %s, " + "\"size\": %lu, " + "\"owner\": \"%s\", " + "\"group\": \"%s\", " + "\"perms\": \"%s\" }", + st.st_mtime, + st.st_ctime, + S_ISDIR(st.st_mode) ? "true" : "false", + S_ISDIR(st.st_mode) ? 0 : st.st_size, + own, + grp, + perms); sent = 1; ws_send(ws_ctx, buf, strlen(buf)); len += strlen(buf); diff --git a/common/rfb/CMakeLists.txt b/common/rfb/CMakeLists.txt index 905e76e..a55ea60 100644 --- a/common/rfb/CMakeLists.txt +++ b/common/rfb/CMakeLists.txt @@ -1,130 +1,151 @@ -include_directories(${CMAKE_SOURCE_DIR}/common ${JPEG_INCLUDE_DIR} ${PNG_INCLUDE_DIR} - ${CMAKE_SOURCE_DIR}/unix/kasmvncpasswd) - set(RFB_SOURCES - Blacklist.cxx - Congestion.cxx - CConnection.cxx - CMsgHandler.cxx - CMsgReader.cxx - CMsgWriter.cxx - CSecurityPlain.cxx - CSecurityStack.cxx - CSecurityVeNCrypt.cxx - CSecurityVncAuth.cxx - ComparingUpdateTracker.cxx - Configuration.cxx - ConnParams.cxx - CopyRectDecoder.cxx - Cursor.cxx - DecodeManager.cxx - Decoder.cxx - d3des.c - EncCache.cxx - EncodeManager.cxx - Encoder.cxx - HextileDecoder.cxx - HextileEncoder.cxx - JpegCompressor.cxx - JpegDecompressor.cxx - KeyRemapper.cxx - LogWriter.cxx - Logger.cxx - Logger_file.cxx - Logger_stdio.cxx - Password.cxx - PixelBuffer.cxx - PixelFormat.cxx - RREEncoder.cxx - RREDecoder.cxx - RawDecoder.cxx - RawEncoder.cxx - Region.cxx - SConnection.cxx - SMsgHandler.cxx - SMsgReader.cxx - SMsgWriter.cxx - ServerCore.cxx - Security.cxx - SecurityServer.cxx - SecurityClient.cxx - SelfBench.cxx - SSecurityPlain.cxx - SSecurityStack.cxx - SSecurityVncAuth.cxx - SSecurityVeNCrypt.cxx - ScaleFilters.cxx - Timer.cxx - TightDecoder.cxx - TightEncoder.cxx - TightJPEGEncoder.cxx - TightWEBPEncoder.cxx - TightQOIEncoder.cxx - UpdateTracker.cxx - VNCSConnectionST.cxx - VNCServerST.cxx - ZRLEEncoder.cxx - ZRLEDecoder.cxx - Watermark.cxx - cpuid.cxx - encodings.cxx - util.cxx - xxhash.c) + benchmark.cxx + Blacklist.cxx + Congestion.cxx + CConnection.cxx + CMsgHandler.cxx + CMsgReader.cxx + CMsgWriter.cxx + CSecurityPlain.cxx + CSecurityStack.cxx + CSecurityVeNCrypt.cxx + CSecurityVncAuth.cxx + ComparingUpdateTracker.cxx + Configuration.cxx + ConnParams.cxx + CopyRectDecoder.cxx + Cursor.cxx + DecodeManager.cxx + Decoder.cxx + d3des.c + EncCache.cxx + EncodeManager.cxx + Encoder.cxx + HextileDecoder.cxx + HextileEncoder.cxx + JpegCompressor.cxx + JpegDecompressor.cxx + KeyRemapper.cxx + LogWriter.cxx + Logger.cxx + Logger_file.cxx + Logger_stdio.cxx + Password.cxx + PixelBuffer.cxx + PixelFormat.cxx + RREEncoder.cxx + RREDecoder.cxx + RawDecoder.cxx + RawEncoder.cxx + Region.cxx + SConnection.cxx + SMsgHandler.cxx + SMsgReader.cxx + SMsgWriter.cxx + ServerCore.cxx + Security.cxx + SecurityServer.cxx + SecurityClient.cxx + SelfBench.cxx + SSecurityPlain.cxx + SSecurityStack.cxx + SSecurityVncAuth.cxx + SSecurityVeNCrypt.cxx + ScaleFilters.cxx + Timer.cxx + TightDecoder.cxx + TightEncoder.cxx + TightJPEGEncoder.cxx + TightWEBPEncoder.cxx + TightQOIEncoder.cxx + UpdateTracker.cxx + VNCSConnectionST.cxx + VNCServerST.cxx + ZRLEEncoder.cxx + ZRLEDecoder.cxx + Watermark.cxx + cpuid.cxx + encodings.cxx + util.cxx + xxhash.c + ffmpeg.cxx +) -if(UNIX) - set(RFB_SOURCES ${RFB_SOURCES} Logger_syslog.cxx) -endif() +if (UNIX) + set(RFB_SOURCES ${RFB_SOURCES} Logger_syslog.cxx) +endif () -if(WIN32) - include_directories(${CMAKE_SOURCE_DIR}/win) - set(RFB_SOURCES ${RFB_SOURCES} WinPasswdValidator.cxx) -endif(WIN32) +if (WIN32) + include_directories(${CMAKE_SOURCE_DIR}/win) + set(RFB_SOURCES ${RFB_SOURCES} WinPasswdValidator.cxx) +endif (WIN32) set(RFB_LIBRARIES ${JPEG_LIBRARIES} ${PNG_LIBRARIES} os rdr Xregion) -if(HAVE_PAM) - set(RFB_SOURCES ${RFB_SOURCES} UnixPasswordValidator.cxx - UnixPasswordValidator.h pam.c pam.h) - set(RFB_LIBRARIES ${RFB_LIBRARIES} ${PAM_LIBS}) -endif() +cmake_host_system_information(RESULT DISTRO QUERY DISTRIB_INFO) +if ((CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10) OR +(DISTRO_PLATFORM_ID MATCHES "platform:el8")) + set(RFB_LIBRARIES ${RFB_LIBRARIES} tbb) +endif () -if(GNUTLS_FOUND) - set(RFB_SOURCES - ${RFB_SOURCES} - CSecurityTLS.cxx - SSecurityTLS.cxx - ) - set(RFB_LIBRARIES - ${RFB_LIBRARIES} - ${GNUTLS_LIBRARIES} - ) -endif() +if (HAVE_PAM) + set(RFB_SOURCES + ${RFB_SOURCES} + UnixPasswordValidator.cxx + UnixPasswordValidator.h pam.c pam.h) + set(RFB_LIBRARIES ${RFB_LIBRARIES} ${PAM_LIBS}) +endif () + +if (GNUTLS_FOUND) + set(RFB_SOURCES + ${RFB_SOURCES} + CSecurityTLS.cxx + SSecurityTLS.cxx + ) + set(RFB_LIBRARIES + ${RFB_LIBRARIES} + ${GNUTLS_LIBRARIES} + ) +endif () # SSE2 set(SSE2_SOURCES - scale_sse2.cxx) + scale_sse2.cxx) set(SCALE_DUMMY_SOURCES - scale_dummy.cxx) + scale_dummy.cxx) -if(COMPILER_SUPPORTS_SSE2) - set_source_files_properties(${SSE2_SOURCES} PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS} -msse2) - set(RFB_SOURCES - ${RFB_SOURCES} - ${SSE2_SOURCES} - ) -else() - set(RFB_SOURCES - ${RFB_SOURCES} - ${SCALE_DUMMY_SOURCES} - ) -endif() +if (COMPILER_SUPPORTS_SSE2) + set_source_files_properties(${SSE2_SOURCES} PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS} -msse2) + set(RFB_SOURCES + ${RFB_SOURCES} + ${SSE2_SOURCES} + ) +else () + set(RFB_SOURCES + ${RFB_SOURCES} + ${SCALE_DUMMY_SOURCES} + ) +endif () + +find_package(PkgConfig REQUIRED) + +pkg_check_modules(FFMPEG REQUIRED libavcodec libavformat libavutil libswscale) add_library(rfb STATIC ${RFB_SOURCES}) -target_link_libraries(rfb ${RFB_LIBRARIES}) +target_include_directories(rfb PRIVATE + ${CMAKE_SOURCE_DIR}/common + ${JPEG_INCLUDE_DIR} + ${PNG_INCLUDE_DIR} + ${CMAKE_SOURCE_DIR}/unix/kasmvncpasswd + ${CMAKE_SOURCE_DIR}/third_party/tinyxml2 + ${FFMPEG_INCLUDE_DIRS} +) -if(UNIX) - libtool_create_control_file(rfb) -endif() +target_link_libraries(rfb PRIVATE ${RFB_LIBRARIES} tinyxml2_objs) + +if (UNIX) + libtool_create_control_file(rfb) +endif () diff --git a/common/rfb/EncodeManager.cxx b/common/rfb/EncodeManager.cxx index 1cbd32d..5dbf1dc 100644 --- a/common/rfb/EncodeManager.cxx +++ b/common/rfb/EncodeManager.cxx @@ -19,8 +19,7 @@ * USA. */ -#include -#include +#include #include #include @@ -44,6 +43,7 @@ #include #include #include +#include using namespace rfb; @@ -93,9 +93,9 @@ struct RectInfo { }; struct QualityInfo { - struct timeval lastUpdate; + struct timeval lastUpdate{}; Rect rect; - unsigned score; + unsigned score{}; }; }; @@ -360,7 +360,6 @@ void EncodeManager::doUpdate(bool allowLossy, const Region& changed_, int nRects; Region changed, cursorRegion; struct timeval start; - unsigned screenArea; updates++; if (conn->cp.supportsUdp) @@ -390,16 +389,7 @@ void EncodeManager::doUpdate(bool allowLossy, const Region& changed_, memset(&webpstats, 0, sizeof(codecstats_t)); if (allowLossy && activeEncoders[encoderFullColour] == encoderTightWEBP) { - const unsigned rate = 1024 * 1000 / rfb::Server::frameRate; - - screenArea = pb->getRect().width() * pb->getRect().height(); - screenArea *= 1024; - screenArea /= 256 * 256; - screenArea *= webpBenchResult; - // Encoding the entire screen would take this many 1024*msecs, worst case - - // Calculate how many us we can send webp for, before switching to jpeg - webpFallbackUs = rate * rate / screenArea; + webpFallbackUs = (1000 * 1000 / rfb::Server::frameRate) * (static_cast(Server::webpEncodingTime) / 100.0); } /* @@ -880,14 +870,12 @@ void EncodeManager::findSolidRect(const Rect& rect, Region *changed, } } -void EncodeManager::checkWebpFallback(const struct timeval *start) { +void EncodeManager::checkWebpFallback(const timeval *start) { // Have we taken too long for the frame? If so, drop from WEBP to JPEG - if (start && activeEncoders[encoderFullColour] == encoderTightWEBP && !webpTookTooLong) { - unsigned us; - us = msSince(start) * 1024; + if (start && activeEncoders[encoderFullColour] == encoderTightWEBP && !webpTookTooLong.load(std::memory_order_relaxed)) { + const auto us = msSince(start) * 1000; if (us > webpFallbackUs) - #pragma omp atomic - webpTookTooLong |= true; + webpTookTooLong.store(true, std::memory_order_relaxed); } } @@ -1122,18 +1110,13 @@ void EncodeManager::writeRects(const Region& changed, const PixelBuffer* pb, const bool mainScreen) { std::vector rects, subrects, scaledrects; - std::vector::const_iterator rect; std::vector encoderTypes; std::vector isWebp, fromCache; std::vector palettes; std::vector > compresseds; std::vector ms; - uint32_t i; - if (rfb::Server::rectThreads > 0) - omp_set_num_threads(rfb::Server::rectThreads); - - webpTookTooLong = false; + webpTookTooLong.store(false, std::memory_order_relaxed); changed.get_rects(&rects); // Update stats @@ -1148,18 +1131,18 @@ void EncodeManager::writeRects(const Region& changed, const PixelBuffer* pb, subrects.reserve(rects.size() * 1.5f); - for (rect = rects.begin(); rect != rects.end(); ++rect) { - int w, h, sw, sh; + for (const auto& rect : rects) { + int sw, sh; Rect sr; - w = rect->width(); - h = rect->height(); + const auto w = rect.width(); + const auto h = rect.height(); // No split necessary? if ((((w*h) < SubRectMaxArea) && (w < SubRectMaxWidth)) || (videoDetected && !encoders[encoderTightWEBP]->isSupported())) { - subrects.push_back(*rect); - trackRectQuality(*rect); + subrects.push_back(rect); + trackRectQuality(rect); continue; } @@ -1170,15 +1153,15 @@ void EncodeManager::writeRects(const Region& changed, const PixelBuffer* pb, sh = SubRectMaxArea / sw; - for (sr.tl.y = rect->tl.y; sr.tl.y < rect->br.y; sr.tl.y += sh) { + for (sr.tl.y = rect.tl.y; sr.tl.y < rect.br.y; sr.tl.y += sh) { sr.br.y = sr.tl.y + sh; - if (sr.br.y > rect->br.y) - sr.br.y = rect->br.y; + if (sr.br.y > rect.br.y) + sr.br.y = rect.br.y; - for (sr.tl.x = rect->tl.x; sr.tl.x < rect->br.x; sr.tl.x += sw) { + for (sr.tl.x = rect.tl.x; sr.tl.x < rect.br.x; sr.tl.x += sw) { sr.br.x = sr.tl.x + sw; - if (sr.br.x > rect->br.x) - sr.br.x = rect->br.x; + if (sr.br.x > rect.br.x) + sr.br.x = rect.br.x; subrects.push_back(sr); trackRectQuality(sr); @@ -1186,13 +1169,18 @@ void EncodeManager::writeRects(const Region& changed, const PixelBuffer* pb, } } - encoderTypes.resize(subrects.size()); - isWebp.resize(subrects.size()); - fromCache.resize(subrects.size()); - palettes.resize(subrects.size()); - compresseds.resize(subrects.size()); - scaledrects.resize(subrects.size()); - ms.resize(subrects.size()); + const size_t subrects_size = subrects.size(); + + std::vector indices(subrects_size); + std::iota(std::begin(indices), std::end(indices), 0); + + encoderTypes.resize(subrects_size); + isWebp.resize(subrects_size); + fromCache.resize(subrects_size); + palettes.resize(subrects_size); + compresseds.resize(subrects_size); + scaledrects.resize(subrects_size); + ms.resize(subrects_size); // In case the current resolution is above the max video res, and video was detected, // scale to that res, keeping aspect ratio @@ -1224,7 +1212,7 @@ void EncodeManager::writeRects(const Region& changed, const PixelBuffer* pb, break; } - for (i = 0; i < subrects.size(); ++i) { + for (uint32_t i = 0; i < subrects_size; ++i) { const Rect old = scaledrects[i] = subrects[i]; scaledrects[i].br.x *= diff; scaledrects[i].br.y *= diff; @@ -1249,15 +1237,15 @@ void EncodeManager::writeRects(const Region& changed, const PixelBuffer* pb, } scalingTime = msSince(&scalestart); - #pragma omp parallel for schedule(dynamic, 1) - for (i = 0; i < subrects.size(); ++i) { - encoderTypes[i] = getEncoderType(subrects[i], pb, &palettes[i], compresseds[i], - &isWebp[i], &fromCache[i], - scaledpb, scaledrects[i], ms[i]); - checkWebpFallback(start); - } + std::for_each(std::execution::par_unseq, std::begin(indices), std::end(indices), [&](size_t i) + { + encoderTypes[i] = getEncoderType(subrects[i], pb, &palettes[i], compresseds[i], + &isWebp[i], &fromCache[i], + scaledpb, scaledrects[i], ms[i]); + checkWebpFallback(start); + }); - for (i = 0; i < subrects.size(); ++i) { + for (uint32_t i = 0; i < subrects_size; ++i) { if (encoderTypes[i] == encoderFullColour) { if (isWebp[i]) webpstats.ms += ms[i]; @@ -1269,7 +1257,7 @@ void EncodeManager::writeRects(const Region& changed, const PixelBuffer* pb, if (start) { encodingTime = msSince(start); - if (vlog.getLevel() >= vlog.LEVEL_DEBUG) { + if (vlog.getLevel() >= rfb::LogWriter::LEVEL_DEBUG) { framesSinceEncPrint++; if (maxEncodingTime < encodingTime) maxEncodingTime = encodingTime; @@ -1284,11 +1272,11 @@ void EncodeManager::writeRects(const Region& changed, const PixelBuffer* pb, } } - if (webpTookTooLong) + if (webpTookTooLong.load(std::memory_order_relaxed)) activeEncoders[encoderFullColour] = encoderTightJPEG; - for (i = 0; i < subrects.size(); ++i) { - if (encCache->enabled && compresseds[i].size() && !fromCache[i] && + for (uint32_t i = 0; i < subrects_size; ++i) { + if (encCache->enabled && !compresseds[i].empty() && !fromCache[i] && !encoders[encoderTightQOI]->isSupported()) { void *tmp = malloc(compresseds[i].size()); memcpy(tmp, &compresseds[i][0], compresseds[i].size()); diff --git a/common/rfb/EncodeManager.h b/common/rfb/EncodeManager.h index 20ba837..7844faa 100644 --- a/common/rfb/EncodeManager.h +++ b/common/rfb/EncodeManager.h @@ -29,9 +29,9 @@ #include #include #include -#include #include +#include #include namespace rfb { @@ -50,7 +50,7 @@ namespace rfb { class EncodeManager: public Timer::Callback { public: EncodeManager(SConnection* conn, EncCache *encCache); - ~EncodeManager(); + ~EncodeManager() override; void logStats(); @@ -72,10 +72,10 @@ namespace rfb { encodingTime = 0; }; - unsigned getEncodingTime() const { + [[nodiscard]] unsigned getEncodingTime() const { return encodingTime; }; - unsigned getScalingTime() const { + [[nodiscard]] unsigned getScalingTime() const { return scalingTime; }; @@ -124,7 +124,8 @@ namespace rfb { uint8_t *fromCache, const PixelBuffer *scaledpb, const Rect& scaledrect, uint32_t &ms) const; - virtual bool handleTimeout(Timer* t); + + bool handleTimeout(Timer* t) override; bool checkSolidTile(const Rect& r, const rdr::U8* colourValue, const PixelBuffer *pb); @@ -199,7 +200,7 @@ namespace rfb { size_t curMaxUpdateSize; unsigned webpFallbackUs; unsigned webpBenchResult; - bool webpTookTooLong; + std::atomic webpTookTooLong{false}; unsigned encodingTime; unsigned maxEncodingTime, framesSinceEncPrint; unsigned scalingTime; @@ -208,14 +209,14 @@ namespace rfb { class OffsetPixelBuffer : public FullFramePixelBuffer { public: - OffsetPixelBuffer() {} - virtual ~OffsetPixelBuffer() {} + OffsetPixelBuffer() = default; + ~OffsetPixelBuffer() override = default; void update(const PixelFormat& pf, int width, int height, const rdr::U8* data_, int stride); private: - virtual rdr::U8* getBufferRW(const Rect& r, int* stride); + rdr::U8* getBufferRW(const Rect& r, int* stride) override; }; }; diff --git a/common/rfb/SConnection.cxx b/common/rfb/SConnection.cxx index f0e6be5..4546742 100644 --- a/common/rfb/SConnection.cxx +++ b/common/rfb/SConnection.cxx @@ -20,8 +20,6 @@ #include #include #include -#include -#include #include #include #include @@ -274,19 +272,16 @@ void SConnection::writeConnFailedFromScratch(const char* msg, os->flush(); } -void SConnection::setEncodings(int nEncodings, const rdr::S32* encodings) -{ - int i; - - preferredEncoding = encodingRaw; - for (i = 0;i < nEncodings;i++) { - if (EncodeManager::supported(encodings[i])) { - preferredEncoding = encodings[i]; - break; +void SConnection::setEncodings(int nEncodings, const rdr::S32 *encodings) { + preferredEncoding = encodingRaw; + for (int i = 0; i < nEncodings; i++) { + if (EncodeManager::supported(encodings[i])) { + preferredEncoding = encodings[i]; + break; + } } - } - SMsgHandler::setEncodings(nEncodings, encodings); + SMsgHandler::setEncodings(nEncodings, encodings); } void SConnection::clearBinaryClipboard() diff --git a/common/rfb/SelfBench.cxx b/common/rfb/SelfBench.cxx index fd224d3..62c0cac 100644 --- a/common/rfb/SelfBench.cxx +++ b/common/rfb/SelfBench.cxx @@ -26,27 +26,32 @@ #include #include #include -#include -#include +#include +#include +#include +#include using namespace rfb; static LogWriter vlog("SelfBench"); static const PixelFormat pfRGBX(32, 24, false, true, 255, 255, 255, 0, 8, 16); -#define RUNS 64 +static constexpr uint32_t RUNS = 64; -#define W 1600 -#define H 1200 +static constexpr uint32_t WIDTH = 1600; +static constexpr uint32_t HEIGHT = 1200; void SelfBench() { + tinyxml2::XMLDocument doc; - unsigned i, runs; - struct timeval start; + auto *test_suit = doc.NewElement("testsuite"); + test_suit->SetAttribute("name", "SelfBench"); - ManagedPixelBuffer f1(pfRGBX, W, H); - ManagedPixelBuffer f2(pfRGBX, W, H); - ManagedPixelBuffer screen(pfRGBX, W, H); + doc.InsertFirstChild(test_suit); + + ManagedPixelBuffer f1(pfRGBX, WIDTH, HEIGHT); + ManagedPixelBuffer f2(pfRGBX, WIDTH, HEIGHT); + ManagedPixelBuffer screen(pfRGBX, WIDTH, HEIGHT); int stride; rdr::U8 *f1ptr = f1.getBufferRW(f1.getRect(), &stride); @@ -56,7 +61,7 @@ void SelfBench() { rdr::U8 * const f1orig = f1ptr; rdr::U8 * const f2orig = f2ptr; - for (i = 0; i < W * H * 4; i += 4) { + for (uint32_t i = 0; i < WIDTH * HEIGHT * 4; i += 4) { f1ptr[0] = rand(); f1ptr[1] = rand(); f1ptr[2] = rand(); @@ -74,124 +79,116 @@ void SelfBench() { // Encoding std::vector vec; - TightJPEGEncoder jpeg(NULL); + TightJPEGEncoder jpeg(nullptr); - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { + uint32_t test_cases {}; + uint64_t total_time {}; + + auto benchmark = [&doc, &test_suit, &test_cases, &total_time](const char *name, uint32_t runs, auto func) { + auto now = std::chrono::high_resolution_clock::now(); + for (uint32_t i = 0; i < runs; i++) { + func(i); + } + + ++test_cases; + auto value = elapsedMs(now); + double junit_value = value / 1000.; + total_time += value; + + vlog.info("%s took %lu ms (%u runs)", name, value, runs); + auto *test_case = doc.NewElement("testcase"); + test_case->SetAttribute("name", name); + test_case->SetAttribute("time", junit_value); + test_case->SetAttribute("runs", runs); + test_case->SetAttribute("classname", "KasmVNC"); + test_suit->InsertEndChild(test_case); + }; + + benchmark("Jpeg compression at quality 8", RUNS, [&jpeg, &vec, &f1](uint32_t) { jpeg.compressOnly(&f1, 8, vec, false); - } - vlog.info("Jpeg compression at quality 8 took %u ms (%u runs)", msSince(&start), runs); + }); - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { + benchmark("Jpeg compression at quality 4", RUNS, [&jpeg, &vec, &f1](uint32_t) { jpeg.compressOnly(&f1, 4, vec, false); - } - vlog.info("Jpeg compression at quality 4 took %u ms (%u runs)", msSince(&start), runs); + }); + TightWEBPEncoder webp(nullptr); - TightWEBPEncoder webp(NULL); - - gettimeofday(&start, NULL); - runs = RUNS / 8; - for (i = 0; i < runs; i++) { + benchmark("Webp compression at quality 8", RUNS / 8, [&webp,&f1, &vec](uint32_t) { webp.compressOnly(&f1, 8, vec, false); - } - vlog.info("Webp compression at quality 8 took %u ms (%u runs)", msSince(&start), runs); + }); - gettimeofday(&start, NULL); - runs = RUNS / 4; - for (i = 0; i < runs; i++) { + benchmark("Webp compression at quality 4", RUNS / 4, [&webp, &f1, &vec](uint32_t) { webp.compressOnly(&f1, 4, vec, false); - } - vlog.info("Webp compression at quality 4 took %u ms (%u runs)", msSince(&start), runs); + }); // Scaling - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { - PixelBuffer *pb = nearestScale(&f1, W * 0.8, H * 0.8, 0.8); + benchmark("Nearest scaling to 80%", RUNS, [&f1](uint32_t) { + PixelBuffer *pb = nearestScale(&f1, WIDTH * 0.8, HEIGHT * 0.8, 0.8); delete pb; - } - vlog.info("Nearest scaling to 80%% took %u ms (%u runs)", msSince(&start), runs); + }); - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { - PixelBuffer *pb = nearestScale(&f1, W * 0.4, H * 0.4, 0.4); + benchmark("Nearest scaling to 40%", RUNS, [&f1](uint32_t) { + PixelBuffer *pb = nearestScale(&f1, WIDTH * 0.4, HEIGHT * 0.4, 0.4); delete pb; - } - vlog.info("Nearest scaling to 40%% took %u ms (%u runs)", msSince(&start), runs); + }); - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { - PixelBuffer *pb = bilinearScale(&f1, W * 0.8, H * 0.8, 0.8); + benchmark("Bilinear scaling to 80%", RUNS, [&f1](uint32_t) { + PixelBuffer *pb = bilinearScale(&f1, WIDTH * 0.8, HEIGHT * 0.8, 0.8); delete pb; - } - vlog.info("Bilinear scaling to 80%% took %u ms (%u runs)", msSince(&start), runs); + }); - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { - PixelBuffer *pb = bilinearScale(&f1, W * 0.4, H * 0.4, 0.4); + benchmark("Bilinear scaling to 40%", RUNS, [&f1](uint32_t) { + PixelBuffer *pb = bilinearScale(&f1, WIDTH * 0.4, HEIGHT * 0.4, 0.4); delete pb; - } - vlog.info("Bilinear scaling to 40%% took %u ms (%u runs)", msSince(&start), runs); + }); - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { - PixelBuffer *pb = progressiveBilinearScale(&f1, W * 0.8, H * 0.8, 0.8); - delete pb; - } - vlog.info("Progressive bilinear scaling to 80%% took %u ms (%u runs)", msSince(&start), runs); - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { - PixelBuffer *pb = progressiveBilinearScale(&f1, W * 0.4, H * 0.4, 0.4); + benchmark("Progressive bilinear scaling to 80%", RUNS, [&f1](uint32_t) { + PixelBuffer *pb = progressiveBilinearScale(&f1, WIDTH * 0.8, HEIGHT * 0.8, 0.8); delete pb; - } - vlog.info("Progressive bilinear scaling to 40%% took %u ms (%u runs)", msSince(&start), runs); + }); + benchmark("Progressive bilinear scaling to 40%", RUNS, [&f1](uint32_t) { + PixelBuffer *pb = progressiveBilinearScale(&f1, WIDTH * 0.4, HEIGHT * 0.4, 0.4); + delete pb; + }); // Analysis - ComparingUpdateTracker *comparer = new ComparingUpdateTracker(&screen); + auto *comparer = new ComparingUpdateTracker(&screen); Region cursorReg; Server::detectScrolling.setParam(false); Server::detectHorizontal.setParam(false); - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { - memcpy(screenptr, i % 2 ? f1orig : f2orig, W * H * 4); - comparer->compare(true, cursorReg); - } - vlog.info("Analysis took %u ms (%u runs) (incl. memcpy overhead)", msSince(&start), runs); + benchmark("Analysis (incl. memcpy overhead)", RUNS, + [&screenptr, &comparer, &cursorReg, f1orig, f2orig](uint32_t i) { + memcpy(screenptr, i % 2 ? f1orig : f2orig, WIDTH * HEIGHT * 4); + comparer->compare(true, cursorReg); + }); Server::detectScrolling.setParam(true); - gettimeofday(&start, NULL); - runs = RUNS; - for (i = 0; i < runs; i++) { - memcpy(screenptr, i % 2 ? f1orig : f2orig, W * H * 4); - comparer->compare(false, cursorReg); - } - vlog.info("Analysis w/ scroll detection took %u ms (%u runs) (incl. memcpy overhead)", msSince(&start), runs); + benchmark("Analysis w/ scroll detection (incl. memcpy overhead)", RUNS, + [&screenptr, &comparer, &cursorReg, f1orig, f2orig](uint32_t i) { + memcpy(screenptr, i % 2 ? f1orig : f2orig, WIDTH * HEIGHT * 4); + comparer->compare(false, cursorReg); + }); Server::detectHorizontal.setParam(true); delete comparer; comparer = new ComparingUpdateTracker(&screen); - gettimeofday(&start, NULL); - runs = RUNS / 2; - for (i = 0; i < runs; i++) { - memcpy(screenptr, i % 2 ? f1orig : f2orig, W * H * 4); - comparer->compare(false, cursorReg); - } - vlog.info("Analysis w/ horizontal scroll detection took %u ms (%u runs) (incl. memcpy overhead)", msSince(&start), runs); + benchmark("Analysis w/ horizontal scroll detection (incl. memcpy overhead)", RUNS / 2, + [&screenptr, &comparer, &cursorReg, f1orig, f2orig](uint32_t i) { + memcpy(screenptr, i % 2 ? f1orig : f2orig, WIDTH * HEIGHT * 4); + comparer->compare(false, cursorReg); + }); + + test_suit->SetAttribute("tests", test_cases); + test_suit->SetAttribute("failures", 0); + test_suit->SetAttribute("time", total_time); + + doc.SaveFile("SelfBench.xml"); exit(0); } diff --git a/common/rfb/ServerCore.cxx b/common/rfb/ServerCore.cxx index 03a69d9..22f872c 100644 --- a/common/rfb/ServerCore.cxx +++ b/common/rfb/ServerCore.cxx @@ -32,15 +32,15 @@ rfb::IntParameter rfb::Server::idleTimeout 0, 0); rfb::IntParameter rfb::Server::maxDisconnectionTime ("MaxDisconnectionTime", - "Terminate when no client has been connected for s seconds", + "Terminate when no client has been connected for s seconds", 0, 0); rfb::IntParameter rfb::Server::maxConnectionTime ("MaxConnectionTime", - "Terminate when a client has been connected for s seconds", + "Terminate when a client has been connected for s seconds", 0, 0); rfb::IntParameter rfb::Server::maxIdleTime ("MaxIdleTime", - "Terminate after s seconds of user inactivity", + "Terminate after s seconds of user inactivity", 0, 0); rfb::IntParameter rfb::Server::clientWaitTimeMillis ("ClientWaitTimeMillis", @@ -117,6 +117,16 @@ rfb::BoolParameter rfb::Server::selfBench ("SelfBench", "Run self-benchmarks and exit.", false); +rfb::StringParameter rfb::Server::benchmark( + "Benchmark", + "Run extended benchmarks and exit.", + ""); + +rfb::StringParameter rfb::Server::benchmarkResults( + "BenchmarkResults", + "The file to save becnhmark results to.", + "Benchmark.xml"); + rfb::IntParameter rfb::Server::dynamicQualityMin ("DynamicQualityMin", "The minimum dynamic JPEG quality, 0 = low, 9 = high", @@ -271,19 +281,24 @@ rfb::IntParameter rfb::Server::udpFullFrameFrequency ("udpFullFrameFrequency", "Send a full frame every N frames for clients using UDP. 0 to disable", 0, 0, 1000); - + rfb::IntParameter rfb::Server::udpPort ("udpPort", "Which port to use for UDP. Default same as websocket", 0, 0, 65535); static void bandwidthPreset() { - rfb::Server::dynamicQualityMin.setParam(2); - rfb::Server::dynamicQualityMax.setParam(9); - rfb::Server::treatLossless.setParam(8); + rfb::Server::dynamicQualityMin.setParam(2); + rfb::Server::dynamicQualityMax.setParam(9); + rfb::Server::treatLossless.setParam(8); } rfb::PresetParameter rfb::Server::preferBandwidth ("PreferBandwidth", "Set various options for lower bandwidth use. The default is off, aka to prefer quality.", false, bandwidthPreset); + +rfb::IntParameter rfb::Server::webpEncodingTime +("webpEncodingTime", + "Percentage of time allotted for encoding a frame, that can be used for encoding rects in webp.", + 30, 0, 100); diff --git a/common/rfb/ServerCore.h b/common/rfb/ServerCore.h index 2035cf6..82a06b5 100644 --- a/common/rfb/ServerCore.h +++ b/common/rfb/ServerCore.h @@ -28,72 +28,71 @@ #include namespace rfb { - - class Server { - public: - - static IntParameter idleTimeout; - static IntParameter maxDisconnectionTime; - static IntParameter maxConnectionTime; - static IntParameter maxIdleTime; - static IntParameter clientWaitTimeMillis; - static IntParameter compareFB; - static IntParameter frameRate; - static IntParameter dynamicQualityMin; - static IntParameter dynamicQualityMax; - static IntParameter treatLossless; - static IntParameter scrollDetectLimit; - static IntParameter rectThreads; - static IntParameter DLP_ClipSendMax; - static IntParameter DLP_ClipAcceptMax; - static IntParameter DLP_ClipDelay; - static IntParameter DLP_KeyRateLimit; - static IntParameter DLP_WatermarkRepeatSpace; - static IntParameter DLP_WatermarkFontSize; - static IntParameter DLP_WatermarkTimeOffset; - static IntParameter DLP_WatermarkTimeOffsetMinutes; - static IntParameter DLP_WatermarkTextAngle; - static StringParameter DLP_ClipLog; - static StringParameter DLP_Region; - static StringParameter DLP_Clip_Types; - static StringParameter DLP_WatermarkImage; - static StringParameter DLP_WatermarkLocation; - static StringParameter DLP_WatermarkTint; - static StringParameter DLP_WatermarkText; - static StringParameter DLP_WatermarkFont; - static BoolParameter DLP_RegionAllowClick; - static BoolParameter DLP_RegionAllowRelease; - static IntParameter jpegVideoQuality; - static IntParameter webpVideoQuality; - static StringParameter maxVideoResolution; - static IntParameter videoTime; - static IntParameter videoOutTime; - static IntParameter videoArea; - static IntParameter videoScaling; - static IntParameter udpFullFrameFrequency; - static IntParameter udpPort; - static StringParameter kasmPasswordFile; - static StringParameter publicIP; - static StringParameter stunServer; - static BoolParameter printVideoArea; - static BoolParameter protocol3_3; - static BoolParameter alwaysShared; - static BoolParameter neverShared; - static BoolParameter disconnectClients; - static BoolParameter acceptKeyEvents; - static BoolParameter acceptPointerEvents; - static BoolParameter acceptCutText; - static BoolParameter sendCutText; - static BoolParameter acceptSetDesktopSize; - static BoolParameter queryConnect; - static BoolParameter detectScrolling; - static BoolParameter detectHorizontal; - static BoolParameter ignoreClientSettingsKasm; - static BoolParameter selfBench; - static PresetParameter preferBandwidth; - }; - + class Server { + public: + static IntParameter idleTimeout; + static IntParameter maxDisconnectionTime; + static IntParameter maxConnectionTime; + static IntParameter maxIdleTime; + static IntParameter clientWaitTimeMillis; + static IntParameter compareFB; + static IntParameter frameRate; + static IntParameter dynamicQualityMin; + static IntParameter dynamicQualityMax; + static IntParameter treatLossless; + static IntParameter scrollDetectLimit; + static IntParameter rectThreads; + static IntParameter DLP_ClipSendMax; + static IntParameter DLP_ClipAcceptMax; + static IntParameter DLP_ClipDelay; + static IntParameter DLP_KeyRateLimit; + static IntParameter DLP_WatermarkRepeatSpace; + static IntParameter DLP_WatermarkFontSize; + static IntParameter DLP_WatermarkTimeOffset; + static IntParameter DLP_WatermarkTimeOffsetMinutes; + static IntParameter DLP_WatermarkTextAngle; + static StringParameter DLP_ClipLog; + static StringParameter DLP_Region; + static StringParameter DLP_Clip_Types; + static StringParameter DLP_WatermarkImage; + static StringParameter DLP_WatermarkLocation; + static StringParameter DLP_WatermarkTint; + static StringParameter DLP_WatermarkText; + static StringParameter DLP_WatermarkFont; + static BoolParameter DLP_RegionAllowClick; + static BoolParameter DLP_RegionAllowRelease; + static IntParameter jpegVideoQuality; + static IntParameter webpVideoQuality; + static StringParameter maxVideoResolution; + static IntParameter videoTime; + static IntParameter videoOutTime; + static IntParameter videoArea; + static IntParameter videoScaling; + static IntParameter udpFullFrameFrequency; + static IntParameter udpPort; + static StringParameter kasmPasswordFile; + static StringParameter publicIP; + static StringParameter stunServer; + static BoolParameter printVideoArea; + static BoolParameter protocol3_3; + static BoolParameter alwaysShared; + static BoolParameter neverShared; + static BoolParameter disconnectClients; + static BoolParameter acceptKeyEvents; + static BoolParameter acceptPointerEvents; + static BoolParameter acceptCutText; + static BoolParameter sendCutText; + static BoolParameter acceptSetDesktopSize; + static BoolParameter queryConnect; + static BoolParameter detectScrolling; + static BoolParameter detectHorizontal; + static BoolParameter ignoreClientSettingsKasm; + static BoolParameter selfBench; + static StringParameter benchmark; + static StringParameter benchmarkResults; + static PresetParameter preferBandwidth; + static IntParameter webpEncodingTime; + }; }; -#endif // __RFB_SERVER_CORE_H__ - +#endif // __RFB_SERVER_CORE_H__ \ No newline at end of file diff --git a/common/rfb/TightJPEGEncoder.cxx b/common/rfb/TightJPEGEncoder.cxx index 8041b76..8311725 100644 --- a/common/rfb/TightJPEGEncoder.cxx +++ b/common/rfb/TightJPEGEncoder.cxx @@ -25,6 +25,12 @@ #include #include +#include +#include +#include +#include + + using namespace rfb; struct TightJPEGConfiguration { diff --git a/common/rfb/TightWEBPEncoder.cxx b/common/rfb/TightWEBPEncoder.cxx index 89d9b3a..2e79c6c 100644 --- a/common/rfb/TightWEBPEncoder.cxx +++ b/common/rfb/TightWEBPEncoder.cxx @@ -259,13 +259,14 @@ void TightWEBPEncoder::writeRect(const PixelBuffer* pb, const Palette& palette) WebPMemoryWriterClear(&wrt); } -// How many milliseconds would it take to encode a 256x256 block at quality 8 +// How many milliseconds would it take to encode a 256x256 block at quality 5 rdr::U32 TightWEBPEncoder::benchmark() const { rdr::U8* buffer; struct timeval start; int stride, i; - const uint8_t quality = 8, method = 2; + // the minimum WebP quality settings used in KasmVNC + const uint8_t quality = 5, method = 0; WebPConfig cfg; WebPPicture pic; WebPMemoryWriter wrt; diff --git a/common/rfb/VNCSConnectionST.cxx b/common/rfb/VNCSConnectionST.cxx index c470de7..ed36bdc 100644 --- a/common/rfb/VNCSConnectionST.cxx +++ b/common/rfb/VNCSConnectionST.cxx @@ -62,7 +62,7 @@ VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s, losslessTimer(this), kbdLogTimer(this), binclipTimer(this), server(server_), updates(false), updateRenderedCursor(false), removeRenderedCursor(false), - continuousUpdates(false), encodeManager(this, &server_->encCache), + continuousUpdates(false), encodeManager(this, &VNCServerST::encCache), needsPermCheck(false), pointerEventTime(0), clientHasCursor(false), accessRights(AccessDefault), startTime(time(0)), frameTracking(false), diff --git a/common/rfb/VNCServerST.cxx b/common/rfb/VNCServerST.cxx index a84ebc9..94979a9 100644 --- a/common/rfb/VNCServerST.cxx +++ b/common/rfb/VNCServerST.cxx @@ -48,8 +48,8 @@ // otherwise blacklisted connections might be "forgotten". -#include -#include +#include +#include #include #include @@ -73,6 +73,8 @@ #include #include #include +#include +#include using namespace rfb; @@ -82,6 +84,8 @@ EncCache VNCServerST::encCache; void SelfBench(); +void benchmark(std::string_view, std::string_view); + // // -=- VNCServerST Implementation // @@ -128,13 +132,13 @@ static void parseRegionPart(const bool percents, rdr::U16 &pcdest, int &dest, VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_) : blHosts(&blacklist), desktop(desktop_), desktopStarted(false), - blockCounter(0), pb(0), blackedpb(0), ledState(ledUnknown), - name(strDup(name_)), pointerClient(0), clipboardClient(0), - comparer(0), cursor(new Cursor(0, 0, Point(), NULL)), + blockCounter(0), pb(nullptr), blackedpb(nullptr), ledState(ledUnknown), + name(strDup(name_)), pointerClient(nullptr), clipboardClient(nullptr), + comparer(nullptr), cursor(new Cursor(0, 0, Point(), nullptr)), renderedCursorInvalid(false), - queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance), + queryConnectionHandler(nullptr), keyRemapper(&KeyRemapper::defInstance), lastConnectionTime(0), disableclients(false), - frameTimer(this), apimessager(NULL), trackingFrameStats(0), + frameTimer(this), apimessager(nullptr), trackingFrameStats(0), clipboardId(0), sendWatermark(false) { lastUserInputTime = lastDisconnectTime = time(0); @@ -198,7 +202,7 @@ VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_) } } - DLPRegion.enabled = 1; + DLPRegion.enabled = true; } kasmpasswdpath[0] = '\0'; @@ -223,11 +227,18 @@ VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_) trackingClient[0] = 0; - if (watermarkData) - sendWatermark = true; + if (watermarkData) + sendWatermark = true; - if (Server::selfBench) - SelfBench(); + if (Server::selfBench) + SelfBench(); + + if (Server::benchmark[0]) { + auto *file_name = Server::benchmark.getValueStr(); + if (!std::filesystem::exists(file_name)) + throw Exception("Benchmarking video file does not exist"); + benchmark(file_name, Server::benchmarkResults.getValueStr()); + } } VNCServerST::~VNCServerST() diff --git a/common/rfb/VNCServerST.h b/common/rfb/VNCServerST.h index 721e028..fee2517 100644 --- a/common/rfb/VNCServerST.h +++ b/common/rfb/VNCServerST.h @@ -148,7 +148,7 @@ namespace rfb { // the connection. enum queryResult { ACCEPT, REJECT, PENDING }; struct QueryConnectionHandler { - virtual ~QueryConnectionHandler() {} + virtual ~QueryConnectionHandler() = default; virtual queryResult queryConnection(network::Socket* sock, const char* userName, char** reason) = 0; diff --git a/common/rfb/benchmark.cxx b/common/rfb/benchmark.cxx new file mode 100644 index 0000000..c837c0f --- /dev/null +++ b/common/rfb/benchmark.cxx @@ -0,0 +1,396 @@ +/* Copyright 2015 Pierre Ossman for Cendio AB + * Copyright (C) 2015 D. R. Commander. All Rights Reserved. + * Copyright (C) 2025 Kasm Technologies Corp + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#include "benchmark.h" +#include +#include +#include +#include +#include +#include + +#include "ServerCore.h" +#include + +#include "EncCache.h" +#include "EncodeManager.h" +#include "SConnection.h" +#include "screenTypes.h" +#include "SMsgWriter.h" +#include "UpdateTracker.h" +#include "rdr/BufferedInStream.h" +#include "rdr/OutStream.h" +#include "ffmpeg.h" + +namespace benchmarking { + class MockBufferStream final : public rdr::BufferedInStream { + bool fillBuffer(size_t maxSize, bool wait) override { + return true; + } + }; + + class MockStream final : public rdr::OutStream { + public: + MockStream() { + offset = 0; + ptr = buf; + end = buf + sizeof(buf); + } + + private: + void overrun(size_t needed) override { + assert(end >= ptr); + if (needed > static_cast(end - ptr)) + flush(); + } + + public: + size_t length() override { + flush(); + return offset; + } + + void flush() override { + offset += ptr - buf; + ptr = buf; + } + + private: + ptrdiff_t offset; + rdr::U8 buf[8192]{}; + }; + + class MockSConnection final : public rfb::SConnection { + public: + MockSConnection() { + setStreams(nullptr, &out); + + setWriter(new rfb::SMsgWriter(&cp, &out, &udps)); + } + + ~MockSConnection() override = default; + + void writeUpdate(const rfb::UpdateInfo &ui, const rfb::PixelBuffer *pb) { + cache.clear(); + + manager.clearEncodingTime(); + if (!ui.is_empty()) { + manager.writeUpdate(ui, pb, nullptr); + } else { + rfb::Region region{pb->getRect()}; + manager.writeLosslessRefresh(region, pb, nullptr, 2000); + } + } + + void setDesktopSize(int fb_width, int fb_height, + const rfb::ScreenSet &layout) override { + cp.width = fb_width; + cp.height = fb_height; + cp.screenLayout = layout; + + writer()->writeExtendedDesktopSize(rfb::reasonServer, 0, cp.width, cp.height, + cp.screenLayout); + } + + void sendStats(const bool toClient) override { + } + + [[nodiscard]] bool canChangeKasmSettings() const override { + return true; + } + + void udpUpgrade(const char *resp) override { + } + + void udpDowngrade(const bool) override { + } + + void subscribeUnixRelay(const char *name) override { + } + + void unixRelay(const char *name, const rdr::U8 *buf, const unsigned len) override { + } + + void handleFrameStats(rdr::U32 all, rdr::U32 render) override { + } + + [[nodiscard]] auto getJpegStats() const { + return manager.jpegstats; + } + + [[nodiscard]] auto getWebPStats() const { + return manager.webpstats; + } + + [[nodiscard]] auto bytes() { return out.length(); } + [[nodiscard]] auto udp_bytes() { return udps.length(); } + + protected: + MockStream out{}; + MockStream udps{}; + + EncCache cache{}; + EncodeManager manager{this, &cache}; + }; + + class MockCConnection final : public MockTestConnection { + public: + explicit MockCConnection(const std::vector &encodings, rfb::ManagedPixelBuffer *pb) { + setStreams(&in, nullptr); + + // Need to skip the initial handshake and ServerInit + setState(RFBSTATE_NORMAL); + // That also means that the reader and writer weren't set up + setReader(new rfb::CMsgReader(this, &in)); + auto &pf = pb->getPF(); + CMsgHandler::setPixelFormat(pf); + + MockCConnection::setDesktopSize(pb->width(), pb->height()); + + cp.setPF(pf); + + sc.cp.setPF(pf); + sc.setEncodings(std::size(encodings), encodings.data()); + + setFramebuffer(pb); + } + + void setCursor(int width, int height, const rfb::Point &hotspot, const rdr::U8 *data, + const bool resizing) override { + } + + ~MockCConnection() override = default; + + struct stats_t { + EncodeManager::codecstats_t jpeg_stats; + EncodeManager::codecstats_t webp_stats; + uint64_t bytes; + uint64_t udp_bytes; + }; + + [[nodiscard]] stats_t getStats() { + return { + sc.getJpegStats(), + sc.getWebPStats(), + sc.bytes(), + sc.udp_bytes() + }; + } + + void setDesktopSize(int w, int h) override { + CConnection::setDesktopSize(w, h); + + if (screen_layout.num_screens()) + screen_layout.remove_screen(0); + + screen_layout.add_screen(rfb::Screen(0, 0, 0, w, h, 0)); + } + + void setNewFrame(const AVFrame *frame) override { + auto *pb = getFramebuffer(); + const int width = pb->width(); + const int height = pb->height(); + const rfb::Rect rect(0, 0, width, height); + + int dstStride{}; + auto *buffer = pb->getBufferRW(rect, &dstStride); + + const rfb::PixelFormat &pf = pb->getPF(); + + // Source data and stride from FFmpeg + const auto *srcData = frame->data[0]; + const int srcStride = frame->linesize[0] / 3; // Convert bytes to pixels + + // Convert from the RGB format to the PixelBuffer's format + pf.bufferFromRGB(buffer, srcData, width, srcStride, height); + + // Commit changes + pb->commitBufferRW(rect); + } + + void framebufferUpdateStart() override { + updates.clear(); + } + + void framebufferUpdateEnd() override { + const rfb::PixelBuffer *pb = getFramebuffer(); + + rfb::UpdateInfo ui; + const rfb::Region clip(pb->getRect()); + + updates.add_changed(pb->getRect()); + + updates.getUpdateInfo(&ui, clip); + sc.writeUpdate(ui, pb); + } + + void dataRect(const rfb::Rect &r, int encoding) override { + } + + void setColourMapEntries(int, int, rdr::U16 *) override { + } + + void bell() override { + } + + void serverCutText(const char *, rdr::U32) override { + } + + void serverCutText(const char *str) override { + } + + protected: + MockBufferStream in; + rfb::ScreenSet screen_layout; + rfb::SimpleUpdateTracker updates; + MockSConnection sc; + }; +} + +void report(std::vector &totals, std::vector &timings, + std::vector &stats, const std::string_view results_file) { + auto totals_sum = std::accumulate(totals.begin(), totals.end(), 0.); + auto totals_avg = totals_sum / static_cast(totals.size()); + + auto variance = 0.; + for (auto t: totals) + variance += (static_cast(t) - totals_avg) * (static_cast(t) - totals_avg); + + variance /= static_cast(totals.size()); + auto stddev = std::sqrt(variance); + + const auto sum = std::accumulate(timings.begin(), timings.end(), 0.); + const auto size = timings.size(); + const auto average = sum / static_cast(size); + + double median{}; + + std::sort(timings.begin(), timings.end()); + if (size % 2 == 0) + median = static_cast(timings[size / 2]); + else + median = static_cast(timings[size / 2 - 1] + timings[size / 2]) / 2.; + + vlog.info("Mean time encoding frame: %f ms", average); + vlog.info("Median time encoding frame: %f ms", median); + vlog.info("Total time (mean): %f ms", totals_avg); + vlog.info("Total time (stddev): %f ms", stddev); + + uint32_t jpeg_sum{}, jpeg_rects{}, webp_sum{}, webp_rects{}; + uint64_t bytes{}; + + for (const auto &item: stats) { + jpeg_sum += item.jpeg_stats.ms; + jpeg_rects += item.jpeg_stats.rects; + webp_sum += item.webp_stats.ms; + webp_rects += item.webp_stats.rects; + bytes += item.bytes; + } + + auto jpeg_ms = jpeg_sum / static_cast(stats.size()); + vlog.info("JPEG stats: %f ms", jpeg_ms); + jpeg_rects /= stats.size(); + vlog.info("JPEG stats: %u rects", jpeg_rects); + auto webp_ms = webp_sum / static_cast(stats.size()); + webp_rects /= stats.size(); + bytes /= stats.size(); + vlog.info("WebP stats: %f ms", webp_ms); + vlog.info("WebP stats: %u rects", webp_rects); + vlog.info("Total bytes sent: %lu bytes", bytes); + + tinyxml2::XMLDocument doc; + + auto *test_suit = doc.NewElement("testsuite"); + test_suit->SetAttribute("name", "Benchmark"); + + doc.InsertFirstChild(test_suit); + auto total_tests{0}; + + auto add_benchmark_item = [&doc, &test_suit, &total_tests](const char *name, auto time_value, auto other_value) { + auto *test_case = doc.NewElement("testcase"); + test_case->SetAttribute("name", name); + test_case->SetAttribute("file", other_value); + test_case->SetAttribute("time", time_value); + test_case->SetAttribute("runs", 1); + test_case->SetAttribute("classname", "KasmVNC"); + + test_suit->InsertEndChild(test_case); + + ++total_tests; + }; + + constexpr auto mult = 1 / 1000.; + add_benchmark_item("Average time encoding frame, ms", average * mult, ""); + add_benchmark_item("Median time encoding frame, ms", median * mult, ""); + add_benchmark_item("Total time encoding, ms", 0, totals_avg); + add_benchmark_item("Total time encoding, stddev", 0, stddev); + add_benchmark_item("Mean JPEG stats, ms", jpeg_ms, ""); + add_benchmark_item("Mean JPEG stats, rects", 0., jpeg_rects); + add_benchmark_item("Mean WebP stats, ms", webp_ms, ""); + add_benchmark_item("Mean WebP stats, rects", 0, webp_rects); + + add_benchmark_item("Data sent, KBs", 0, bytes / 1024); + + doc.SaveFile(results_file.data()); +} + +void benchmark(std::string_view path, const std::string_view results_file) { + try { + vlog.info("Benchmarking with video file %s", path.data()); + FFmpegFrameFeeder frame_feeder{}; + frame_feeder.open(path); + + static const rfb::PixelFormat pf{32, 24, false, true, 0xFF, 0xFF, 0xFF, 0, 8, 16}; + std::vector encodings{ + std::begin(benchmarking::default_encodings), std::end(benchmarking::default_encodings) + }; + + constexpr auto runs = 20; + std::vector totals(runs, 0); + std::vector stats(runs); + std::vector timings{}; + auto [width, height] = frame_feeder.get_frame_dimensions(); + + for (int run = 0; run < runs; ++run) { + auto *pb = new rfb::ManagedPixelBuffer{pf, width, height}; + benchmarking::MockCConnection connection{encodings, pb}; + + vlog.info("RUN %d. Reading frames...", run); + auto play_stats = frame_feeder.play(&connection); + vlog.info("RUN %d. Done reading frames...", run); + + timings.insert(timings.end(), play_stats.timings.begin(), play_stats.timings.end()); + + totals[run] = play_stats.total; + stats[run] = connection.getStats(); + vlog.info("JPEG stats: %u ms", stats[run].jpeg_stats.ms); + vlog.info("WebP stats: %u ms", stats[run].webp_stats.ms); + vlog.info("RUN %d. Bytes sent %lu..", run, stats[run].bytes); + } + + if (!timings.empty()) + report(totals, timings, stats, results_file); + + exit(0); + } catch (std::exception &e) { + vlog.error("Benchmarking failed: %s", e.what()); + exit(1); + } +} diff --git a/common/rfb/benchmark.h b/common/rfb/benchmark.h new file mode 100644 index 0000000..1feaa3e --- /dev/null +++ b/common/rfb/benchmark.h @@ -0,0 +1,67 @@ +/* Copyright (C) 2025 Kasm Technologies Corp +* + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#pragma once + +#include "CConnection.h" +#include "CMsgReader.h" +#include "LogWriter.h" + +extern "C" { +#include +} + +static rfb::LogWriter vlog("Benchmarking"); + +namespace benchmarking { + using namespace rfb; + + class MockTestConnection : public CConnection { + public: + virtual void setNewFrame(const AVFrame *frame) = 0; + }; + + static constexpr rdr::S32 default_encodings[] = { + + encodingTight, + encodingHextile, + encodingRRE, + encodingRaw, + pseudoEncodingQualityLevel0 + 6, + pseudoEncodingCompressLevel0 + 2, + pseudoEncodingDesktopSize, + pseudoEncodingLastRect, + pseudoEncodingQEMUKeyEvent, + pseudoEncodingExtendedDesktopSize, + pseudoEncodingFence, + pseudoEncodingContinuousUpdates, + pseudoEncodingDesktopName, + pseudoEncodingExtendedClipboard, + pseudoEncodingWEBP, + pseudoEncodingJpegVideoQualityLevel0 + 7, + pseudoEncodingWebpVideoQualityLevel0 + 7, + pseudoEncodingTreatLosslessLevel0 + 7, + pseudoEncodingDynamicQualityMinLevel0 + 4, + pseudoEncodingDynamicQualityMaxLevel0 + 9, + pseudoEncodingVideoAreaLevel1 - 1 + 65, + pseudoEncodingVideoTimeLevel0 + 5, + pseudoEncodingVideoOutTimeLevel1 - 1 + 3, + pseudoEncodingFrameRateLevel10 - 10 + 60, + pseudoEncodingMaxVideoResolution + }; +} diff --git a/common/rfb/ffmpeg.cxx b/common/rfb/ffmpeg.cxx new file mode 100644 index 0000000..2c803d0 --- /dev/null +++ b/common/rfb/ffmpeg.cxx @@ -0,0 +1,210 @@ +/* Copyright (C) 2025 Kasm Technologies Corp +* + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#include "ffmpeg.h" +#include +#include +#include + +FFmpegFrameFeeder::FFmpegFrameFeeder() { + static constexpr std::array paths = { + "/usr/lib/", + "/usr/lib64" + }; + + namespace fs = std::filesystem; + using namespace std::string_literals; + + auto load_lib = [](auto *lib) { + void *handle{}; + for (const auto &dir: paths) { + if (!fs::exists(dir) || !fs::is_directory(dir)) + continue; + + for (const auto &entry: fs::recursive_directory_iterator(dir)) { + if (!entry.is_regular_file()) + continue; + + const std::string filename = entry.path().filename().string(); + if (filename.find(lib) != std::string::npos) { + handle = dlopen(filename.c_str(), RTLD_LAZY); + + break; + } + } + } + + if (!handle) + throw std::runtime_error("Could not open "s + lib); + + return DlHandlerGuard{handle}; + }; + + // libavformat + libavformat = load_lib("libavformat.so"); + auto handle = libavformat.get(); + + avformat_open_input_f = D_LOOKUP_SYM(handle, avformat_open_input); + avformat_find_stream_info_f = D_LOOKUP_SYM(handle, avformat_find_stream_info); + avcodec_find_decoder_f = D_LOOKUP_SYM(handle, avcodec_find_decoder); + avcodec_parameters_to_context_f = D_LOOKUP_SYM(handle, avcodec_parameters_to_context); + av_read_frame_f = D_LOOKUP_SYM(handle, av_read_frame); + av_seek_frame_f = D_LOOKUP_SYM(handle, av_seek_frame); + avformat_close_input_f = D_LOOKUP_SYM(handle, avformat_close_input); + + vlog.info("libavformat.so loaded"); + + // libavutil + libavutil = load_lib("libavutil.so"); + handle = libavutil.get(); + + av_frame_free_f = D_LOOKUP_SYM(handle, av_frame_free); + av_frame_alloc_f = D_LOOKUP_SYM(handle, av_frame_alloc); + av_frame_get_buffer_f = D_LOOKUP_SYM(handle, av_frame_get_buffer); + + vlog.info("libavutil.so loaded"); + + // libswscale + libswscale = load_lib("libswscale.so"); + handle = libswscale.get(); + + sws_freeContext_f = D_LOOKUP_SYM(handle, sws_freeContext); + sws_getContext_f = D_LOOKUP_SYM(handle, sws_getContext); + sws_scale_f = D_LOOKUP_SYM(handle, sws_scale); + + // libavcodec + libavcodec = load_lib("libavcodec.so"); + handle = libavcodec.get(); + + avcodec_open2_f = D_LOOKUP_SYM(handle, avcodec_open2); + avcodec_alloc_context3_f = D_LOOKUP_SYM(handle, avcodec_alloc_context3); + avcodec_send_packet_f = D_LOOKUP_SYM(handle, avcodec_send_packet); + avcodec_receive_frame_f = D_LOOKUP_SYM(handle, avcodec_receive_frame); + av_packet_unref_f = D_LOOKUP_SYM(handle, av_packet_unref); + avcodec_flush_buffers_f = D_LOOKUP_SYM(handle, avcodec_flush_buffers); + avcodec_close_f = D_LOOKUP_SYM(handle, avcodec_close); + av_packet_alloc_f = D_LOOKUP_SYM(handle, av_packet_alloc); + av_packet_free_f = D_LOOKUP_SYM(handle, av_packet_free); +} + +FFmpegFrameFeeder::~FFmpegFrameFeeder() { + avformat_close_input_f(&format_ctx); + avcodec_close_f(codec_ctx); + avcodec_free_context_f(&codec_ctx); +} + +void FFmpegFrameFeeder::open(const std::string_view path) { + if (avformat_open_input_f(&format_ctx, path.data(), nullptr, nullptr) < 0) + throw std::runtime_error("Could not open video file"); + + // Find stream info + if (avformat_find_stream_info_f(format_ctx, nullptr) < 0) + throw std::runtime_error("Could not find stream info"); + + // Find video stream + for (uint32_t i = 0; i < format_ctx->nb_streams; ++i) { + if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + video_stream_idx = static_cast(i); + break; + } + } + + if (video_stream_idx == -1) + throw std::runtime_error("No video stream found"); + + // Get codec parameters and decoder + const auto *codec_parameters = format_ctx->streams[video_stream_idx]->codecpar; + const auto *codec = avcodec_find_decoder_f(codec_parameters->codec_id); + if (!codec) + throw std::runtime_error("Codec not found"); + + codec_ctx = avcodec_alloc_context3_f(codec); + if (!codec_ctx || avcodec_parameters_to_context_f(codec_ctx, codec_parameters) < 0) + throw std::runtime_error("Failed to set up codec context"); + + if (avcodec_open2_f(codec_ctx, codec, nullptr) < 0) + throw std::runtime_error("Could not open codec"); +} + +FFmpegFrameFeeder::play_stats_t FFmpegFrameFeeder::play(benchmarking::MockTestConnection *connection) const { + // Allocate frame and packet + const FrameGuard frame{av_frame_alloc_f()}; + const PacketGuard packet{av_packet_alloc_f()}; + + if (!frame || !packet) + throw std::runtime_error("Could not allocate frame or packet"); + + // Scaling context to convert to RGB24 + SwsContext *sws_ctx = sws_getContext_f( + codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, + codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, + SWS_BILINEAR, nullptr, nullptr, nullptr + ); + if (!sws_ctx) + throw std::runtime_error("Could not create scaling context"); + + const std::unique_ptr sws_ctx_guard{sws_ctx, sws_freeContext_f}; + + const FrameGuard rgb_frame{av_frame_alloc_f()}; + if (!rgb_frame) + throw std::runtime_error("Could not allocate frame"); + + rgb_frame->format = AV_PIX_FMT_RGB24; + rgb_frame->width = codec_ctx->width; + rgb_frame->height = codec_ctx->height; + + if (av_frame_get_buffer_f(rgb_frame.get(), 0) != 0) + throw std::runtime_error("Could not allocate frame data"); + + play_stats_t stats{}; + const auto total_frame_count = get_total_frame_count(); + stats.timings.reserve(total_frame_count > 0 ? total_frame_count : 2048); + + while (av_read_frame_f(format_ctx, packet.get()) == 0) { + if (packet->stream_index == video_stream_idx) { + if (avcodec_send_packet_f(codec_ctx, packet.get()) == 0) { + while (avcodec_receive_frame_f(codec_ctx, frame.get()) == 0) { + // Convert to RGB + sws_scale_f(sws_ctx_guard.get(), frame->data, frame->linesize, 0, + frame->height, + rgb_frame->data, rgb_frame->linesize); + + connection->framebufferUpdateStart(); + connection->setNewFrame(rgb_frame.get()); + using namespace std::chrono; + + auto now = high_resolution_clock::now(); + connection->framebufferUpdateEnd(); + const auto duration = duration_cast(high_resolution_clock::now() - now).count(); + + //vlog.info("Frame took %lu ms", duration); + stats.total += duration; + stats.timings.push_back(duration); + } + } + } + av_packet_unref_f(packet.get()); + } + + if (av_seek_frame_f(format_ctx, video_stream_idx, 0, AVSEEK_FLAG_BACKWARD) < 0) + throw std::runtime_error("Could not seek to start of video"); + + avcodec_flush_buffers_f(codec_ctx); + + return stats; +} diff --git a/common/rfb/ffmpeg.h b/common/rfb/ffmpeg.h new file mode 100644 index 0000000..61edbf2 --- /dev/null +++ b/common/rfb/ffmpeg.h @@ -0,0 +1,168 @@ +/* Copyright (C) 2025 Kasm Technologies Corp +* + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#pragma once + +#include +#include +#include +#include "LogWriter.h" + +extern "C" { +#include +#include +#include +} + +#include "benchmark.h" + +#define STR_HELPER(x) #x +#define STR(x) STR_HELPER(x) +#define CONCAT_STR(a, b) a b + +#define D_LOOKUP_SYM(handle, name) \ + [](auto handle, auto *sym_name) -> auto { \ + auto *sym = reinterpret_cast(dlsym(handle, sym_name)); \ + if (!sym) \ + throw std::runtime_error("Failed to load symbol "s + sym_name); \ + return sym; \ + }(handle, STR(name)) + +#define DEFINE_GUARD(name, type, deleter) \ + using name##Guard = std::unique_ptr; + +//using SwsContextGuard = std::unique_ptr; + +class FFmpegFrameFeeder final { + // libavformat + using avformat_close_input_func = void(*)(AVFormatContext **); + using avformat_open_input_func = int(*)(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, + AVDictionary **options); + using avformat_find_stream_info_func = int (*)(AVFormatContext *ic, AVDictionary **options); + using av_read_frame_func = int (*)(AVFormatContext *s, AVPacket *pkt); + using av_seek_frame_func = int (*)(AVFormatContext *s, int stream_index, int64_t timestamp, int flags); + + // libavutil + using av_frame_free_func = void (*)(AVFrame **); + using av_frame_alloc_func = AVFrame *(*)(); + using av_frame_get_buffer_func = int (*)(AVFrame *frame, int align); + + // libswscale + using sws_freeContext_func = void (*)(SwsContext *); + using sws_getContext_func = SwsContext * (*)(int srcW, int srcH, AVPixelFormat srcFormat, int dstW, int dstH, + AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, + SwsFilter *dstFilter, const double *param); + + using sws_scale_func = int(*)(SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, + int srcSliceH, uint8_t *const dst[], const int dstStride[]); + + // libavcodec + using avcodec_free_context_func = void (*)(AVCodecContext **); + using av_packet_free_func = void (*)(AVPacket **); + using avcodec_find_decoder_func = const AVCodec * (*)(AVCodecID id); + using avcodec_alloc_context3_func = AVCodecContext* (*)(const AVCodec *codec); + using avcodec_parameters_to_context_func = int (*)(AVCodecContext *codec, const AVCodecParameters *par); + using avcodec_open2_func = int (*)(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options); + using av_packet_alloc_func = AVPacket *(*)(); + using avcodec_send_packet_func = int(*)(AVCodecContext *avctx, const AVPacket *avpkt); + using avcodec_receive_frame_func = int(*)(AVCodecContext *avctx, AVFrame *frame); + using av_packet_unref_func = void (*)(AVPacket *pkt); + using avcodec_flush_buffers_func = void (*)(AVCodecContext *avctx); + using avcodec_close_func = int (*)(AVCodecContext *avctx); + + struct DlHandler { + void operator()(void *handle) const { + dlclose(handle); + } + }; + + using DlHandlerGuard = std::unique_ptr; + + // libavformat + avformat_close_input_func avformat_close_input_f{}; + avformat_open_input_func avformat_open_input_f{}; + avformat_find_stream_info_func avformat_find_stream_info_f{}; + av_read_frame_func av_read_frame_f{}; + av_seek_frame_func av_seek_frame_f{}; + + // libavutil + static inline av_frame_free_func av_frame_free_f{}; + av_frame_alloc_func av_frame_alloc_f{}; + av_frame_get_buffer_func av_frame_get_buffer_f{}; + + // libswscale + sws_freeContext_func sws_freeContext_f{}; + sws_getContext_func sws_getContext_f{}; + sws_scale_func sws_scale_f{}; + + // libavcodec + avcodec_free_context_func avcodec_free_context_f{}; + static inline av_packet_free_func av_packet_free_f{}; + avcodec_find_decoder_func avcodec_find_decoder_f{}; + avcodec_alloc_context3_func avcodec_alloc_context3_f{}; + avcodec_parameters_to_context_func avcodec_parameters_to_context_f{}; + avcodec_open2_func avcodec_open2_f{}; + av_packet_alloc_func av_packet_alloc_f{}; + avcodec_send_packet_func avcodec_send_packet_f{}; + avcodec_receive_frame_func avcodec_receive_frame_f{}; + av_packet_unref_func av_packet_unref_f{}; + avcodec_flush_buffers_func avcodec_flush_buffers_f{}; + avcodec_close_func avcodec_close_f{}; + + rfb::LogWriter vlog{"FFmpeg"}; + + DEFINE_GUARD(Frame, AVFrame, av_frame_free) + DEFINE_GUARD(Packet, AVPacket, av_packet_free) + + AVFormatContext *format_ctx{}; + AVCodecContext *codec_ctx{}; + int video_stream_idx{-1}; + + DlHandlerGuard libavformat{}; + DlHandlerGuard libavutil{}; + DlHandlerGuard libswscale{}; + DlHandlerGuard libavcodec{}; + +public: + FFmpegFrameFeeder(); + + ~FFmpegFrameFeeder(); + + void open(std::string_view path); + + [[nodiscard]] int64_t get_total_frame_count() const { + return format_ctx->streams[video_stream_idx]->nb_frames; + } + + struct frame_dimensions_t { + int width{}; + int height{}; + }; + + [[nodiscard]] frame_dimensions_t get_frame_dimensions() const { + return {codec_ctx->width, codec_ctx->height}; + } + + struct play_stats_t { + uint64_t frames{}; + uint64_t total{}; + std::vector timings; + }; + + play_stats_t play(benchmarking::MockTestConnection *connection) const; +}; diff --git a/common/rfb/util.cxx b/common/rfb/util.cxx index 649eb0b..a0f5cac 100644 --- a/common/rfb/util.cxx +++ b/common/rfb/util.cxx @@ -21,8 +21,8 @@ #include #endif -#include -#include +#include +#include #include #include @@ -572,6 +572,11 @@ namespace rfb { return msBetween(then, &now); } + uint64_t elapsedMs(std::chrono::high_resolution_clock::time_point start) + { + return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - start).count(); + } + bool isBefore(const struct timeval *first, const struct timeval *second) { diff --git a/common/rfb/util.h b/common/rfb/util.h index 3100f90..18d38ac 100644 --- a/common/rfb/util.h +++ b/common/rfb/util.h @@ -28,8 +28,9 @@ #include #endif -#include -#include +#include +#include +#include struct timeval; @@ -127,6 +128,15 @@ namespace rfb { // Returns time elapsed since given moment in milliseconds. unsigned msSince(const struct timeval *then); + /** + * Calculates the number of milliseconds elapsed since a given starting point. + * + * @param start The starting time point of type `std::chrono::high_resolution_clock::time_point`. + * + * @return The elapsed time in milliseconds as an unsigned 64-bit integer. + */ + uint64_t elapsedMs(std::chrono::high_resolution_clock::time_point start); + // Returns true if first happened before seconds bool isBefore(const struct timeval *first, const struct timeval *second); diff --git a/debian/changelog b/debian/changelog index 4e2536b..3cb1e97 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,13 @@ +kasmvnc (1.3.4-1) unstable; urgency=medium + + * Add configuration key network.udp.payload_size. + * Remove support for distro versions that reached end-of-life. + * Add missing dependency on libdatetime-perl. + * Remove webpack to reduce security vulnerabilities. + * Special characters in filenames are now properly escaped, preventing invalid JSON. + + -- Kasm Technologies LLC Thu, 20 Mar 2025 03:21:46 +0000 + kasmvnc (1.3.3-1) unstable; urgency=medium * Allow disabling IP blacklist diff --git a/debian/control b/debian/control index bff5f2a..11fac2f 100644 --- a/debian/control +++ b/debian/control @@ -14,7 +14,7 @@ Architecture: amd64 arm64 Depends: ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends}, ssl-cert, xauth, x11-xkb-utils, xkb-data, procps, libswitch-perl, libyaml-tiny-perl, libhash-merge-simple-perl, libscalar-list-utils-perl, liblist-moreutils-perl, - libtry-tiny-perl, libdatetime-timezone-perl, libgbm1 + libtry-tiny-perl, libdatetime-perl, libdatetime-timezone-perl, libgbm1 Provides: vnc-server Description: KasmVNC provides remote web-based access to a Desktop or application. While VNC is in the name, KasmVNC differs from other VNC variants such diff --git a/fedora/kasmvncserver.spec b/fedora/kasmvncserver.spec index 260d5ce..d9232b3 100644 --- a/fedora/kasmvncserver.spec +++ b/fedora/kasmvncserver.spec @@ -1,5 +1,5 @@ Name: kasmvncserver -Version: 1.3.3 +Version: 1.3.4 Release: 1%{?dist} Summary: VNC server accessible from a web browser @@ -7,7 +7,7 @@ License: GPLv2+ URL: https://github.com/kasmtech/KasmVNC BuildRequires: rsync -Requires: xorg-x11-xauth, xkeyboard-config, xkbcomp, openssl, perl, perl-Switch, perl-YAML-Tiny, perl-Hash-Merge-Simple, perl-Scalar-List-Utils, perl-List-MoreUtils, perl-Try-Tiny, perl-DateTime-TimeZone, mesa-libgbm, libxshmfence +Requires: xorg-x11-xauth, xkeyboard-config, xkbcomp, openssl, perl, perl-Switch, perl-YAML-Tiny, perl-Hash-Merge-Simple, perl-Scalar-List-Utils, perl-List-MoreUtils, perl-Try-Tiny, perl-DateTime-TimeZone, mesa-libgbm, libxshmfence, hostname Conflicts: tigervnc-server, tigervnc-server-minimal %description @@ -83,6 +83,12 @@ cd $DST_MAN && ln -s vncpasswd.1 kasmvncpasswd.1; %doc /usr/share/doc/kasmvncserver/README.md %changelog +* Thu Mar 20 2025 KasmTech - 1.3.4-1 +- Add configuration key network.udp.payload_size. +- Remove support for distro versions that reached end-of-life. +- Add missing dependency on hostname. +- Remove webpack to reduce security vulnerabilities. +- Special characters in filenames are now properly escaped, preventing invalid JSON. * Fri Oct 25 2024 KasmTech - 1.3.3-1 - Allow disabling IP blacklist - Downloads API for detailed file downloads information diff --git a/kasmweb b/kasmweb index bce2d6a..0d8a3c5 160000 --- a/kasmweb +++ b/kasmweb @@ -1 +1 @@ -Subproject commit bce2d6a7048025c6e6c05df9d98b206c23f6dbab +Subproject commit 0d8a3c5f07defffe340dd67a1485be5214bec4ee diff --git a/opensuse/kasmvncserver.spec b/opensuse/kasmvncserver.spec index e7fdba1..275eb21 100644 --- a/opensuse/kasmvncserver.spec +++ b/opensuse/kasmvncserver.spec @@ -1,5 +1,5 @@ Name: kasmvncserver -Version: 1.3.3 +Version: 1.3.4 Release: leap15 Summary: VNC server accessible from a web browser @@ -81,6 +81,12 @@ cd $DST_MAN && ln -s vncpasswd.1 kasmvncpasswd.1; %doc /usr/share/doc/kasmvncserver/README.md %changelog +* Thu Mar 20 2025 KasmTech - 1.3.4-leap15 +- Add configuration key network.udp.payload_size. +- Remove support for distro versions that reached end-of-life. +- Add missing dependency on hostname. +- Remove webpack to reduce security vulnerabilities. +- Special characters in filenames are now properly escaped, preventing invalid JSON. * Fri Oct 25 2024 KasmTech - 1.3.3-1 - Allow disabling IP blacklist - Downloads API for detailed file downloads information diff --git a/oracle/kasmvncserver.spec b/oracle/kasmvncserver.spec index 5edd055..85751bd 100644 --- a/oracle/kasmvncserver.spec +++ b/oracle/kasmvncserver.spec @@ -1,5 +1,5 @@ Name: kasmvncserver -Version: 1.3.3 +Version: 1.3.4 Release: 1%{?dist} Summary: VNC server accessible from a web browser @@ -82,6 +82,12 @@ cd $DST_MAN && ln -s vncpasswd.1 kasmvncpasswd.1; %doc /usr/share/doc/kasmvncserver/README.md %changelog +* Thu Mar 20 2025 KasmTech - 1.3.4-1 +- Add configuration key network.udp.payload_size. +- Remove support for distro versions that reached end-of-life. +- Add missing dependency on hostname. +- Remove webpack to reduce security vulnerabilities. +- Special characters in filenames are now properly escaped, preventing invalid JSON. * Fri Oct 25 2024 KasmTech - 1.3.3-1 - Allow disabling IP blacklist - Downloads API for detailed file downloads information diff --git a/oracle/kasmvncserver9.spec b/oracle/kasmvncserver9.spec index 482587c..8611c30 100644 --- a/oracle/kasmvncserver9.spec +++ b/oracle/kasmvncserver9.spec @@ -1,5 +1,5 @@ Name: kasmvncserver -Version: 1.3.3 +Version: 1.3.4 Release: 1%{?dist} Summary: VNC server accessible from a web browser @@ -82,6 +82,12 @@ cd $DST_MAN && ln -s vncpasswd.1 kasmvncpasswd.1; %doc /usr/share/doc/kasmvncserver/README.md %changelog +* Thu Mar 20 2025 KasmTech - 1.3.4-1 +- Add configuration key network.udp.payload_size. +- Remove support for distro versions that reached end-of-life. +- Add missing dependency on hostname. +- Remove webpack to reduce security vulnerabilities. +- Special characters in filenames are now properly escaped, preventing invalid JSON. * Fri Oct 25 2024 KasmTech - 1.3.3-1 - Allow disabling IP blacklist - Downloads API for detailed file downloads information diff --git a/spec/helper/spec_helper.py b/spec/helper/spec_helper.py index cb12c9f..c31692e 100644 --- a/spec/helper/spec_helper.py +++ b/spec/helper/spec_helper.py @@ -23,6 +23,18 @@ def write_config(config_text): f.write(config_text) +def clean_locks(): + tmp = '/tmp' + temporary_lock_file = os.path.join(tmp, '.X1-lock') + if (os.path.exists(temporary_lock_file)): + os.remove(temporary_lock_file) + + temporary_lock_file = os.path.join(tmp, '.X11-unix') + temporary_lock_file = os.path.join(temporary_lock_file, 'X1') + if (os.path.exists(temporary_lock_file)): + os.remove(temporary_lock_file) + + def clean_env(): clean_kasm_users() @@ -30,6 +42,7 @@ def clean_env(): vnc_dir = os.path.join(home_dir, ".vnc") Path(vnc_dir).rmtree(ignore_errors=True) + clean_locks() def clean_kasm_users(): home_dir = os.environ['HOME'] @@ -91,3 +104,4 @@ def kill_xvnc(): run_cmd('vncserver -kill :1', print_stderr=False) running_xvnc = False + clean_locks() diff --git a/spec/vncserver_adv_benchmarking_spec.py b/spec/vncserver_adv_benchmarking_spec.py new file mode 100644 index 0000000..8e4b10f --- /dev/null +++ b/spec/vncserver_adv_benchmarking_spec.py @@ -0,0 +1,15 @@ +from mamba import description, context, it, fit, before, after +from expects import expect, equal, contain, match +from helper.spec_helper import run_cmd, clean_env, kill_xvnc + +with description("Benchmarking"): + with before.each: + clean_env() + with after.each: + kill_xvnc() + with it("runs benchmarks"): + run_cmd("wget --no-check-certificate https://kasmweb-build-artifacts.s3.us-east-1.amazonaws.com/kasmvnc/static/127072-737747495_small.mp4 -O /tmp/video.mp4") + completed_process = run_cmd("Xvnc -interface 0.0.0.0 :1 -Benchmark /tmp/video.mp4 -VideoArea 100") + command = '''sed -i "s/KasmVNC/$(grep -E '^ID=' /etc/os-release | cut -d= -f2 | tr -d '"') $(grep -E '^VERSION_CODENAME=' /etc/os-release | cut -d= -f2 | tr -d '"')/g" Benchmark.xml''' + run_cmd(command) + expect(completed_process.returncode).to(equal(0)) diff --git a/spec/vncserver_benchmarking_spec.py b/spec/vncserver_benchmarking_spec.py new file mode 100644 index 0000000..a78d1e1 --- /dev/null +++ b/spec/vncserver_benchmarking_spec.py @@ -0,0 +1,12 @@ +from mamba import description, context, it, fit, before, after +from expects import expect, equal, contain, match +from helper.spec_helper import run_cmd, clean_env, kill_xvnc, clean_locks + +with description("Benchmarking"): + with before.each: + clean_env() + with after.each: + kill_xvnc() + with it("runs benchmarks"): + completed_process = run_cmd("Xvnc -interface 0.0.0.0 :1 -selfBench") + expect(completed_process.returncode).to(equal(0)) \ No newline at end of file diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt new file mode 100644 index 0000000..8572f8d --- /dev/null +++ b/third_party/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(tinyxml2) \ No newline at end of file diff --git a/third_party/tinyxml2/CMakeLists.txt b/third_party/tinyxml2/CMakeLists.txt new file mode 100644 index 0000000..db809df --- /dev/null +++ b/third_party/tinyxml2/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(tinyxml2_objs OBJECT tinyxml2.cpp) +if (NOT WIN32) + set_target_properties(tinyxml2_objs PROPERTIES POSITION_INDEPENDENT_CODE ON) +endif () +target_include_directories(tinyxml2_objs PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/third_party/tinyxml2/LICENSE.txt b/third_party/tinyxml2/LICENSE.txt new file mode 100644 index 0000000..85a6a36 --- /dev/null +++ b/third_party/tinyxml2/LICENSE.txt @@ -0,0 +1,18 @@ +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. diff --git a/third_party/tinyxml2/tinyxml2.cpp b/third_party/tinyxml2/tinyxml2.cpp new file mode 100644 index 0000000..8bb9635 --- /dev/null +++ b/third_party/tinyxml2/tinyxml2.cpp @@ -0,0 +1,3019 @@ +/* +Original code by Lee Thomason (www.grinninglizard.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#include "tinyxml2.h" + +#include // yes, this one new style header, is in the Android SDK. +#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__) || defined(__CC_ARM) +# include +# include +#else +# include +# include +#endif + +#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE) + // Microsoft Visual Studio, version 2005 and higher. Not WinCE. + /*int _snprintf_s( + char *buffer, + size_t sizeOfBuffer, + size_t count, + const char *format [, + argument] ... + );*/ + static inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... ) + { + va_list va; + va_start( va, format ); + const int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va ); + va_end( va ); + return result; + } + + static inline int TIXML_VSNPRINTF( char* buffer, size_t size, const char* format, va_list va ) + { + const int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va ); + return result; + } + + #define TIXML_VSCPRINTF _vscprintf + #define TIXML_SSCANF sscanf_s +#elif defined _MSC_VER + // Microsoft Visual Studio 2003 and earlier or WinCE + #define TIXML_SNPRINTF _snprintf + #define TIXML_VSNPRINTF _vsnprintf + #define TIXML_SSCANF sscanf + #if (_MSC_VER < 1400 ) && (!defined WINCE) + // Microsoft Visual Studio 2003 and not WinCE. + #define TIXML_VSCPRINTF _vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK doesn't have. + #else + // Microsoft Visual Studio 2003 and earlier or WinCE. + static inline int TIXML_VSCPRINTF( const char* format, va_list va ) + { + int len = 512; + for (;;) { + len = len*2; + char* str = new char[len](); + const int required = _vsnprintf(str, len, format, va); + delete[] str; + if ( required != -1 ) { + TIXMLASSERT( required >= 0 ); + len = required; + break; + } + } + TIXMLASSERT( len >= 0 ); + return len; + } + #endif +#else + // GCC version 3 and higher + //#warning( "Using sn* functions." ) + #define TIXML_SNPRINTF snprintf + #define TIXML_VSNPRINTF vsnprintf + static inline int TIXML_VSCPRINTF( const char* format, va_list va ) + { + int len = vsnprintf( 0, 0, format, va ); + TIXMLASSERT( len >= 0 ); + return len; + } + #define TIXML_SSCANF sscanf +#endif + +#if defined(_WIN64) + #define TIXML_FSEEK _fseeki64 + #define TIXML_FTELL _ftelli64 +#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__CYGWIN__) + #define TIXML_FSEEK fseeko + #define TIXML_FTELL ftello +#elif defined(__ANDROID__) && __ANDROID_API__ > 24 + #define TIXML_FSEEK fseeko64 + #define TIXML_FTELL ftello64 +#else + #define TIXML_FSEEK fseek + #define TIXML_FTELL ftell +#endif + + +static const char LINE_FEED = static_cast(0x0a); // all line endings are normalized to LF +static const char LF = LINE_FEED; +static const char CARRIAGE_RETURN = static_cast(0x0d); // CR gets filtered out +static const char CR = CARRIAGE_RETURN; +static const char SINGLE_QUOTE = '\''; +static const char DOUBLE_QUOTE = '\"'; + +// Bunch of unicode info at: +// http://www.unicode.org/faq/utf_bom.html +// ef bb bf (Microsoft "lead bytes") - designates UTF-8 + +static const unsigned char TIXML_UTF_LEAD_0 = 0xefU; +static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU; +static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU; + +namespace tinyxml2 +{ + +struct Entity { + const char* pattern; + int length; + char value; +}; + +static const int NUM_ENTITIES = 5; +static const Entity entities[NUM_ENTITIES] = { + { "quot", 4, DOUBLE_QUOTE }, + { "amp", 3, '&' }, + { "apos", 4, SINGLE_QUOTE }, + { "lt", 2, '<' }, + { "gt", 2, '>' } +}; + + +StrPair::~StrPair() +{ + Reset(); +} + + +void StrPair::TransferTo( StrPair* other ) +{ + if ( this == other ) { + return; + } + // This in effect implements the assignment operator by "moving" + // ownership (as in auto_ptr). + + TIXMLASSERT( other != 0 ); + TIXMLASSERT( other->_flags == 0 ); + TIXMLASSERT( other->_start == 0 ); + TIXMLASSERT( other->_end == 0 ); + + other->Reset(); + + other->_flags = _flags; + other->_start = _start; + other->_end = _end; + + _flags = 0; + _start = 0; + _end = 0; +} + + +void StrPair::Reset() +{ + if ( _flags & NEEDS_DELETE ) { + delete [] _start; + } + _flags = 0; + _start = 0; + _end = 0; +} + + +void StrPair::SetStr( const char* str, int flags ) +{ + TIXMLASSERT( str ); + Reset(); + size_t len = strlen( str ); + TIXMLASSERT( _start == 0 ); + _start = new char[ len+1 ]; + memcpy( _start, str, len+1 ); + _end = _start + len; + _flags = flags | NEEDS_DELETE; +} + + +char* StrPair::ParseText( char* p, const char* endTag, int strFlags, int* curLineNumPtr ) +{ + TIXMLASSERT( p ); + TIXMLASSERT( endTag && *endTag ); + TIXMLASSERT(curLineNumPtr); + + char* start = p; + const char endChar = *endTag; + size_t length = strlen( endTag ); + + // Inner loop of text parsing. + while ( *p ) { + if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) { + Set( start, p, strFlags ); + return p + length; + } else if (*p == '\n') { + ++(*curLineNumPtr); + } + ++p; + TIXMLASSERT( p ); + } + return 0; +} + + +char* StrPair::ParseName( char* p ) +{ + if ( !p || !(*p) ) { + return 0; + } + if ( !XMLUtil::IsNameStartChar( static_cast(*p) ) ) { + return 0; + } + + char* const start = p; + ++p; + while ( *p && XMLUtil::IsNameChar( static_cast(*p) ) ) { + ++p; + } + + Set( start, p, 0 ); + return p; +} + + +void StrPair::CollapseWhitespace() +{ + // Adjusting _start would cause undefined behavior on delete[] + TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 ); + // Trim leading space. + _start = XMLUtil::SkipWhiteSpace( _start, 0 ); + + if ( *_start ) { + const char* p = _start; // the read pointer + char* q = _start; // the write pointer + + while( *p ) { + if ( XMLUtil::IsWhiteSpace( *p )) { + p = XMLUtil::SkipWhiteSpace( p, 0 ); + if ( *p == 0 ) { + break; // don't write to q; this trims the trailing space. + } + *q = ' '; + ++q; + } + *q = *p; + ++q; + ++p; + } + *q = 0; + } +} + + +const char* StrPair::GetStr() +{ + TIXMLASSERT( _start ); + TIXMLASSERT( _end ); + if ( _flags & NEEDS_FLUSH ) { + *_end = 0; + _flags ^= NEEDS_FLUSH; + + if ( _flags ) { + const char* p = _start; // the read pointer + char* q = _start; // the write pointer + + while( p < _end ) { + if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) { + // CR-LF pair becomes LF + // CR alone becomes LF + // LF-CR becomes LF + if ( *(p+1) == LF ) { + p += 2; + } + else { + ++p; + } + *q = LF; + ++q; + } + else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) { + if ( *(p+1) == CR ) { + p += 2; + } + else { + ++p; + } + *q = LF; + ++q; + } + else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) { + // Entities handled by tinyXML2: + // - special entities in the entity table [in/out] + // - numeric character reference [in] + // 中 or 中 + + if ( *(p+1) == '#' ) { + const int buflen = 10; + char buf[buflen] = { 0 }; + int len = 0; + const char* adjusted = const_cast( XMLUtil::GetCharacterRef( p, buf, &len ) ); + if ( adjusted == 0 ) { + *q = *p; + ++p; + ++q; + } + else { + TIXMLASSERT( 0 <= len && len <= buflen ); + TIXMLASSERT( q + len <= adjusted ); + p = adjusted; + memcpy( q, buf, len ); + q += len; + } + } + else { + bool entityFound = false; + for( int i = 0; i < NUM_ENTITIES; ++i ) { + const Entity& entity = entities[i]; + if ( strncmp( p + 1, entity.pattern, entity.length ) == 0 + && *( p + entity.length + 1 ) == ';' ) { + // Found an entity - convert. + *q = entity.value; + ++q; + p += entity.length + 2; + entityFound = true; + break; + } + } + if ( !entityFound ) { + // fixme: treat as error? + ++p; + ++q; + } + } + } + else { + *q = *p; + ++p; + ++q; + } + } + *q = 0; + } + // The loop below has plenty going on, and this + // is a less useful mode. Break it out. + if ( _flags & NEEDS_WHITESPACE_COLLAPSING ) { + CollapseWhitespace(); + } + _flags = (_flags & NEEDS_DELETE); + } + TIXMLASSERT( _start ); + return _start; +} + + + + +// --------- XMLUtil ----------- // + +const char* XMLUtil::writeBoolTrue = "true"; +const char* XMLUtil::writeBoolFalse = "false"; + +void XMLUtil::SetBoolSerialization(const char* writeTrue, const char* writeFalse) +{ + static const char* defTrue = "true"; + static const char* defFalse = "false"; + + writeBoolTrue = (writeTrue) ? writeTrue : defTrue; + writeBoolFalse = (writeFalse) ? writeFalse : defFalse; +} + + +const char* XMLUtil::ReadBOM( const char* p, bool* bom ) +{ + TIXMLASSERT( p ); + TIXMLASSERT( bom ); + *bom = false; + const unsigned char* pu = reinterpret_cast(p); + // Check for BOM: + if ( *(pu+0) == TIXML_UTF_LEAD_0 + && *(pu+1) == TIXML_UTF_LEAD_1 + && *(pu+2) == TIXML_UTF_LEAD_2 ) { + *bom = true; + p += 3; + } + TIXMLASSERT( p ); + return p; +} + + +void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ) +{ + const unsigned long BYTE_MASK = 0xBF; + const unsigned long BYTE_MARK = 0x80; + const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; + + if (input < 0x80) { + *length = 1; + } + else if ( input < 0x800 ) { + *length = 2; + } + else if ( input < 0x10000 ) { + *length = 3; + } + else if ( input < 0x200000 ) { + *length = 4; + } + else { + *length = 0; // This code won't convert this correctly anyway. + return; + } + + output += *length; + + // Scary scary fall throughs are annotated with carefully designed comments + // to suppress compiler warnings such as -Wimplicit-fallthrough in gcc + switch (*length) { + case 4: + --output; + *output = static_cast((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + //fall through + case 3: + --output; + *output = static_cast((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + //fall through + case 2: + --output; + *output = static_cast((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + //fall through + case 1: + --output; + *output = static_cast(input | FIRST_BYTE_MARK[*length]); + break; + default: + TIXMLASSERT( false ); + } +} + + +const char* XMLUtil::GetCharacterRef(const char* p, char* value, int* length) +{ + // Assume an entity, and pull it out. + *length = 0; + + static const uint32_t MAX_CODE_POINT = 0x10FFFF; + + if (*(p + 1) == '#' && *(p + 2)) { + uint32_t ucs = 0; + ptrdiff_t delta = 0; + uint32_t mult = 1; + static const char SEMICOLON = ';'; + + bool hex = false; + uint32_t radix = 10; + const char* q = 0; + char terminator = '#'; + + if (*(p + 2) == 'x') { + // Hexadecimal. + hex = true; + radix = 16; + terminator = 'x'; + + q = p + 3; + } + else { + // Decimal. + q = p + 2; + } + if (!(*q)) { + return 0; + } + + q = strchr(q, SEMICOLON); + if (!q) { + return 0; + } + TIXMLASSERT(*q == SEMICOLON); + + delta = q - p; + --q; + + while (*q != terminator) { + uint32_t digit = 0; + + if (*q >= '0' && *q <= '9') { + digit = *q - '0'; + } + else if (hex && (*q >= 'a' && *q <= 'f')) { + digit = *q - 'a' + 10; + } + else if (hex && (*q >= 'A' && *q <= 'F')) { + digit = *q - 'A' + 10; + } + else { + return 0; + } + TIXMLASSERT(digit < radix); + + const unsigned int digitScaled = mult * digit; + ucs += digitScaled; + mult *= radix; + + // Security check: could a value exist that is out of range? + // Easily; limit to the MAX_CODE_POINT, which also allows for a + // bunch of leading zeroes. + if (mult > MAX_CODE_POINT) { + mult = MAX_CODE_POINT; + } + --q; + } + // Out of range: + if (ucs > MAX_CODE_POINT) { + return 0; + } + // convert the UCS to UTF-8 + ConvertUTF32ToUTF8(ucs, value, length); + if (length == 0) { + // If length is 0, there was an error. (Security? Bad input?) + // Fail safely. + return 0; + } + return p + delta + 1; + } + return p + 1; +} + +void XMLUtil::ToStr( int v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%d", v ); +} + + +void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%u", v ); +} + + +void XMLUtil::ToStr( bool v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? writeBoolTrue : writeBoolFalse); +} + +/* + ToStr() of a number is a very tricky topic. + https://github.com/leethomason/tinyxml2/issues/106 +*/ +void XMLUtil::ToStr( float v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v ); +} + + +void XMLUtil::ToStr( double v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v ); +} + + +void XMLUtil::ToStr( int64_t v, char* buffer, int bufferSize ) +{ + // horrible syntax trick to make the compiler happy about %lld + TIXML_SNPRINTF(buffer, bufferSize, "%lld", static_cast(v)); +} + +void XMLUtil::ToStr( uint64_t v, char* buffer, int bufferSize ) +{ + // horrible syntax trick to make the compiler happy about %llu + TIXML_SNPRINTF(buffer, bufferSize, "%llu", static_cast(v)); +} + +bool XMLUtil::ToInt(const char* str, int* value) +{ + if (IsPrefixHex(str)) { + unsigned v; + if (TIXML_SSCANF(str, "%x", &v) == 1) { + *value = static_cast(v); + return true; + } + } + else { + if (TIXML_SSCANF(str, "%d", value) == 1) { + return true; + } + } + return false; +} + +bool XMLUtil::ToUnsigned(const char* str, unsigned* value) +{ + if (TIXML_SSCANF(str, IsPrefixHex(str) ? "%x" : "%u", value) == 1) { + return true; + } + return false; +} + +bool XMLUtil::ToBool( const char* str, bool* value ) +{ + int ival = 0; + if ( ToInt( str, &ival )) { + *value = (ival==0) ? false : true; + return true; + } + static const char* TRUE_VALS[] = { "true", "True", "TRUE", 0 }; + static const char* FALSE_VALS[] = { "false", "False", "FALSE", 0 }; + + for (int i = 0; TRUE_VALS[i]; ++i) { + if (StringEqual(str, TRUE_VALS[i])) { + *value = true; + return true; + } + } + for (int i = 0; FALSE_VALS[i]; ++i) { + if (StringEqual(str, FALSE_VALS[i])) { + *value = false; + return true; + } + } + return false; +} + + +bool XMLUtil::ToFloat( const char* str, float* value ) +{ + if ( TIXML_SSCANF( str, "%f", value ) == 1 ) { + return true; + } + return false; +} + + +bool XMLUtil::ToDouble( const char* str, double* value ) +{ + if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) { + return true; + } + return false; +} + + +bool XMLUtil::ToInt64(const char* str, int64_t* value) +{ + if (IsPrefixHex(str)) { + unsigned long long v = 0; // horrible syntax trick to make the compiler happy about %llx + if (TIXML_SSCANF(str, "%llx", &v) == 1) { + *value = static_cast(v); + return true; + } + } + else { + long long v = 0; // horrible syntax trick to make the compiler happy about %lld + if (TIXML_SSCANF(str, "%lld", &v) == 1) { + *value = static_cast(v); + return true; + } + } + return false; +} + + +bool XMLUtil::ToUnsigned64(const char* str, uint64_t* value) { + unsigned long long v = 0; // horrible syntax trick to make the compiler happy about %llu + if(TIXML_SSCANF(str, IsPrefixHex(str) ? "%llx" : "%llu", &v) == 1) { + *value = static_cast(v); + return true; + } + return false; +} + + +char* XMLDocument::Identify( char* p, XMLNode** node, bool first ) +{ + TIXMLASSERT( node ); + TIXMLASSERT( p ); + char* const start = p; + int const startLine = _parseCurLineNum; + p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum ); + if( !*p ) { + *node = 0; + TIXMLASSERT( p ); + return p; + } + + // These strings define the matching patterns: + static const char* xmlHeader = { "( _commentPool ); + returnNode->_parseLineNum = _parseCurLineNum; + p += xmlHeaderLen; + } + else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) { + returnNode = CreateUnlinkedNode( _commentPool ); + returnNode->_parseLineNum = _parseCurLineNum; + p += commentHeaderLen; + } + else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) { + XMLText* text = CreateUnlinkedNode( _textPool ); + returnNode = text; + returnNode->_parseLineNum = _parseCurLineNum; + p += cdataHeaderLen; + text->SetCData( true ); + } + else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) { + returnNode = CreateUnlinkedNode( _commentPool ); + returnNode->_parseLineNum = _parseCurLineNum; + p += dtdHeaderLen; + } + else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) { + + // Preserve whitespace pedantically before closing tag, when it's immediately after opening tag + if (WhitespaceMode() == PEDANTIC_WHITESPACE && first && p != start && *(p + elementHeaderLen) == '/') { + returnNode = CreateUnlinkedNode(_textPool); + returnNode->_parseLineNum = startLine; + p = start; // Back it up, all the text counts. + _parseCurLineNum = startLine; + } + else { + returnNode = CreateUnlinkedNode(_elementPool); + returnNode->_parseLineNum = _parseCurLineNum; + p += elementHeaderLen; + } + } + else { + returnNode = CreateUnlinkedNode( _textPool ); + returnNode->_parseLineNum = _parseCurLineNum; // Report line of first non-whitespace character + p = start; // Back it up, all the text counts. + _parseCurLineNum = startLine; + } + + TIXMLASSERT( returnNode ); + TIXMLASSERT( p ); + *node = returnNode; + return p; +} + + +bool XMLDocument::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + if ( visitor->VisitEnter( *this ) ) { + for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) { + if ( !node->Accept( visitor ) ) { + break; + } + } + } + return visitor->VisitExit( *this ); +} + + +// --------- XMLNode ----------- // + +XMLNode::XMLNode( XMLDocument* doc ) : + _document( doc ), + _parent( 0 ), + _value(), + _parseLineNum( 0 ), + _firstChild( 0 ), _lastChild( 0 ), + _prev( 0 ), _next( 0 ), + _userData( 0 ), + _memPool( 0 ) +{ +} + + +XMLNode::~XMLNode() +{ + DeleteChildren(); + if ( _parent ) { + _parent->Unlink( this ); + } +} + +// ChildElementCount was originally suggested by msteiger on the sourceforge page for TinyXML and modified by KB1SPH for TinyXML-2. + +int XMLNode::ChildElementCount(const char *value) const { + int count = 0; + + const XMLElement *e = FirstChildElement(value); + + while (e) { + e = e->NextSiblingElement(value); + count++; + } + + return count; +} + +int XMLNode::ChildElementCount() const { + int count = 0; + + const XMLElement *e = FirstChildElement(); + + while (e) { + e = e->NextSiblingElement(); + count++; + } + + return count; +} + +const char* XMLNode::Value() const +{ + // Edge case: XMLDocuments don't have a Value. Return null. + if ( this->ToDocument() ) + return 0; + return _value.GetStr(); +} + +void XMLNode::SetValue( const char* str, bool staticMem ) +{ + if ( staticMem ) { + _value.SetInternedStr( str ); + } + else { + _value.SetStr( str ); + } +} + +XMLNode* XMLNode::DeepClone(XMLDocument* target) const +{ + XMLNode* clone = this->ShallowClone(target); + if (!clone) return 0; + + for (const XMLNode* child = this->FirstChild(); child; child = child->NextSibling()) { + XMLNode* childClone = child->DeepClone(target); + TIXMLASSERT(childClone); + clone->InsertEndChild(childClone); + } + return clone; +} + +void XMLNode::DeleteChildren() +{ + while( _firstChild ) { + TIXMLASSERT( _lastChild ); + DeleteChild( _firstChild ); + } + _firstChild = _lastChild = 0; +} + + +void XMLNode::Unlink( XMLNode* child ) +{ + TIXMLASSERT( child ); + TIXMLASSERT( child->_document == _document ); + TIXMLASSERT( child->_parent == this ); + if ( child == _firstChild ) { + _firstChild = _firstChild->_next; + } + if ( child == _lastChild ) { + _lastChild = _lastChild->_prev; + } + + if ( child->_prev ) { + child->_prev->_next = child->_next; + } + if ( child->_next ) { + child->_next->_prev = child->_prev; + } + child->_next = 0; + child->_prev = 0; + child->_parent = 0; +} + + +void XMLNode::DeleteChild( XMLNode* node ) +{ + TIXMLASSERT( node ); + TIXMLASSERT( node->_document == _document ); + TIXMLASSERT( node->_parent == this ); + Unlink( node ); + TIXMLASSERT(node->_prev == 0); + TIXMLASSERT(node->_next == 0); + TIXMLASSERT(node->_parent == 0); + DeleteNode( node ); +} + + +XMLNode* XMLNode::InsertEndChild( XMLNode* addThis ) +{ + TIXMLASSERT( addThis ); + if ( addThis->_document != _document ) { + TIXMLASSERT( false ); + return 0; + } + InsertChildPreamble( addThis ); + + if ( _lastChild ) { + TIXMLASSERT( _firstChild ); + TIXMLASSERT( _lastChild->_next == 0 ); + _lastChild->_next = addThis; + addThis->_prev = _lastChild; + _lastChild = addThis; + + addThis->_next = 0; + } + else { + TIXMLASSERT( _firstChild == 0 ); + _firstChild = _lastChild = addThis; + + addThis->_prev = 0; + addThis->_next = 0; + } + addThis->_parent = this; + return addThis; +} + + +XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis ) +{ + TIXMLASSERT( addThis ); + if ( addThis->_document != _document ) { + TIXMLASSERT( false ); + return 0; + } + InsertChildPreamble( addThis ); + + if ( _firstChild ) { + TIXMLASSERT( _lastChild ); + TIXMLASSERT( _firstChild->_prev == 0 ); + + _firstChild->_prev = addThis; + addThis->_next = _firstChild; + _firstChild = addThis; + + addThis->_prev = 0; + } + else { + TIXMLASSERT( _lastChild == 0 ); + _firstChild = _lastChild = addThis; + + addThis->_prev = 0; + addThis->_next = 0; + } + addThis->_parent = this; + return addThis; +} + + +XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ) +{ + TIXMLASSERT( addThis ); + if ( addThis->_document != _document ) { + TIXMLASSERT( false ); + return 0; + } + + TIXMLASSERT( afterThis ); + + if ( afterThis->_parent != this ) { + TIXMLASSERT( false ); + return 0; + } + if ( afterThis == addThis ) { + // Current state: BeforeThis -> AddThis -> OneAfterAddThis + // Now AddThis must disappear from it's location and then + // reappear between BeforeThis and OneAfterAddThis. + // So just leave it where it is. + return addThis; + } + + if ( afterThis->_next == 0 ) { + // The last node or the only node. + return InsertEndChild( addThis ); + } + InsertChildPreamble( addThis ); + addThis->_prev = afterThis; + addThis->_next = afterThis->_next; + afterThis->_next->_prev = addThis; + afterThis->_next = addThis; + addThis->_parent = this; + return addThis; +} + + + + +const XMLElement* XMLNode::FirstChildElement( const char* name ) const +{ + for( const XMLNode* node = _firstChild; node; node = node->_next ) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { + return element; + } + } + return 0; +} + + +const XMLElement* XMLNode::LastChildElement( const char* name ) const +{ + for( const XMLNode* node = _lastChild; node; node = node->_prev ) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { + return element; + } + } + return 0; +} + + +const XMLElement* XMLNode::NextSiblingElement( const char* name ) const +{ + for( const XMLNode* node = _next; node; node = node->_next ) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { + return element; + } + } + return 0; +} + + +const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const +{ + for( const XMLNode* node = _prev; node; node = node->_prev ) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { + return element; + } + } + return 0; +} + + +char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) +{ + // This is a recursive method, but thinking about it "at the current level" + // it is a pretty simple flat list: + // + // + // + // With a special case: + // + // + // + // + // Where the closing element (/foo) *must* be the next thing after the opening + // element, and the names must match. BUT the tricky bit is that the closing + // element will be read by the child. + // + // 'endTag' is the end tag for this node, it is returned by a call to a child. + // 'parentEnd' is the end tag for the parent, which is filled in and returned. + + XMLDocument::DepthTracker tracker(_document); + if (_document->Error()) + return 0; + + bool first = true; + while( p && *p ) { + XMLNode* node = 0; + + p = _document->Identify( p, &node, first ); + TIXMLASSERT( p ); + if ( node == 0 ) { + break; + } + first = false; + + const int initialLineNum = node->_parseLineNum; + + StrPair endTag; + p = node->ParseDeep( p, &endTag, curLineNumPtr ); + if ( !p ) { + _document->DeleteNode( node ); + if ( !_document->Error() ) { + _document->SetError( XML_ERROR_PARSING, initialLineNum, 0); + } + break; + } + + const XMLDeclaration* const decl = node->ToDeclaration(); + if ( decl ) { + // Declarations are only allowed at document level + // + // Multiple declarations are allowed but all declarations + // must occur before anything else. + // + // Optimized due to a security test case. If the first node is + // a declaration, and the last node is a declaration, then only + // declarations have so far been added. + bool wellLocated = false; + + if (ToDocument()) { + if (FirstChild()) { + wellLocated = + FirstChild() && + FirstChild()->ToDeclaration() && + LastChild() && + LastChild()->ToDeclaration(); + } + else { + wellLocated = true; + } + } + if ( !wellLocated ) { + _document->SetError( XML_ERROR_PARSING_DECLARATION, initialLineNum, "XMLDeclaration value=%s", decl->Value()); + _document->DeleteNode( node ); + break; + } + } + + XMLElement* ele = node->ToElement(); + if ( ele ) { + // We read the end tag. Return it to the parent. + if ( ele->ClosingType() == XMLElement::CLOSING ) { + if ( parentEndTag ) { + ele->_value.TransferTo( parentEndTag ); + } + node->_memPool->SetTracked(); // created and then immediately deleted. + DeleteNode( node ); + return p; + } + + // Handle an end tag returned to this level. + // And handle a bunch of annoying errors. + bool mismatch = false; + if ( endTag.Empty() ) { + if ( ele->ClosingType() == XMLElement::OPEN ) { + mismatch = true; + } + } + else { + if ( ele->ClosingType() != XMLElement::OPEN ) { + mismatch = true; + } + else if ( !XMLUtil::StringEqual( endTag.GetStr(), ele->Name() ) ) { + mismatch = true; + } + } + if ( mismatch ) { + _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, initialLineNum, "XMLElement name=%s", ele->Name()); + _document->DeleteNode( node ); + break; + } + } + InsertEndChild( node ); + } + return 0; +} + +/*static*/ void XMLNode::DeleteNode( XMLNode* node ) +{ + if ( node == 0 ) { + return; + } + TIXMLASSERT(node->_document); + if (!node->ToDocument()) { + node->_document->MarkInUse(node); + } + + MemPool* pool = node->_memPool; + node->~XMLNode(); + pool->Free( node ); +} + +void XMLNode::InsertChildPreamble( XMLNode* insertThis ) const +{ + TIXMLASSERT( insertThis ); + TIXMLASSERT( insertThis->_document == _document ); + + if (insertThis->_parent) { + insertThis->_parent->Unlink( insertThis ); + } + else { + insertThis->_document->MarkInUse(insertThis); + insertThis->_memPool->SetTracked(); + } +} + +const XMLElement* XMLNode::ToElementWithName( const char* name ) const +{ + const XMLElement* element = this->ToElement(); + if ( element == 0 ) { + return 0; + } + if ( name == 0 ) { + return element; + } + if ( XMLUtil::StringEqual( element->Name(), name ) ) { + return element; + } + return 0; +} + +// --------- XMLText ---------- // +char* XMLText::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) +{ + if ( this->CData() ) { + p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); + if ( !p ) { + _document->SetError( XML_ERROR_PARSING_CDATA, _parseLineNum, 0 ); + } + return p; + } + else { + int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES; + if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) { + flags |= StrPair::NEEDS_WHITESPACE_COLLAPSING; + } + + p = _value.ParseText( p, "<", flags, curLineNumPtr ); + if ( p && *p ) { + return p-1; + } + if ( !p ) { + _document->SetError( XML_ERROR_PARSING_TEXT, _parseLineNum, 0 ); + } + } + return 0; +} + + +XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern? + text->SetCData( this->CData() ); + return text; +} + + +bool XMLText::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLText* text = compare->ToText(); + return ( text && XMLUtil::StringEqual( text->Value(), Value() ) ); +} + + +bool XMLText::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + return visitor->Visit( *this ); +} + + +// --------- XMLComment ---------- // + +XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLComment::~XMLComment() +{ +} + + +char* XMLComment::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) +{ + // Comment parses as text. + p = _value.ParseText( p, "-->", StrPair::COMMENT, curLineNumPtr ); + if ( p == 0 ) { + _document->SetError( XML_ERROR_PARSING_COMMENT, _parseLineNum, 0 ); + } + return p; +} + + +XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern? + return comment; +} + + +bool XMLComment::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLComment* comment = compare->ToComment(); + return ( comment && XMLUtil::StringEqual( comment->Value(), Value() )); +} + + +bool XMLComment::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + return visitor->Visit( *this ); +} + + +// --------- XMLDeclaration ---------- // + +XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLDeclaration::~XMLDeclaration() +{ + //printf( "~XMLDeclaration\n" ); +} + + +char* XMLDeclaration::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) +{ + // Declaration parses as text. + p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); + if ( p == 0 ) { + _document->SetError( XML_ERROR_PARSING_DECLARATION, _parseLineNum, 0 ); + } + return p; +} + + +XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern? + return dec; +} + + +bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLDeclaration* declaration = compare->ToDeclaration(); + return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() )); +} + + + +bool XMLDeclaration::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + return visitor->Visit( *this ); +} + +// --------- XMLUnknown ---------- // + +XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLUnknown::~XMLUnknown() +{ +} + + +char* XMLUnknown::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) +{ + // Unknown parses as text. + p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); + if ( !p ) { + _document->SetError( XML_ERROR_PARSING_UNKNOWN, _parseLineNum, 0 ); + } + return p; +} + + +XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern? + return text; +} + + +bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLUnknown* unknown = compare->ToUnknown(); + return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() )); +} + + +bool XMLUnknown::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + return visitor->Visit( *this ); +} + +// --------- XMLAttribute ---------- // + +const char* XMLAttribute::Name() const +{ + return _name.GetStr(); +} + +const char* XMLAttribute::Value() const +{ + return _value.GetStr(); +} + +char* XMLAttribute::ParseDeep( char* p, bool processEntities, int* curLineNumPtr ) +{ + // Parse using the name rules: bug fix, was using ParseText before + p = _name.ParseName( p ); + if ( !p || !*p ) { + return 0; + } + + // Skip white space before = + p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); + if ( *p != '=' ) { + return 0; + } + + ++p; // move up to opening quote + p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); + if ( *p != '\"' && *p != '\'' ) { + return 0; + } + + const char endTag[2] = { *p, 0 }; + ++p; // move past opening quote + + p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES, curLineNumPtr ); + return p; +} + + +void XMLAttribute::SetName( const char* n ) +{ + _name.SetStr( n ); +} + + +XMLError XMLAttribute::QueryIntValue( int* value ) const +{ + if ( XMLUtil::ToInt( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const +{ + if ( XMLUtil::ToUnsigned( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryInt64Value(int64_t* value) const +{ + if (XMLUtil::ToInt64(Value(), value)) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryUnsigned64Value(uint64_t* value) const +{ + if(XMLUtil::ToUnsigned64(Value(), value)) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryBoolValue( bool* value ) const +{ + if ( XMLUtil::ToBool( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryFloatValue( float* value ) const +{ + if ( XMLUtil::ToFloat( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryDoubleValue( double* value ) const +{ + if ( XMLUtil::ToDouble( Value(), value )) { + return XML_SUCCESS; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +void XMLAttribute::SetAttribute( const char* v ) +{ + _value.SetStr( v ); +} + + +void XMLAttribute::SetAttribute( int v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + +void XMLAttribute::SetAttribute( unsigned v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + +void XMLAttribute::SetAttribute(int64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + _value.SetStr(buf); +} + +void XMLAttribute::SetAttribute(uint64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + _value.SetStr(buf); +} + + +void XMLAttribute::SetAttribute( bool v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + +void XMLAttribute::SetAttribute( double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + +void XMLAttribute::SetAttribute( float v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + +// --------- XMLElement ---------- // +XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ), + _closingType( OPEN ), + _rootAttribute( 0 ) +{ +} + + +XMLElement::~XMLElement() +{ + while( _rootAttribute ) { + XMLAttribute* next = _rootAttribute->_next; + DeleteAttribute( _rootAttribute ); + _rootAttribute = next; + } +} + + +const XMLAttribute* XMLElement::FindAttribute( const char* name ) const +{ + for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) { + if ( XMLUtil::StringEqual( a->Name(), name ) ) { + return a; + } + } + return 0; +} + + +const char* XMLElement::Attribute( const char* name, const char* value ) const +{ + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return 0; + } + if ( !value || XMLUtil::StringEqual( a->Value(), value )) { + return a->Value(); + } + return 0; +} + +int XMLElement::IntAttribute(const char* name, int defaultValue) const +{ + int i = defaultValue; + QueryIntAttribute(name, &i); + return i; +} + +unsigned XMLElement::UnsignedAttribute(const char* name, unsigned defaultValue) const +{ + unsigned i = defaultValue; + QueryUnsignedAttribute(name, &i); + return i; +} + +int64_t XMLElement::Int64Attribute(const char* name, int64_t defaultValue) const +{ + int64_t i = defaultValue; + QueryInt64Attribute(name, &i); + return i; +} + +uint64_t XMLElement::Unsigned64Attribute(const char* name, uint64_t defaultValue) const +{ + uint64_t i = defaultValue; + QueryUnsigned64Attribute(name, &i); + return i; +} + +bool XMLElement::BoolAttribute(const char* name, bool defaultValue) const +{ + bool b = defaultValue; + QueryBoolAttribute(name, &b); + return b; +} + +double XMLElement::DoubleAttribute(const char* name, double defaultValue) const +{ + double d = defaultValue; + QueryDoubleAttribute(name, &d); + return d; +} + +float XMLElement::FloatAttribute(const char* name, float defaultValue) const +{ + float f = defaultValue; + QueryFloatAttribute(name, &f); + return f; +} + +const char* XMLElement::GetText() const +{ + /* skip comment node */ + const XMLNode* node = FirstChild(); + while (node) { + if (node->ToComment()) { + node = node->NextSibling(); + continue; + } + break; + } + + if ( node && node->ToText() ) { + return node->Value(); + } + return 0; +} + + +void XMLElement::SetText( const char* inText ) +{ + if ( FirstChild() && FirstChild()->ToText() ) + FirstChild()->SetValue( inText ); + else { + XMLText* theText = GetDocument()->NewText( inText ); + InsertFirstChild( theText ); + } +} + + +void XMLElement::SetText( int v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( unsigned v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText(int64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + SetText(buf); +} + +void XMLElement::SetText(uint64_t v) { + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + SetText(buf); +} + + +void XMLElement::SetText( bool v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( float v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +void XMLElement::SetText( double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + SetText( buf ); +} + + +XMLError XMLElement::QueryIntText( int* ival ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToInt( t, ival ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToUnsigned( t, uval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryInt64Text(int64_t* ival) const +{ + if (FirstChild() && FirstChild()->ToText()) { + const char* t = FirstChild()->Value(); + if (XMLUtil::ToInt64(t, ival)) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryUnsigned64Text(uint64_t* uval) const +{ + if(FirstChild() && FirstChild()->ToText()) { + const char* t = FirstChild()->Value(); + if(XMLUtil::ToUnsigned64(t, uval)) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryBoolText( bool* bval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToBool( t, bval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryDoubleText( double* dval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToDouble( t, dval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryFloatText( float* fval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->Value(); + if ( XMLUtil::ToFloat( t, fval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + +int XMLElement::IntText(int defaultValue) const +{ + int i = defaultValue; + QueryIntText(&i); + return i; +} + +unsigned XMLElement::UnsignedText(unsigned defaultValue) const +{ + unsigned i = defaultValue; + QueryUnsignedText(&i); + return i; +} + +int64_t XMLElement::Int64Text(int64_t defaultValue) const +{ + int64_t i = defaultValue; + QueryInt64Text(&i); + return i; +} + +uint64_t XMLElement::Unsigned64Text(uint64_t defaultValue) const +{ + uint64_t i = defaultValue; + QueryUnsigned64Text(&i); + return i; +} + +bool XMLElement::BoolText(bool defaultValue) const +{ + bool b = defaultValue; + QueryBoolText(&b); + return b; +} + +double XMLElement::DoubleText(double defaultValue) const +{ + double d = defaultValue; + QueryDoubleText(&d); + return d; +} + +float XMLElement::FloatText(float defaultValue) const +{ + float f = defaultValue; + QueryFloatText(&f); + return f; +} + + +XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) +{ + XMLAttribute* last = 0; + XMLAttribute* attrib = 0; + for( attrib = _rootAttribute; + attrib; + last = attrib, attrib = attrib->_next ) { + if ( XMLUtil::StringEqual( attrib->Name(), name ) ) { + break; + } + } + if ( !attrib ) { + attrib = CreateAttribute(); + TIXMLASSERT( attrib ); + if ( last ) { + TIXMLASSERT( last->_next == 0 ); + last->_next = attrib; + } + else { + TIXMLASSERT( _rootAttribute == 0 ); + _rootAttribute = attrib; + } + attrib->SetName( name ); + } + return attrib; +} + + +void XMLElement::DeleteAttribute( const char* name ) +{ + XMLAttribute* prev = 0; + for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) { + if ( XMLUtil::StringEqual( name, a->Name() ) ) { + if ( prev ) { + prev->_next = a->_next; + } + else { + _rootAttribute = a->_next; + } + DeleteAttribute( a ); + break; + } + prev = a; + } +} + + +char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr ) +{ + XMLAttribute* prevAttribute = 0; + + // Read the attributes. + while( p ) { + p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); + if ( !(*p) ) { + _document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, "XMLElement name=%s", Name() ); + return 0; + } + + // attribute. + if (XMLUtil::IsNameStartChar( static_cast(*p) ) ) { + XMLAttribute* attrib = CreateAttribute(); + TIXMLASSERT( attrib ); + attrib->_parseLineNum = _document->_parseCurLineNum; + + const int attrLineNum = attrib->_parseLineNum; + + p = attrib->ParseDeep( p, _document->ProcessEntities(), curLineNumPtr ); + if ( !p || Attribute( attrib->Name() ) ) { + DeleteAttribute( attrib ); + _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, attrLineNum, "XMLElement name=%s", Name() ); + return 0; + } + // There is a minor bug here: if the attribute in the source xml + // document is duplicated, it will not be detected and the + // attribute will be doubly added. However, tracking the 'prevAttribute' + // avoids re-scanning the attribute list. Preferring performance for + // now, may reconsider in the future. + if ( prevAttribute ) { + TIXMLASSERT( prevAttribute->_next == 0 ); + prevAttribute->_next = attrib; + } + else { + TIXMLASSERT( _rootAttribute == 0 ); + _rootAttribute = attrib; + } + prevAttribute = attrib; + } + // end of the tag + else if ( *p == '>' ) { + ++p; + break; + } + // end of the tag + else if ( *p == '/' && *(p+1) == '>' ) { + _closingType = CLOSED; + return p+2; // done; sealed element. + } + else { + _document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, 0 ); + return 0; + } + } + return p; +} + +void XMLElement::DeleteAttribute( XMLAttribute* attribute ) +{ + if ( attribute == 0 ) { + return; + } + MemPool* pool = attribute->_memPool; + attribute->~XMLAttribute(); + pool->Free( attribute ); +} + +XMLAttribute* XMLElement::CreateAttribute() +{ + TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() ); + XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); + TIXMLASSERT( attrib ); + attrib->_memPool = &_document->_attributePool; + attrib->_memPool->SetTracked(); + return attrib; +} + + +XMLElement* XMLElement::InsertNewChildElement(const char* name) +{ + XMLElement* node = _document->NewElement(name); + return InsertEndChild(node) ? node : 0; +} + +XMLComment* XMLElement::InsertNewComment(const char* comment) +{ + XMLComment* node = _document->NewComment(comment); + return InsertEndChild(node) ? node : 0; +} + +XMLText* XMLElement::InsertNewText(const char* text) +{ + XMLText* node = _document->NewText(text); + return InsertEndChild(node) ? node : 0; +} + +XMLDeclaration* XMLElement::InsertNewDeclaration(const char* text) +{ + XMLDeclaration* node = _document->NewDeclaration(text); + return InsertEndChild(node) ? node : 0; +} + +XMLUnknown* XMLElement::InsertNewUnknown(const char* text) +{ + XMLUnknown* node = _document->NewUnknown(text); + return InsertEndChild(node) ? node : 0; +} + + + +// +// +// foobar +// +char* XMLElement::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) +{ + // Read the element name. + p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); + + // The closing element is the form. It is + // parsed just like a regular element then deleted from + // the DOM. + if ( *p == '/' ) { + _closingType = CLOSING; + ++p; + } + + p = _value.ParseName( p ); + if ( _value.Empty() ) { + return 0; + } + + p = ParseAttributes( p, curLineNumPtr ); + if ( !p || !*p || _closingType != OPEN ) { + return p; + } + + p = XMLNode::ParseDeep( p, parentEndTag, curLineNumPtr ); + return p; +} + + + +XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern? + for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) { + element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern? + } + return element; +} + + +bool XMLElement::ShallowEqual( const XMLNode* compare ) const +{ + TIXMLASSERT( compare ); + const XMLElement* other = compare->ToElement(); + if ( other && XMLUtil::StringEqual( other->Name(), Name() )) { + + const XMLAttribute* a=FirstAttribute(); + const XMLAttribute* b=other->FirstAttribute(); + + while ( a && b ) { + if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) { + return false; + } + a = a->Next(); + b = b->Next(); + } + if ( a || b ) { + // different count + return false; + } + return true; + } + return false; +} + + +bool XMLElement::Accept( XMLVisitor* visitor ) const +{ + TIXMLASSERT( visitor ); + if ( visitor->VisitEnter( *this, _rootAttribute ) ) { + for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) { + if ( !node->Accept( visitor ) ) { + break; + } + } + } + return visitor->VisitExit( *this ); +} + + +// --------- XMLDocument ----------- // + +// Warning: List must match 'enum XMLError' +const char* XMLDocument::_errorNames[XML_ERROR_COUNT] = { + "XML_SUCCESS", + "XML_NO_ATTRIBUTE", + "XML_WRONG_ATTRIBUTE_TYPE", + "XML_ERROR_FILE_NOT_FOUND", + "XML_ERROR_FILE_COULD_NOT_BE_OPENED", + "XML_ERROR_FILE_READ_ERROR", + "XML_ERROR_PARSING_ELEMENT", + "XML_ERROR_PARSING_ATTRIBUTE", + "XML_ERROR_PARSING_TEXT", + "XML_ERROR_PARSING_CDATA", + "XML_ERROR_PARSING_COMMENT", + "XML_ERROR_PARSING_DECLARATION", + "XML_ERROR_PARSING_UNKNOWN", + "XML_ERROR_EMPTY_DOCUMENT", + "XML_ERROR_MISMATCHED_ELEMENT", + "XML_ERROR_PARSING", + "XML_CAN_NOT_CONVERT_TEXT", + "XML_NO_TEXT_NODE", + "XML_ELEMENT_DEPTH_EXCEEDED" +}; + + +XMLDocument::XMLDocument( bool processEntities, Whitespace whitespaceMode ) : + XMLNode( 0 ), + _writeBOM( false ), + _processEntities( processEntities ), + _errorID(XML_SUCCESS), + _whitespaceMode( whitespaceMode ), + _errorStr(), + _errorLineNum( 0 ), + _charBuffer( 0 ), + _parseCurLineNum( 0 ), + _parsingDepth(0), + _unlinked(), + _elementPool(), + _attributePool(), + _textPool(), + _commentPool() +{ + // avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+) + _document = this; +} + + +XMLDocument::~XMLDocument() +{ + Clear(); +} + + +void XMLDocument::MarkInUse(const XMLNode* const node) +{ + TIXMLASSERT(node); + TIXMLASSERT(node->_parent == 0); + + for (size_t i = 0; i < _unlinked.Size(); ++i) { + if (node == _unlinked[i]) { + _unlinked.SwapRemove(i); + break; + } + } +} + +void XMLDocument::Clear() +{ + DeleteChildren(); + while( _unlinked.Size()) { + DeleteNode(_unlinked[0]); // Will remove from _unlinked as part of delete. + } + +#ifdef TINYXML2_DEBUG + const bool hadError = Error(); +#endif + ClearError(); + + delete [] _charBuffer; + _charBuffer = 0; + _parsingDepth = 0; + +#if 0 + _textPool.Trace( "text" ); + _elementPool.Trace( "element" ); + _commentPool.Trace( "comment" ); + _attributePool.Trace( "attribute" ); +#endif + +#ifdef TINYXML2_DEBUG + if ( !hadError ) { + TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() ); + TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() ); + TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() ); + TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() ); + } +#endif +} + + +void XMLDocument::DeepCopy(XMLDocument* target) const +{ + TIXMLASSERT(target); + if (target == this) { + return; // technically success - a no-op. + } + + target->Clear(); + for (const XMLNode* node = this->FirstChild(); node; node = node->NextSibling()) { + target->InsertEndChild(node->DeepClone(target)); + } +} + +XMLElement* XMLDocument::NewElement( const char* name ) +{ + XMLElement* ele = CreateUnlinkedNode( _elementPool ); + ele->SetName( name ); + return ele; +} + + +XMLComment* XMLDocument::NewComment( const char* str ) +{ + XMLComment* comment = CreateUnlinkedNode( _commentPool ); + comment->SetValue( str ); + return comment; +} + + +XMLText* XMLDocument::NewText( const char* str ) +{ + XMLText* text = CreateUnlinkedNode( _textPool ); + text->SetValue( str ); + return text; +} + + +XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) +{ + XMLDeclaration* dec = CreateUnlinkedNode( _commentPool ); + dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" ); + return dec; +} + + +XMLUnknown* XMLDocument::NewUnknown( const char* str ) +{ + XMLUnknown* unk = CreateUnlinkedNode( _commentPool ); + unk->SetValue( str ); + return unk; +} + +static FILE* callfopen( const char* filepath, const char* mode ) +{ + TIXMLASSERT( filepath ); + TIXMLASSERT( mode ); +#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE) + FILE* fp = 0; + const errno_t err = fopen_s( &fp, filepath, mode ); + if ( err ) { + return 0; + } +#else + FILE* fp = fopen( filepath, mode ); +#endif + return fp; +} + +void XMLDocument::DeleteNode( XMLNode* node ) { + TIXMLASSERT( node ); + TIXMLASSERT(node->_document == this ); + if (node->_parent) { + node->_parent->DeleteChild( node ); + } + else { + // Isn't in the tree. + // Use the parent delete. + // Also, we need to mark it tracked: we 'know' + // it was never used. + node->_memPool->SetTracked(); + // Call the static XMLNode version: + XMLNode::DeleteNode(node); + } +} + + +XMLError XMLDocument::LoadFile( const char* filename ) +{ + if ( !filename ) { + TIXMLASSERT( false ); + SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=" ); + return _errorID; + } + + Clear(); + FILE* fp = callfopen( filename, "rb" ); + if ( !fp ) { + SetError( XML_ERROR_FILE_NOT_FOUND, 0, "filename=%s", filename ); + return _errorID; + } + LoadFile( fp ); + fclose( fp ); + return _errorID; +} + +XMLError XMLDocument::LoadFile( FILE* fp ) +{ + Clear(); + + TIXML_FSEEK( fp, 0, SEEK_SET ); + if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) { + SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); + return _errorID; + } + + TIXML_FSEEK( fp, 0, SEEK_END ); + + unsigned long long filelength; + { + const long long fileLengthSigned = TIXML_FTELL( fp ); + TIXML_FSEEK( fp, 0, SEEK_SET ); + if ( fileLengthSigned == -1L ) { + SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); + return _errorID; + } + TIXMLASSERT( fileLengthSigned >= 0 ); + filelength = static_cast(fileLengthSigned); + } + + const size_t maxSizeT = static_cast(-1); + // We'll do the comparison as an unsigned long long, because that's guaranteed to be at + // least 8 bytes, even on a 32-bit platform. + if ( filelength >= static_cast(maxSizeT) ) { + // Cannot handle files which won't fit in buffer together with null terminator + SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); + return _errorID; + } + + if ( filelength == 0 ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return _errorID; + } + + const size_t size = static_cast(filelength); + TIXMLASSERT( _charBuffer == 0 ); + _charBuffer = new char[size+1]; + const size_t read = fread( _charBuffer, 1, size, fp ); + if ( read != size ) { + SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); + return _errorID; + } + + _charBuffer[size] = 0; + + Parse(); + return _errorID; +} + + +XMLError XMLDocument::SaveFile( const char* filename, bool compact ) +{ + if ( !filename ) { + TIXMLASSERT( false ); + SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=" ); + return _errorID; + } + + FILE* fp = callfopen( filename, "w" ); + if ( !fp ) { + SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=%s", filename ); + return _errorID; + } + SaveFile(fp, compact); + fclose( fp ); + return _errorID; +} + + +XMLError XMLDocument::SaveFile( FILE* fp, bool compact ) +{ + // Clear any error from the last save, otherwise it will get reported + // for *this* call. + ClearError(); + XMLPrinter stream( fp, compact ); + Print( &stream ); + return _errorID; +} + + +XMLError XMLDocument::Parse( const char* xml, size_t nBytes ) +{ + Clear(); + + if ( nBytes == 0 || !xml || !*xml ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return _errorID; + } + if ( nBytes == static_cast(-1) ) { + nBytes = strlen( xml ); + } + TIXMLASSERT( _charBuffer == 0 ); + _charBuffer = new char[ nBytes+1 ]; + memcpy( _charBuffer, xml, nBytes ); + _charBuffer[nBytes] = 0; + + Parse(); + if ( Error() ) { + // clean up now essentially dangling memory. + // and the parse fail can put objects in the + // pools that are dead and inaccessible. + DeleteChildren(); + _elementPool.Clear(); + _attributePool.Clear(); + _textPool.Clear(); + _commentPool.Clear(); + } + return _errorID; +} + + +void XMLDocument::Print( XMLPrinter* streamer ) const +{ + if ( streamer ) { + Accept( streamer ); + } + else { + XMLPrinter stdoutStreamer( stdout ); + Accept( &stdoutStreamer ); + } +} + + +void XMLDocument::ClearError() { + _errorID = XML_SUCCESS; + _errorLineNum = 0; + _errorStr.Reset(); +} + + +void XMLDocument::SetError( XMLError error, int lineNum, const char* format, ... ) +{ + TIXMLASSERT(error >= 0 && error < XML_ERROR_COUNT); + _errorID = error; + _errorLineNum = lineNum; + _errorStr.Reset(); + + const size_t BUFFER_SIZE = 1000; + char* buffer = new char[BUFFER_SIZE]; + + TIXMLASSERT(sizeof(error) <= sizeof(int)); + TIXML_SNPRINTF(buffer, BUFFER_SIZE, "Error=%s ErrorID=%d (0x%x) Line number=%d", + ErrorIDToName(error), static_cast(error), static_cast(error), lineNum); + + if (format) { + size_t len = strlen(buffer); + TIXML_SNPRINTF(buffer + len, BUFFER_SIZE - len, ": "); + len = strlen(buffer); + + va_list va; + va_start(va, format); + TIXML_VSNPRINTF(buffer + len, BUFFER_SIZE - len, format, va); + va_end(va); + } + _errorStr.SetStr(buffer); + delete[] buffer; +} + + +/*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID) +{ + TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT ); + const char* errorName = _errorNames[errorID]; + TIXMLASSERT( errorName && errorName[0] ); + return errorName; +} + +const char* XMLDocument::ErrorStr() const +{ + return _errorStr.Empty() ? "" : _errorStr.GetStr(); +} + + +void XMLDocument::PrintError() const +{ + printf("%s\n", ErrorStr()); +} + +const char* XMLDocument::ErrorName() const +{ + return ErrorIDToName(_errorID); +} + +void XMLDocument::Parse() +{ + TIXMLASSERT( NoChildren() ); // Clear() must have been called previously + TIXMLASSERT( _charBuffer ); + _parseCurLineNum = 1; + _parseLineNum = 1; + char* p = _charBuffer; + p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum ); + p = const_cast( XMLUtil::ReadBOM( p, &_writeBOM ) ); + if ( !*p ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return; + } + ParseDeep(p, 0, &_parseCurLineNum ); +} + +void XMLDocument::PushDepth() +{ + _parsingDepth++; + if (_parsingDepth == TINYXML2_MAX_ELEMENT_DEPTH) { + SetError(XML_ELEMENT_DEPTH_EXCEEDED, _parseCurLineNum, "Element nesting is too deep." ); + } +} + +void XMLDocument::PopDepth() +{ + TIXMLASSERT(_parsingDepth > 0); + --_parsingDepth; +} + +XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) : + _elementJustOpened( false ), + _stack(), + _firstElement( true ), + _fp( file ), + _depth( depth ), + _textDepth( -1 ), + _processEntities( true ), + _compactMode( compact ), + _buffer() +{ + for( int i=0; i(entityValue); + TIXMLASSERT( flagIndex < ENTITY_RANGE ); + _entityFlag[flagIndex] = true; + } + _restrictedEntityFlag[static_cast('&')] = true; + _restrictedEntityFlag[static_cast('<')] = true; + _restrictedEntityFlag[static_cast('>')] = true; // not required, but consistency is nice + _buffer.Push( 0 ); +} + + +void XMLPrinter::Print( const char* format, ... ) +{ + va_list va; + va_start( va, format ); + + if ( _fp ) { + vfprintf( _fp, format, va ); + } + else { + const int len = TIXML_VSCPRINTF( format, va ); + // Close out and re-start the va-args + va_end( va ); + TIXMLASSERT( len >= 0 ); + va_start( va, format ); + TIXMLASSERT( _buffer.Size() > 0 && _buffer[_buffer.Size() - 1] == 0 ); + char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator. + TIXML_VSNPRINTF( p, len+1, format, va ); + } + va_end( va ); +} + + +void XMLPrinter::Write( const char* data, size_t size ) +{ + if ( _fp ) { + fwrite ( data , sizeof(char), size, _fp); + } + else { + char* p = _buffer.PushArr( static_cast(size) ) - 1; // back up over the null terminator. + memcpy( p, data, size ); + p[size] = 0; + } +} + + +void XMLPrinter::Putc( char ch ) +{ + if ( _fp ) { + fputc ( ch, _fp); + } + else { + char* p = _buffer.PushArr( sizeof(char) ) - 1; // back up over the null terminator. + p[0] = ch; + p[1] = 0; + } +} + + +void XMLPrinter::PrintSpace( int depth ) +{ + for( int i=0; i 0 && *q < ENTITY_RANGE ) { + // Check for entities. If one is found, flush + // the stream up until the entity, write the + // entity, and keep looking. + if ( flag[static_cast(*q)] ) { + while ( p < q ) { + const size_t delta = q - p; + const int toPrint = ( INT_MAX < delta ) ? INT_MAX : static_cast(delta); + Write( p, toPrint ); + p += toPrint; + } + bool entityPatternPrinted = false; + for( int i=0; i(delta); + Write( p, toPrint ); + } + } + else { + Write( p ); + } +} + + +void XMLPrinter::PushHeader( bool writeBOM, bool writeDec ) +{ + if ( writeBOM ) { + static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 }; + Write( reinterpret_cast< const char* >( bom ) ); + } + if ( writeDec ) { + PushDeclaration( "xml version=\"1.0\"" ); + } +} + +void XMLPrinter::PrepareForNewNode( bool compactMode ) +{ + SealElementIfJustOpened(); + + if ( compactMode ) { + return; + } + + if ( _firstElement ) { + PrintSpace (_depth); + } else if ( _textDepth < 0) { + Putc( '\n' ); + PrintSpace( _depth ); + } + + _firstElement = false; +} + +void XMLPrinter::OpenElement( const char* name, bool compactMode ) +{ + PrepareForNewNode( compactMode ); + _stack.Push( name ); + + Write ( "<" ); + Write ( name ); + + _elementJustOpened = true; + ++_depth; +} + + +void XMLPrinter::PushAttribute( const char* name, const char* value ) +{ + TIXMLASSERT( _elementJustOpened ); + Putc ( ' ' ); + Write( name ); + Write( "=\"" ); + PrintString( value, false ); + Putc ( '\"' ); +} + + +void XMLPrinter::PushAttribute( const char* name, int v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, unsigned v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute(const char* name, int64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + PushAttribute(name, buf); +} + + +void XMLPrinter::PushAttribute(const char* name, uint64_t v) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(v, buf, BUF_SIZE); + PushAttribute(name, buf); +} + + +void XMLPrinter::PushAttribute( const char* name, bool v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::CloseElement( bool compactMode ) +{ + --_depth; + const char* name = _stack.Pop(); + + if ( _elementJustOpened ) { + Write( "/>" ); + } + else { + if ( _textDepth < 0 && !compactMode) { + Putc( '\n' ); + PrintSpace( _depth ); + } + Write ( "" ); + } + + if ( _textDepth == _depth ) { + _textDepth = -1; + } + if ( _depth == 0 && !compactMode) { + Putc( '\n' ); + } + _elementJustOpened = false; +} + + +void XMLPrinter::SealElementIfJustOpened() +{ + if ( !_elementJustOpened ) { + return; + } + _elementJustOpened = false; + Putc( '>' ); +} + + +void XMLPrinter::PushText( const char* text, bool cdata ) +{ + _textDepth = _depth-1; + + SealElementIfJustOpened(); + if ( cdata ) { + Write( "" ); + } + else { + PrintString( text, true ); + } +} + + +void XMLPrinter::PushText( int64_t value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( uint64_t value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr(value, buf, BUF_SIZE); + PushText(buf, false); +} + + +void XMLPrinter::PushText( int value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( unsigned value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( bool value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( float value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( double value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushComment( const char* comment ) +{ + PrepareForNewNode( _compactMode ); + + Write( "" ); +} + + +void XMLPrinter::PushDeclaration( const char* value ) +{ + PrepareForNewNode( _compactMode ); + + Write( "" ); +} + + +void XMLPrinter::PushUnknown( const char* value ) +{ + PrepareForNewNode( _compactMode ); + + Write( "' ); +} + + +bool XMLPrinter::VisitEnter( const XMLDocument& doc ) +{ + _processEntities = doc.ProcessEntities(); + if ( doc.HasBOM() ) { + PushHeader( true, false ); + } + return true; +} + + +bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) +{ + const XMLElement* parentElem = 0; + if ( element.Parent() ) { + parentElem = element.Parent()->ToElement(); + } + const bool compactMode = parentElem ? CompactMode( *parentElem ) : _compactMode; + OpenElement( element.Name(), compactMode ); + while ( attribute ) { + PushAttribute( attribute->Name(), attribute->Value() ); + attribute = attribute->Next(); + } + return true; +} + + +bool XMLPrinter::VisitExit( const XMLElement& element ) +{ + CloseElement( CompactMode(element) ); + return true; +} + + +bool XMLPrinter::Visit( const XMLText& text ) +{ + PushText( text.Value(), text.CData() ); + return true; +} + + +bool XMLPrinter::Visit( const XMLComment& comment ) +{ + PushComment( comment.Value() ); + return true; +} + +bool XMLPrinter::Visit( const XMLDeclaration& declaration ) +{ + PushDeclaration( declaration.Value() ); + return true; +} + + +bool XMLPrinter::Visit( const XMLUnknown& unknown ) +{ + PushUnknown( unknown.Value() ); + return true; +} + +} // namespace tinyxml2 diff --git a/third_party/tinyxml2/tinyxml2.h b/third_party/tinyxml2/tinyxml2.h new file mode 100644 index 0000000..9626be8 --- /dev/null +++ b/third_party/tinyxml2/tinyxml2.h @@ -0,0 +1,2383 @@ +/* +Original code by Lee Thomason (www.grinninglizard.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef TINYXML2_INCLUDED +#define TINYXML2_INCLUDED + +#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__) +# include +# include +# include +# include +# include +# if defined(__PS3__) +# include +# endif +#else +# include +# include +# include +# include +# include +#endif +#include + +/* + gcc: + g++ -Wall -DTINYXML2_DEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe + + Formatting, Artistic Style: + AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h +*/ + +#if defined( _DEBUG ) || defined (__DEBUG__) +# ifndef TINYXML2_DEBUG +# define TINYXML2_DEBUG +# endif +#endif + +#ifdef _MSC_VER +# pragma warning(push) +# pragma warning(disable: 4251) +#endif + +#ifdef _MSC_VER +# ifdef TINYXML2_EXPORT +# define TINYXML2_LIB __declspec(dllexport) +# elif defined(TINYXML2_IMPORT) +# define TINYXML2_LIB __declspec(dllimport) +# else +# define TINYXML2_LIB +# endif +#elif __GNUC__ >= 4 +# define TINYXML2_LIB __attribute__((visibility("default"))) +#else +# define TINYXML2_LIB +#endif + + +#if !defined(TIXMLASSERT) +#if defined(TINYXML2_DEBUG) +# if defined(_MSC_VER) +# // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like +# define TIXMLASSERT( x ) do { if ( !((void)0,(x))) { __debugbreak(); } } while(false) +# elif defined (ANDROID_NDK) +# include +# define TIXMLASSERT( x ) do { if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); } } while(false) +# else +# include +# define TIXMLASSERT assert +# endif +#else +# define TIXMLASSERT( x ) do {} while(false) +#endif +#endif + +/* Versioning, past 1.0.14: + http://semver.org/ +*/ +static const int TIXML2_MAJOR_VERSION = 11; +static const int TIXML2_MINOR_VERSION = 0; +static const int TIXML2_PATCH_VERSION = 0; + +#define TINYXML2_MAJOR_VERSION 11 +#define TINYXML2_MINOR_VERSION 0 +#define TINYXML2_PATCH_VERSION 0 + +// A fixed element depth limit is problematic. There needs to be a +// limit to avoid a stack overflow. However, that limit varies per +// system, and the capacity of the stack. On the other hand, it's a trivial +// attack that can result from ill, malicious, or even correctly formed XML, +// so there needs to be a limit in place. +static const int TINYXML2_MAX_ELEMENT_DEPTH = 500; + +namespace tinyxml2 +{ +class XMLDocument; +class XMLElement; +class XMLAttribute; +class XMLComment; +class XMLText; +class XMLDeclaration; +class XMLUnknown; +class XMLPrinter; + +/* + A class that wraps strings. Normally stores the start and end + pointers into the XML file itself, and will apply normalization + and entity translation if actually read. Can also store (and memory + manage) a traditional char[] + + Isn't clear why TINYXML2_LIB is needed; but seems to fix #719 +*/ +class TINYXML2_LIB StrPair +{ +public: + enum Mode { + NEEDS_ENTITY_PROCESSING = 0x01, + NEEDS_NEWLINE_NORMALIZATION = 0x02, + NEEDS_WHITESPACE_COLLAPSING = 0x04, + + TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, + TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, + ATTRIBUTE_NAME = 0, + ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, + ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, + COMMENT = NEEDS_NEWLINE_NORMALIZATION + }; + + StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {} + ~StrPair(); + + void Set( char* start, char* end, int flags ) { + TIXMLASSERT( start ); + TIXMLASSERT( end ); + Reset(); + _start = start; + _end = end; + _flags = flags | NEEDS_FLUSH; + } + + const char* GetStr(); + + bool Empty() const { + return _start == _end; + } + + void SetInternedStr( const char* str ) { + Reset(); + _start = const_cast(str); + } + + void SetStr( const char* str, int flags=0 ); + + char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr ); + char* ParseName( char* in ); + + void TransferTo( StrPair* other ); + void Reset(); + +private: + void CollapseWhitespace(); + + enum { + NEEDS_FLUSH = 0x100, + NEEDS_DELETE = 0x200 + }; + + int _flags; + char* _start; + char* _end; + + StrPair( const StrPair& other ); // not supported + void operator=( const StrPair& other ); // not supported, use TransferTo() +}; + + +/* + A dynamic array of Plain Old Data. Doesn't support constructors, etc. + Has a small initial memory pool, so that low or no usage will not + cause a call to new/delete +*/ +template +class DynArray +{ +public: + DynArray() : + _mem( _pool ), + _allocated( INITIAL_SIZE ), + _size( 0 ) + { + } + + ~DynArray() { + if ( _mem != _pool ) { + delete [] _mem; + } + } + + void Clear() { + _size = 0; + } + + void Push( T t ) { + TIXMLASSERT( _size < INT_MAX ); + EnsureCapacity( _size+1 ); + _mem[_size] = t; + ++_size; + } + + T* PushArr( size_t count ) { + TIXMLASSERT( _size <= SIZE_MAX - count ); + EnsureCapacity( _size+count ); + T* ret = &_mem[_size]; + _size += count; + return ret; + } + + T Pop() { + TIXMLASSERT( _size > 0 ); + --_size; + return _mem[_size]; + } + + void PopArr( size_t count ) { + TIXMLASSERT( _size >= count ); + _size -= count; + } + + bool Empty() const { + return _size == 0; + } + + T& operator[](size_t i) { + TIXMLASSERT( i < _size ); + return _mem[i]; + } + + const T& operator[](size_t i) const { + TIXMLASSERT( i < _size ); + return _mem[i]; + } + + const T& PeekTop() const { + TIXMLASSERT( _size > 0 ); + return _mem[ _size - 1]; + } + + size_t Size() const { + TIXMLASSERT( _size >= 0 ); + return _size; + } + + size_t Capacity() const { + TIXMLASSERT( _allocated >= INITIAL_SIZE ); + return _allocated; + } + + void SwapRemove(size_t i) { + TIXMLASSERT(i < _size); + TIXMLASSERT(_size > 0); + _mem[i] = _mem[_size - 1]; + --_size; + } + + const T* Mem() const { + TIXMLASSERT( _mem ); + return _mem; + } + + T* Mem() { + TIXMLASSERT( _mem ); + return _mem; + } + +private: + DynArray( const DynArray& ); // not supported + void operator=( const DynArray& ); // not supported + + void EnsureCapacity( size_t cap ) { + TIXMLASSERT( cap > 0 ); + if ( cap > _allocated ) { + TIXMLASSERT( cap <= SIZE_MAX / 2 / sizeof(T)); + const size_t newAllocated = cap * 2; + T* newMem = new T[newAllocated]; + TIXMLASSERT( newAllocated >= _size ); + memcpy( newMem, _mem, sizeof(T) * _size ); // warning: not using constructors, only works for PODs + if ( _mem != _pool ) { + delete [] _mem; + } + _mem = newMem; + _allocated = newAllocated; + } + } + + T* _mem; + T _pool[INITIAL_SIZE]; + size_t _allocated; // objects allocated + size_t _size; // number objects in use +}; + + +/* + Parent virtual class of a pool for fast allocation + and deallocation of objects. +*/ +class MemPool +{ +public: + MemPool() {} + virtual ~MemPool() {} + + virtual size_t ItemSize() const = 0; + virtual void* Alloc() = 0; + virtual void Free( void* ) = 0; + virtual void SetTracked() = 0; +}; + + +/* + Template child class to create pools of the correct type. +*/ +template< size_t ITEM_SIZE > +class MemPoolT : public MemPool +{ +public: + MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {} + ~MemPoolT() { + MemPoolT< ITEM_SIZE >::Clear(); + } + + void Clear() { + // Delete the blocks. + while( !_blockPtrs.Empty()) { + Block* lastBlock = _blockPtrs.Pop(); + delete lastBlock; + } + _root = 0; + _currentAllocs = 0; + _nAllocs = 0; + _maxAllocs = 0; + _nUntracked = 0; + } + + virtual size_t ItemSize() const override { + return ITEM_SIZE; + } + size_t CurrentAllocs() const { + return _currentAllocs; + } + + virtual void* Alloc() override{ + if ( !_root ) { + // Need a new block. + Block* block = new Block; + _blockPtrs.Push( block ); + + Item* blockItems = block->items; + for( size_t i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) { + blockItems[i].next = &(blockItems[i + 1]); + } + blockItems[ITEMS_PER_BLOCK - 1].next = 0; + _root = blockItems; + } + Item* const result = _root; + TIXMLASSERT( result != 0 ); + _root = _root->next; + + ++_currentAllocs; + if ( _currentAllocs > _maxAllocs ) { + _maxAllocs = _currentAllocs; + } + ++_nAllocs; + ++_nUntracked; + return result; + } + + virtual void Free( void* mem ) override { + if ( !mem ) { + return; + } + --_currentAllocs; + Item* item = static_cast( mem ); +#ifdef TINYXML2_DEBUG + memset( item, 0xfe, sizeof( *item ) ); +#endif + item->next = _root; + _root = item; + } + void Trace( const char* name ) { + printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n", + name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs, + ITEM_SIZE, _nAllocs, _blockPtrs.Size() ); + } + + void SetTracked() override { + --_nUntracked; + } + + size_t Untracked() const { + return _nUntracked; + } + + // This number is perf sensitive. 4k seems like a good tradeoff on my machine. + // The test file is large, 170k. + // Release: VS2010 gcc(no opt) + // 1k: 4000 + // 2k: 4000 + // 4k: 3900 21000 + // 16k: 5200 + // 32k: 4300 + // 64k: 4000 21000 + // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK + // in private part if ITEMS_PER_BLOCK is private + enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE }; + +private: + MemPoolT( const MemPoolT& ); // not supported + void operator=( const MemPoolT& ); // not supported + + union Item { + Item* next; + char itemData[static_cast(ITEM_SIZE)]; + }; + struct Block { + Item items[ITEMS_PER_BLOCK]; + }; + DynArray< Block*, 10 > _blockPtrs; + Item* _root; + + size_t _currentAllocs; + size_t _nAllocs; + size_t _maxAllocs; + size_t _nUntracked; +}; + + + +/** + Implements the interface to the "Visitor pattern" (see the Accept() method.) + If you call the Accept() method, it requires being passed a XMLVisitor + class to handle callbacks. For nodes that contain other nodes (Document, Element) + you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs + are simply called with Visit(). + + If you return 'true' from a Visit method, recursive parsing will continue. If you return + false, no children of this node or its siblings will be visited. + + All flavors of Visit methods have a default implementation that returns 'true' (continue + visiting). You need to only override methods that are interesting to you. + + Generally Accept() is called on the XMLDocument, although all nodes support visiting. + + You should never change the document from a callback. + + @sa XMLNode::Accept() +*/ +class TINYXML2_LIB XMLVisitor +{ +public: + virtual ~XMLVisitor() {} + + /// Visit a document. + virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { + return true; + } + /// Visit a document. + virtual bool VisitExit( const XMLDocument& /*doc*/ ) { + return true; + } + + /// Visit an element. + virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { + return true; + } + /// Visit an element. + virtual bool VisitExit( const XMLElement& /*element*/ ) { + return true; + } + + /// Visit a declaration. + virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { + return true; + } + /// Visit a text node. + virtual bool Visit( const XMLText& /*text*/ ) { + return true; + } + /// Visit a comment node. + virtual bool Visit( const XMLComment& /*comment*/ ) { + return true; + } + /// Visit an unknown node. + virtual bool Visit( const XMLUnknown& /*unknown*/ ) { + return true; + } +}; + +// WARNING: must match XMLDocument::_errorNames[] +enum XMLError { + XML_SUCCESS = 0, + XML_NO_ATTRIBUTE, + XML_WRONG_ATTRIBUTE_TYPE, + XML_ERROR_FILE_NOT_FOUND, + XML_ERROR_FILE_COULD_NOT_BE_OPENED, + XML_ERROR_FILE_READ_ERROR, + XML_ERROR_PARSING_ELEMENT, + XML_ERROR_PARSING_ATTRIBUTE, + XML_ERROR_PARSING_TEXT, + XML_ERROR_PARSING_CDATA, + XML_ERROR_PARSING_COMMENT, + XML_ERROR_PARSING_DECLARATION, + XML_ERROR_PARSING_UNKNOWN, + XML_ERROR_EMPTY_DOCUMENT, + XML_ERROR_MISMATCHED_ELEMENT, + XML_ERROR_PARSING, + XML_CAN_NOT_CONVERT_TEXT, + XML_NO_TEXT_NODE, + XML_ELEMENT_DEPTH_EXCEEDED, + + XML_ERROR_COUNT +}; + + +/* + Utility functionality. +*/ +class TINYXML2_LIB XMLUtil +{ +public: + static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) { + TIXMLASSERT( p ); + + while( IsWhiteSpace(*p) ) { + if (curLineNumPtr && *p == '\n') { + ++(*curLineNumPtr); + } + ++p; + } + TIXMLASSERT( p ); + return p; + } + static char* SkipWhiteSpace( char* const p, int* curLineNumPtr ) { + return const_cast( SkipWhiteSpace( const_cast(p), curLineNumPtr ) ); + } + + // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't + // correct, but simple, and usually works. + static bool IsWhiteSpace( char p ) { + return !IsUTF8Continuation(p) && isspace( static_cast(p) ); + } + + inline static bool IsNameStartChar( unsigned char ch ) { + if ( ch >= 128 ) { + // This is a heuristic guess in attempt to not implement Unicode-aware isalpha() + return true; + } + if ( isalpha( ch ) ) { + return true; + } + return ch == ':' || ch == '_'; + } + + inline static bool IsNameChar( unsigned char ch ) { + return IsNameStartChar( ch ) + || isdigit( ch ) + || ch == '.' + || ch == '-'; + } + + inline static bool IsPrefixHex( const char* p) { + p = SkipWhiteSpace(p, 0); + return p && *p == '0' && ( *(p + 1) == 'x' || *(p + 1) == 'X'); + } + + inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) { + if ( p == q ) { + return true; + } + TIXMLASSERT( p ); + TIXMLASSERT( q ); + TIXMLASSERT( nChar >= 0 ); + return strncmp( p, q, static_cast(nChar) ) == 0; + } + + inline static bool IsUTF8Continuation( const char p ) { + return ( p & 0x80 ) != 0; + } + + static const char* ReadBOM( const char* p, bool* hasBOM ); + // p is the starting location, + // the UTF-8 value of the entity will be placed in value, and length filled in. + static const char* GetCharacterRef( const char* p, char* value, int* length ); + static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ); + + // converts primitive types to strings + static void ToStr( int v, char* buffer, int bufferSize ); + static void ToStr( unsigned v, char* buffer, int bufferSize ); + static void ToStr( bool v, char* buffer, int bufferSize ); + static void ToStr( float v, char* buffer, int bufferSize ); + static void ToStr( double v, char* buffer, int bufferSize ); + static void ToStr(int64_t v, char* buffer, int bufferSize); + static void ToStr(uint64_t v, char* buffer, int bufferSize); + + // converts strings to primitive types + static bool ToInt( const char* str, int* value ); + static bool ToUnsigned( const char* str, unsigned* value ); + static bool ToBool( const char* str, bool* value ); + static bool ToFloat( const char* str, float* value ); + static bool ToDouble( const char* str, double* value ); + static bool ToInt64(const char* str, int64_t* value); + static bool ToUnsigned64(const char* str, uint64_t* value); + // Changes what is serialized for a boolean value. + // Default to "true" and "false". Shouldn't be changed + // unless you have a special testing or compatibility need. + // Be careful: static, global, & not thread safe. + // Be sure to set static const memory as parameters. + static void SetBoolSerialization(const char* writeTrue, const char* writeFalse); + +private: + static const char* writeBoolTrue; + static const char* writeBoolFalse; +}; + + +/** XMLNode is a base class for every object that is in the + XML Document Object Model (DOM), except XMLAttributes. + Nodes have siblings, a parent, and children which can + be navigated. A node is always in a XMLDocument. + The type of a XMLNode can be queried, and it can + be cast to its more defined type. + + A XMLDocument allocates memory for all its Nodes. + When the XMLDocument gets deleted, all its Nodes + will also be deleted. + + @verbatim + A Document can contain: Element (container or leaf) + Comment (leaf) + Unknown (leaf) + Declaration( leaf ) + + An Element can contain: Element (container or leaf) + Text (leaf) + Attributes (not on tree) + Comment (leaf) + Unknown (leaf) + + @endverbatim +*/ +class TINYXML2_LIB XMLNode +{ + friend class XMLDocument; + friend class XMLElement; +public: + + /// Get the XMLDocument that owns this XMLNode. + const XMLDocument* GetDocument() const { + TIXMLASSERT( _document ); + return _document; + } + /// Get the XMLDocument that owns this XMLNode. + XMLDocument* GetDocument() { + TIXMLASSERT( _document ); + return _document; + } + + /// Safely cast to an Element, or null. + virtual XMLElement* ToElement() { + return 0; + } + /// Safely cast to Text, or null. + virtual XMLText* ToText() { + return 0; + } + /// Safely cast to a Comment, or null. + virtual XMLComment* ToComment() { + return 0; + } + /// Safely cast to a Document, or null. + virtual XMLDocument* ToDocument() { + return 0; + } + /// Safely cast to a Declaration, or null. + virtual XMLDeclaration* ToDeclaration() { + return 0; + } + /// Safely cast to an Unknown, or null. + virtual XMLUnknown* ToUnknown() { + return 0; + } + + virtual const XMLElement* ToElement() const { + return 0; + } + virtual const XMLText* ToText() const { + return 0; + } + virtual const XMLComment* ToComment() const { + return 0; + } + virtual const XMLDocument* ToDocument() const { + return 0; + } + virtual const XMLDeclaration* ToDeclaration() const { + return 0; + } + virtual const XMLUnknown* ToUnknown() const { + return 0; + } + + // ChildElementCount was originally suggested by msteiger on the sourceforge page for TinyXML and modified by KB1SPH for TinyXML-2. + + int ChildElementCount(const char *value) const; + + int ChildElementCount() const; + + /** The meaning of 'value' changes for the specific type. + @verbatim + Document: empty (NULL is returned, not an empty string) + Element: name of the element + Comment: the comment text + Unknown: the tag contents + Text: the text string + @endverbatim + */ + const char* Value() const; + + /** Set the Value of an XML node. + @sa Value() + */ + void SetValue( const char* val, bool staticMem=false ); + + /// Gets the line number the node is in, if the document was parsed from a file. + int GetLineNum() const { return _parseLineNum; } + + /// Get the parent of this node on the DOM. + const XMLNode* Parent() const { + return _parent; + } + + XMLNode* Parent() { + return _parent; + } + + /// Returns true if this node has no children. + bool NoChildren() const { + return !_firstChild; + } + + /// Get the first child node, or null if none exists. + const XMLNode* FirstChild() const { + return _firstChild; + } + + XMLNode* FirstChild() { + return _firstChild; + } + + /** Get the first child element, or optionally the first child + element with the specified name. + */ + const XMLElement* FirstChildElement( const char* name = 0 ) const; + + XMLElement* FirstChildElement( const char* name = 0 ) { + return const_cast(const_cast(this)->FirstChildElement( name )); + } + + /// Get the last child node, or null if none exists. + const XMLNode* LastChild() const { + return _lastChild; + } + + XMLNode* LastChild() { + return _lastChild; + } + + /** Get the last child element or optionally the last child + element with the specified name. + */ + const XMLElement* LastChildElement( const char* name = 0 ) const; + + XMLElement* LastChildElement( const char* name = 0 ) { + return const_cast(const_cast(this)->LastChildElement(name) ); + } + + /// Get the previous (left) sibling node of this node. + const XMLNode* PreviousSibling() const { + return _prev; + } + + XMLNode* PreviousSibling() { + return _prev; + } + + /// Get the previous (left) sibling element of this node, with an optionally supplied name. + const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ; + + XMLElement* PreviousSiblingElement( const char* name = 0 ) { + return const_cast(const_cast(this)->PreviousSiblingElement( name ) ); + } + + /// Get the next (right) sibling node of this node. + const XMLNode* NextSibling() const { + return _next; + } + + XMLNode* NextSibling() { + return _next; + } + + /// Get the next (right) sibling element of this node, with an optionally supplied name. + const XMLElement* NextSiblingElement( const char* name = 0 ) const; + + XMLElement* NextSiblingElement( const char* name = 0 ) { + return const_cast(const_cast(this)->NextSiblingElement( name ) ); + } + + /** + Add a child node as the last (right) child. + If the child node is already part of the document, + it is moved from its old location to the new location. + Returns the addThis argument or 0 if the node does not + belong to the same document. + */ + XMLNode* InsertEndChild( XMLNode* addThis ); + + XMLNode* LinkEndChild( XMLNode* addThis ) { + return InsertEndChild( addThis ); + } + /** + Add a child node as the first (left) child. + If the child node is already part of the document, + it is moved from its old location to the new location. + Returns the addThis argument or 0 if the node does not + belong to the same document. + */ + XMLNode* InsertFirstChild( XMLNode* addThis ); + /** + Add a node after the specified child node. + If the child node is already part of the document, + it is moved from its old location to the new location. + Returns the addThis argument or 0 if the afterThis node + is not a child of this node, or if the node does not + belong to the same document. + */ + XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ); + + /** + Delete all the children of this node. + */ + void DeleteChildren(); + + /** + Delete a child of this node. + */ + void DeleteChild( XMLNode* node ); + + /** + Make a copy of this node, but not its children. + You may pass in a Document pointer that will be + the owner of the new Node. If the 'document' is + null, then the node returned will be allocated + from the current Document. (this->GetDocument()) + + Note: if called on a XMLDocument, this will return null. + */ + virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0; + + /** + Make a copy of this node and all its children. + + If the 'target' is null, then the nodes will + be allocated in the current document. If 'target' + is specified, the memory will be allocated in the + specified XMLDocument. + + NOTE: This is probably not the correct tool to + copy a document, since XMLDocuments can have multiple + top level XMLNodes. You probably want to use + XMLDocument::DeepCopy() + */ + XMLNode* DeepClone( XMLDocument* target ) const; + + /** + Test if 2 nodes are the same, but don't test children. + The 2 nodes do not need to be in the same Document. + + Note: if called on a XMLDocument, this will return false. + */ + virtual bool ShallowEqual( const XMLNode* compare ) const = 0; + + /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the + XML tree will be conditionally visited and the host will be called back + via the XMLVisitor interface. + + This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse + the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this + interface versus any other.) + + The interface has been based on ideas from: + + - http://www.saxproject.org/ + - http://c2.com/cgi/wiki?HierarchicalVisitorPattern + + Which are both good references for "visiting". + + An example of using Accept(): + @verbatim + XMLPrinter printer; + tinyxmlDoc.Accept( &printer ); + const char* xmlcstr = printer.CStr(); + @endverbatim + */ + virtual bool Accept( XMLVisitor* visitor ) const = 0; + + /** + Set user data into the XMLNode. TinyXML-2 in + no way processes or interprets user data. + It is initially 0. + */ + void SetUserData(void* userData) { _userData = userData; } + + /** + Get user data set into the XMLNode. TinyXML-2 in + no way processes or interprets user data. + It is initially 0. + */ + void* GetUserData() const { return _userData; } + +protected: + explicit XMLNode( XMLDocument* ); + virtual ~XMLNode(); + + virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr); + + XMLDocument* _document; + XMLNode* _parent; + mutable StrPair _value; + int _parseLineNum; + + XMLNode* _firstChild; + XMLNode* _lastChild; + + XMLNode* _prev; + XMLNode* _next; + + void* _userData; + +private: + MemPool* _memPool; + void Unlink( XMLNode* child ); + static void DeleteNode( XMLNode* node ); + void InsertChildPreamble( XMLNode* insertThis ) const; + const XMLElement* ToElementWithName( const char* name ) const; + + XMLNode( const XMLNode& ); // not supported + XMLNode& operator=( const XMLNode& ); // not supported +}; + + +/** XML text. + + Note that a text node can have child element nodes, for example: + @verbatim + This is bold + @endverbatim + + A text node can have 2 ways to output the next. "normal" output + and CDATA. It will default to the mode it was parsed from the XML file and + you generally want to leave it alone, but you can change the output mode with + SetCData() and query it with CData(). +*/ +class TINYXML2_LIB XMLText : public XMLNode +{ + friend class XMLDocument; +public: + virtual bool Accept( XMLVisitor* visitor ) const override; + + virtual XMLText* ToText() override { + return this; + } + virtual const XMLText* ToText() const override { + return this; + } + + /// Declare whether this should be CDATA or standard text. + void SetCData( bool isCData ) { + _isCData = isCData; + } + /// Returns true if this is a CDATA text element. + bool CData() const { + return _isCData; + } + + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + explicit XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {} + virtual ~XMLText() {} + + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override; + +private: + bool _isCData; + + XMLText( const XMLText& ); // not supported + XMLText& operator=( const XMLText& ); // not supported +}; + + +/** An XML Comment. */ +class TINYXML2_LIB XMLComment : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLComment* ToComment() override { + return this; + } + virtual const XMLComment* ToComment() const override { + return this; + } + + virtual bool Accept( XMLVisitor* visitor ) const override; + + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + explicit XMLComment( XMLDocument* doc ); + virtual ~XMLComment(); + + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr) override; + +private: + XMLComment( const XMLComment& ); // not supported + XMLComment& operator=( const XMLComment& ); // not supported +}; + + +/** In correct XML the declaration is the first entry in the file. + @verbatim + + @endverbatim + + TinyXML-2 will happily read or write files without a declaration, + however. + + The text of the declaration isn't interpreted. It is parsed + and written as a string. +*/ +class TINYXML2_LIB XMLDeclaration : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLDeclaration* ToDeclaration() override { + return this; + } + virtual const XMLDeclaration* ToDeclaration() const override { + return this; + } + + virtual bool Accept( XMLVisitor* visitor ) const override; + + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + explicit XMLDeclaration( XMLDocument* doc ); + virtual ~XMLDeclaration(); + + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override; + +private: + XMLDeclaration( const XMLDeclaration& ); // not supported + XMLDeclaration& operator=( const XMLDeclaration& ); // not supported +}; + + +/** Any tag that TinyXML-2 doesn't recognize is saved as an + unknown. It is a tag of text, but should not be modified. + It will be written back to the XML, unchanged, when the file + is saved. + + DTD tags get thrown into XMLUnknowns. +*/ +class TINYXML2_LIB XMLUnknown : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLUnknown* ToUnknown() override { + return this; + } + virtual const XMLUnknown* ToUnknown() const override { + return this; + } + + virtual bool Accept( XMLVisitor* visitor ) const override; + + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + explicit XMLUnknown( XMLDocument* doc ); + virtual ~XMLUnknown(); + + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override; + +private: + XMLUnknown( const XMLUnknown& ); // not supported + XMLUnknown& operator=( const XMLUnknown& ); // not supported +}; + + + +/** An attribute is a name-value pair. Elements have an arbitrary + number of attributes, each with a unique name. + + @note The attributes are not XMLNodes. You may only query the + Next() attribute in a list. +*/ +class TINYXML2_LIB XMLAttribute +{ + friend class XMLElement; +public: + /// The name of the attribute. + const char* Name() const; + + /// The value of the attribute. + const char* Value() const; + + /// Gets the line number the attribute is in, if the document was parsed from a file. + int GetLineNum() const { return _parseLineNum; } + + /// The next attribute in the list. + const XMLAttribute* Next() const { + return _next; + } + + /** IntValue interprets the attribute as an integer, and returns the value. + If the value isn't an integer, 0 will be returned. There is no error checking; + use QueryIntValue() if you need error checking. + */ + int IntValue() const { + int i = 0; + QueryIntValue(&i); + return i; + } + + int64_t Int64Value() const { + int64_t i = 0; + QueryInt64Value(&i); + return i; + } + + uint64_t Unsigned64Value() const { + uint64_t i = 0; + QueryUnsigned64Value(&i); + return i; + } + + /// Query as an unsigned integer. See IntValue() + unsigned UnsignedValue() const { + unsigned i=0; + QueryUnsignedValue( &i ); + return i; + } + /// Query as a boolean. See IntValue() + bool BoolValue() const { + bool b=false; + QueryBoolValue( &b ); + return b; + } + /// Query as a double. See IntValue() + double DoubleValue() const { + double d=0; + QueryDoubleValue( &d ); + return d; + } + /// Query as a float. See IntValue() + float FloatValue() const { + float f=0; + QueryFloatValue( &f ); + return f; + } + + /** QueryIntValue interprets the attribute as an integer, and returns the value + in the provided parameter. The function will return XML_SUCCESS on success, + and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful. + */ + XMLError QueryIntValue( int* value ) const; + /// See QueryIntValue + XMLError QueryUnsignedValue( unsigned int* value ) const; + /// See QueryIntValue + XMLError QueryInt64Value(int64_t* value) const; + /// See QueryIntValue + XMLError QueryUnsigned64Value(uint64_t* value) const; + /// See QueryIntValue + XMLError QueryBoolValue( bool* value ) const; + /// See QueryIntValue + XMLError QueryDoubleValue( double* value ) const; + /// See QueryIntValue + XMLError QueryFloatValue( float* value ) const; + + /// Set the attribute to a string value. + void SetAttribute( const char* value ); + /// Set the attribute to value. + void SetAttribute( int value ); + /// Set the attribute to value. + void SetAttribute( unsigned value ); + /// Set the attribute to value. + void SetAttribute(int64_t value); + /// Set the attribute to value. + void SetAttribute(uint64_t value); + /// Set the attribute to value. + void SetAttribute( bool value ); + /// Set the attribute to value. + void SetAttribute( double value ); + /// Set the attribute to value. + void SetAttribute( float value ); + +private: + enum { BUF_SIZE = 200 }; + + XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {} + virtual ~XMLAttribute() {} + + XMLAttribute( const XMLAttribute& ); // not supported + void operator=( const XMLAttribute& ); // not supported + void SetName( const char* name ); + + char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr ); + + mutable StrPair _name; + mutable StrPair _value; + int _parseLineNum; + XMLAttribute* _next; + MemPool* _memPool; +}; + + +/** The element is a container class. It has a value, the element name, + and can contain other elements, text, comments, and unknowns. + Elements also contain an arbitrary number of attributes. +*/ +class TINYXML2_LIB XMLElement : public XMLNode +{ + friend class XMLDocument; +public: + /// Get the name of an element (which is the Value() of the node.) + const char* Name() const { + return Value(); + } + /// Set the name of the element. + void SetName( const char* str, bool staticMem=false ) { + SetValue( str, staticMem ); + } + + virtual XMLElement* ToElement() override { + return this; + } + virtual const XMLElement* ToElement() const override { + return this; + } + virtual bool Accept( XMLVisitor* visitor ) const override; + + /** Given an attribute name, Attribute() returns the value + for the attribute of that name, or null if none + exists. For example: + + @verbatim + const char* value = ele->Attribute( "foo" ); + @endverbatim + + The 'value' parameter is normally null. However, if specified, + the attribute will only be returned if the 'name' and 'value' + match. This allow you to write code: + + @verbatim + if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar(); + @endverbatim + + rather than: + @verbatim + if ( ele->Attribute( "foo" ) ) { + if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar(); + } + @endverbatim + */ + const char* Attribute( const char* name, const char* value=0 ) const; + + /** Given an attribute name, IntAttribute() returns the value + of the attribute interpreted as an integer. The default + value will be returned if the attribute isn't present, + or if there is an error. (For a method with error + checking, see QueryIntAttribute()). + */ + int IntAttribute(const char* name, int defaultValue = 0) const; + /// See IntAttribute() + unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const; + /// See IntAttribute() + int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const; + /// See IntAttribute() + uint64_t Unsigned64Attribute(const char* name, uint64_t defaultValue = 0) const; + /// See IntAttribute() + bool BoolAttribute(const char* name, bool defaultValue = false) const; + /// See IntAttribute() + double DoubleAttribute(const char* name, double defaultValue = 0) const; + /// See IntAttribute() + float FloatAttribute(const char* name, float defaultValue = 0) const; + + /** Given an attribute name, QueryIntAttribute() returns + XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion + can't be performed, or XML_NO_ATTRIBUTE if the attribute + doesn't exist. If successful, the result of the conversion + will be written to 'value'. If not successful, nothing will + be written to 'value'. This allows you to provide default + value: + + @verbatim + int value = 10; + QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10 + @endverbatim + */ + XMLError QueryIntAttribute( const char* name, int* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryIntValue( value ); + } + + /// See QueryIntAttribute() + XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryUnsignedValue( value ); + } + + /// See QueryIntAttribute() + XMLError QueryInt64Attribute(const char* name, int64_t* value) const { + const XMLAttribute* a = FindAttribute(name); + if (!a) { + return XML_NO_ATTRIBUTE; + } + return a->QueryInt64Value(value); + } + + /// See QueryIntAttribute() + XMLError QueryUnsigned64Attribute(const char* name, uint64_t* value) const { + const XMLAttribute* a = FindAttribute(name); + if(!a) { + return XML_NO_ATTRIBUTE; + } + return a->QueryUnsigned64Value(value); + } + + /// See QueryIntAttribute() + XMLError QueryBoolAttribute( const char* name, bool* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryBoolValue( value ); + } + /// See QueryIntAttribute() + XMLError QueryDoubleAttribute( const char* name, double* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryDoubleValue( value ); + } + /// See QueryIntAttribute() + XMLError QueryFloatAttribute( const char* name, float* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryFloatValue( value ); + } + + /// See QueryIntAttribute() + XMLError QueryStringAttribute(const char* name, const char** value) const { + const XMLAttribute* a = FindAttribute(name); + if (!a) { + return XML_NO_ATTRIBUTE; + } + *value = a->Value(); + return XML_SUCCESS; + } + + + + /** Given an attribute name, QueryAttribute() returns + XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion + can't be performed, or XML_NO_ATTRIBUTE if the attribute + doesn't exist. It is overloaded for the primitive types, + and is a generally more convenient replacement of + QueryIntAttribute() and related functions. + + If successful, the result of the conversion + will be written to 'value'. If not successful, nothing will + be written to 'value'. This allows you to provide default + value: + + @verbatim + int value = 10; + QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10 + @endverbatim + */ + XMLError QueryAttribute( const char* name, int* value ) const { + return QueryIntAttribute( name, value ); + } + + XMLError QueryAttribute( const char* name, unsigned int* value ) const { + return QueryUnsignedAttribute( name, value ); + } + + XMLError QueryAttribute(const char* name, int64_t* value) const { + return QueryInt64Attribute(name, value); + } + + XMLError QueryAttribute(const char* name, uint64_t* value) const { + return QueryUnsigned64Attribute(name, value); + } + + XMLError QueryAttribute( const char* name, bool* value ) const { + return QueryBoolAttribute( name, value ); + } + + XMLError QueryAttribute( const char* name, double* value ) const { + return QueryDoubleAttribute( name, value ); + } + + XMLError QueryAttribute( const char* name, float* value ) const { + return QueryFloatAttribute( name, value ); + } + + XMLError QueryAttribute(const char* name, const char** value) const { + return QueryStringAttribute(name, value); + } + + /// Sets the named attribute to value. + void SetAttribute( const char* name, const char* value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, int value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, unsigned value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + + /// Sets the named attribute to value. + void SetAttribute(const char* name, int64_t value) { + XMLAttribute* a = FindOrCreateAttribute(name); + a->SetAttribute(value); + } + + /// Sets the named attribute to value. + void SetAttribute(const char* name, uint64_t value) { + XMLAttribute* a = FindOrCreateAttribute(name); + a->SetAttribute(value); + } + + /// Sets the named attribute to value. + void SetAttribute( const char* name, bool value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, double value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, float value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + + /** + Delete an attribute. + */ + void DeleteAttribute( const char* name ); + + /// Return the first attribute in the list. + const XMLAttribute* FirstAttribute() const { + return _rootAttribute; + } + /// Query a specific attribute in the list. + const XMLAttribute* FindAttribute( const char* name ) const; + + /** Convenience function for easy access to the text inside an element. Although easy + and concise, GetText() is limited compared to getting the XMLText child + and accessing it directly. + + If the first child of 'this' is a XMLText, the GetText() + returns the character string of the Text node, else null is returned. + + This is a convenient method for getting the text of simple contained text: + @verbatim + This is text + const char* str = fooElement->GetText(); + @endverbatim + + 'str' will be a pointer to "This is text". + + Note that this function can be misleading. If the element foo was created from + this XML: + @verbatim + This is text + @endverbatim + + then the value of str would be null. The first child node isn't a text node, it is + another element. From this XML: + @verbatim + This is text + @endverbatim + GetText() will return "This is ". + */ + const char* GetText() const; + + /** Convenience function for easy access to the text inside an element. Although easy + and concise, SetText() is limited compared to creating an XMLText child + and mutating it directly. + + If the first child of 'this' is a XMLText, SetText() sets its value to + the given string, otherwise it will create a first child that is an XMLText. + + This is a convenient method for setting the text of simple contained text: + @verbatim + This is text + fooElement->SetText( "Hullaballoo!" ); + Hullaballoo! + @endverbatim + + Note that this function can be misleading. If the element foo was created from + this XML: + @verbatim + This is text + @endverbatim + + then it will not change "This is text", but rather prefix it with a text element: + @verbatim + Hullaballoo!This is text + @endverbatim + + For this XML: + @verbatim + + @endverbatim + SetText() will generate + @verbatim + Hullaballoo! + @endverbatim + */ + void SetText( const char* inText ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( int value ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( unsigned value ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText(int64_t value); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText(uint64_t value); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( bool value ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( double value ); + /// Convenience method for setting text inside an element. See SetText() for important limitations. + void SetText( float value ); + + /** + Convenience method to query the value of a child text node. This is probably best + shown by example. Given you have a document is this form: + @verbatim + + 1 + 1.4 + + @endverbatim + + The QueryIntText() and similar functions provide a safe and easier way to get to the + "value" of x and y. + + @verbatim + int x = 0; + float y = 0; // types of x and y are contrived for example + const XMLElement* xElement = pointElement->FirstChildElement( "x" ); + const XMLElement* yElement = pointElement->FirstChildElement( "y" ); + xElement->QueryIntText( &x ); + yElement->QueryFloatText( &y ); + @endverbatim + + @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted + to the requested type, and XML_NO_TEXT_NODE if there is no child text to query. + + */ + XMLError QueryIntText( int* ival ) const; + /// See QueryIntText() + XMLError QueryUnsignedText( unsigned* uval ) const; + /// See QueryIntText() + XMLError QueryInt64Text(int64_t* uval) const; + /// See QueryIntText() + XMLError QueryUnsigned64Text(uint64_t* uval) const; + /// See QueryIntText() + XMLError QueryBoolText( bool* bval ) const; + /// See QueryIntText() + XMLError QueryDoubleText( double* dval ) const; + /// See QueryIntText() + XMLError QueryFloatText( float* fval ) const; + + int IntText(int defaultValue = 0) const; + + /// See QueryIntText() + unsigned UnsignedText(unsigned defaultValue = 0) const; + /// See QueryIntText() + int64_t Int64Text(int64_t defaultValue = 0) const; + /// See QueryIntText() + uint64_t Unsigned64Text(uint64_t defaultValue = 0) const; + /// See QueryIntText() + bool BoolText(bool defaultValue = false) const; + /// See QueryIntText() + double DoubleText(double defaultValue = 0) const; + /// See QueryIntText() + float FloatText(float defaultValue = 0) const; + + /** + Convenience method to create a new XMLElement and add it as last (right) + child of this node. Returns the created and inserted element. + */ + XMLElement* InsertNewChildElement(const char* name); + /// See InsertNewChildElement() + XMLComment* InsertNewComment(const char* comment); + /// See InsertNewChildElement() + XMLText* InsertNewText(const char* text); + /// See InsertNewChildElement() + XMLDeclaration* InsertNewDeclaration(const char* text); + /// See InsertNewChildElement() + XMLUnknown* InsertNewUnknown(const char* text); + + + // internal: + enum ElementClosingType { + OPEN, // + CLOSED, // + CLOSING // + }; + ElementClosingType ClosingType() const { + return _closingType; + } + virtual XMLNode* ShallowClone( XMLDocument* document ) const override; + virtual bool ShallowEqual( const XMLNode* compare ) const override; + +protected: + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override; + +private: + XMLElement( XMLDocument* doc ); + virtual ~XMLElement(); + XMLElement( const XMLElement& ); // not supported + void operator=( const XMLElement& ); // not supported + + XMLAttribute* FindOrCreateAttribute( const char* name ); + char* ParseAttributes( char* p, int* curLineNumPtr ); + static void DeleteAttribute( XMLAttribute* attribute ); + XMLAttribute* CreateAttribute(); + + enum { BUF_SIZE = 200 }; + ElementClosingType _closingType; + // The attribute list is ordered; there is no 'lastAttribute' + // because the list needs to be scanned for dupes before adding + // a new attribute. + XMLAttribute* _rootAttribute; +}; + + +enum Whitespace { + PRESERVE_WHITESPACE, + COLLAPSE_WHITESPACE, + PEDANTIC_WHITESPACE +}; + + +/** A Document binds together all the functionality. + It can be saved, loaded, and printed to the screen. + All Nodes are connected and allocated to a Document. + If the Document is deleted, all its Nodes are also deleted. +*/ +class TINYXML2_LIB XMLDocument : public XMLNode +{ + friend class XMLElement; + // Gives access to SetError and Push/PopDepth, but over-access for everything else. + // Wishing C++ had "internal" scope. + friend class XMLNode; + friend class XMLText; + friend class XMLComment; + friend class XMLDeclaration; + friend class XMLUnknown; +public: + /// constructor + XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE ); + ~XMLDocument(); + + virtual XMLDocument* ToDocument() override { + TIXMLASSERT( this == _document ); + return this; + } + virtual const XMLDocument* ToDocument() const override { + TIXMLASSERT( this == _document ); + return this; + } + + /** + Parse an XML file from a character string. + Returns XML_SUCCESS (0) on success, or + an errorID. + + You may optionally pass in the 'nBytes', which is + the number of bytes which will be parsed. If not + specified, TinyXML-2 will assume 'xml' points to a + null terminated string. + */ + XMLError Parse( const char* xml, size_t nBytes=static_cast(-1) ); + + /** + Load an XML file from disk. + Returns XML_SUCCESS (0) on success, or + an errorID. + */ + XMLError LoadFile( const char* filename ); + + /** + Load an XML file from disk. You are responsible + for providing and closing the FILE*. + + NOTE: The file should be opened as binary ("rb") + not text in order for TinyXML-2 to correctly + do newline normalization. + + Returns XML_SUCCESS (0) on success, or + an errorID. + */ + XMLError LoadFile( FILE* ); + + /** + Save the XML file to disk. + Returns XML_SUCCESS (0) on success, or + an errorID. + */ + XMLError SaveFile( const char* filename, bool compact = false ); + + /** + Save the XML file to disk. You are responsible + for providing and closing the FILE*. + + Returns XML_SUCCESS (0) on success, or + an errorID. + */ + XMLError SaveFile( FILE* fp, bool compact = false ); + + bool ProcessEntities() const { + return _processEntities; + } + Whitespace WhitespaceMode() const { + return _whitespaceMode; + } + + /** + Returns true if this document has a leading Byte Order Mark of UTF8. + */ + bool HasBOM() const { + return _writeBOM; + } + /** Sets whether to write the BOM when writing the file. + */ + void SetBOM( bool useBOM ) { + _writeBOM = useBOM; + } + + /** Return the root element of DOM. Equivalent to FirstChildElement(). + To get the first node, use FirstChild(). + */ + XMLElement* RootElement() { + return FirstChildElement(); + } + const XMLElement* RootElement() const { + return FirstChildElement(); + } + + /** Print the Document. If the Printer is not provided, it will + print to stdout. If you provide Printer, this can print to a file: + @verbatim + XMLPrinter printer( fp ); + doc.Print( &printer ); + @endverbatim + + Or you can use a printer to print to memory: + @verbatim + XMLPrinter printer; + doc.Print( &printer ); + // printer.CStr() has a const char* to the XML + @endverbatim + */ + void Print( XMLPrinter* streamer=0 ) const; + virtual bool Accept( XMLVisitor* visitor ) const override; + + /** + Create a new Element associated with + this Document. The memory for the Element + is managed by the Document. + */ + XMLElement* NewElement( const char* name ); + /** + Create a new Comment associated with + this Document. The memory for the Comment + is managed by the Document. + */ + XMLComment* NewComment( const char* comment ); + /** + Create a new Text associated with + this Document. The memory for the Text + is managed by the Document. + */ + XMLText* NewText( const char* text ); + /** + Create a new Declaration associated with + this Document. The memory for the object + is managed by the Document. + + If the 'text' param is null, the standard + declaration is used.: + @verbatim + + @endverbatim + */ + XMLDeclaration* NewDeclaration( const char* text=0 ); + /** + Create a new Unknown associated with + this Document. The memory for the object + is managed by the Document. + */ + XMLUnknown* NewUnknown( const char* text ); + + /** + Delete a node associated with this document. + It will be unlinked from the DOM. + */ + void DeleteNode( XMLNode* node ); + + /// Clears the error flags. + void ClearError(); + + /// Return true if there was an error parsing the document. + bool Error() const { + return _errorID != XML_SUCCESS; + } + /// Return the errorID. + XMLError ErrorID() const { + return _errorID; + } + const char* ErrorName() const; + static const char* ErrorIDToName(XMLError errorID); + + /** Returns a "long form" error description. A hopefully helpful + diagnostic with location, line number, and/or additional info. + */ + const char* ErrorStr() const; + + /// A (trivial) utility function that prints the ErrorStr() to stdout. + void PrintError() const; + + /// Return the line where the error occurred, or zero if unknown. + int ErrorLineNum() const + { + return _errorLineNum; + } + + /// Clear the document, resetting it to the initial state. + void Clear(); + + /** + Copies this document to a target document. + The target will be completely cleared before the copy. + If you want to copy a sub-tree, see XMLNode::DeepClone(). + + NOTE: that the 'target' must be non-null. + */ + void DeepCopy(XMLDocument* target) const; + + // internal + char* Identify( char* p, XMLNode** node, bool first ); + + // internal + void MarkInUse(const XMLNode* const); + + virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const override{ + return 0; + } + virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const override{ + return false; + } + +private: + XMLDocument( const XMLDocument& ); // not supported + void operator=( const XMLDocument& ); // not supported + + bool _writeBOM; + bool _processEntities; + XMLError _errorID; + Whitespace _whitespaceMode; + mutable StrPair _errorStr; + int _errorLineNum; + char* _charBuffer; + int _parseCurLineNum; + int _parsingDepth; + // Memory tracking does add some overhead. + // However, the code assumes that you don't + // have a bunch of unlinked nodes around. + // Therefore it takes less memory to track + // in the document vs. a linked list in the XMLNode, + // and the performance is the same. + DynArray _unlinked; + + MemPoolT< sizeof(XMLElement) > _elementPool; + MemPoolT< sizeof(XMLAttribute) > _attributePool; + MemPoolT< sizeof(XMLText) > _textPool; + MemPoolT< sizeof(XMLComment) > _commentPool; + + static const char* _errorNames[XML_ERROR_COUNT]; + + void Parse(); + + void SetError( XMLError error, int lineNum, const char* format, ... ); + + // Something of an obvious security hole, once it was discovered. + // Either an ill-formed XML or an excessively deep one can overflow + // the stack. Track stack depth, and error out if needed. + class DepthTracker { + public: + explicit DepthTracker(XMLDocument * document) { + this->_document = document; + document->PushDepth(); + } + ~DepthTracker() { + _document->PopDepth(); + } + private: + XMLDocument * _document; + }; + void PushDepth(); + void PopDepth(); + + template + NodeType* CreateUnlinkedNode( MemPoolT& pool ); +}; + +template +inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT& pool ) +{ + TIXMLASSERT( sizeof( NodeType ) == PoolElementSize ); + TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() ); + NodeType* returnNode = new (pool.Alloc()) NodeType( this ); + TIXMLASSERT( returnNode ); + returnNode->_memPool = &pool; + + _unlinked.Push(returnNode); + return returnNode; +} + +/** + A XMLHandle is a class that wraps a node pointer with null checks; this is + an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2 + DOM structure. It is a separate utility class. + + Take an example: + @verbatim + + + + + + + @endverbatim + + Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very + easy to write a *lot* of code that looks like: + + @verbatim + XMLElement* root = document.FirstChildElement( "Document" ); + if ( root ) + { + XMLElement* element = root->FirstChildElement( "Element" ); + if ( element ) + { + XMLElement* child = element->FirstChildElement( "Child" ); + if ( child ) + { + XMLElement* child2 = child->NextSiblingElement( "Child" ); + if ( child2 ) + { + // Finally do something useful. + @endverbatim + + And that doesn't even cover "else" cases. XMLHandle addresses the verbosity + of such code. A XMLHandle checks for null pointers so it is perfectly safe + and correct to use: + + @verbatim + XMLHandle docHandle( &document ); + XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement(); + if ( child2 ) + { + // do something useful + @endverbatim + + Which is MUCH more concise and useful. + + It is also safe to copy handles - internally they are nothing more than node pointers. + @verbatim + XMLHandle handleCopy = handle; + @endverbatim + + See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects. +*/ +class TINYXML2_LIB XMLHandle +{ +public: + /// Create a handle from any node (at any depth of the tree.) This can be a null pointer. + explicit XMLHandle( XMLNode* node ) : _node( node ) { + } + /// Create a handle from a node. + explicit XMLHandle( XMLNode& node ) : _node( &node ) { + } + /// Copy constructor + XMLHandle( const XMLHandle& ref ) : _node( ref._node ) { + } + /// Assignment + XMLHandle& operator=( const XMLHandle& ref ) { + _node = ref._node; + return *this; + } + + /// Get the first child of this handle. + XMLHandle FirstChild() { + return XMLHandle( _node ? _node->FirstChild() : 0 ); + } + /// Get the first child element of this handle. + XMLHandle FirstChildElement( const char* name = 0 ) { + return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 ); + } + /// Get the last child of this handle. + XMLHandle LastChild() { + return XMLHandle( _node ? _node->LastChild() : 0 ); + } + /// Get the last child element of this handle. + XMLHandle LastChildElement( const char* name = 0 ) { + return XMLHandle( _node ? _node->LastChildElement( name ) : 0 ); + } + /// Get the previous sibling of this handle. + XMLHandle PreviousSibling() { + return XMLHandle( _node ? _node->PreviousSibling() : 0 ); + } + /// Get the previous sibling element of this handle. + XMLHandle PreviousSiblingElement( const char* name = 0 ) { + return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 ); + } + /// Get the next sibling of this handle. + XMLHandle NextSibling() { + return XMLHandle( _node ? _node->NextSibling() : 0 ); + } + /// Get the next sibling element of this handle. + XMLHandle NextSiblingElement( const char* name = 0 ) { + return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 ); + } + + /// Safe cast to XMLNode. This can return null. + XMLNode* ToNode() { + return _node; + } + /// Safe cast to XMLElement. This can return null. + XMLElement* ToElement() { + return ( _node ? _node->ToElement() : 0 ); + } + /// Safe cast to XMLText. This can return null. + XMLText* ToText() { + return ( _node ? _node->ToText() : 0 ); + } + /// Safe cast to XMLUnknown. This can return null. + XMLUnknown* ToUnknown() { + return ( _node ? _node->ToUnknown() : 0 ); + } + /// Safe cast to XMLDeclaration. This can return null. + XMLDeclaration* ToDeclaration() { + return ( _node ? _node->ToDeclaration() : 0 ); + } + +private: + XMLNode* _node; +}; + + +/** + A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the + same in all regards, except for the 'const' qualifiers. See XMLHandle for API. +*/ +class TINYXML2_LIB XMLConstHandle +{ +public: + explicit XMLConstHandle( const XMLNode* node ) : _node( node ) { + } + explicit XMLConstHandle( const XMLNode& node ) : _node( &node ) { + } + XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) { + } + + XMLConstHandle& operator=( const XMLConstHandle& ref ) { + _node = ref._node; + return *this; + } + + const XMLConstHandle FirstChild() const { + return XMLConstHandle( _node ? _node->FirstChild() : 0 ); + } + const XMLConstHandle FirstChildElement( const char* name = 0 ) const { + return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 ); + } + const XMLConstHandle LastChild() const { + return XMLConstHandle( _node ? _node->LastChild() : 0 ); + } + const XMLConstHandle LastChildElement( const char* name = 0 ) const { + return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 ); + } + const XMLConstHandle PreviousSibling() const { + return XMLConstHandle( _node ? _node->PreviousSibling() : 0 ); + } + const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const { + return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 ); + } + const XMLConstHandle NextSibling() const { + return XMLConstHandle( _node ? _node->NextSibling() : 0 ); + } + const XMLConstHandle NextSiblingElement( const char* name = 0 ) const { + return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 ); + } + + + const XMLNode* ToNode() const { + return _node; + } + const XMLElement* ToElement() const { + return ( _node ? _node->ToElement() : 0 ); + } + const XMLText* ToText() const { + return ( _node ? _node->ToText() : 0 ); + } + const XMLUnknown* ToUnknown() const { + return ( _node ? _node->ToUnknown() : 0 ); + } + const XMLDeclaration* ToDeclaration() const { + return ( _node ? _node->ToDeclaration() : 0 ); + } + +private: + const XMLNode* _node; +}; + + +/** + Printing functionality. The XMLPrinter gives you more + options than the XMLDocument::Print() method. + + It can: + -# Print to memory. + -# Print to a file you provide. + -# Print XML without a XMLDocument. + + Print to Memory + + @verbatim + XMLPrinter printer; + doc.Print( &printer ); + SomeFunction( printer.CStr() ); + @endverbatim + + Print to a File + + You provide the file pointer. + @verbatim + XMLPrinter printer( fp ); + doc.Print( &printer ); + @endverbatim + + Print without a XMLDocument + + When loading, an XML parser is very useful. However, sometimes + when saving, it just gets in the way. The code is often set up + for streaming, and constructing the DOM is just overhead. + + The Printer supports the streaming case. The following code + prints out a trivially simple XML file without ever creating + an XML document. + + @verbatim + XMLPrinter printer( fp ); + printer.OpenElement( "foo" ); + printer.PushAttribute( "foo", "bar" ); + printer.CloseElement(); + @endverbatim +*/ +class TINYXML2_LIB XMLPrinter : public XMLVisitor +{ +public: + /** Construct the printer. If the FILE* is specified, + this will print to the FILE. Else it will print + to memory, and the result is available in CStr(). + If 'compact' is set to true, then output is created + with only required whitespace and newlines. + */ + XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 ); + virtual ~XMLPrinter() {} + + /** If streaming, write the BOM and declaration. */ + void PushHeader( bool writeBOM, bool writeDeclaration ); + /** If streaming, start writing an element. + The element must be closed with CloseElement() + */ + void OpenElement( const char* name, bool compactMode=false ); + /// If streaming, add an attribute to an open element. + void PushAttribute( const char* name, const char* value ); + void PushAttribute( const char* name, int value ); + void PushAttribute( const char* name, unsigned value ); + void PushAttribute( const char* name, int64_t value ); + void PushAttribute( const char* name, uint64_t value ); + void PushAttribute( const char* name, bool value ); + void PushAttribute( const char* name, double value ); + /// If streaming, close the Element. + virtual void CloseElement( bool compactMode=false ); + + /// Add a text node. + void PushText( const char* text, bool cdata=false ); + /// Add a text node from an integer. + void PushText( int value ); + /// Add a text node from an unsigned. + void PushText( unsigned value ); + /// Add a text node from a signed 64bit integer. + void PushText( int64_t value ); + /// Add a text node from an unsigned 64bit integer. + void PushText( uint64_t value ); + /// Add a text node from a bool. + void PushText( bool value ); + /// Add a text node from a float. + void PushText( float value ); + /// Add a text node from a double. + void PushText( double value ); + + /// Add a comment + void PushComment( const char* comment ); + + void PushDeclaration( const char* value ); + void PushUnknown( const char* value ); + + virtual bool VisitEnter( const XMLDocument& /*doc*/ ) override; + virtual bool VisitExit( const XMLDocument& /*doc*/ ) override { + return true; + } + + virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) override; + virtual bool VisitExit( const XMLElement& element ) override; + + virtual bool Visit( const XMLText& text ) override; + virtual bool Visit( const XMLComment& comment ) override; + virtual bool Visit( const XMLDeclaration& declaration ) override; + virtual bool Visit( const XMLUnknown& unknown ) override; + + /** + If in print to memory mode, return a pointer to + the XML file in memory. + */ + const char* CStr() const { + return _buffer.Mem(); + } + /** + If in print to memory mode, return the size + of the XML file in memory. (Note the size returned + includes the terminating null.) + */ + size_t CStrSize() const { + return _buffer.Size(); + } + /** + If in print to memory mode, reset the buffer to the + beginning. + */ + void ClearBuffer( bool resetToFirstElement = true ) { + _buffer.Clear(); + _buffer.Push(0); + _firstElement = resetToFirstElement; + } + +protected: + virtual bool CompactMode( const XMLElement& ) { return _compactMode; } + + /** Prints out the space before an element. You may override to change + the space and tabs used. A PrintSpace() override should call Print(). + */ + virtual void PrintSpace( int depth ); + virtual void Print( const char* format, ... ); + virtual void Write( const char* data, size_t size ); + virtual void Putc( char ch ); + + inline void Write(const char* data) { Write(data, strlen(data)); } + + void SealElementIfJustOpened(); + bool _elementJustOpened; + DynArray< const char*, 10 > _stack; + +private: + /** + Prepares to write a new node. This includes sealing an element that was + just opened, and writing any whitespace necessary if not in compact mode. + */ + void PrepareForNewNode( bool compactMode ); + void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities. + + bool _firstElement; + FILE* _fp; + int _depth; + int _textDepth; + bool _processEntities; + bool _compactMode; + + enum { + ENTITY_RANGE = 64, + BUF_SIZE = 200 + }; + bool _entityFlag[ENTITY_RANGE]; + bool _restrictedEntityFlag[ENTITY_RANGE]; + + DynArray< char, 20 > _buffer; + + // Prohibit cloning, intentionally not implemented + XMLPrinter( const XMLPrinter& ); + XMLPrinter& operator=( const XMLPrinter& ); +}; + + +} // namespace tinyxml2 + +#if defined(_MSC_VER) +# pragma warning(pop) +#endif + +#endif // TINYXML2_INCLUDED diff --git a/unix/kasmvnc_defaults.yaml b/unix/kasmvnc_defaults.yaml index 292e93d..afad666 100644 --- a/unix/kasmvnc_defaults.yaml +++ b/unix/kasmvnc_defaults.yaml @@ -18,6 +18,7 @@ network: udp: public_ip: auto port: auto + payload_size: auto stun_server: auto ssl: pem_certificate: /etc/ssl/certs/ssl-cert-snakeoil.pem @@ -36,7 +37,7 @@ user_session: keyboard: remap_keys: - # - 0x22->0x40 + # - 0x22->0x40 ignore_numlock: false raw_keyboard: false @@ -128,6 +129,7 @@ encoding: logging: level: off scaling_algorithm: progressive_bilinear + webp_encoding_time: 30 compare_framebuffer: auto zrle_zlib_level: auto diff --git a/unix/vncserver b/unix/vncserver index ecdec9e..1ac99c8 100755 --- a/unix/vncserver +++ b/unix/vncserver @@ -2068,6 +2068,15 @@ sub DefineConfigToCLIConversion { $value; } }), + KasmVNC::CliOption->new({ + name => 'WebpEncodingTime', + configKeys => [ + KasmVNC::ConfigKey->new({ + name => "encoding.video_encoding_mode.webp_encoding_time", + type => KasmVNC::ConfigKey::INT + }) + ] + }), KasmVNC::CliOption->new({ name => 'CompareFB', configKeys => [ @@ -2366,6 +2375,24 @@ sub DefineConfigToCLIConversion { $value; } }), + KasmVNC::CliOption->new({ + name => 'udpSize', + configKeys => [ + KasmVNC::ConfigKey->new({ + name => "network.udp.payload_size", + validator => KasmVNC::PatternValidator->new({ + pattern => qr/^(auto|\d+)$/, + errorMessage => "must be 'auto' or an integer" + }), + }) + ], + isActiveSub => sub { + $self = shift; + + my $value = $self->configValue(); + isPresent($value) && $value ne 'auto'; + } + }), KasmVNC::CliOption->new({ name => 'udpPort', configKeys => [ diff --git a/unix/xserver/hw/vnc/Makefile.am b/unix/xserver/hw/vnc/Makefile.am index a25d214..c3226eb 100644 --- a/unix/xserver/hw/vnc/Makefile.am +++ b/unix/xserver/hw/vnc/Makefile.am @@ -52,7 +52,7 @@ Xvnc_CPPFLAGS = $(XVNC_CPPFLAGS) -DKASMVNC -DNO_MODULE_EXTS \ -I$(top_srcdir)/dri3 @LIBDRM_CFLAGS@ Xvnc_LDADD = $(XVNC_LIBS) libvnccommon.la $(COMMON_LIBS) \ - $(XSERVER_LIBS) $(XSERVER_SYS_LIBS) $(XVNC_SYS_LIBS) -lX11 -lwebp -lssl -lcrypto -lcrypt \ + $(XSERVER_LIBS) $(XSERVER_SYS_LIBS) $(XVNC_SYS_LIBS) -lX11 -lwebp -lsharpyuv -lssl -lcrypto -lcrypt \ -lfreetype Xvnc_LDFLAGS = $(LD_EXPORT_SYMBOLS_FLAG) -fopenmp diff --git a/unix/xserver/hw/vnc/Xvnc.man b/unix/xserver/hw/vnc/Xvnc.man index c23bccd..206784c 100644 --- a/unix/xserver/hw/vnc/Xvnc.man +++ b/unix/xserver/hw/vnc/Xvnc.man @@ -574,6 +574,16 @@ When \fBNoClipboard\fP parameter is set, allowing override of \fBSendCutText\fP and \fBAcceptCutText\fP has no effect. Default is \fBdesktop,AcceptPointerEvents,SendCutText,AcceptCutText,SendPrimary,SetPrimary\fP. +. +TP +.B -Benchmark +Run the built-in benchmarking routines on the specified video file and exit. +When this option is used, benchmarking results can be saved to a file specified by the \fB-BenchmarkResults\fP option; otherwise, the results are saved to \fBBenchmark.xml\fP by default. +. +.TP +.B -BenchmarkResults +Save the benchmarking results to the specified file. +Use this option together with \fB-Benchmark\fP to output the report to a custom file. .SH USAGE WITH INETD By configuring the \fBinetd\fP(1) service appropriately, Xvnc can be launched diff --git a/unix/xserver/hw/vnc/vncExtInit.cc b/unix/xserver/hw/vnc/vncExtInit.cc index c8ea188..8c74aa8 100644 --- a/unix/xserver/hw/vnc/vncExtInit.cc +++ b/unix/xserver/hw/vnc/vncExtInit.cc @@ -184,7 +184,7 @@ static void parseClipTypes() vlog.debug("Adding DLP binary mime type %s", m.mime); } - vlog.debug("Total %u binary mime types", dlp_mimetypes.size()); + vlog.debug("Total %lu binary mime types", dlp_mimetypes.size()); free(origstr); } diff --git a/unix/xserver/hw/vnc/xvnc.c b/unix/xserver/hw/vnc/xvnc.c index cc500f4..7c196dc 100644 --- a/unix/xserver/hw/vnc/xvnc.c +++ b/unix/xserver/hw/vnc/xvnc.c @@ -95,7 +95,7 @@ from the X Consortium. #undef VENDOR_STRING #include "version-config.h" -#define XVNCVERSION "KasmVNC 1.3.3" +#define XVNCVERSION "KasmVNC 1.3.4" #define XVNCCOPYRIGHT ("Copyright (C) 1999-2018 KasmVNC Team and many others (see README.me)\n" \ "See http://kasmweb.com for information on KasmVNC.\n")